We need to find the largest divisor x of p such that x is not divisible by q.
Constraints: t up to 50, p up to 1e18, q up to 1e9. We need to compute efficiently.
Observations: We need max divisor x of p, with x % q != 0 (i.e., q does NOT divide x). So we want the largest divisor of p that is not a multiple of q. If p itself is not divisible by q, answer is p. Else, p is divisible by q, then we need to find a divisor smaller than p that is not divisible by q, and as large as possible.
Given p can be huge (1e18) but q up to 1e9, we can factor q? But p may be large, factorization may be hard, but we only need divisors of p that are not multiples of q. We can think: if p is divisible by q, then p = q^k * s, where s is not divisible by q (i.e., gcd(s, q)=1). Then divisors of p can be expressed as d = q^a * d', where d' divides s, and 0 ≤ a ≤ k. Condition: x not divisible by q means a = 0. Because if a>=1, then q divides x. So the largest divisor of p that is not divisible by q is exactly the divisor with a=0 and the largest possible d' i.e., d' = s, because s is the maximal divisor of p that is coprime to q? Actually s might not be the largest divisor not divisible by q? Let's check: p = q^k * s, with s not divisible by q. The divisors not divisible by q are those with exponent of q = 0. So they are divisors of s. So the largest such divisor is s itself. So answer would be s. But wait, is s necessarily the largest divisor of p that is not divisible by q? Consider p = 12, q=6. Factor q=23. Write p=12=2^23. But q=6=23. In terms of q^ks representation: we want to write p = q^k * s where s not divisible by q. q=6, p=12, 12/6=2, so k=1, s=2. s=2 is not divisible by 6. Then the largest divisor of p not divisible by 6 would be s=2? But example answer is 4. So something's off.
Because we require x divides p, and x is not divisible by q. For p=12, divisors: 12,6,4,3,2,1. Those not divisible by 6: 4,3,2,1. Largest is 4. But s=2. So s is not the largest. The reason is that s=2 is the cofactor after dividing out one factor of q. But there might be divisors that include some primes from q but not the full combination that makes q divide it. For example, 4 includes factor 2^2, but q=6=2*3. For a divisor to be divisible by q, it must have at least one factor of 2 and one factor of 3. So if we have a divisor that contains only 2's (no 3), it is not divisible by q, even if it contains some factor of q's prime powers. So representation p = q^k * s with s not divisible by q ensures that s is not divisible by q, but s may be smaller than some other divisors that are not multiples of q but have some prime factors overlapping with q. In fact, we can include some primes of q in x, but not all at the same time to keep q not dividing x. So we need to consider prime factorization of q.
Let q have prime factorization: q = ∏_{j} r_j^{e_j}. For x to be divisible by q, for each prime r_j, the exponent of r_j in x must be at least e_j. So x is NOT divisible by q if there exists at least one prime r_j such that its exponent in x is less than e_j. Equivalently, x fails to have at least the required exponent in some prime.
We want the largest divisor x of p that fails that condition.
If p is not divisible by q, answer is p. So assume p divisible by q.
We need to find max x|p, such that for some prime r dividing q, v_r(x) < v_r(q). (where v_r denotes exponent). Equivalent: not all prime exponent conditions are met.
One approach: For each prime factor r of q, consider x = p / r^{v_r(p) - (v_r(q)-1)}? Actually, we could try to reduce the exponent of one prime to just below the required threshold, while keeping as much of the rest of p as possible.
Specifically, for each prime r dividing q, define the exponent of r in p: a = v_r(p). Since p is divisible by q, we have a >= e = v_r(q). To make x not divisible by q, we need to ensure that for at least one prime r, v_r(x) < e. But we want x as large as possible, so we would want to only reduce the exponent of one prime (or possibly multiple) minimally, i.e., reduce it to e-1 (if possible) and keep all other primes at their maximum (i.e., same as in p). However, we must also consider that for other primes, we could keep their exponents as in p, because if we reduce other primes we would shrink x further. So the optimal candidate for each prime r is: x_r = p / r^{a - (e-1)} if a > e-1, i.e., if a >= e, we can reduce the exponent of r to e-1 by dividing p by r^{a - (e-1)}. Then v_r(x_r) = e-1 < e, so condition satisfied. And for other primes, exponents remain the same as p. That yields a divisor of p (since we are just dividing out some power of r). This x_r is a candidate. Among all primes r, we take the maximum such x_r.
But is it always optimal? Could there be a candidate that reduces multiple primes to achieve an even larger x? That would involve dividing p by more factors, making it smaller, so no. So best is to reduce as little as possible, i.e., minimal division, to break the divisibility condition. That suggests we only need to consider, for each prime factor r of q, the divisor obtained by reducing the exponent of r to exactly e-1 (if possible). But what if a = e? Then we need to reduce exponent from e to e-1, i.e., divide by r^1. That is fine. If a > e, we can divide by r^{a - (e-1)} = r^{(a - e + 1)} to reduce exponent to e-1. That might be a larger division than just r^1? Actually if a is larger, we need to reduce by more to get to e-1, because we want exactly e-1, not just below e. But maybe we don't need to reduce exactly to e-1; we could reduce to any value less than e. To minimize the division (maximize x), we want to reduce by as little as possible: i.e., we want to reduce the exponent just enough so that it becomes less than e. That minimal reduction is to set the exponent to e-1, i.e., reduce by (a - (e-1)) factors. If we reduce more, we get smaller x. So that's optimal for that prime.
But what if a = e? Then minimal reduction is divide by r^1. If a = e-1? Actually since p divisible by q, we have a >= e, so a cannot be less than e. So a >= e.
Thus candidates: for each prime r in q, compute x_r = p / r^{ a - (e - 1) } where a = v_r(p). That's the largest divisor of p that is not divisible by q and for which the deficiency is only in prime r (or at least in r, but we specifically forced r's exponent to be e-1). However, there could be other divisors that are not divisible by q but are not of this form? For example, consider p = 2^2 * 3^2, q = 6 (2^1*3^1). The candidates:
- For r=2: a=2, e=1 => x = p / 2^{2 - (1-1)} = p / 2^{2} = 3^2 =9.
- For r=3: a=2, e=1 => x = p / 3^{2 - (1-1)} = p / 3^{2} = 2^2=4.
Max = 9. But is 9 a divisor of p? 9 divides 36? 36/9=4, yes. 9 not divisible by 6 (since 9 not multiple of 6). So answer would be 9. But is there any larger divisor not divisible by 6? The divisors of 36: 36,18,12,9,6,4,3,2,1. Those not divisible by 6: 36? 36/6=6 so divisible by 6, not allowed. 18 divisible by 6? 18/6=3, so yes. 12 divisible by 6, yes. 9 not divisible by 6; 4,3,2,1 also not. So 9 is largest, matches candidate.
What about p=12, q=6? p=2^2 * 3^1, q=2^1*3^1. Candidates:
- r=2: a=2, e=1 => x = 12 / 2^{2 - 0} = 12 / 4 =3? Actually e-1 =0, so exponent target 0. So we divide by 2^{2 - 0}=2^2=4 => x=3. v2(x)=0<1, so OK.
- r=3: a=1, e=1 => target exponent e-1=0 => divide by 3^{1 - 0}=3 => x=4. v3(x)=0<1. So candidate x=4. Max=4, matches answer.
So that works.
Now consider case where q has a prime factor r such that a = v_r(p) is exactly e (the minimal). Then x_r = p / r. That's often the candidate. But maybe we could also consider not dividing by a full r, but maybe dividing by something else? Suppose we reduce multiple primes? That would be smaller, so no.
But is it possible that the optimal x is not obtained by reducing a single prime to just below threshold, but rather by eliminating all factors of some prime completely? That is what we are doing: we are reducing to e-1, which could be zero if e=1. That might be division by a larger amount than necessary? Actually if e=1, target exponent is 0, i.e., remove all factors of that prime. That is indeed minimal removal? To make exponent less than 1, we must have exponent 0, so need to remove all factors of that prime. So indeed we must divide by r^a. That's exactly x_r = p / r^a. That yields a divisor with no r factors. Could there be a divisor that still has some r factors but not enough to be divisible by q? For e=1, any exponent >=1 would make it divisible by r (since e=1 means q includes r^1). Wait condition: x divisible by q requires for each prime r: v_r(x) >= v_r(q) = e. If e=1, then v_r(x) >= 1 means x contains r. So if x contains any factor r, then it is divisible by r. But does it cause x to be divisible by q? Only if it contains all primes of q with at least required exponents. Since q has multiple primes, having r factor alone doesn't make x divisible by q; we also need the other primes. Actually, to be divisible by q, we need all prime conditions to hold simultaneously. So if we keep r exponent at 1, but we reduce exponent of another prime to below its e, then x may not be divisible by q. So for e=1 on prime r, we could still keep some r factors and break divisibility by reducing some other prime. That might result in a larger x than removing all r factors. In our candidate set, we consider reducing each prime individually. For each prime, we can choose to reduce that prime's exponent to below e (if e>1) or to zero (if e=1). But also, we could reduce a prime with e>1 to something like e-1 (which is >=0), and keep others intact. For a prime with e=1, the minimal reduction to make it deficient is to reduce its exponent to 0, i.e., remove all of that prime. But we could also reduce a different prime to break the condition, while leaving this prime's exponent as is. So for prime r with e=1, we might not need to remove r at all if we break using another prime. So the candidate we compute for r (dividing out all r factors) might be too small. But we also consider candidates for other primes. So the maximum among all candidates is still the answer? Let's test: p=12, q=6: q=2^1*3^1 (both e=1). For prime 2, x_2 = p / 2^{2} = 3 (removing all 2's). For prime 3, x_3 = p / 3^{1} = 4. The max is 4, which is correct. So candidate from prime 2 gave 3, which is not max. So indeed we consider all primes, and the max is answer.
But consider a scenario where q has a prime with e=1, but its exponent in p is huge. If we reduce that prime, we might lose a lot. But we can instead reduce another prime where the cost is smaller (i.e., where a - (e-1) is minimal). So the answer should be p divided by the minimal factor needed to break divisibility by q. In other words, for each prime r, the required division to break via that prime is r^{a - (e-1)}. We want the smallest division (i.e., smallest factor we need to remove) to get a divisor not divisible by q? Actually we want the resulting x = p / d as large as possible, so we want d as small as possible. So we want the minimal d such that p/d is not divisible by q, and d is a divisor of p that removes some prime factors. But note that we are only considering removing powers of a single prime to make it deficient. Could there be a case where the minimal d is not a pure power of a prime? For example, maybe we could remove a composite factor that is smaller than the required power for any single prime? That seems unlikely because if we need to break the condition, we need to reduce at least one prime's exponent below e. The minimal removal to achieve that for a given prime r is r^{a - (e-1)} (assuming a>=e). This is a power of r. If we consider removing a factor that includes multiple primes, it would be at least as large as the product of some removals, which is larger than removing only one prime factor. However, could there be a scenario where for one prime, the required removal is r^{k} with k large, but by removing a small amount of another prime (like dividing by a small prime factor), we break the condition with smaller d? That's exactly covered by considering each prime and taking the smallest d. So indeed, we compute for each prime factor r of q: d_r = r^{a - (e-1)}. Then candidate x_r = p / d_r. Among those, max x is answer.
But also consider the possibility of breaking condition by removing factors that affect multiple primes at once, but with a d that is smaller than any single prime's required d? Since d_r are powers of a single prime, they could be large if exponent a is large. But maybe we can break by reducing a prime that doesn't require removing all its factors? For e>1, we only need to reduce exponent to e-1, which might be a relatively small removal (dividing by r^{a - (e-1)}). Could that be larger than dividing by a product that includes a smaller prime? For example, q = 2^2 * 3^2 = 36. p = 2^100 * 3^100. For prime 2, we need to divide by 2^{100 - (2-1)} = 2^{99}. That's huge. For prime 3, similarly huge. But could we break the condition by dividing by something like 2^1? Let's see: If we divide by 2 (i.e., x = p/2), then v2(x) = 99, still >=2, so condition for 2 still satisfied. But condition for 3: v3(x)=100 >=2, still satisfied. So x would still be divisible by 36? Because both exponents are still >=2. So no, not broken. We need to reduce at least one prime exponent below 2. So minimal removal for prime 2 is to reduce its exponent to 1, i.e., divide by 2^{99}. That's enormous. But maybe we could reduce both exponents a little bit, like divide by 2^{50} * 3^{50}? That product might be smaller than 2^{99}? Let's compare: 2^{99} is huge, but 2^{50}*3^{50} is even larger? Possibly not. 2^{99} vs 2^{50}*3^{50}. 2^{99} = 2^{99}. 2^{50}3^{50} = 6^{50} which is roughly (6)^{50} vs 2^{99}. Since 2^{99} = 2^{99}, 6^{50} = (23)^{50} = 2^{50} * 3^{50}. Compare exponents: log2(2^{50}3^{50}) = 50 + 50log2(3) ≈ 50 + 79.25 ≈ 129.25, while log2(2^{99}) = 99. So 2^{50}*3^{50} is actually larger than 2^{99} because 3^{50} is huge. So dividing by 2^{99} yields a larger x than dividing by 2^{50}*3^{50}. However, could there be a scenario where dividing by a small composite factor that reduces multiple primes a little yields a smaller d than the minimal single prime reduction? Let's analyze. For each prime r_i, the deficit needed to make exponent < e_i is to reduce exponent from a_i to at most e_i - 1. The amount to remove for that prime if we only reduce that one is r_i^{a_i - (e_i - 1)}. If we reduce multiple primes, the removal d would be product over some set of r_i^{removal_i} where removal_i >= a_i - (e_i - 1) if we fully reduce that prime to target, but we could also reduce a prime partially, not all the way to e_i - 1, but still not break condition? Actually to break condition, we need at least one prime to have exponent < e_i. If we reduce multiple primes, we could reduce one prime to e_i - 1 and leave others unchanged, which is already covered. Or we could reduce multiple primes but each not enough individually to break condition (i.e., each still >= e_i), but collectively? No, condition is about each prime individually; all must be >= e_i for divisibility by q. If none of them are < e_i, then q divides x. So to break, at least one prime must drop below e_i. So the minimal removal for that prime is as described. If we also reduce other primes, it would increase d, making x smaller. So no benefit.
But wait: What if a_i is exactly e_i? Then d_r = r_i^{1} = r_i. That's small. If a_i is large, d_r is huge. Could we break condition by instead of reducing a prime with large a_i, we reduce a different prime that has a_i = e_i (i.e., minimal) and thus d is small? That's covered by considering all primes, we take the min d (max x) across them.
But consider a scenario where for all primes, a_i is huge, i.e., p contains very high powers of each prime. Then d_r for each prime is huge (since a_i - (e_i - 1) is large). Could there be a way to break condition by dividing by something that is not a pure power of a prime but still yields a smaller d? For instance, we could divide by q itself? That would give x = p/q. Does x satisfy condition? Check: x = p/q. v_r(x) = a_i - e_i. Since a_i >= e_i, v_r(x) could be >=0. But is it possible that for some prime, v_r(x) < e_i? v_r(x) = a_i - e_i. Since a_i >= e_i, we have a_i - e_i could be 0 or positive. For x to be not divisible by q, we need at least one prime with v_r(x) < e_i. That means a_i - e_i < e_i, i.e., a_i < 2 e_i? Actually v_r(x) = a_i - e_i. We need a_i - e_i < e_i → a_i < 2 e_i. So if for all primes, a_i >= 2 e_i, then p/q is still divisible by q? Let's test: q = 2^23^2=36, p = 2^4 * 3^4 = 1681=1296. Then p/q = 2^2 * 3^2 = 36, which is divisible by 36. So not valid. So dividing by q might not break condition if exponents are high. Could dividing by some factor smaller than some d_r break condition? Suppose we divide by r_i^{k} where k is less than a_i - (e_i - 1). Then v_r(x) = a_i - k, which is still >= e_i (since k < a_i - (e_i -1) => a_i - k > e_i -1, so >= e_i). So that prime's condition still holds. If we don't reduce any other prime below e_i, then q still divides x. So to break, we must reduce some prime by at least a_i - (e_i -1). That is the minimal amount for that prime. So the minimal d is indeed min over primes of r_i^{a_i - (e_i -1)}. But is that minimal d always a power of a prime? Could there be a scenario where the minimal d is a product of a small prime factor from a different prime? For example, if we have two primes, and for prime A, d_A = r_A^{a_A - (e_A -1)} is huge, but for prime B, d_B is huge as well. Could we break by reducing prime A slightly (but not enough) and also reducing prime B slightly, such that after both reductions, maybe one of them falls below e? Actually if we reduce both, we could try to get one of them below e by a combination? But the reduction amount for a prime is the exponent we remove. To get prime A below e_A, we need to remove at least a_A - (e_A -1) of A. If we remove less, its exponent remains >= e_A. So it's still not deficient. Similarly for B. So even if we remove a bit from both, neither becomes deficient, so condition not broken. So we must cause deficiency in at least one prime. So we need to remove at least the required amount from some prime. So the minimal divisor to divide by is exactly the smallest among these required powers (maybe we could remove that required power from a prime, but also maybe include extra factors? That would be larger). So yes, the optimal is to take the prime that requires the smallest divisor removal, i.e., minimal r_i^{a_i - (e_i -1)}. However, careful: The d we compute is a divisor of p, but we must ensure that after division, the resulting x is an integer, which it is because we are dividing by a factor that divides p (since we remove only whole prime powers). So candidate x = p / d where d = min_{r|q} r^{a_i - (e_i -1)}. But is it always the case that the prime giving the smallest d yields the largest x? Since x = p/d, smaller d gives larger x. So we want d as small as possible. So answer = p / min_{r|q} r^{a_i - (e_i -1)}. However, we should check if there is any candidate that does not correspond to reducing a single prime to exactly e_i -1 but maybe reducing it to something less, which would result in a larger d, thus smaller x. Not beneficial.
But also consider the scenario where p is divisible by q, but maybe we can also consider removing some factor that is not a pure power but a combination that yields a d smaller than the minimal required power? That seems impossible because any d that causes deficiency in prime r must be a multiple of r^{a_i - (e_i -1)} (since we need to reduce exponent of r by at least that amount). Let's formalize: Suppose we divide p by some divisor d of p, giving x = p/d. Let v_r(p) = a, v_r(d) = b, so v_r(x) = a - b. For x not to be divisible by q, there exists r such that a - b < e. Thus b > a - e. Since b is integer, b >= a - e + 1. So d must be divisible by r^{a - e + 1} (because b >= a - e + 1). Therefore d must be at least r^{a - e + 1} in terms of containing that factor. However, d could have other prime factors as well, making it larger. So the minimal d that works for a given prime r is exactly r^{a - e + 1} (assuming that divides p, which it does). So the globally minimal d is min over r of r^{a - e + 1}. So answer = p / (min_r r^{a - e + 1}).
But careful: a - e + 1 = a - (e - 1). Yes.
Thus algorithm: For each test case, if p % q != 0, answer is p. Else, factor q (since q <= 1e9, we can factor q quickly by trial division up to sqrt(q) which is up to 31623, which is fine for t <= 50). For each prime factor r of q, compute e = exponent in q, compute a = exponent of r in p. Since p up to 1e18, we can compute a by repeatedly dividing p by r while divisible, but careful: p can be huge, but dividing by r many times is okay because a could be up to log_2(p) ~ 60. So we can compute a by: a = 0; temp = p; while temp % r == 0: temp //= r; a++. But we must avoid overflow: use Python's big integers, fine.
Then compute d_r = r**(a - e + 1). But a - e + 1 could be large, leading to huge exponentiation. However, we don't need d_r as a number directly maybe; we need to compare d_r values to find the smallest. But these numbers can be huge (p is up to 1e18, but d_r could also be up to p). Since we only need to compute p // d_r as answer, we could compute candidate x_r = p // (r**(a - e + 1)). We can compute that directly using integer arithmetic, taking care of exponentiation not causing overflow in Python (it handles big ints). But exponent could be up to a which up to about 60, so r**(60) is fine (r up to 1e9, but 1e960 is astronomically huge, beyond Python's capacity? Actually Python's integers are arbitrary precision but 1e960 would be about 10^(540) which is huge memory and time, but we might not need to compute that because if exponent is large, the resulting d_r would be > p? Actually p is limited to 1e18, so a cannot be that large if r >= 2. Since p <= 1e18, maximum exponent for r=2 is about 60 (2^60~1e18). For r=1e9, exponent a is at most 2 because (1e9)^2 = 1e18, so a is at most 2. So exponent a - e + 1 is at most around 60. So rexp is at most p, which is <= 1e18, so it's safe. Actually wait: if r is as large as 1e9, a could be 1 or 2. So exponent is small. So we can safely compute rexp as integer within 1e18. So no problem.
Thus we can compute candidate x_r = p // (r**(a - e + 1)). Then answer = max over candidates? Since smaller d gives larger x, we want max x_r. But max over x_r corresponds to min over d_r. So answer = max(x_r). But we need to ensure we consider all prime factors of q. Also note that if q has repeated prime factors, we include each.
Edge Cases: What if q is prime and p is divisible by q? Then a >= 1, e = 1. For that prime, d = r^{a - 1 + 1} = r^{a}. That is dividing by r^a, i.e., removing all factors of r from p. So candidate x = p / r^a, which is the part of p coprime to q. That's s as earlier. But maybe there is another prime? Only one prime. So answer = p / r^a. But is it always the largest? Let's test p=12, q=2. q=2 (prime). p divisible by 2. Divisors of 12 not divisible by 2: odd divisors: 3,1. Largest is 3. p / 2^a: a = v2(12)=2, so p/4 = 3. Works.
p=18, q=3. a=v3(18)=2, p/9=2. Divisors of 18 not divisible by 3: 2,1? Actually 18 divisors: 18,9,6,3,2,1. Those not divisible by 3: 2,1. Largest 2. Correct.
p=8, q=4 (2^2). q has prime 2 with e=2. a=v2(8)=3. d = 2^{3-2+1}=2^{2}=4. x = 8/4=2. Divisors of 8: 8,4,2,1. Not divisible by 4: 2,1 (since 4 and 8 are divisible by 4). Largest is 2. Works.
p=16, q=8 (2^3). a=4, e=3 -> d=2^{4-3+1}=2^{2}=4, x=4. Divisors not divisible by 8: 4,2,1; largest 4. Good.
p=72=89=2^33^2, q=12=2^23^1? Actually q=12=2^23. e2=2, e3=1. a2=3, a3=2. For r=2: d2 = 2^{3-2+1}=2^{2}=4 => x=72/4=18. For r=3: d3 = 3^{2-1+1}=3^{2}=9 => x=72/9=8. So max=18. Divisors of 72 not divisible by 12: Let's check 72,36,24,18,12,9,8,6,4,3,2,1? Actually need to list all divisors. 72 divisible by 12? 72/12=6, yes so 72 is divisible by 12 -> not allowed. 36/12=3 -> divisible, not allowed. 24/12=2 -> divisible. 18/12=1.5 -> not divisible, allowed. 12 obviously not allowed. 9 not divisible by 12? 9 not multiple of 12, allowed. 8 allowed. So largest allowed is 18, matches.
Thus approach seems correct.
But we must be careful: We assumed p is divisible by q. If not, answer is p. So handle that.
Now consider potential pitfalls: q may be up to 1e9, but not necessarily square-free; we need prime factorization with exponents.
Edge case: q might be 1? But constraints say q_i >= 2, so okay.
What about p extremely large, but we need to compute exponents a for each prime factor r of q. Since p is up to 1e18, we can compute a by dividing p by r while possible. However, if r is large, we might do only a couple divisions. So fine.
Now, is there any scenario where the answer is p divided by something else not captured by a single prime reduction? Let's test a tricky scenario: q has multiple primes, and the minimal d_r might be from a prime where a is exactly e, so d_r = r. But what if r is larger than product of some other primes? For example, q = 6 = 23. p = 2 * 3^100? Actually suppose p = 2^1 * 3^100. Then a2=1 e2=1, d2 = 2^{1-1+1}=2^1=2 -> candidate x2 = p/2 = 3^100. a3=100 e3=1, d3 = 3^{100-1+1}=3^{100} huge -> candidate x3 = 2. So max = 3^100, which is p/2. So answer = 3^100. That's the largest divisor not divisible by 6? Let's check: p itself is 23^100, divisible by 6? Since it has both 2 and 3, yes, 6 divides p. Divisors not divisible by 6: Those that lack either 2 or 3. The largest such would be either p/2 = 3^100 (lacks factor 2) or p/3^{100} = 2 (lacks factor 3). Clearly 3^100 is larger. So correct.
Another scenario: p = 2^100 * 3, q=6. a2=100 e2=1 => d2 = 2^{100}, x2=3; a3=1 e3=1 => d3 = 3^1=3, x3=2^{100}. max = 2^{100}. Largest divisor not divisible by 6 is either 2^{100} (no factor 3) or 3 (no factor 2). 2^{100} larger. So correct.
Now consider q with exponent >1: p = 2^5 * 3^5, q = 49=36? Actually q=4=2^2 only? Let's try q = 4 (2^2). Then only prime 2. a=5 e=2 => d=2^{5-2+1}=2^{4}=16, x = p/16 = 2^13^5? Actually p = 2^53^5 = 32243=7776. /16 = 2^1 * 3^5 = 2*243=486. Largest divisor not divisible by 4: Among divisors of 7776, we want not multiple of 4, i.e., v2(x) <=1. The largest such would be with v2=1 and maximize the rest: 2 * 3^5 = 486. Or v2=0 and rest max: 3^5=243. So 486 is larger, matches candidate. Good.
Now consider q = 12 = 2^2 * 3^1. p = 2^2 * 3^2 = 36. a2=2 e2=2 => d2 = 2^{2-2+1}=2^1=2 => x2=18; a3=2 e3=1 => d3 = 3^{2-1+1}=3^2=9 => x3=4. max=18. Is 18 not divisible by 12? 18/12=1.5, so no. Largest divisor of 36 not divisible by 12: Divisors: 36 (divisible by 12? 36/12=3, yes), 18 (not), 12 (yes), 9 (not), 6 (6/12 no? 6 not multiple of 12, but 6 also not divisible by 12 because 12 does not divide 6; but check condition: x not divisible by q means q does not divide x. 12 does not divide 6, so 6 is also allowed. 4,3,2,1. So allowed: 18,9,6,4,3,2,1. Largest is 18. So correct.
Now what about p = 2^3 * 3^3 = 216, q = 12. a2=3 e2=2 => d2 = 2^{3-2+1}=2^{2}=4 => x2=54; a3=3 e3=1 => d3 = 3^{3-1+1}=3^{3}=27 => x3=8. max=54. Allowed divisors: need largest not multiple of 12. 216/12=18, so 216 is divisible. 108? 108/12=9, divisible. 72? 72/12=6, divisible. 54? 54/12=4.5, not divisible. So 54 is largest? Check 96? Not divisor. So likely.
Thus the algorithm works.
But we must also consider the possibility that after dividing by d_r, the resulting x might still be divisible by q? We ensured that for prime r, v_r(x) = e-1 < e, so definitely not divisible by q. So it's valid.
Now, we must also consider if there are other divisors that are larger than all these x_r? Could there be a divisor that is not of form p / r^{a - e + 1}? That would require a different d. Since d must be at least some r^{a - e + 1}. If we choose a prime r with minimal d_r, then x = p / d_r is the maximum possible because any other valid d is at least as large as the minimal d_r. Because any valid d must be >= some r^{a - e + 1} (since it must contain that factor). The minimal possible d is indeed min_r r^{a - e + 1}. So x = p / min_d is the maximum possible. So answer = p // min_d.
Thus we don't even need to compute all x_r and take max; we can compute the minimal d and then x = p / min_d. But be careful: if multiple primes have the same minimal d, fine.
Now, we must ensure that d = min_r r^{a - e + 1} is indeed a divisor of p. Since a - e + 1 >= 1, and a is exponent, r^{a - e + 1} divides p (since a >= e, so a - e + 1 <= a). So yes.
Now, what about the case where q has multiple prime factors but one of them gives d=1? Could d=1 happen? That would require r^{a - e + 1}=1, which only if exponent zero, i.e., a - e + 1 = 0 => a = e - 1, but since p divisible by q, a >= e, so a - e + 1 >= 1, so d >= r >= 2. So d cannot be 1. So we always divide by at least 2. That's okay.
Thus solution: For each test case:
-
Read p, q.
-
If p % q != 0: print(p); continue.
-
Factorize q into primes with exponents. Since q <= 1e9, we can do trial division up to sqrt(q). Use a list to store (prime, exponent_in_q).
-
Initialize best = p (or we need min_d = infinity). Actually we want max_x, so maybe compute max_x = 0. For each prime r, exponent e in q:
- Compute a = exponent of r in p. Since p divisible by q, we know at least e, but we can compute by while loop dividing a copy of p? But careful: p may be huge, but we can compute a by repeatedly dividing a temporary variable (say temp = p) by r. But if we do that for each prime, we will be dividing the same p multiple times. That's okay as total divisions limited. However, we must be careful about overflow? Python handles big ints. But we might also compute a using while p % r == 0: but p itself is big, but we can use a copy or compute using repeated division on a variable that we update. However, note that we need a for each r; but these divisions may alter the variable if we use the same variable; we should compute a separately for each prime, perhaps by using a temporary number that we divide? Since p is divisible by q, we could factor p partially? But we only need the exponent of r in p, we can compute by:
a = 0
temp = p
while temp % r == 0:
temp //= r
a += 1
This is fine; but if we do this for each prime, the total operations are sum over primes of exponent a, which is at most total prime factors of p (log2(p) ~ 60) per prime? Actually each prime we might loop up to a times; but a may be up to 60, and there are at most about 9 prime factors for q (since 23571113171923 > 1e9? Actually product of first few primes: 23571113171923 = 223092870 > 1e9? 223092870 is ~2.2e8, times 29 = 6.5e9 > 1e9, so maximum distinct primes maybe 9? Actually 23571113=30030, 17=510510, 19=9699690, 23=223092870, 29=6469693230 > 1e9, so up to 9 primes? Let's compute: product of first 9 primes: 235711131719*23 = 223092870 (that's 2.23e8) still less than 1e9. Next prime 29 gives product ~6.47e9 > 1e9. So max distinct primes is 9. So at most 9 prime factors. For each, we may loop up to ~60 divisions, total max 540 divisions, trivial.
-
Compute d = r**(a - e + 1). Since numbers within 1e18, we can compute directly. Then candidate_x = p // d.
-
Keep max_x = max(max_x, candidate_x).
-
Print max_x.
Alternatively compute min_d and then x = p // min_d. But need to be careful to avoid overflow if computing d directly: d could be up to p (1e18), fine.
Thus algorithm complexity: O(t * sqrt(q) + t * (number of prime factors of q) * (log p)). sqrt(q) up to 31623, t=50, so up to 50*31623 ≈ 1.5 million operations, acceptable.
But we can factor q more efficiently: trial division up to sqrt(q). Since q up to 1e9, sqrt is 31623, fine.
Edge Cases: p can be as large as 1e18, but p % q == 0 ensures p divisible by q. That also implies that for each prime factor r of q, we have a >= e. So exponent calculation will produce a >= e.
Now, confirm with sample:
Input:
3
10 4
12 6
179 822
Case1: p=10, q=4 => p%q=2 !=0, so answer 10. matches.
Case2: p=12, q=6 => p%6=0. Factor q=6: primes 2^1,3^1.
- r=2, e=1. Compute a=v2(12)=2. d=2^{2-1+1}=2^2=4, x=12/4=3.
- r=3, e=1, a=v3(12)=1, d=3^{1-1+1}=3^1=3, x=12/3=4. max=4. matches.
Case3: p=179, q=822. Check p%q? 179 < 822, so not divisible. So answer 179. matches.
So code works for sample.
Now, let's think about potential pitfalls: Our reasoning seems solid, but we must verify there is no scenario where the optimal x is not of the form p divided by a pure power of a prime. Consider q = 4 (2^2) and p = 12? 12 divisible by 4? 12%4=0, yes. p=12. a= v2(12)=2, e=2, so d=2^{2-2+1}=2, x=6. But 6 is divisible by 4? 6%4=2, not divisible, so valid. However, is there a larger divisor than 6? Divisors of 12: 12,6,4,3,2,1. 12 divisible by 4? 12%4=0, so not allowed. 6 allowed, 4 not allowed, 3 allowed but smaller. So max is 6. So our candidate gives 6. Good.
Now consider q = 8 (2^3), p = 24. 24%8=0. a=v2(24)=3 (since 24=8*3, 8=2^3), e=3 => d=2^{3-3+1}=2^1=2, x=12. Check: 12 divisible by 8? No, 12%8=4, not divisible. Divisors of 24: 24,12,8,6,4,3,2,1. Largest not multiple of 8: 12 (since 24 is multiple, 8 is multiple, 12 not). So 12 correct.
Now consider q = 36 (2^2 * 3^2), p = 72. p=72 divisible by 36? 72/36=2, yes. a2=3, e2=2 => d2=2^{2}=4, x2=18; a3=2, e3=2 => d3=3^{1}=3? Wait compute: a3=2, e3=2 => a-e+1=2-2+1=1, d3=3^1=3, x3=24. Actually 72/3=24. So candidate from r=3 yields x=24. Check: 24 divisible by 36? No, because 24<36, cannot be divisible (but condition is x divisible by q? Actually x not divisible by q, so okay). However, is 24 a divisor of 72? 72/24=3, yes. 24 not divisible by 36? 36 does not divide 24, so valid. But is there a divisor larger than 24? The divisors of 72: 72,36,24,18,12,9,8,6,4,3,2,1. 72 and 36 are divisible by 36? 72/36=2, so 72 divisible; 36/36=1, so 36 divisible; so not allowed. Next largest is 24, allowed. So answer 24. But our max among candidates gave 24 (from r=3) and 18 from r=2. So works.
But wait, candidate from r=2 gave 18, from r=3 gave 24. So answer 24. However, what about x = 72/2? =36, which is divisible by 36, not allowed. 72/6? 12, smaller. So fine.
Now consider a scenario where both d_r are equal? Fine.
Now, important nuance: For each prime r, we compute d_r = r^{a - e + 1}. This is the minimal d for which the resulting x is not divisible by q and the deficiency is due to r. But is it always possible to achieve a valid x by dividing by exactly that d? Yes, because we are dividing p by a factor that is a divisor of p (since a >= e, so a - e + 1 <= a). So x = p / d_r is integer. And v_r(x) = a - (a - e + 1) = e - 1 < e, so indeed r's exponent is deficient. So valid.
Now, could there be a scenario where the minimal d is not exactly r^{a - e + 1} for a single prime, but rather a product of powers that is smaller? For example, consider q=12 (2^2 * 3^1), p=2^3 * 3^2 = 72 as before. d2 = 2^{3-2+1}=2^{2}=4, d3 = 3^{2-1+1}=3^{2}=9? Wait earlier we computed for r=3: a=2, e=1 => a - e + 1 = 2-1+1=2, so d3=3^2=9? But we previously computed d3=3? I think I made a mistake: for p=72: 72=2^3 * 3^2. q=12=2^23^1. For r=2: a=3, e=2 => a - e + 1 = 2, d=2^2=4, x=72/4=18. For r=3: a=2, e=1 => a - e + 1 = 2-1+1 = 2, d=3^2=9, x=72/9=8. That yields x=8, not 24. But earlier I computed x=24 erroneously. Let's recalc: 72/3=24, but 3 is not d3 if d3=9. Where did 24 come from? Let's recompute carefully. For r=3, to break condition via r=3, we need v3(x) < e3 = 1, i.e., v3(x) = 0. So we need to remove all factors of 3 from p. Since a=2, we need to divide by 3^{2} = 9. Then x = 72/9 = 8. So that's the candidate from r=3. But earlier I said 24, that would correspond to dividing by 3, which would leave v3(x)=1, which is not <1, so still divisible by 3 (part of q). But does 24 satisfy not divisible by q? 24: v2(24)=3 (since 24=83? Actually 24=2^3 * 3^1). v2=3 >= e2=2, v3=1 >= e3=1, so both conditions met, thus 24 is divisible by 12? 24/12=2, so yes divisible by 12. So 24 is NOT valid. So my earlier erroneous candidate was invalid. So the correct candidates are 18 and 8, max=18. But earlier we thought answer should be 24? Let's manually check divisors of 72 not divisible by 12: list divisors: 1,2,3,4,6,8,9,12,18,24,36,72. Among these, which are divisible by 12? 12,24,36,72 are divisible by 12? Check 24/12=2 -> divisible. 36/12=3 -> divisible. 72/12=6 -> divisible. So allowed: 1,2,3,4,6,8,9,18. Largest is 18. So answer should be 18, not 24. So our algorithm yields 18, which is correct.
But earlier we considered a scenario q=36, p=72, and we got candidate 24 from r=3? Let's re-evaluate that: q=36 = 2^2 * 3^2. p=72=2^3*3^2. For r=2: a=3, e=2 => d=2^{3-2+1}=2^2=4, x=72/4=18; For r=3: a=2, e=2 => a - e + 1 = 1, d=3^1=3, x=72/3=24. Check validity of x=24: v2(24)=3 >=2, v3(24)=1 <2, so deficient in prime 3, so not divisible by 36. So valid. So indeed for q=36, answer is 24. That's consistent. So our algorithm works.
Now, we need to be careful to compute a correctly. Since p can be up to 1e18, but we can compute a by while loops. However, we might also compute using integer arithmetic: a = 0; while p % r == 0: p //= r; a+=1. But we would be modifying p, which we need for later calculations for other primes. So we should use a copy: temp = p; while temp % r == 0: temp //= r; a+=1. But if we do that for each prime independently, we recompute from p each time, that's fine.
Alternatively, we could factor p along with q? Not needed.
Now, we must also consider that p might be divisible by q but q might have prime factors with exponents that are not fully present? Actually p divisible by q ensures that for each prime r dividing q, v_r(p) >= v_r(q). So a >= e.
Now, could there be a scenario where the minimal d is not from a prime factor of q but from something like if we divide by a composite factor that is smaller than any r^{a - e + 1}? Let's analyze theoretically. For any valid d, there exists at least one prime r such that d contains r^{a - e + 1} as a factor. So d >= r^{a - e + 1} for that r. Thus min_d >= min_{r} r^{a - e + 1}. And we can achieve equality by taking d = r^{a - e + 1} for the r that gives the minimum. So indeed min_d = min_{r} r^{a - e + 1}. So our solution is correct.
Now, implementation details:
- Use Python, since t small and big integers fine.
- For each test case:
- Read p, q as ints.
- If p % q != 0: print(p); continue.
- Factor q: We'll get list of (prime, exponent) pairs.
- Initialize best = 0 (to store max x).
- For each (r, e) in prime_factors:
- compute a: count how many times p divisible by r.
temp = p
a = 0
while temp % r == 0:
temp //= r
a += 1
- compute d = r ** (a - e + 1)
- candidate = p // d
- if candidate > best: best = candidate
- Print best.
But careful: For prime factorization of q, we may have repeated primes. So we should extract primes with exponent. For trial division, we iterate i from 2 to sqrt(q). But note q up to 1e9, so sqrt 31623. We'll loop i while i*i <= q. For each i that divides q, count exponent, add to list. After loop, if q>1, add q as prime with exponent 1.
Now, we should ensure that we handle large exponents correctly: r**(a - e + 1) could be computed with pow(r, a - e + 1). Python's pow works.
Now, test with some custom cases.
Case: t=1, p=1000000000000000000 (1e18), q=1000000000 (1e9). Let's compute: q=10^9 = 2^9 * 5^9? Actually 10^9 = (25)^9 = 2^9 * 5^9 = 512 * 1953125 = 1e9. p=10^18 = 2^18 * 5^18. So p divisible by q. a2=18, e2=9 => a - e + 1 = 18-9+1=10, d2 = 2^10 = 1024; candidate2 = p/1024 = 10^18 / 1024 = approx 9.765e14? Actually 10^18/1024 = 976562500000000? We'll compute later. a5=18, e5=9 => d5 = 5^10 = 9765625? Actually 5^10 = 9765625. candidate5 = p/9765625 = 10^18 / 9765625 = 102400000000? Let's compute precisely: 10^18 = (10^9)^2? Actually 10^18 = 10^9 * 10^9. So p / (5^10) = (2^18 * 5^18) / 5^10 = 2^18 * 5^8 = 262144 * 390625 = 102400000000? Let's compute 2^18=262144, 5^8=390625, product=262144390625 = 262144390625 = 262144400000 - 2621449375 = 104857600000 - 2457600000 = 102400000000 exactly. So candidate5 = 102,400,000,000. candidate2 = p/2^10 = 2^185^18 / 2^10 = 2^8 * 5^18 = 256 * 3814697265625 = 976562500000000? Actually 5^18 = 3814697265625, times 256 = 976562500000000 (since 5^182^8 = (5^92^4)^2? Not needed). So candidate2 is about 9.765e14, which is larger than candidate5? Actually candidate2 = 976,562,500,000,000 (9.765e14), candidate5 = 102,400,000,000 (1.024e11). So candidate2 is much larger. So best is candidate2 = 976562500000000. Check if candidate2 is not divisible by q? v2(candidate2)=8, e2=9 => 8<9, so deficient in 2, so not divisible by q. So answer should be candidate2. Is there any divisor larger? The maximum possible is p itself, but p is divisible by q, so not allowed. So candidate2 seems plausible. Could there be a divisor larger than candidate2? That would require a smaller d than 2^10. The smallest d among primes is min(2^10,5^10)=1024 vs 9765625, so min d=1024. So candidate = p/1024 is largest. So correct.
Now, we should also consider if q has a prime factor that is not in p? That cannot happen because p divisible by q, so all primes of q are in p. So fine.
Now, what about the case where q is a prime power? Then only one prime factor. So min_d = r^{a - e + 1}. So answer = p / r^{a - e + 1}.
Now, we should also consider the possibility of overflow in languages like C++ but we are using Python, so fine.
Now, double-check the reasoning: Is it always that the minimal d is min_{r} r^{a - e + 1}? Could there be a scenario where dividing by a different factor that is not a pure power, but still has size less than min_r r^{a - e + 1}? Since any valid d must be at least some r^{a - e + 1}. The minimum of these lower bounds is min_r r^{a - e + 1}. And we can achieve exactly that lower bound by taking d = that power. So yes.
But what about the possibility that the minimal r^{a - e + 1} might not be a divisor of p? Actually it is, because a - e + 1 <= a, so r^{a - e + 1} divides p. So fine.
Thus algorithm stands.
Now, for each test case, we factor q. But we may need to compute a, the exponent of r in p. Since p can be up to 1e18, we can compute a via while loop. However, note that if r is large (close to 1e9), then a is small (0,1,2). So fine.
Time complexity: factoring q up to sqrt(q) per test case (31623) times 50 = 1.58 million, plus for each prime factor, we compute a with up to ~60 divisions. So total < 2 million operations, fine.
We can also optimize factorization by precomputing primes up to 31623 using sieve, but not necessary for 50 tests. However, 50 * 31623 ~ 1.5 million trial divisions, which is fine in Python if we break early when ii > n, but naive loop from 2 to sqrt might be slower but still acceptable (1.5 million iterations). Actually 31623 iterations per test case is 5031623 = 1.58e6, that's trivial. So we can just do trial division without sieve.
Edge Cases: q might be a perfect square, etc. That is handled.
Now, what about p being extremely large, but we compute candidate = p // d, which is integer division. Works.
Now, check more edge cases:
Case: p = q. Then p divisible by q. For each prime factor r, a = e. So d = r^{a - e + 1} = r^{1} = r. The minimal d would be the smallest prime factor of q. So answer = p / min_prime_factor(q). For q=6, answer = 6/2=3? But is 3 the largest divisor of 6 not divisible by 6? Divisors of 6: 6,3,2,1. Those not divisible by 6: 3,2,1. Largest is 3. So answer = 3, correct. For q=4, answer = 4/2=2, correct. For q=9, answer = 9/3=3, correct (divisors: 9,3,1; 9 not allowed; 3 allowed). For q=8, answer = 8/2=4, divisors: 8,4,2,1; largest not divisible by 8 is 4, correct. So works.
Case: p = q * k, where k is coprime to q, i.e., p = q * s with gcd(s, q)=1. Then for each prime r of q, a = e + v_r(s) but since s coprime, v_r(s)=0, so a = e. So d_r = r^{1} = r. So minimal d is smallest prime factor of q. So answer = p / min_prime(q). That gives x = (q * s) / min_prime = (q/min_prime) * s. Since q/min_prime is integer and may still have other prime factors. Is this the largest divisor not divisible by q? Let's test: p=65=30, q=6, min_prime=2 => x=15. Divisors of 30 not divisible by 6: 30? 30/6=5, divisible, no. 15? 15 not divisible by 6, yes. 10? 10 not divisible by 6. 5,3,2,1. Largest is 15. So correct. Another: p=125=60, q=12, min_prime=2 => x=30. Divisors of 60 not divisible by 12: 60? divisible; 30? 30/12=2.5 not divisible; 20? 20 not divisible; 15? 15; 10; etc. Largest is 30? Check 30, yes. So correct.
Now consider p = q^2. Then p = ∏ r^{2e}. For each r, a = 2e, so d_r = r^{2e - e + 1} = r^{e+1}. Minimal d would be for the smallest prime factor (lowest base) raised to e+1. But maybe a different prime with smaller exponent? Actually exponent e+1 might be larger than another prime's exponent? But base matters. For example, q=12=2^23^1. Then p = 12^2 = 2^43^2. For r=2: e=2, a=4, d=2^{4-2+1}=2^3=8; for r=3: e=1, a=2, d=3^{2-1+1}=3^2=9. So min d = 8, answer = 144/8=18. Check divisors of 144 not divisible by 12: The largest? 144? divisible; 72? 72/12=6 divisible; 48? 48/12=4 divisible; 36? 36/12=3 divisible; 24? 24/12=2 divisible; 18? 18/12=1.5 not divisible. Next is 16? 16 not divisible, but 18 is larger. So 18 is largest, matches.
Now consider q=2^2 * 3^2 = 36, p = 36^2 = 2^4*3^4 = 1296. d2=2^{4-2+1}=2^3=8, x=1296/8=162; d3=3^{4-2+1}=3^3=27, x=1296/27=48. So best=162. Check divisors of 1296 not divisible by 36: 1296? divisible; 648? /36=18 divisible; 432? 432/36=12 divisible; 324? 324/36=9 divisible; 216? 216/36=6 divisible; 162? 162/36=4.5 not divisible; So largest is 162? What about 192? Not divisor. So correct.
Thus algorithm seems consistent.
Now, we need to produce answer for each test case.
Now, check complexity when q has large prime factor near 1e9, say q is prime ~1e9+7? But q <= 1e9, so could be 999999937 (prime). Then factoring q will require trial division up to sqrt(q) ~31623, but we can check up to that, and after loop if q>1, we treat it as prime. That's fine. Then a = exponent of that prime in p. Since p divisible by q, a >=1. We compute a by while loop: p % r == 0, but r is huge, so p % r maybe 0? Actually p divisible by r at least once. But we need to compute exponent a. Could be >1 if p contains r^2? But r^2 > 1e18? Since r ~ 1e9, r^2 = 1e18, so p up to 1e18 could have at most 2 factors of r. So a is either 1 or 2. So while loop runs at most 2 times, cheap.
Now, what about q with many small prime factors? We'll factor quickly.
Now, we should also consider the possibility of overflow when computing r**(a - e + 1) if exponent large but base small. For r=2, exponent a - e + 1 could be up to ~60, 2^60 ~ 1e18, fits in Python integer. So fine.
Now, we should also consider that p may be divisible by q but p // d might overflow? It's within p, so within 1e18.
Now, is there any corner case where p is divisible by q but after dividing by d, the result might still be divisible by q? We argued that it's not because for that prime r we reduced exponent to e-1. But what if there is another prime factor that also becomes deficient due to the division? That cannot happen because we only divided by powers of r, which only affect exponent of r. So other primes' exponents remain >= e (since originally a_j >= e_j, and we didn't change them). So x is not divisible by q. Good.
Thus solution is sound.
Now, let's think about proof: For any divisor x of p not divisible by q, write x = p / d for some divisor d of p. Since x not divisible by q, there exists prime r with v_r(x) < v_r(q) = e. But v_r(x) = a - v_r(d). So a - v_r(d) < e => v_r(d) > a - e. Since v_r(d) integer, v_r(d) >= a - e + 1. Thus d >= r^{a - e + 1}. Hence d >= min_{r} r^{a - e + 1}. And we can achieve d = min_{r} r^{a - e + 1} by picking r that minimizes that value. So max x = p / min_{r} r^{a - e + 1}.
Now, we need to compute a = v_r(p) for each r dividing q. Since p divisible by q, a >= e. So a - e + 1 >= 1.
Implementation steps in code:
def solve():
import sys
input = sys.stdin.readline
t = int(input().strip())
for _ in range(t):
p_str, q_str = input().split()
p = int(p_str)
q = int(q_str)
if p % q != 0:
print(p)
continue
# factorize q
temp_q = q
factors = [] # list of (prime, exponent)
i = 2
while i * i <= temp_q:
if temp_q % i == 0:
exp = 0
while temp_q % i == 0:
temp_q //= i
exp += 1
factors.append((i, exp))
i += 1 if i == 2 else 2? Actually we can increment by 1; but 31623*50 ~1.5e6, incremental by 1 is fine. However, we can speed by checking 2 then odd numbers.
if temp_q > 1:
factors.append((temp_q, 1))
best = 0
for r, e in factors:
# compute exponent a of r in p
a = 0
temp_p = p
while temp_p % r == 0:
temp_p //= r
a += 1
# Since p % q == 0, we have a >= e, but just in case if not? It must be.
d = pow(r, a - e + 1)
candidate = p // d
if candidate > best:
best = candidate
print(best)
But wait: We are recomputing a by dividing temp_p = p each time. That's fine. However, note that for each factor we are recomputing from scratch, which might be a bit inefficient but fine. However, we could compute a using the original p and dividing, but careful: If we modify p (like p //= r) in loop, that would change p for subsequent primes. So we use temp_p.
Alternatively, we can compute a using while p % r == 0 on the original p, but we need to not modify p. We can do:
a = 0
tmp = p
while tmp % r == 0:
tmp //= r
a += 1
That works.
Now, we should consider if there is any scenario where a - e + 1 is huge leading to d > p? That can't happen because r^{a - e + 1} divides p, so d <= p.
Now, check with sample: Good.
Now, test a few more cases:
Case: t=1, p=1000000000000000000, q=1000000000. We already computed answer ~976562500000000. Let's compute exactly: p=10^18, q=10^9. factor q: 2^9 5^9. a2: count exponent of 2 in p: p=10^18=2^18 5^18, so a2=18. e2=9 => a - e + 1=10 => d2=2^10=1024 => candidate2 = p/1024 = 10^18/1024 = (10^18 >> 10) = 976562500000000? Actually 10^18 / 1024 = (10^18) * (1/1024) = 976562500000000 exactly? Because 10^18 / 2^10 = 5^18 * 2^8 = 5^18 * 256. 5^18 = 3814697265625, times 256 = 976562500000000 (since 5^182^8 = (5^92^4)^2? Let's compute: 5^9 = 1953125, 2^4=16, product=31250000, square=976562500000000). So candidate2 = 976562500000000. For 5: a5=18, e5=9 => d5=5^10=9765625, candidate5=10^18/9765625 = 102400000000. So best=976562500000000.
Check if candidate2 divisible by q? v2(candidate2)=8 (<9), so not divisible. So correct.
Now, test a case where q has a prime factor with exponent larger than 1, and p has huge exponent for that prime, but a smaller base yields smaller d. For example, q = 2^10 * 3^2, p = 2^100 * 3^100. Then d2 = 2^{100-10+1}=2^{91}, d3 = 3^{100-2+1}=3^{99}. Which is smaller? 2^91 vs 3^99. Compare logs: 91ln2 ≈ 910.693=63.1, 99ln3≈991.099=108.8, so 2^91 is much smaller. So min d = 2^91, answer = p / 2^91 = 2^9 * 3^100 = 512 * 3^100. That's huge. So answer seems plausible.
Now, verify with code.
Now, edge case: p divisible by q, but q = 1? Not allowed.
Now, think about potential issue: p may be 0? No, p >=1.
Now, if p is divisible by q, but q has a prime factor r where a = e exactly, then d = r^1, candidate = p/r. That candidate might be divisible by q? Let's test: p = q * k, with k not necessarily coprime. If a = e, that means the exponent of r in p is exactly e, not more. Then candidate = p/r reduces exponent of r to e-1, so deficiency. But could it still be divisible by q because other primes? For q to divide candidate, we would need for all primes s, v_s(candidate) >= e_s. For r, we have e-1 < e, so fails. So candidate is not divisible by q. Good.
Now, what if q has multiple prime factors and we pick the one with minimal d. That d is a divisor of p. But could it be that after dividing by that d, the resulting x is still divisible by q? Since we chose the prime r where d = r^{a - e + 1}, we ensure v_r(x)=e-1, so fails. So safe.
Now, double-check the math: For each r, d_r = r^{a - e + 1}. But what if for some r, a - e + 1 = 0? That would happen if a = e - 1, which is impossible because a >= e. So always at least 1.
Thus answer is straightforward.
Now, one more nuance: Our factorization of q may include primes that are not actually prime? In trial division, if we iterate i up to sqrt and divide out factors, we add i as prime. But i might be composite if we don't skip composites. However, since we start from 2 and divide out all factors of i, by the time we reach a composite number, its prime factors would have been divided out already, so the composite will not divide the remaining number. So it's safe; we only add i when temp_q % i == 0, and then we divide out all factors of i, so i is guaranteed to be prime at that point because any composite factor would have smaller prime factors that would have been removed earlier. So we don't need a sieve.
Now, implement solve() accordingly.
Now, think about possible optimization: For each test case, we could compute the minimal d without storing all candidates. But storing is fine.
Now, confirm with another example: p=1e18, q=2. Then p%2=0. factors: (2,1). a = v2(p) = count trailing zeros of p. 1e18 = 10^18 = 2^18 * 5^18, so a=18, e=1 => d=2^{18-1+1}=2^{18}=262144, candidate = p/2^18 = 5^18 * 2^0? Actually p / 2^18 = 2^18*5^18 / 2^18 = 5^18 = 3814697265625. So answer = 5^18. Check: largest divisor of p not divisible by 2 is the odd part, i.e., p with all factors of 2 removed, which is 5^18. So correct.
Now, p=2^60 (approx 1.15e18), q=4. Then p divisible by 4. factors: (2,2). a=60, e=2 => d=2^{60-2+1}=2^{59}, candidate = 2^{60}/2^{59}=2. So answer 2. Divisors of 2^60 not divisible by 4: those with exponent of 2 <=1. Largest such is 2^1=2. So correct.
Now, consider p=2^60 * 3^60, q=6. factors: (2,1),(3,1). a2=60, e2=1 => d2=2^{60}, candidate2=3^{60}. a3=60, d3=3^{60}, candidate3=2^{60}. So best = max(3^60,2^60). 3^60 > 2^60, so answer 3^60. Largest divisor not divisible by 6 would be either the part without 2 or without 3. Since 3^60 > 2^60, answer = 3^60. Check: 3^60 is divisor, and it's not divisible by 6 because lacks factor 2. So correct.
Now, consider p = 2^60 * 3^30, q=6. a2=60, d2=2^{60} => candidate2=3^30; a3=30, d3=3^{30} => candidate3=2^60. So best = 2^60 (since 2^60 > 3^30? 2^60 ≈ 1.15e18, 3^30 ≈ 2.05e14, so 2^60 bigger). So answer = 2^60. Check: 2^60 not divisible by 6 because no factor 3. So largest divisor lacking either 2 or 3 is max(3^30,2^60). Indeed 2^60 is larger.
All good.
Thus implementation is ready.
But we must ensure we treat p and q as Python ints, and that the while loops for exponent counting are efficient. However, note that when a is large (like 60), the while loop does 60 divisions. That's fine.
Now, also note that we can compute a more efficiently using built-in functions? Not needed.
Now, an alternative: Instead of factoring q each time, we could compute the minimal d by iterating over primes dividing q. But factoring is necessary.
Now, consider if q is a perfect power of a prime, we will have only one factor. So best is that candidate.
Now, we must ensure to handle large numbers when computing pow(r, a - e + 1). But Python's pow with integers is fine.
Now, let's test with some random small values via brute force to ensure correctness.
We could write a quick brute for small p and q to compare. But for reasoning, let's do mental checks.
Edge case: p = q^2 where q is prime. Then answer = p / q = q. Since the divisors not divisible by q are those not multiples of q. For p = q^2, divisors: q^2, q, 1. q is divisible by q? Actually q is divisible by q (since q%q==0). Condition requires x not divisible by q. So q is not allowed. So only 1 is allowed? Wait, need to re-evaluate: For p = q^2 where q is prime, say q=3, p=9. Divisors: 9,3,1. Condition: x such that p divisible by x, and x not divisible by q. 9 divisible by 3? 9%3=0, so x=9 is divisible by q, not allowed. 3 is divisible by 3? 3%3=0, so not allowed. 1 is not divisible by 3, allowed. So answer should be 1. But our algorithm for q prime: a = v_q(p) = 2, e=1 => d = q^{2-1+1}=q^2 = p, candidate = p/p = 1. So answer 1. Good.
Now, what about p = q^2 where q = 4? q=4=2^2, p=16. divisors: 16,8,4,2,1. Not divisible by 4: those not multiple of 4: 2,1. Largest is 2. Our algorithm: factor q: (2,2). a = v2(16)=4, e=2 => d=2^{4-2+1}=2^3=8, candidate=16/8=2. Good.
Now, test p = q^2 but q = 6, p=36. divisors: 36,18,12,9,6,4,3,2,1. Not divisible by 6: those not multiple of 6: 4? 4%6 !=0, so allowed; 9 allowed; 3,2,1 allowed; 18? 18%6=0 not allowed; 12%6=0 not; 6 not; 36 not. Largest allowed? 9? 9 is allowed, 4 is allowed, but which is larger? 9 > 4, so answer should be 9. Our algorithm: q=6=23. a2=2 (since 36=2^23^2? Actually 36=2^23^2? 6^2 = (23)^2 = 2^2*3^2, yes), e2=1; d2=2^{2-1+1}=2^2=4, candidate2=36/4=9. a3=2, e3=1; d3=3^{2-1+1}=3^2=9, candidate3=36/9=4. Max=9. So correct.
Now, consider p = 235=30, q=6. p%6=0. factors: (2,1),(3,1). a2=1, d2=2^{1-1+1}=2, candidate2=15; a3=1, d3=3, candidate3=10. Max=15. Divisors not divisible by 6: 30? divisible; 15 not; 10 not; 6? not in divisors? Actually divisors of 30: 30,15,10,6,5,3,2,1. Allowed: 15,10,5,3,2,1. Largest 15. Good.
Now, test case: p=2^3 * 3^3 = 216, q=12 (2^2*3). a2=3, e2=2 => d2=2^{2}=4, candidate=54; a3=3, e3=1 => d3=3^{3}=27, candidate=8. Max=54. Divisors not divisible by 12: Let's list some: 216 (divisible), 108 (108/12=9 divisible), 72 (72/12=6 divisible), 54 (54/12=4.5 not), 36 (divisible), 27 (27/12=2.25 not), 24 (24/12=2 divisible), 18 (18/12=1.5 not), etc. Largest among allowed is 54? What about 96? Not divisor. So 54 seems max.
Now, test p=2^3 * 3^3, q=18 (2*3^2). a2=3, e2=1 => d2=2^{3}=8, candidate=27; a3=3, e3=2 => d3=3^{2}=9, candidate=24. Max=27? But 27 is 3^3, check if 27 divisible by 18? 27%18=9, not divisible. So allowed. 24 is also allowed. 27 > 24, so answer 27. Divisors: 216,108,72,54,36,27,24,18,12,9,8,6,4,3,2,1. Allowed not divisible by 18: 27,24,12,9,8,6,4,3,2,1. Largest 27. So correct.
Now, consider if q has a prime factor that appears with exponent 0? No.
Now, think about a scenario where p is extremely large but q is composite with many primes. Our algorithm handles.
Now, potential pitfalls: The while loop for factorization of q: if we increment i by 1, it's fine. But we might consider that after dividing by 2, we can increment only odd numbers to speed up. But not necessary.
Now, we should also consider that q might be a perfect square of a prime, our factorization will have (prime,2). Fine.
Now, we must ensure to use integer sqrt for loop condition: while i * i <= temp_q. Since temp_q decreases, we could compute sqrt each time, but i*i is fine.
Now, one more nuance: Our calculation of a (exponent in p) might be impacted by the fact that p is divisible by q, so a >= e. But what if p has more factors than we think? No issue.
Now, let's think about a scenario where q has a prime factor r, but p contains r with exponent a that is less than e? That would mean p not divisible by q, but we already check p % q != 0, so if p%q==0, it ensures that for all primes r, a >= e. However, p % q == 0 is a necessary and sufficient condition? For composite q, p%q==0 ensures that p is divisible by q as an integer, which implies that for each prime factor, the exponent condition holds? Actually consider p=4, q=6? 4%6 !=0, so not in this branch. For p=12, q=6, 12%6=0, and indeed exponents: v2(12)=2>=1, v3(12)=1>=1. So yes. But could there be a case where p%q==0 but some prime exponent in p is less than that in q? That would contradict p%q==0 because if a prime exponent is less, then q would not divide p. So impossible.
Thus safe.
Now, final code.
But wait, there is a potential oversight: Our reasoning that the optimal x is p divided by the minimal d among r^{a - e + 1} works if we can indeed achieve that minimal d. However, what if the minimal d is not a divisor of p? But it is, as argued. But could there be a scenario where the minimal d is not exactly that power because the prime r that gives minimal d may have exponent a - e + 1, but maybe dividing by that exact power yields an x that still is divisible by q? We already argued it does not, because v_r(x) becomes e-1. However, we must ensure that after dividing by that d, we haven't inadvertently reduced some other prime's exponent below required? No, we only divided by powers of r. So other primes' exponents remain unchanged, and they were at least e. So they remain at least e. So x is not divisible by q because for r it fails. So it's valid.
Thus answer is correct.
Now, implement.
But before finalizing, think about possible alternative: Instead of factoring q, we could compute the minimal d by iterating over divisors of q? That would be less efficient.
Now, code structure:
def solve():
import sys
input = sys.stdin.readline
t = int(input())
out_lines = []
for _ in range(t):
line = input().strip()
while line == '':
line = input().strip()
p, q = map(int, line.split())
if p % q != 0:
out_lines.append(str(p))
continue
# factorize q
q_temp = q
factors = []
# handle 2 separately
if q_temp % 2 == 0:
exp = 0
while q_temp % 2 == 0:
q_temp //= 2
exp += 1
factors.append((2, exp))
i = 3
while i * i <= q_temp:
if q_temp % i == 0:
exp = 0
while q_temp % i == 0:
q_temp //= i
exp += 1
factors.append((i, exp))
i += 2
if q_temp > 1:
factors.append((q_temp, 1))
best = 0
for r, e in factors:
# compute exponent of r in p
a = 0
temp = p
while temp % r == 0:
temp //= r
a += 1
# now compute candidate
d = pow(r, a - e + 1)
candidate = p // d
if candidate > best:
best = candidate
out_lines.append(str(best))
sys.stdout.write("\n".join(out_lines))
That should be correct.
Now, test with sample:
Input:
3
10 4
12 6
179 822
Output:
10
4
179
Matches.
Now, consider additional test:
1
12 4
Our code: p%4=0, factor q: 4 -> (2,2). a = v2(12)=2 (since 12=4*3, 2^2 *3). e=2 => a - e + 1 = 1 => d = 2^1=2, candidate = 12/2=6. Check: 6 not divisible by 4? 6%4=2, not divisible. Divisors of 12 not divisible by 4: 6,3,2,1. Largest 6. So answer 6. Good.
Now, test: 1
16 8
p=16, q=8: factor 8 = (2,3). a=v2(16)=4, e=3 => d=2^{4-3+1}=2^2=4, candidate=16/4=4. Divisors of 16 not divisible by 8: 4,2,1. Largest 4. Good.
Now, test: 1
72 36
p=72, q=36: factor 36 = (2,2),(3,2). a2=3, e2=2 => d2=2^{2}=4, candidate2=18; a3=2, e3=2 => d3=3^{1}=3? Wait a3=2, e3=2 => a-e+1=1 => d3=3^1=3, candidate3=24. So best=24. Divisors: largest not divisible by 36 is 24? Check: 72 divisible; 36 divisible; 24 not; 18 not; 12 not; 9; etc. So 24 largest. Good.
Now, test: 1
100 20
p=100, q=20. p%20=0? 100%20=0, yes. Factor 20: (2,2),(5,1). a2: v2(100)=2? 100=425=2^25^2, so a2=2, e2=2 => d2=2^{2-2+1}=2, candidate2=50; a5=2, e5=1 => d5=5^{2-1+1}=5^2=25, candidate5=4. Max=50. Check divisors of 100 not divisible by 20: 100? 100/20=5, divisible, not allowed. 50? 50/20=2.5 not divisible, allowed. 25? 25 not divisible; 20? no; 10? 10 not divisible; 5,4,2,1. Largest allowed is 50. Good.
Now, test: 1
100 10
p%10=0. factor 10: (2,1),(5,1). a2=2, d2=2^{2}=4, candidate2=25; a5=2, d5=5^{2}=25, candidate5=4. Max=25. Divisors of 100 not divisible by 10: 100? divisible; 50? 50/10=5 divisible; 25? 25/10=2.5 not; 20? divisible; 10? no; 5? not; 4? not; 2? not; 1. Largest 25. Good.
Now, test: 1
100 25
q=25 (5^2). factor: (5,2). a5=2 (since 100=2^2*5^2), e=2 => d=5^{2-2+1}=5^1=5, candidate=20. Divisors of 100 not divisible by 25: 100? 100/25=4 divisible; 50? 50/25=2 divisible; 25? divisible; 20? 20/25 not integer, so not divisible, allowed; 10? not; 5? not; 4,2,1. Largest allowed is 20. Good.
Now, test: 1
100 40
q=40=2^3*5. p=100, 100%40=20 !=0? Actually 100%40=20, not 0. So answer 100. But is 100 not divisible by 40? Condition: p not divisible by q, so answer p=100. Check: 100 is divisible by 40? 100/40=2.5, not integer, but condition says p divisible by x; we need x dividing p. So x=100 works because 100 divides 100, and 100 is not divisible by 40? 100%40=20, so not divisible, so valid. So answer 100 is correct.
Now, test: 1
40 100
p=40, q=100, p%q? 40%100=40 !=0, answer 40. Check: x=40 divides 40, and 40 not divisible by 100, yes. So answer 40.
Now, test: 1
1000000000000000000 1000000000
We computed answer 976562500000000.
Now, test: 1
999999937000000000? but not necessary.
Thus code seems correct.
Now, we should ensure we use fast I/O because t is small, but still.
Now, final step: Provide answer.
But before finalizing, we need to re-read problem statement: "p_i is divisible by x_i; x_i is not divisible by q_i." That's what we used.
Also, note that p_i up to 10^18, q_i up to 10^9.
Now, any possibility of overflow when computing pow(r, a - e + 1) if a - e + 1 is large? For r=2, a max is about 60, so exponent up to 60, okay. For r=3, exponent up to maybe 38? Since 3^38 ~ 4.5e18 > 1e18, but a can't be > 38 because p <= 1e18. So exponent <= 38. So safe.
Now, we might consider using pow(r, a - e + 1, something) but we need exact integer. Use pow(r, a - e + 1) which returns int.
Now, we should also consider that we can compute candidate directly without computing d: candidate = p // (r ** (a - e + 1)). That's fine.
Now, what about memory? Fine.
Now, we should also consider that when we compute temp = p while counting a, we are dividing temp by r repeatedly. This may be slow if a is 60 and we do it for each prime factor, but it's fine.
Alternatively, we could compute a using the built-in function: while p % r == 0: p //= r? But that would alter p. So we must use a copy.
But we could compute a by using a loop that divides a temporary variable. That's fine.
Now, potential improvement: Instead of factoring q for each test case, we could precompute primes up to 31623 using sieve and then use them for factorization. But not necessary.
Now, let's consider if there's any tricky scenario where the answer is not p divided by min d because maybe p / min_d might still be divisible by q? Let's double-check the condition: We want x such that p divisible by x, and x not divisible by q. In our candidate x = p / d, where d = r^{a - e + 1}. For the chosen r, v_r(x) = a - (a - e + 1) = e - 1 < e, so fails. So x is not divisible by q. So valid.
But is it possible that for the r that gives min d, the exponent a - e + 1 is such that after dividing, the exponent of some other prime becomes less than required? No.
Thus done.
Now, think about if there are multiple primes that achieve the same minimal d, any works.
Now, we should also consider that the answer might be p divided by d, but what if p/d is still divisible by q? That would require that for all primes s, v_s(p/d) >= e_s. For the chosen r, v_r = e-1, which is not >= e, so fails. So it's fine.
Now, confirm with a scenario where q has a prime factor r, but p has a huge exponent a, so d is large. For the minimal d, we might choose r with smallest r^{a - e + 1}. That's fine.
Now, we must consider that p might be divisible by q, but q might be 1? Not allowed.
Now, ensure to handle t=0? t>=1.
Now, final code.
But wait, the problem statement says "q_i (2 ≤ q_i ≤ 10^9)". So q >= 2, so no q=1.
Now, I'll produce final answer.
But I recall that there is Codeforces problem "Oleg and shares" maybe, but this is CF Round # (I recall a problem "Oleg and shares"? Actually this is problem 1370B? Not sure. But the solution approach is as described.
Now, one more nuance: In the factorization of q, we should include all prime factors. For q = 1e9, which is 10^9, factorization yields 2^9 * 5^9. Our loop while i*i <= q_temp will check i up to sqrt(10^9) ≈ 31623. After dividing out 2 and odd numbers, we will get q_temp maybe 1 or >1. After dividing out all small factors, if q_temp > 1, it's prime. So it's fine.
Now, check for q = 999999937 (prime close to 1e9). Our loop will go up to 31623, and since it's prime, q_temp remains that number >1, and we add it as prime with exponent 1. Good.
Now, we must be careful that when we compute a (exponent of r in p) for large prime r, the while loop: while temp % r == 0: temp //= r. Since r is large, temp % r is computed using big integers, but that's fine.
Now, test with p = 999999937^2? That would be >1e18? 999999937^2 ≈ 10^18 - something, maybe around 1e18. Could be slightly less than 1e18? Actually (1e9 - 63)^2 ≈ 1e18 - 126e9 + ... so around 0.999e18, could be ≤ 1e18. So possible. Then p could be up to that. But our code would need to compute a for r = 999999937. That would require while loop: first check p % r == 0? Since p = r^2, yes. Then temp //= r will give r, then check temp % r == 0, true, then temp//=r gives 1. So two iterations, fine.
Now, check memory and time: fine.
Thus final solution is ready.
Now, I will produce final answer in the required format.
But wait: There is also a scenario where p is divisible by q, but the minimal d might be something like r^{a - e + 1} but that d might not be the smallest factor we need to remove because we could also consider dividing by some factor that is not a pure power but still smaller than that? As argued, any valid d must contain at least that factor, so d >= that factor. So cannot be smaller.
Now, think about a counterexample: Suppose q = 12 = 2^2 * 3. p = 2^3 * 3^2 = 72. As computed, d2=4, d3=9. min d = 4. x = 18. Could we get a larger x by dividing by 2? That would give x=36, but 36 is divisible by 12? 36/12=3, so not allowed. Dividing by 3 gives 24, but 24 is divisible by 12? 24/12=2, so not allowed. Dividing by 6? 72/6=12, divisible by 12. Dividing by 8? 72/8=9, not integer? 8 not divisor? Actually 8 does not divide 72. So 18 is the largest.
Thus correct.
Now, final code.
But also note: There might be a simpler approach: For each prime factor of q, compute candidate = p divided by that prime^(v_p(p) - v_p(q) + 1) and take max. That's what we do.
Now, implement.
Now, let's write final solution.
But before finalizing, consider reading input: there may be extra spaces, but split handles.
Now, test with sample:
Input:
3
10 4
12 6
179 822
Our code: for 10 4: p%4 !=0 => print 10. For 12 6: factors of 6: (2,1),(3,1). a2=2 => candidate2=12//(2^2)=3; a3=1 => candidate3=12//(3^1)=4; max=4 => print 4. For 179 822: 179%822 !=0 => print 179. Output matches.
Now, I think it's correct.
Now, I'll write final solution explanation and code. The problem requires finding the greatest divisor (x) of (p) that is not divisible by (q).
If (p) is not a multiple of (q), the answer is simply (p) because (p) itself satisfies the conditions.
Otherwise, (q \mid p). Let the prime factorisation of (q) be
[
q = \prod_{i} r_i^{e_i}.
]
For each prime (r_i), let (a_i = v_{r_i}(p)) (the exponent of (r_i) in (p)). Since (q \mid p), we have (a_i \ge e_i).
For a divisor (x = p / d) (where (d) divides (p)) to be not divisible by (q), there must exist at least one prime (r_i) such that
[
v_{r_i}(x) = a_i - v_{r_i}(d) < e_i \quad\Longrightarrow\quad v_{r_i}(d) \ge a_i - e_i + 1.
]
Hence any valid (d) must be at least (r_i^{a_i - e_i + 1}) for some (i).
The smallest possible (d) is therefore
[
d_{\min} = \min_i ; r_i^{a_i - e_i + 1}.
]
Taking (d = d_{\min}) gives the largest possible (x = p / d_{\min}), and this (x) is valid because the chosen prime satisfies the deficiency condition.
Thus the algorithm is:
- If (p % q \ne 0), output (p).
- Otherwise, factorise (q).
- For every prime factor (r) with exponent (e) in (q):
- Compute (a = v_r(p)) by repeated division.
- Compute candidate (x = p ;/; r^{,a - e + 1}).
- Output the maximum among these candidates.
Since (t \le 50), (q \le 10^9) and (p \le 10^{18}), trial division up to (\sqrt{q}) and the exponent computations are fast enough.
import sys
def solve() -> None:
it = iter(sys.stdin.read().strip().split())
t = int(next(it))
out = []
for _ in range(t):
p = int(next(it))
q = int(next(it))
if p % q != 0:
out.append(str(p))
continue
# factorise q
temp_q = q
factors = []
# handle factor 2 separately
if temp_q % 2 == 0:
e = 0
while temp_q % 2 == 0:
temp_q //= 2
e += 1
factors.append((2, e))
i = 3
while i * i <= temp_q:
if temp_q % i == 0:
e = 0
while temp_q % i == 0:
temp_q //= i
e += 1
factors.append((i, e))
i += 2
if temp_q > 1:
factors.append((temp_q, 1))
best = 0
for r, e in factors:
# exponent of r in p
a = 0
tmp = p
while tmp % r == 0:
tmp //= r
a += 1
# a >= e because q | p
d = pow(r, a - e + 1)
cand = p // d
if cand > best:
best = cand
out.append(str(best))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()