CF BUDDY
← Problems·

1444A · Division

1500 · brute force, math, number theory

Problem: Oleg's favorite subjects are History and Math, and his favorite branch of mathematics is division.

To improve his division skills, Oleg came up with tt pairs of integers pip_i and qiq_i and for each pair decided to find the greatest integer xix_i, such that:

  • pip_i is divisible by xix_i;
  • xix_i is not divisible by qiq_i.

Input Format: The first line contains an integer tt (1t501 \le t \le 50) — the number of pairs.

Each of the following tt lines contains two integers pip_i and qiq_i (1pi10181 \le p_i \le 10^{18}; 2qi1092 \le q_i \le 10^{9}) — the ii-th pair of integers.

Output Format: Print tt integers: the ii-th integer is the largest xix_i such that pip_i is divisible by xix_i, but xix_i is not divisible by qiq_i.

One can show that there is always at least one value of xix_i satisfying the divisibility conditions for the given constraints.

Note: For the first pair, where p1=10p_1 = 10 and q1=4q_1 = 4, the answer is x1=10x_1 = 10, since it is the greatest divisor of 1010 and 1010 is not divisible by 44.

For the second pair, where p2=12p_2 = 12 and q2=6q_2 = 6, note that

  • 1212 is not a valid x2x_2, since 1212 is divisible by q2=6q_2 = 6;
  • 66 is not valid x2x_2 as well: 66 is also divisible by q2=6q_2 = 6.

Sample Cases

Case 1

Input

3
10 4
12 6
179 822

Output

10
4
179

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback