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 $t$ pairs of integers $p_i$ and $q_i$ and for each pair decided to find the greatest integer $x_i$, such that:   $p_i$ is divisible by $x_i$;  $x_i$ is not divisible by $q_i$.  Oleg is really good at division and managed to find all the answers quickly, how about you?


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 50$) — the number of pairs.

Each of the following $t$ lines contains two integers $p_i$ and $q_i$ ($1 \le p_i \le 10^{18}$; $2 \le q_i \le 10^{9}$) — the $i$-th pair of integers.


-----Output-----

Print $t$ integers: the $i$-th integer is the largest $x_i$ such that $p_i$ is divisible by $x_i$, but $x_i$ is not divisible by $q_i$.

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


-----Example-----
Input
3
10 4
12 6
179 822

Output
10
4
179



-----Note-----

For the first pair, where $p_1 = 10$ and $q_1 = 4$, the answer is $x_1 = 10$, since it is the greatest divisor of $10$ and $10$ is not divisible by $4$.

For the second pair, where $p_2 = 12$ and $q_2 = 6$, note that   $12$ is not a valid $x_2$, since $12$ is divisible by $q_2 = 6$;  $6$ is not valid $x_2$ as well: $6$ is also divisible by $q_2 = 6$.  The next available divisor of $p_2 = 12$ is $4$, which is the answer, since $4$ is not divisible by $6$.

Difficulty: MEDIUM

Solutions:
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	d = {}
	ans = []
	if p % q != 0:
		print(p)
	else:
		for i in range(2, int(q ** 0.5) + 1):
			count = 0
			while q % i == 0:
				count += 1
				q //= i
			if count != 0:
				d[i] = count
		if q >= 2:
			d[q] = 1
		for i in d:
			tmp = p
			while tmp % i == 0:
				tmp //= i
			tmp *= i ** (d[i] - 1)
			ans.append(tmp)
		print(max(ans))


def factor(q):
	i = 2
	counters = {}
	while i * i <= q:
		while q % i == 0:
			counters.setdefault(i, 0)
			counters[i] += 1
			q //= i
		i += 1
	if q > 1:
		counters.setdefault(q, 0)
		counters[q] += 1
	return counters

def factor2(keys, p):
	p_divisors = {}
	for k in q_divisors:
		p_divisors.setdefault(k, 0)
		while p % k == 0:
			p //= k
			p_divisors[k] += 1
	return p_divisors
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		q_divisors = factor(q)
		p_divisors = factor2(q_divisors.keys(), p)
		ans = 1
		for k in q_divisors:
			ans = max(ans, p // k ** (p_divisors[k] - q_divisors[k] + 1))
		print(ans)


from queue import PriorityQueue
from queue import Queue
import math
from collections import *
import sys
import operator as op
from functools import reduce
MOD = int(1000000000.0 + 7)
input = sys.stdin.readline

def ii():
	return list(map(int, input().strip().split()))

def ist():
	return input().strip().split()
for _ in range(int(input())):
	(p, q) = ii()
	x = q
	(arr, j) = ([], 2)
	while j * j <= x:
		if x % j == 0:
			arr.append(j)
			while x % j == 0:
				x = x // j
		j += 1
	if x > 1:
		arr.append(x)
		x = 1
	for k in arr:
		y = p
		while y % q == 0:
			y = y // k
		x = max(x, y)
	print(x)


from sys import *
input = stdin.readline
import random
from bisect import *
import math
from collections import *
import operator
from heapq import *
from itertools import *
inf = 10 ** 18
mod = 10 ** 9 + 7
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	j = 2
	x = q
	a = []
	while j * j <= x:
		if x % j == 0:
			a.append(j)
			while x % j == 0:
				x //= j
		j += 1
	if x > 1:
		a.append(x)
		x = 1
	for i in a:
		k = p
		while k % q == 0:
			k //= i
		x = max(x, k)
	print(x)


import math

def prime_factors(a):
	s = int(math.sqrt(a)) + 1
	pfs = {}
	for i in range(2, s + 1):
		if i > a:
			break
		ic = 0
		while a % i == 0:
			a //= i
			ic += 1
		if ic > 0:
			pfs[i] = ic
	if a > 1:
		pfs[a] = 1
	return pfs
t = int(input())
results = []
for i in range(t):
	pq = input().split()
	p = int(pq[0])
	q = int(pq[1])
	if p % q != 0:
		results.append(p)
	else:
		result = 1
		pfs = prime_factors(q)
		r = p // q
		pfs_2 = {}
		for k in pfs.keys():
			ic = 0
			while r % k == 0:
				r //= k
				ic += 1
			if ic > 0:
				pfs_2[k] = ic
		for (k, v1) in pfs.items():
			if k in pfs_2:
				v2 = pfs_2[k]
				prod = p // k ** (v2 + 1)
			else:
				prod = p // k
			if prod > result:
				result = prod
		results.append(result)
for result in results:
	print(result)


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
from itertools import *
from heapq import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	(p, q) = ma()
	if q > p or p % q:
		print(p)
		continue
	di = {}
	qq = q
	if q % 2 == 0:
		di[2] = 0
		while qq % 2 == 0:
			qq = qq // 2
			di[2] += 1
	for i in range(3, int(q ** 0.5) + 2, 2):
		if qq % i == 0:
			di[i] = 0
			while qq % i == 0:
				qq = qq // i
				di[i] += 1
	ff = -1
	if qq > 2:
		di[qq] = 1
	pp = p
	for i in di.keys():
		ha = 0
		while pp % i == 0:
			pp = pp // i
			ha += 1
		re = ha - di[i] + 1
		ff = max(ff, p // i ** re)
	print(max(ff, 1))


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
from itertools import permutations, combinations

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = 998244353
for t in range(int(data())):
	(p, q) = mdata()
	ans = 1
	for i in range(1, int(q ** 0.5) + 1):
		if q % i == 0:
			if i != 1:
				a = p
				while a % q == 0:
					a //= i
				ans = max(ans, a)
			a = p
			while a % q == 0:
				a //= q // i
			ans = max(ans, a)
	out(ans)


for s in [*open(0)][1:]:
	(p, q) = map(int, s.split())
	l = []
	j = 2
	a = q
	while j * j <= a:
		if a % j == 0:
			l += [j]
			while a % j == 0:
				a //= j
		j += 1
	if a > 1:
		l += [a]
		a = 1
	for j in l:
		k = p
		while k % q == 0:
			k //= j
		a = max(k, a)
	print(a)
num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()


t = int(input())
for a in range(t):
	(p, q) = map(int, input().strip(' ').split(' '))
	factor = set([q])
	for i in range(2, int(q ** 0.5) + 1):
		if q % i == 0:
			factor.add(i)
			x = q // i
			if x != i:
				factor.add(x)
	if p % q:
		print(p)
	else:
		x = 0
		for i in factor:
			y = p
			while y % i == 0:
				y = y // i
				if y % q:
					x = max(x, y)
					break
		print(x)


import math

def factorize(n):
	factors = []
	count = 0
	while (n % 2 > 0) == False:
		n >>= 1
		count += 1
	if count > 0:
		factors.append(2)
	for i in range(3, int(math.sqrt(n)) + 1):
		count = 0
		while n % i == 0:
			count += 1
			n = int(n / i)
		if count > 0:
			factors.append(i)
		i += 2
	if n > 2:
		factors.append(n)
	return factors
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	ans = -1
	fact = factorize(q)
	for i in fact:
		x = p
		while x % q == 0:
			x = x // i
		ans = max(ans, x)
	print(ans)


def solve():
	l = []
	j = 2
	z = q
	while j * j <= z:
		if z % j == 0:
			l.append(j)
			while z % j == 0:
				z //= j
		j += 1
	if z > 1:
		l.append(z)
	return l
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	l = solve()
	x = 1
	for s in l:
		a = p
		while a % q == 0:
			a //= s
		x = max(x, a)
	print(x)


from collections import Counter

def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
primes = []
is_prime = [True for _ in range(100000)]
is_prime[0] = is_prime[1] = False
for i in range(2, 100000):
	if is_prime[i]:
		primes.append(i)
	for j in range(i * i, 100000, i):
		is_prime[j] = False
t = read_int()
for case_num in range(t):
	(p, q) = read_ints()
	if p % q != 0:
		print(p)
	else:
		cnt = Counter()
		x = q
		for prime in primes:
			if x % prime == 0:
				while x % prime == 0:
					x //= prime
					cnt[prime] += 1
		if x != 1:
			cnt[x] += 1
		lo = int(1e+18)
		y = p
		for key in cnt:
			curr = 0
			while y % key == 0:
				y //= key
				curr += 1
			lo = min(lo, pow(key, curr - cnt[key] + 1))
		print(p // lo)


t = int(input())
while t:
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		lst = []
		i = 2
		while i * i <= q:
			if not q % i:
				lst.append(i)
				if i != q // i:
					lst.append(q // i)
			i += 1
		lst.append(q)
		num = 1
		for k in lst:
			ele = p
			while not ele % k:
				ele //= k
				if ele % q:
					num = max(num, ele)
					break
		print(num)
	t = t - 1


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * (2 * 10 ** 5 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj
mod = 10 ** 9 + 7

def solve(i, t):
	(p, q) = IP()
	if p % q != 0:
		print(p)
	else:
		li = []
		cur = q
		for i in range(2, int(mt.sqrt(q)) + 1):
			if cur % i == 0:
				cnt = 0
				while cur % i == 0:
					cur = cur // i
					cnt += 1
				li.append([i, cnt])
		if cur >= 2:
			li.append([cur, 1])
		mx = -1
		for i in range(len(li)):
			drop = li[i][0]
			till = li[i][1]
			cur = p
			while cur % q == 0:
				cur = cur // drop
			mx = max(mx, cur)
		print(mx)
	return
t = II()
for i in range(t):
	solve(i, t)


def f(n):
	if n == 1:
		return 1
	x = p
	while x % q == 0:
		x //= n
	return x
T = int(input())
for t in range(T):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		res = 1
		for i in range(1, q):
			if i * i > q:
				break
			if q % i == 0:
				res = max(res, f(i), f(q // i))
		print(res)


from math import *
for u in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		l = []
		m = 1
		for i in range(2, int(sqrt(q)) + 1):
			if q % i == 0:
				l.append(i)
				if q // i != i:
					l.append(q // i)
		l.append(q)
		for i in l:
			d = p
			while d % i == 0:
				d = d // i
				if d % q != 0:
					m = max(m, d)
					break
		print(m)


def f(a):
	A = {}

	def one_mnogetel(a):
		for i in range(2, int(a ** 0.5) + 1):
			if a % i == 0:
				return i
		return a
	while one_mnogetel(a) != a:
		g = one_mnogetel(a)
		if g in A:
			A[g] += 1
		else:
			A[g] = 1
		a = a // g
	if a in A:
		A[a] += 1
	else:
		A[a] = 1
	return A
x = int(input())
for _ in range(x):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	elif p == 1:
		print(1)
	elif q == p:
		z = True
		for i in range(2, int(q ** 0.5) + 1):
			if q % i == 0:
				print(q // i)
				z = False
				break
		if z:
			print(1)
	else:
		p0 = int(p)
		A = f(q)
		n = 10 ** 20
		for key in A:
			z = 0
			while p % key == 0:
				p //= key
				z += 1
			n = min(n, key ** (z - A[key] + 1))
		print(p0 // n)


import math
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	(i, x, prime) = (2, q, [])
	while i * i <= q:
		if x % i == 0:
			prime.append(i)
			while x % i == 0:
				x //= i
		i += 1
	if x > 1:
		prime.append(x)
		x = 1
	for i in prime:
		y = p
		while y % q == 0:
			y //= i
		x = max(x, y)
	print(x)


import itertools

def prime_sieve(limit):
	prime = [True] * limit
	prime[0] = prime[1] = False
	for (i, is_prime) in enumerate(prime):
		if is_prime:
			yield i
			for n in range(i * i, limit, i):
				prime[n] = False
primes = list(prime_sieve(10 ** 6))

def factors(n):
	global primes
	prod = 1
	fs = []
	for p in primes:
		if p * prod > n:
			break
		n_ = n
		i = 0
		while n_ % p == 0:
			i += 1
			prod *= p
			n_ = n_ // p
		if i > 0:
			fs.append((p, i))
	if len(fs) == 0:
		fs.append((n, 1))
	return fs

def combinations(xs):
	ss = []
	for (a, n) in xs:
		ys = []
		for i in range(n + 1):
			ys.append((a, i))
		ss.append(ys)
	return itertools.product(*ss)

def divisors(n):
	fs = factors(n)
	ds = []
	for c in combinations(fs):
		v = 1
		for (a, i) in c:
			v *= a ** i
		ds.append(v)
	return sorted(ds)

def main():
	n = int(input())
	for _ in range(n):
		(p, q) = tuple(map(int, input().split()))
		print(f(p, q))

def f(p, q):
	if p % q != 0:
		return p
	ds = factors(q)
	cand = []
	for (d, i) in ds:
		p_ = p
		d_ = d
		while p_ % q == 0:
			if p_ % d_ == 0:
				p_ = p_ // d_
			else:
				break
		cand.append(p_)
	return max(cand)
main()


import sys, math
input = sys.stdin.readline

def findval(p, q):
	for i in range(p, 0, -1):
		if p % i == 0 and i % q != 0:
			return i

def finddiv(n):
	i = 2
	A = []
	while i * i < n:
		if n % i == 0:
			A.append(i)
		i += 1
	for i in range(int(math.sqrt(n)), 0, -1):
		if n % i == 0:
			A.append(n // i)
	return A
T = int(input())
for _ in range(T):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		ans = 1
		A = finddiv(q)
		for i in A:
			v = p
			while v % i == 0 and v % q == 0:
				v = v // i
			ans = max(ans, v)
		print(ans)


import math

def getFactors(num):
	factors = {}
	for i in range(2, int(math.sqrt(num)) + 1):
		while num % i == 0:
			if i not in factors:
				factors[i] = 0
			factors[i] += 1
			num //= i
	if num >= 2:
		if num not in factors:
			factors[num] = 0
		factors[num] += 1
	return factors

def getGCD(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a

def solve(p, q):
	gcd = getGCD(p, q)
	remain = p // gcd
	if gcd != q:
		return p
	gcdFact = getFactors(gcd)
	minDiv = math.inf
	for factor in gcdFact:
		r = remain
		rFact = 0
		while r % factor == 0:
			r //= factor
			rFact += 1
		div = factor ** (rFact + 1)
		minDiv = min(div, minDiv)
	return p // minDiv
T = int(input())
for _ in range(T):
	(p, q) = [int(x) for x in input().split()]
	print(solve(p, q))


t = int(input())
for i in range(0, t):
	l1 = [int(x) for x in input().split()]
	p = l1[0]
	q = l1[1]
	v1 = []
	v2 = []
	for j in range(1, int(q ** 0.5) + 1):
		if q % j == 0:
			if q // j == j:
				v1.append(j)
			else:
				v1.append(j)
				v2.append(q // j)
	for j in range(len(v2) - 1, -1, -1):
		v1.append(v2[j])
	if p % q != 0:
		print(p)
	else:
		j = 0
		max_val = 0
		while True and j < len(v1):
			out = False
			if v1[j] == 1:
				j += 1
				continue
			p_dash = p
			while p_dash % v1[j] == 0:
				p_dash //= v1[j]
				if p_dash % q != 0:
					if p_dash > max_val:
						max_val = p_dash
					out = True
			j += 1
		print(max_val)


import sys
input = sys.stdin.readline
from collections import defaultdict
from math import sqrt

def div(n):
	d = defaultdict(int)
	while n % 2 == 0:
		n //= 2
		d[2] += 1
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			n //= i
			d[i] += 1
	if n > 2:
		d[n] += 1
	return d
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		qf = div(q)
		pf = defaultdict(int)
		for i in qf:
			t = p
			while t % i == 0:
				t //= i
				pf[i] += 1
		x = 0
		for i in pf:
			x = max(x, p // pow(i, pf[i] - (qf[i] - 1)))
		print(x)


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
		continue
	lst = []
	for i in range(1, math.ceil(math.sqrt(q)) + 1):
		if q % i == 0:
			lst.append(i)
			lst.append(q // i)
	lst = list(set(lst))
	lst.sort()
	ans = 0
	for i in range(1, len(lst)):
		x = p
		while x % q == 0:
			x = x // lst[i]
		ans = max(ans, x)
	print(ans)


def find_prime(n):
	ans = []
	i = 2
	x = n
	while i * i <= n:
		if x % i == 0:
			ans.append(i)
			while x % i == 0:
				x //= i
		i += 1
	if x > 1:
		ans.append(x)
	return ans
for _ in range(int(input())):
	(p, a) = map(int, input().split())
	if p % a != 0:
		print(p)
	else:
		mul = find_prime(a)
		ans = 1
		for i in mul:
			t = p
			while t % a == 0:
				t //= i
			ans = max(ans, t)
		print(ans)


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	j = 2
	x = q
	a = []
	while j * j <= x:
		if x % j == 0:
			a.append(j)
			while x % j == 0:
				x //= j
		j += 1
	if x > 1:
		a.append(x)
		x = 1
	for i in a:
		k = p
		while k % q == 0:
			k //= i
		x = max(k, x)
	print(x)


import math
from sys import *
for _ in range(int(input())):
	(n, m) = map(int, iter(stdin.readline().split()))
	(M, D) = ([], {})
	ans = n
	ord = n
	for i in range(2, int(math.sqrt(m)) + 1):
		if m % i == 0:
			while m % i == 0:
				if i in D:
					D[i] += 1
				else:
					D[i] = 1
				m //= i
	if m != 1:
		D.update({m: 1})
	for (key, values) in D.items():
		count = 0
		while n % key == 0:
			n //= key
			count += 1
		req = max(count - values + 1, -999999999)
		val = 1
		for i in range(0, req):
			val *= key
		ans = min(ans, val)
	stdout.write(str(ord // ans) + '\n')


from sys import stdin
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return int(input())

def arrinp():
	return list(map(int, input().split()))
import math
from math import ceil, gcd

def main():
	for _ in range(inp()):
		(p, q) = arrinp()
		if q > p:
			print(p)
			continue
		if p % q != 0:
			print(p)
			continue
		if p == q:
			i = 2
			while p % i != 0:
				i += 1
			print(p // i)
			continue
		factors = primeFactors(q)
		ans = 1
		for fc in factors:
			c = p // fc
			while c % fc == 0:
				c = c // fc
			c = c * fc ** (factors[fc] - 1)
			ans = max(ans, c)
		print(ans)

def primeFactors(n):
	primes = dict()
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes
main()


from math import ceil
for ad in range(int(input())):
	(p, q) = list(map(int, input().split()))
	if p % q != 0:
		print(p)
	else:
		x = []
		y = []
		qq = q
		for i in range(2, int(q ** 0.5) + 2):
			c = 0
			if qq % i == 0:
				x.append(i)
			while qq % i == 0:
				qq //= i
				c += 1
			if c != 0:
				y.append(c)
		if qq != 1:
			x.append(qq)
			y.append(1)
		z = []
		alpha = p
		for i in range(len(x)):
			c = 0
			while p % x[i] == 0:
				c += 1
				p //= x[i]
			z.append(x[i] ** (c - y[i] + 1))
		print(alpha // min(z))


for _ in ' ' * int(input()):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		lst = []
		i = 2
		while i * i <= q:
			if not q % i:
				lst.append(i)
				if i != q // i:
					lst.append(q // i)
			i += 1
		lst.append(q)
		num = 1
		for i in lst:
			ele = p
			while not ele % i:
				ele //= i
				if ele % q:
					num = max(num, ele)
		print(num)


import math
from collections import deque
from sys import stdin, stdout, setrecursionlimit
from string import ascii_letters
letters = ascii_letters[:26]
from collections import defaultdict
input = stdin.readline
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if q > p:
		print(p)
		continue
	if p % q:
		print(p)
		continue
	dels = []
	start = 2
	dels = defaultdict(int)
	while start * start <= q:
		while not q % start:
			dels[start] += 1
			q //= start
		start += 1
	if q != 1:
		dels[q] += 1
	res = 1
	for i in dels.items():
		cnt = 0
		bf = p
		while not bf % i[0]:
			bf //= i[0]
			cnt += 1
		bf = p
		while cnt >= i[1]:
			cnt -= 1
			bf //= i[0]
		res = max(res, bf)
	print(res)


import sys
from collections import Counter
from math import sqrt, ceil
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
T = int(input())
for t in range(T):
	(a, b) = map(int, input().split())
	if a % b:
		print(a)
		continue
	lim = ceil(sqrt(b))
	(b_copy, ans) = (b, 1)
	pc = Counter()
	i = 2
	while i * i <= b:
		if b % i:
			i += 1
			continue
		while b % i == 0:
			pc[i] += 1
			b //= i
		i += 1
	if b > 1:
		pc[b] = 1
	for (p, cnt) in pc.items():
		if a % p:
			continue
		count = 0
		aa = a
		while aa % p == 0 and aa % b_copy == 0:
			count += 1
			aa //= p
		ans = max(ans, aa)
	print(ans)


gans = []
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	q0 = q
	if p % q != 0:
		gans.append(p)
		continue
	i = 2
	u = []
	while i * i <= q:
		if q % i == 0:
			u.append(i)
		while q % i == 0:
			q //= i
		i += 1
	if q != 1:
		u.append(q)
	ans = p
	q = q0
	for i in range(len(u)):
		cur = 1
		while p % cur == 0 and p // cur % q == 0:
			cur *= u[i]
		if p % cur == 0:
			ans = min(ans, cur)
	gans.append(p // ans)
print(*gans, sep='\n')


import math
import os
import sys
from io import BytesIO, IOBase

def fact(n):
	ans = dict()
	while n % 2 == 0:
		ans[2] = ans.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			ans[i] = ans.get(i, 0) + 1
			n = n // i
	if n > 2:
		ans[n] = ans.get(n, 0) + 1
	return ans

def count(n, p):
	count = 0
	while n % p == 0:
		count += 1
		n = n // p
	return count

def main():
	t = int(input())
	for _ in range(t):
		(p, q) = map(int, input().split())
		if p < q:
			print(p)
		elif p > q:
			if p % q != 0:
				print(p)
			else:
				Q = fact(q)
				toDivide = list()
				for f in Q.keys():
					p1 = count(p, f)
					q1 = Q[f]
					toDivide.append(pow(f, p1 - q1 + 1))
				print(p // min(toDivide))
		else:
			P = fact(p)
			factors = list(P.keys())
			print(p // min(factors))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = lambda s: self.buffer.write(s.encode()) if self.writable else None

	def read(self):
		if self.buffer.tell():
			return self.buffer.read().decode('ascii')
		return os.read(self._fd, os.fstat(self._fd).st_size).decode('ascii')

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline().decode('ascii')

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

def print(*args, sep=' ', end='\n', file=sys.stdout, flush=False):
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(end)
	if flush:
		file.flush()
(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10000)
main()


t = int(input())
for i in range(t):
	ans = 1
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		for i in range(2, int(q ** 0.5 + 1)):
			if q % i == 0:
				p1 = p
				while p1 % q == 0:
					p1 //= i
				ans = max(ans, p1)
				p1 = p
				while p1 % q == 0:
					p1 //= q // i
				ans = max(ans, p1)
		while p % q == 0:
			p //= q
		print(max(ans, p))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	(p, q) = sep()
	if p % q != 0:
		print(p)
		return
	cnt = {}
	temp = q
	for i in range(2, floor(temp ** 0.5) + 1):
		if temp % i == 0:
			cnt[i] = 0
			while temp % i == 0:
				cnt[i] += 1
				temp = temp // i
	if temp != 1:
		cnt[temp] = 1
	ans = 1
	for i in cnt:
		temp = p
		while temp % i == 0:
			temp = temp // i
		ans = max(ans, temp * i ** (cnt[i] - 1))
	print(ans)
testcase(int(inp()))


t = int(input())
for i in range(t):
	(p, q) = input().split()
	(p, q) = (int(p), int(q))
	if p % q != 0:
		print(p)
	else:
		Max = 1
		i = 2
		while i * i <= q:
			if q % i == 0:
				temp = p
				while temp % i == 0:
					temp = temp // i
					if not temp % q == 0:
						Max = max(Max, temp)
				Max = max(Max, temp)
				temp = p
				k = q // i
				while temp % k == 0:
					temp = temp // k
					if not temp % q == 0:
						Max = max(Max, temp)
				Max = max(Max, temp)
			i += 1
		temp = p
		while temp % q == 0:
			temp = temp // q
			if not temp % q == 0:
				Max = max(Max, temp)
		Max = max(Max, temp)
		print(Max)


for _ in range(int(input())):
	(p, q) = [int(x) for x in input().split()]
	c = q
	d = p
	i = 1
	factor = []
	while i * i <= q:
		if q % i == 0:
			factor.append(i)
			if q // i != i:
				factor.append(q // i)
		i += 1
	factor.remove(1)
	m = 1
	for i in factor:
		d = p
		while d % c == 0:
			d //= i
		m = max(m, d)
	print(m)


def how_many_of_this_factor(p, factor):
	total = 0
	while p % factor == 0:
		p //= factor
		total += 1
	return total

def add_factor(factors, factor):
	if factor not in factors:
		factors[factor] = 1
	else:
		factors[factor] += 1

def factorize(n):
	factors = {}
	if n == 3:
		return {3: 1}
	while n % 2 == 0:
		add_factor(factors, 2)
		n //= 2
	z = 3
	hi = int(n ** 0.5)
	while z <= hi:
		got_one = False
		while n % z == 0:
			got_one = True
			n //= z
			add_factor(factors, z)
		if got_one:
			hi = int(n ** 0.5)
		z += 2
	if hi != 1 or n == 3:
		add_factor(factors, n)
	return factors

def solve(p, q):
	if p % q != 0:
		return p
	factors = factorize(q)
	m = p
	for factor in factors:
		p_has = how_many_of_this_factor(p, factor)
		q_has = factors[factor]
		toss = p_has - (q_has - 1)
		m = min(m, factor ** toss)
	return p // m

def main():
	t = int(input())
	for _ in range(t):
		(p, q) = map(int, input().split())
		print(solve(p, q))
main()


import sys, math
input = sys.stdin.readline

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for j in range(int(input())):
	(p, q) = map(int, input().split())
	com = gcd(p, q)
	if com != q:
		print(p)
	else:
		maxsofar = 0
		for s in range(1, math.ceil(math.sqrt(com)) + 1):
			if com % s == 0:
				if s != 1:
					temp = p
					while temp % q == 0:
						temp = temp // s
					maxsofar = max(maxsofar, temp)
				temp2 = p
				div = com // s
				if div != 1:
					while temp2 % q == 0:
						temp2 = temp2 // div
					maxsofar = max(maxsofar, temp2)
		print(maxsofar)


import math

def prime_factors(a):
	pfs = {}
	s = int(math.sqrt(a)) + 1
	for i in range(2, s + 1):
		if i > a:
			break
		ic = 0
		while a % i == 0:
			a //= i
			ic += 1
		if ic > 0:
			pfs[i] = ic
	if a > 1:
		pfs[a] = 1
	return pfs
t = int(input())
results = []
for i in range(t):
	pq = input().split()
	p = int(pq[0])
	q = int(pq[1])
	if p % q != 0:
		results.append(p)
	else:
		r = p // q
		pfs_q = prime_factors(q)
		pfs_r = {}
		for k in pfs_q.keys():
			ic = 0
			while r % k == 0:
				ic += 1
				r //= k
			if ic > 0:
				pfs_r[k] = ic
		result = 1
		for k in pfs_q.keys():
			if k in pfs_r:
				w = pfs_r[k]
				x = p // k ** (w + 1)
			else:
				x = p // k
			if x > result:
				result = x
		results.append(result)
for result in results:
	print(result)


import math
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	qq = q
	c = 0
	if p % q != 0:
		print(p)
	else:
		ans = 1
		prime = []
		if q & 1 == 0:
			prime.append(2)
			while q & 1 == 0:
				q //= 2
		i = 3
		while i <= math.ceil(math.sqrt(q)):
			if q % i == 0:
				prime.append(i)
				while q % i == 0:
					q //= i
			i += 2
		if q > 1:
			prime.append(q)
		for i in prime:
			j = 1
			while True:
				j *= i
				temp = p // j
				if temp % qq != 0:
					ans = max(ans, temp)
					break
		print(ans)


from sys import stdin
_input = stdin.readline
(_range, _max, _int, _dict) = (range, max, int, dict)
from collections import defaultdict

def factorize(digit):
	i = 2
	ans = defaultdict(_int)
	while i * i <= digit:
		if digit % i == 0:
			ans[i] += 1
			digit //= i
		else:
			i += 1
	if digit > 1:
		ans[digit] += 1
	return ans

def solution():
	for _ in _range(_int(_input())):
		(p, q) = [_int(i) for i in _input().split()]
		if p % q != 0:
			print(p)
			continue
		f_q = factorize(q)
		ans = 1
		for i in f_q.keys():
			z = p
			while z % q == 0:
				z //= i
			ans = _max(ans, z)
		print(ans)
solution()


t = int(input())

def getSmallestPrimeFac(n):
	s = int(pow(n, 0.5))
	n2 = n
	count = 0
	for i in range(2, s + 1):
		if n % i == 0:
			while n % i == 0:
				count += 1
				n = n // i
			return (i, count)
		return (n, 1)

def reduce(n, d):
	s = 0
	while n % d == 0:
		n //= d
		s += 1
	return (n, s)

def solve(n, m):
	s = int(m ** 0.5)
	orig_n = n
	orig_m = m
	d_min = orig_n
	for i in range(2, s + 1):
		count_n = 0
		count_m = 0
		(n, count_n) = reduce(n, i)
		(m, count_m) = reduce(m, i)
		if count_m > count_n:
			return 1
		if count_n >= count_m:
			a = i ** (count_n - count_m + 1)
			if count_m > 0:
				if a < d_min:
					d_min = a
	if m > 1:
		(n, count_n) = reduce(n, m)
		if count_n != 0:
			return min(d_min, m ** count_n)
		return 1
	return d_min
for _ in range(0, t):
	(p, q) = [int(x) for x in input().split(' ')]
	d_min = solve(p, q)
	print(p // d_min)


import sys
input = sys.stdin.readline

def div(n):
	lst = []
	for i in range(2, int(n ** 0.5 + 1)):
		if n % i == 0:
			cnt = 0
			while n % i == 0:
				cnt += 1
				n //= i
			lst.append((i, cnt))
	if n != 1:
		lst.append((n, 1))
	return lst

def main():
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
		return
	ans = 10 ** 20
	lst = div(q)
	pp = p
	for (i, cnt) in lst:
		tmp = i ** cnt
		cnt2 = 0
		while p % tmp == 0:
			p //= i
			cnt2 += 1
		ans = min(ans, i ** cnt2)
	print(pp // ans)
for _ in range(int(input())):
	main()


T = int(input())
for _ in range(T):
	(p, q) = map(int, input().split())
	if p < q:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		s = set()
		i = 2
		while i * i <= q:
			if q % i == 0:
				s.add(i)
				s.add(q // i)
			i += 1
		s.add(q)
		l = list(s)
		lis = [1]
		for i in l:
			ch = p
			while ch % i == 0:
				ch //= i
				if ch % q:
					lis.append(ch)
					break
		print(max(lis))


import sys
import bisect
from bisect import bisect_right
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def gcd(a, b):
	if a < b:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def extendedGcd(a, b):
	if a < b:
		return extendedGcd(b, a)
	if b == 0:
		return [a, 1, 0]
	res = extendedGcd(b, a % b)
	x = res[2]
	y = res[1] - math.floor(a / b) * res[2]
	res[1] = x
	res[2] = y
	return res

def iterativeModularFunc(a, b, c):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % c
		a = a * a % c
		b = b // 2
	return res

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(input())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def Solution(p, q):
	if p % q != 0:
		print(p)
		return
	x = 1
	i = 1
	while i * i <= q:
		if q % i == 0:
			Ptmp = p
			while i != 1 and Ptmp % i == 0:
				Ptmp = Ptmp // i
				if Ptmp % q != 0:
					x = max(x, Ptmp)
					break
			Ptmp = p
			while Ptmp % (q // i) == 0:
				Ptmp = Ptmp // (q // i)
				if Ptmp % q != 0:
					x = max(x, Ptmp)
					break
		i += 1
	print(x)

def main():
	for _ in range(get_int()):
		(p, q) = get_ints_in_variables()
		Solution(p, q)
main()


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
prime = []
vis = [0] * 100001
for i in range(2, 100000 + 1):
	if not vis[i]:
		vis[i] = 1
		prime.append(i)
	for j in prime:
		if i * j >= 100000:
			break
		vis[i * j] = 1
		if i % j == 0:
			break
t = int(input())
for cnt in range(t):
	(p, q) = map(int, input().split())
	(pc, qc) = (p, q)
	if p % q:
		print(p)
	else:
		d1 = collections.defaultdict(int)
		d2 = collections.defaultdict(int)
		for c in prime:
			if c > q:
				break
			if q % c == 0:
				while q % c == 0:
					d2[c] += 1
					q //= c
				while p % c == 0:
					d1[c] += 1
					p //= c
		if d2:
			res = 1
			for k in d2:
				res = max(res, pc // k ** (d1[k] - d2[k] + 1))
			print(res)
		else:
			while p % q == 0:
				p //= q
			print(p)


for _ in range(0, int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	primes = {}
	cur = 2
	ans = 1
	while cur * cur <= q:
		if q % cur == 0:
			primes[cur] = 0
			while q % cur == 0:
				primes[cur] += 1
				q //= cur
		cur += 1
	if q != 1:
		primes[q] = 1
	for prime in primes:
		quot = p
		while quot % prime == 0:
			quot //= prime
		quot *= prime ** (primes[prime] - 1)
		ans = max(ans, quot)
	print(ans)


import math
for _ in range(int(input())):
	(p, n) = map(int, input().split())
	q = n
	a1 = []
	while n % 2 == 0:
		(a1.append(2),)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			(a1.append(int(i)),)
			n = n / i
	if n > 2:
		a1.append(int(n))
	freq = {}
	for i in a1:
		if i not in freq:
			freq[i] = 1
		else:
			freq[i] += 1
	if p % q != 0:
		print(p)
	else:
		ans = 1
		for i in freq.keys():
			num = p
			while num % q == 0:
				num //= i
			if num % q != 0:
				ans = max(ans, num)
		print(ans)


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q > 0:
		print(p)
	else:
		n = q
		ans = 1
		k = []
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				k.append(i)
				while n % i == 0:
					n = n // i
		if n != 1:
			k.append(n)
		for i in k:
			z = p
			while z % i == 0:
				z = z // i
				if z % q > 0:
					ans = max(ans, z)
		print(ans)


import math

def primeFactors(n, d):
	if n % 2 == 0:
		d[2] = 0
	while n % 2 == 0:
		d[2] += 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		if n % i == 0:
			d[i] = 0
		while n % i == 0:
			d[i] += 1
			n = n // i
	if n > 2:
		d[n] = 1
t = int(input())
for i in range(t):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		d = {}
		d1 = {}
		primeFactors(q, d)
		for key in d:
			p1 = p + 0
			if p1 % key == 0:
				d1[key] = 0
				while p1 % key == 0:
					d1[key] += 1
					p1 //= key
		l = []
		for key in d:
			l.append(pow(key, d1[key] - (d[key] - 1)))
		print(p // min(l))


for _ in range(int(input())):
	(p, q) = list(map(int, input().split()))
	if p % q != 0:
		print(p)
		continue
	pr = {}
	i = 2
	while i * i <= q:
		if q % i == 0:
			x = 0
			while q % i == 0:
				q //= i
				x += 1
			pr[i] = x
		i += 1
	if q != 1:
		pr[q] = 1
	res = 1
	for k in pr:
		v = pr[k]
		pp = p
		x = 0
		while pp % k == 0:
			pp //= k
			x += 1
		res = max(res, p // k ** (x - (v - 1)))
	print(res)


for j in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	(i, x, pr) = (2, q, [])
	while i * i <= q:
		if x % i == 0:
			pr.append(i)
			while x % i == 0:
				x //= i
		i += 1
	if x > 1:
		pr.append(x)
		x = 1
	for h in pr:
		y = p
		while y % q == 0:
			y //= h
		if y > x:
			x = y
	print(x)


import sys
import math
import collections
input = sys.stdin.readline
t = int(input())
for w in range(t):
	(p, q) = (int(i) for i in input().split())
	x = 1
	if p % q != 0:
		print(p)
	else:
		l = []
		for i in range(2, int(math.sqrt(q)) + 1):
			if q % i == 0:
				l.append(i)
				if q // i != i:
					l.append(q // i)
		l.append(q)
		for i in l:
			p1 = p
			while p1 % i == 0:
				p1 //= i
				if p1 % q != 0:
					x = max(p1, x)
					break
		print(x)


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
int1 = lambda x: int(x) - 1

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def printDivisors(n):
	A = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				A.append(i)
			else:
				A.append(i)
				A.append(n // i)
		i = i + 1
	return A

def solve():
	for _ in range(int(input())):
		(p, q) = aj()
		if p % q != 0:
			print(p)
		else:
			d = printDivisors(q)
			d.pop(0)
			ans = 1
			d.sort()
			for i in d:
				r = p
				while r % i == 0:
					if r % q:
						break
					r = r // i
				if r % q:
					ans = max(ans, r)
			print(ans)
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
solve()


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	(p, q) = mp()
	i = 2
	k = q
	ml = []
	while i * i <= q:
		if k % i == 0:
			ml.append(i)
			k //= i
			while k % i == 0:
				k //= i
		i += 1
	if k > 1:
		ml.append(k)
	mx = 0
	for i in ml:
		k = p
		while k % q == 0:
			k //= i
		mx = max(k, mx)
	print(mx)


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if False:
		print(p)
	else:
		a = []
		i = 2
		g = q
		while i * i <= g:
			if g % i == 0:
				g //= i
				a.append(i)
			else:
				i += 1
		if g > 1:
			a.append(g)
		ans = 0
		for i in range(len(a)):
			f = p
			while True:
				if f % q == 0:
					f //= a[i]
				else:
					ans = max(ans, f)
					break
		print(ans)


primes = []
preprocessing = [1] * pow(10, 5)
preprocessing[0] = 0
preprocessing[1] = 0
preprocessing[2] = 1
ind = 2
while ind < len(preprocessing):
	if preprocessing[ind]:
		primes.append(ind)
		k = 2
		while ind * k < len(preprocessing):
			preprocessing[ind * k] = 0
			k += 1
	ind += 1
T = int(input())
for t in range(T):
	(p, q) = [int(k) for k in input().split()]
	qq = q
	factors = []
	for prime in primes:
		if qq % prime == 0:
			while qq % prime == 0:
				qq //= prime
			factors.append(prime)
	largest = 0
	for f in factors:
		x = p
		if p % x == 0 and x % q != 0:
			largest = x
		while x % f == 0:
			x //= f
			if p % x == 0 and x % q != 0:
				largest = max(x, largest)
	tmp = q
	for f in factors:
		while tmp % f == 0:
			tmp //= f
	if tmp > 1:
		f = tmp
		x = p
		if p % x == 0 and x % q != 0:
			largest = x
		while x % f == 0:
			x //= f
			tmp //= f
			if p % x == 0 and x % q != 0:
				largest = max(x, largest)
	print(largest)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()

def PrimeFactorization(x):

	def plist(x):
		if x < 2:
			return []
		if x & 1 == 0:
			return [2] + plist(x >> 1)
		for p in range(3, x + 1, 2):
			if x % p == 0:
				return [p] + plist(x // p)
			if p ** 2 > x:
				return [x]
	pl = plist(x)
	(pp, ee) = ([], [])
	for p in pl:
		if not pp or p != pp[-1]:
			pp += [p]
			ee += [0]
		ee[-1] += 1
	return [(p, e) for (p, e) in zip(pp, ee)]

def solve():
	if p % q:
		return p
	be = PrimeFactorization(q)
	mn = p
	for (b, e) in be:
		c = p
		c //= b ** e
		cur = b
		while c % b == 0:
			cur *= b
			c //= b
		mn = min(mn, cur)
	return p // mn
for _ in range(II()):
	(p, q) = MI()
	print(solve())


from sys import stdin, stdout
input = stdin.readline
import math

def printDivisors(n, q):
	i = 1
	ans = -1
	while i <= math.sqrt(q):
		if n % i == 0:
			if n / i == i and i % q != 0:
				ans = max(ans, i)
			else:
				if i % q != 0:
					ans = max(ans, i)
				if n // i % q != 0:
					ans = max(ans, n // i)
		i = i + 1
	print(ans)

def primeFactors(n):
	arr = set()
	while n % 2 == 0:
		arr.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			arr.add(i)
			n = n // i
	if n > 2:
		arr.add(n)
	return arr
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if p < q:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		q1 = q
		q2 = p // q
		arr = list(primeFactors(q1))
		ans = -1
		for i in arr:
			val1 = q1 // i
			val2 = q2
			while val2 % i == 0:
				val2 = val2 // i
			ans = max(ans, val1 * val2)
		stdout.write(str(ans) + '\n')


def prime_factorization(num):
	dic = {}
	i = 2
	lim = num ** (1 / 2)
	while num != 1 and i <= lim:
		if num % i == 0:
			dic[i] = 0
			while num % i == 0:
				num //= i
				dic[i] += 1
		i += 1
	if num != 1:
		dic[int(num)] = 1
	return dic
for _ in range(int(input())):
	(p, q) = list(map(int, input().split()))
	if p % q != 0:
		print(p)
		continue
	qPrimes = prime_factorization(q)
	chkflag = True
	mx = 0
	for prime in qPrimes:
		tem = p
		while not tem % q:
			tem //= prime
		mx = max(mx, tem)
	print(mx)


mod = 1000000007
eps = 10 ** (-9)

def main():
	import sys
	input = sys.stdin.readline

	def PrimeDecomposition(N):
		ret = {}
		n = int(N ** 0.5)
		for d in range(2, n + 1):
			while N % d == 0:
				if d not in ret:
					ret[d] = 1
				else:
					ret[d] += 1
				N //= d
			if N == 1:
				break
		if N != 1:
			ret[N] = 1
		return ret
	for _ in range(int(input())):
		(p, q) = list(map(int, input().split()))
		if p % q != 0:
			print(p)
			continue
		prime = PrimeDecomposition(q)
		C = {}
		mi = p
		for pr in prime:
			C = 0
			tmp = p
			while tmp % pr == 0:
				C += 1
				tmp //= pr
			mi = min(mi, pr ** (C - prime[pr] + 1))
		print(p // mi)

def __starting_point():
	main()
__starting_point()


from math import sqrt

def div(n):
	l = []
	for i in range(2, int(sqrt(n)) + 3):
		if n % i == 0:
			l.append(i)
		while n % i == 0:
			n = n // i
	if n > 1:
		l.append(n)
	return l
t = int(input())
for i in range(t):
	a = input()
	A = a.split()
	for i in range(2):
		A[i] = int(A[i])
	if A[0] % A[1] != 0:
		print(A[0])
	else:
		ss = div(A[1])
		max = 1
		for i in ss:
			s = A[0]
			while s % A[1] == 0:
				if s % i == 0:
					s = s // i
			if s > max:
				max = s
		print(max)


import math

def pf(n):
	fac = {}
	while n % 2 == 0:
		fac[2] = fac.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			fac[i] = fac.get(i, 0) + 1
			n = n // i
	if n > 2:
		fac[n] = fac.get(n, 0) + 1
	return fac

def gp(f, p):
	s = 1
	e = int(math.ceil(math.log(p, f)))
	val = 1
	while s <= e:
		mid = (s + e) // 2
		if p % f ** mid != 0:
			e = mid - 1
		else:
			s = mid + 1
			val = mid
	return val
test = int(input())
for _ in range(test):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		pfac = pf(q)
		val = float('inf')
		for k in pfac:
			u = gp(k, p)
			val = min(val, k ** (u - pfac[k] + 1))
		ans = p // val
		print(ans)


def divisors(q):
	d = 2
	while d <= int(q ** 0.5) + 1:
		if q % d == 0:
			div.append(d)
			if q // d != d:
				div.append(q // d)
		d += 1
	div.append(q)
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		div = []
		for i in range(2, int(q ** 0.5) + 1):
			if q % i == 0:
				div.append(i)
				div.append(q // i)
		div.append(q)
		ans = 1
		for i in div:
			temp = p
			while temp % i == 0:
				temp = temp // i
				if temp % q != 0:
					ans = max(ans, temp)
					break
		print(ans)


t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	ans = []
	nq = q
	for test in range(2, 10 ** 5):
		if q % test == 0:
			p2 = p
			while p2 % q == 0:
				p2 //= test
			ans.append(p2)
		while nq % test == 0:
			nq //= test
	if nq > 1:
		while p % q == 0:
			p //= nq
		ans.append(p)
	print(max(ans))


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	(p, q) = R()
	x = q
	a = []
	if q % 2 == 0:
		a += (2,)
		while q % 2 == 0:
			q >>= 1
	for i in range(3, int(sqrt(q)) + 1, 2):
		if q % i == 0:
			a += (i,)
			while q % i == 0:
				q //= i
	if q != 1:
		a += (q,)
	ans = -inf
	for i in a:
		n = p
		while n % x == 0:
			n //= i
		ans = max(ans, n)
	print(ans)


import sys
import math
from collections import defaultdict

def factorize(q):
	factoredItems = defaultdict(int)
	counter = 2
	k = math.sqrt(q) + 1
	while counter <= k:
		if q % counter == 0:
			factoredItems[counter] = 1
			factoredItems[q // counter] = 1
		counter += 1
	if q not in factoredItems:
		factoredItems[q] = 1
	return factoredItems

def solve(p, q):
	qFactors = factorize(q)
	mx = 0
	for key in qFactors.keys():
		if key == 1:
			continue
		div = key
		temp = p
		while temp % q == 0:
			temp //= div
		mx = max(mx, temp)
	return mx

def readinput():
	tc = int(sys.stdin.readline().strip())
	for r in range(tc):
		(n, x) = map(int, sys.stdin.readline().rstrip().split(' '))
		print(solve(n, x))
readinput()


for j in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	p //= q
	x = p
	i = 2
	ans = 1
	while i * i <= q:
		if q % i:
			i += 1
			continue
		x = p
		k = q // i
		while x % i == 0:
			x //= i
		ans = max(ans, x * k)
		x = p
		while x % k == 0:
			x //= k
		ans = max(ans, x * i)
		i += 1
	while p % q == 0:
		p //= q
	print(max(ans, p))


def solve(P, Q):
	can = []
	q = Q
	i = 2
	while i ** 2 <= q:
		if q % i == 0:
			can.append(i)
			while q % i == 0:
				q //= i
		i += 1
	if q > 1:
		can.append(q)
	ans = 0
	for c in can:
		p = P
		while p % Q == 0:
			p //= c
		ans = max(ans, p)
	return ans
T = int(input())
for _ in range(T):
	(P, Q) = map(int, input().split())
	print(solve(P, Q))


import sys, io, os
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(io.IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrimeMR(n):
	d = n - 1
	d = d // (d & -d)
	L = [2, 7, 61] if n < 1 << 32 else [2, 3, 5, 7, 11, 13, 17] if n < 1 << 48 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
	for a in L:
		t = d
		y = pow(a, t, n)
		if y == 1:
			continue
		while y != n - 1:
			y = y * y % n
			if y == 1 or t == n - 1:
				return 0
			t <<= 1
	return 1

def findFactorRho(n):
	m = 1 << n.bit_length() // 8
	for c in range(1, 99):
		f = lambda x: (x * x + c) % n
		(y, r, q, g) = (2, 1, 1, 1)
		while g == 1:
			x = y
			for i in range(r):
				y = f(y)
			k = 0
			while k < r and g == 1:
				ys = y
				for i in range(min(m, r - k)):
					y = f(y)
					q = q * abs(x - y) % n
				g = gcd(q, n)
				k += m
			r <<= 1
		if g == n:
			g = 1
			while g == 1:
				ys = f(ys)
				g = gcd(abs(x - ys), n)
		if g < n:
			if isPrimeMR(g):
				return g
			elif isPrimeMR(n // g):
				return n // g
			return findFactorRho(g)

def primeFactor(n):
	i = 2
	ret = {}
	rhoFlg = 0
	while i * i <= n:
		k = 0
		while n % i == 0:
			n //= i
			k += 1
		if k:
			ret[i] = k
		i += i % 2 + (3 if i % 3 == 1 else 1)
		if i == 101 and n >= 2 ** 20:
			while n > 1:
				if isPrimeMR(n):
					(ret[n], n) = (1, 1)
				else:
					rhoFlg = 1
					j = findFactorRho(n)
					k = 0
					while n % j == 0:
						n //= j
						k += 1
					ret[j] = k
	if n > 1:
		ret[n] = 1
	if rhoFlg:
		ret = {x: ret[x] for x in sorted(ret)}
	return ret

def divisors(N):
	pf = primeFactor(N)
	ret = [1]
	for p in pf:
		ret_prev = ret
		ret = []
		for i in range(pf[p] + 1):
			for r in ret_prev:
				ret.append(r * p ** i)
	return sorted(ret)
T = int(input())
for _ in range(T):
	(P, Q) = map(int, input().split())
	if P % Q:
		print(P)
		continue
	pfs = primeFactor(Q)
	ans = 1
	for (p, n) in pfs.items():
		x = P
		while x % Q == 0:
			if x % p:
				break
			x //= p
		else:
			if ans < x:
				ans = x
	print(ans)


n = int(input())
ans = [0] * n
for i in range(n):
	(p, q) = map(int, input().split())
	j = 2
	x = q
	a = []
	while j * j <= q:
		if x % j == 0:
			a.append(j)
			while x % j == 0:
				x //= j
		j += 1
	if x > 1:
		a.append(x)
		x = 1
	for l in a:
		k = p
		while k % q == 0:
			k //= l
		x = max(k, x)
	ans[i] = x
for el in ans:
	print(el)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		ok = True
		i = 2
		factors = []
		p_original = p
		q_original = q
		while i * i <= q:
			c = 0
			while q % i == 0:
				q //= i
				c += 1
			if c > 0:
				factors.append((i, c))
			i += 1
		if q > 1:
			factors.append((q, 1))
		extra = []
		for (factor, c) in factors:
			cnt = 0
			while p % factor == 0:
				p //= factor
				cnt += 1
			extra.append(factor ** (cnt - c + 1))
		print(p_original // min(extra))


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	res = q
	arr = []
	j = 2
	while j * j <= res:
		if res % j == 0:
			arr.append(j)
			while res % j == 0:
				res = res // j
		j += 1
	if res > 1:
		arr.append(res)
		res = 1
	for k in arr:
		y = p
		while y % q == 0:
			y = y // k
		res = max(res, y)
	print(res)


from collections import *
from copy import *

def facts(n):
	l = defaultdict(lambda : 1)
	while n % 2 == 0:
		l[2] *= 2
		n //= 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			l[i] *= i
			n //= i
	if n > 2:
		l[n] *= n
	return l
for i in range(int(input())):
	(a, b) = map(int, input().split())
	if a % b:
		print(a)
	else:
		B = facts(b)
		ans = 0
		for (num, val) in B.items():
			curr = deepcopy(a)
			while curr % val == 0:
				curr //= num
			ans = max(curr, ans)
		print(ans)


import math

def primeFactors(n):
	factors = []
	while n % 2 == 0:
		factors.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n = n // i
	if n > 2:
		factors.append(n)
	return factors
t = int(input())
for q in range(t):
	(p, q) = map(int, input().split())
	fact = primeFactors(q)
	ans = p * 1
	res = 0
	for i in range(len(fact)):
		ans = p * 1
		while ans % q == 0 and ans >= q:
			ans = ans // fact[i]
		res = max(res, ans)
	print(res)


import math
n = int(input())

def pfact(q):
	factors = {}
	temp = q
	for i in range(2, math.floor(math.sqrt(q)) + 1):
		while temp % i == 0:
			temp //= i
			if i not in factors.keys():
				factors[i] = 1
			else:
				factors[i] += 1
	if temp not in factors.keys():
		factors[temp] = 1
	else:
		factors[temp] += 1
	return factors
for _ in range(n):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
		continue
	prq = pfact(q)
	maxs = 0
	for pr in prq.keys():
		if pr == 1:
			continue
		temp = p
		while temp % pr == 0:
			temp = temp // pr
		temp = temp * pr ** (prq[pr] - 1)
		maxs = max(temp, maxs)
	print(maxs)


import sys
from math import gcd, sqrt, ceil, log2
from collections import defaultdict, Counter, deque
from bisect import bisect_left, bisect_right
import math
sys.setrecursionlimit(2 * 10 ** 5 + 10)
import heapq
from itertools import permutations
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
aa = 'abcdefghijklmnopqrstuvwxyz'

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_sum(bit, i):
	s = 0
	i += 1
	while i > 0:
		s += bit[i]
		i -= i & -i
	return s

def update(bit, n, i, v):
	i += 1
	while i <= n:
		bit[i] += v
		i += i & -i

def modInverse(b, m):
	g = math.gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def primeFactors(n):
	sa = []
	while n % 2 == 0:
		sa.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			sa.append(i)
			n = n // i
	if n > 2:
		sa.append(n)
	return sa

def seive(n):
	pri = [True] * (n + 1)
	p = 2
	while p * p <= n:
		if pri[p] == True:
			for i in range(p * p, n + 1, p):
				pri[i] = False
		p += 1
	return pri

def check_prim(n):
	if n < 0:
		return False
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1

def search(text, pattern):
	concat = pattern + '$' + text
	l = len(concat)
	z = [0] * l
	getZarr(concat, z)
	ha = []
	for i in range(l):
		if z[i] == len(pattern):
			ha.append(i - len(pattern) - 1)
	return ha

def mergeIntervals(arr):
	arr.sort(key=lambda x: x[0])
	m = []
	s = -10000
	max = -100000
	for i in range(len(arr)):
		a = arr[i]
		if a[0] > max:
			if i != 0:
				m.append([s, max])
			max = a[1]
			s = a[0]
		elif a[1] >= max:
			max = a[1]
	if max != -100000 and [s, max] not in m:
		m.append([s, max])
	return m

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def sol(n):
	seti = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			seti.add(n // i)
			seti.add(i)
	return seti

def lcm(a, b):
	return a * b // gcd(a, b)
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	pr1 = primeFactors(q)
	hash = defaultdict(int)
	if p % q != 0:
		print(p)
		continue
	maxi = 1
	for i in pr1:
		hash[i] += 1
	for i in hash:
		cnt = 0
		temp = p
		while temp % i == 0:
			temp //= i
		maxi = max(temp * i ** (hash[i] - 1), maxi)
	print(maxi)


import math
import sys, bisect
from heapq import *
from itertools import *
from collections import *
sys.setrecursionlimit(10 ** 6)
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))

def factorize(n):
	count = 0
	d = defaultdict(int)
	while (n % 2 > 0) == False:
		n >>= 1
		count += 1
	if count > 0:
		d[2] += count
	for i in range(3, int(math.sqrt(n)) + 1):
		count = 0
		while n % i == 0:
			count += 1
			n = n // i
		if count > 0:
			d[i] += count
		i += 2
	if n > 2:
		d[n] += 1
	return d
for _ in range(I()):
	(p, q) = neo()
	(d, Ans) = ({}, [])
	if p % q != 0:
		print(p)
		continue
	d = factorize(q)
	Ans = 0
	for (i, j) in d.items():
		t = p
		while t % i == 0:
			t = t // i
		Ans = max(Ans, t * i ** (j - 1))
	print(Ans)


from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + '\n')
from math import sqrt
from collections import defaultdict
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	fact_q = defaultdict(int)
	fact_p = defaultdict(int)
	(tq, tp) = (q, p)
	for i in range(2, int(sqrt(q) + 1)):
		while tq % i == 0:
			fact_q[i] += 1
			tq //= i
	if tq != 1:
		fact_q[tq] += 1
	for i in fact_q:
		while tp % i == 0:
			fact_p[i] += 1
			tp //= i
	ans = 1
	for i in fact_q:
		ans = max(ans, p // i ** (fact_p[i] - fact_q[i] + 1))
	print(ans)


primes = []
max_ = int(100000.0)
prime = [True] * max_
prime[0] = prime[1] = False
i = 2
while i * i <= max_:
	if prime[i]:
		for j in range(2 * i, max_, i):
			prime[j] = False
	i += 1
for i in range(max_):
	if prime[i]:
		primes.append(i)
t = int(input())
while t > 0:
	(p, q) = map(int, input().split())
	xx = q
	to_ = []
	for i in primes:
		if xx % i == 0:
			to_.append(i)
		while xx % i == 0:
			xx //= i
		if xx == 1:
			break
	if xx != 1:
		to_.append(xx)
	if p % q != 0:
		print(p)
	else:
		ans = 1
		for j in to_:
			p1 = p
			while p1 % q == 0:
				p1 //= j
			if p1 > ans:
				ans = p1
		print(ans)
	t -= 1


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max

def primeFactors(n):
	pf = []
	while n % 2 == 0:
		pf.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			pf.append(int(i))
			n = n / i
	if n > 2:
		pf.append(int(n))
	return pf

def main():
	mod = 1000000007
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = ri()
	for _ in range(tc):
		(p, q) = ria()
		if p % q:
			print(p)
		else:
			pf = primeFactors(q)
			pf = sorted(pf)
			d = Counter(pf)
			val = []
			pw = p
			for i in d:
				pw = p
				while pw % q == 0:
					pw = pw // i
				val.append(pw)
			val = sorted(val)
			print(val[-1])
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


from sys import stdin, stdout
import math
import heapq
import collections
input = stdin.readline

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstrings():
	return [x for x in input().split()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	(p, q) = inputnums()
	q1 = q
	best = 0
	nums = set()
	i = 2
	while i * i <= q1:
		while q1 % i == 0:
			nums.add(i)
			q1 //= i
		i += 1
	if q1 > 1:
		nums.add(q1)
	for j in nums:
		p1 = p
		while p1 % q == 0:
			p1 //= j
		best = max(best, p1)
	print(best)


import math
for _ in range(int(input())):
	(p, n) = map(int, input().split())
	q = n
	a1 = []
	while n % 2 == 0:
		(a1.append(2),)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			(a1.append(int(i)),)
			n = n / i
	if n > 2:
		a1.append(int(n))
	a1.sort(reverse=True)
	if p % q != 0:
		print(p)
	else:
		ans = 1
		for i in a1:
			temp = p
			while temp % q == 0:
				temp //= i
			ans = max(ans, temp)
		print(ans)


import sys
input = sys.stdin.buffer.readline

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrimeMR(n):
	d = n - 1
	d = d // (d & -d)
	L = [2]
	for a in L:
		t = d
		y = pow(a, t, n)
		if y == 1:
			continue
		while y != n - 1:
			y = y * y % n
			if y == 1 or t == n - 1:
				return 0
			t <<= 1
	return 1

def findFactorRho(n):
	m = 1 << n.bit_length() // 8
	for c in range(1, 99):
		f = lambda x: (x * x + c) % n
		(y, r, q, g) = (2, 1, 1, 1)
		while g == 1:
			x = y
			for i in range(r):
				y = f(y)
			k = 0
			while k < r and g == 1:
				ys = y
				for i in range(min(m, r - k)):
					y = f(y)
					q = q * abs(x - y) % n
				g = gcd(q, n)
				k += m
			r <<= 1
		if g == n:
			g = 1
			while g == 1:
				ys = f(ys)
				g = gcd(abs(x - ys), n)
		if g < n:
			if isPrimeMR(g):
				return g
			elif isPrimeMR(n // g):
				return n // g
			return findFactorRho(g)

def primeFactor(n):
	i = 2
	ret = {}
	rhoFlg = 0
	while i * i <= n:
		k = 0
		while n % i == 0:
			n //= i
			k += 1
		if k:
			ret[i] = k
		i += 1 + i % 2
		if i == 101 and n >= 2 ** 20:
			while n > 1:
				if isPrimeMR(n):
					(ret[n], n) = (1, 1)
				else:
					rhoFlg = 1
					j = findFactorRho(n)
					k = 0
					while n % j == 0:
						n //= j
						k += 1
					ret[j] = k
	if n > 1:
		ret[n] = 1
	return ret
T = int(input())
for testcase in range(T):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		r1 = primeFactor(p)
		r2 = primeFactor(q)
		mi = 10 ** 19
		for e in r1:
			if e in r2:
				tmp = e ** (r1[e] - r2[e] + 1)
				if mi > tmp:
					mi = tmp
		print(p // mi)


def getPrimeFactorCounts(x):
	primeFactorCnts = []
	for i in range(2, int(x ** 0.5) + 1):
		cnt = 0
		while x % i == 0:
			cnt += 1
			x //= i
		if cnt > 0:
			primeFactorCnts.append((i, cnt))
	if x > 1:
		primeFactorCnts.append((x, 1))
	return primeFactorCnts
t = int(input())
for _ in range(t):
	(p, q) = [int(x) for x in input().split()]
	qf = getPrimeFactorCounts(q)
	qpf = []
	for (f, c) in qf:
		qpf.append(f)
	ans = -1
	for f in qpf:
		p2 = p
		while True:
			if p2 % q != 0:
				ans = max(ans, p2)
				break
			if p2 % f == 0:
				p2 //= f
			else:
				break
	print(ans)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c
for xyz in range(0, int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		r = int(q ** 0.5)
		ans = 1
		for i in range(2, r + 1):
			if q % i == 0:
				(f1, f2) = (i, q // i)
				t = p
				while t % f1 == 0:
					t //= f1
					if t % q != 0:
						ans = max(t, ans)
				t = p
				while t % f2 == 0:
					t //= f2
					if t % q != 0:
						ans = max(t, ans)
		t = p
		while t % q == 0:
			t //= q
			if t % q != 0:
				ans = max(t, ans)
		print(ans)


from collections import defaultdict

def seive(n):
	dic = defaultdict(int)
	i = 2
	tmp = n
	while i * i <= tmp:
		count = 0
		while n % i == 0:
			n = n // i
			count += 1
		if count:
			dic[i] = count
		i += 1
	if n - 1:
		dic[n] = 1
	return dic
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	factors = seive(q)
	ans = 1
	for fact in factors:
		tmp = p
		while tmp % fact == 0:
			tmp = tmp // fact
		tmp = tmp * fact ** (factors[fact] - 1)
		ans = max(ans, tmp)
	print(ans)


import math

def prime_factorization(x):
	result = []
	for i in range(2, int(math.sqrt(x)) + 1):
		if x % i == 0:
			count = 0
			while x % i == 0:
				count += 1
				x //= i
			result.append((i, count))
	if x > 1:
		result.append((x, 1))
	return result

def find_all_divisors_of_a_number(x):
	result = []
	for i in range(1, int(math.sqrt(x)) + 1):
		if x % i == 0:
			result.append(i)
			if i * i != x:
				result.append(x // i)
	return result
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p < q:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		now = prime_factorization(q)
		max_value = 0
		for i in now:
			cur = p
			prime = i[0]
			cnt = i[1]
			while cur % int(prime ** cnt) == 0:
				cur //= prime
			max_value = max(max_value, cur)
		print(max_value)


def f(x):
	f1 = {}
	for i in range(2, int(x ** 0.5) + 3):
		if x % i == 0:
			tmp = 1
			while x % i == 0:
				tmp *= i
				x //= i
			f1[i] = tmp
	if x > 1:
		f1[x] = x
	return f1
t = int(input())
INF = 10 ** 20
for _ in range(t):
	(p, q) = map(int, input().split())
	p_save = p
	if p % q:
		print(p)
		continue
	q_f = f(q)
	p_f = {pi: pi for pi in q_f.keys() if p % pi == 0}
	div = p
	for pi in p_f:
		while p % pi == 0:
			p_f[pi] *= pi
			p //= pi
		div = min(div, p_f[pi] // q_f[pi])
	print(p_save // div)


def prime(m):
	a = set()
	i = 2
	while i * i <= m:
		if m % i == 0:
			m //= i
			a.add(i)
		else:
			i += 1
	a.add(m)
	return a
for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	if m > n or n % m != 0:
		print(n)
	else:
		ans = 1
		for i in prime(m):
			t = n
			while t % i == 0:
				t //= i
				if t % m != 0:
					ans = max(t, ans)
					break
		print(ans)


def primeFactors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return factors
for _ in range(int(input())):
	(p, q) = list(map(int, input().split()))
	qFactors = primeFactors(q)
	possible = []
	for num in qFactors:
		temp = p
		while temp % q == 0:
			temp //= num
		possible.append(temp)
	print(max(possible))


for _ in range(int(input())):
	(p, q) = [int(z) for z in input().split()]
	(i, x, lst) = (2, q, [])
	while i ** 2 <= q:
		if x % i == 0:
			lst.append(i)
			while x % i == 0:
				x //= i
		i += 1
	if x > 1:
		lst.append(x)
		x = 1
	for elem in lst:
		y = p
		while y % q == 0:
			y //= elem
		if y > x:
			x = y
	print(x)


from sys import stdout, stdin
from collections import defaultdict, deque
import math

def primeFactors(n):
	l = []
	if n % 2 == 0:
		l.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		if n % i == 0:
			l.append(i)
			n = n // i
	if n > 2:
		l.append(n)
	return l
t = int(stdin.readline())
for _ in range(t):
	(a, b) = map(int, stdin.readline().split())
	if a % b == 0:
		l = primeFactors(b)
		ans = 0
		for i in l:
			z = a
			while z % i == 0:
				z = z // i
				if z % b != 0:
					break
			if z > ans:
				ans = z
		print(ans)
	else:
		print(a)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math
from functools import reduce
from math import sqrt

def factors(n):
	step = 2 if n % 2 else 1
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(sqrt(n)) + 1, step) if n % i == 0)))
from collections import defaultdict

def primeFactors(n):
	ans = defaultdict(lambda : 0)
	while n % 2 == 0:
		ans[2] += 1
		n = n // 2
	while n != 1:
		found = False
		for i in range(3, int(math.sqrt(n)) + 1, 2):
			if n % i == 0:
				found = True
				ans[i] += 1
				n = n // i
				break
		if not found:
			ans[n] += 1
			n = 1
	return list(ans.items())

def gift():
	for _ in range(t):
		(xi, pi) = list(map(int, input().split()))
		if pi > xi:
			yield xi
		elif xi % pi:
			yield xi
		else:
			right = pi
			array = sorted(factors(right), reverse=True)
			ans = 1
			for ele in array:
				if xi // ele % right:
					ans = max(ans, xi // ele)
			pFactors = primeFactors(pi)
			for i in range(len(pFactors)):
				counter = 1
				while True:
					now = pFactors[i][0] ** (pFactors[i][1] + counter)
					if xi % now == 0:
						if xi // now % pi:
							ans = max(ans, xi // now)
					else:
						break
					counter += 1
			yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


def primeFactors(n):
	pn = n
	l = {}
	while n % 2 == 0:
		if 2 not in l:
			l[2] = 1
		else:
			l[2] += 1
		n = n // 2
	i = 3
	while i * i <= n:
		while n % i == 0:
			if i not in l:
				l[i] = 1
			else:
				l[i] += 1
			n = n // i
		i += 2
	if n > 2:
		l[n] = 1
	return l

def solve(a, b):
	if a % b != 0:
		return a
	bp = primeFactors(b)
	ans = a
	for x in bp:
		powa = 0
		oa = a
		while oa % x == 0:
			oa //= x
			powa += 1
		dif = powa - bp[x] + 1
		div = 1
		for i in range(dif):
			div *= x
		ans = min(ans, div)
	return a // ans
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	print(solve(a, b))


def read_generator():
	while True:
		tokens = input().split(' ')
		for t in tokens:
			yield t
reader = read_generator()

def readword():
	return next(reader)

def readint():
	return int(next(reader))

def readfloat():
	return float(next(reader))

def readline():
	return input()
import math

def solve(a, b):
	if a < b:
		return a
	if a > b and a % b != 0:
		return a
	d = []
	if a % b == 0:
		d.append(b)
	for i in range(2, math.ceil(math.sqrt(b) + 1)):
		if b % i == 0:
			d.append(i)
			if a % (b // i) == 0:
				d.append(b // i)
	res = []
	for v in d:
		if v != 1:
			t = a
			while t % v == 0:
				t //= v
				if t % b != 0:
					res.append(t)
					break
	res.append(1)
	return max(res)

def solve2(a, b):
	for i in range(a, 0, -1):
		if a % i == 0 and i % b != 0:
			return i
tests = readint()
for _ in range(tests):
	(a, b) = (readint(), readint())
	print(solve(a, b))


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def prime_factorize(n):
	a = defaultdict(int)
	if n == 1:
		a[1] = 1
		return a
	while n % 2 == 0:
		a[2] += 1
		n //= 2
	f = 3
	while f * f <= n:
		if n % f == 0:
			a[f] += 1
			n //= f
		else:
			f += 2
	if n != 1:
		a[n] += 1
	return a

def a_count(key):
	n = p
	cnt = 0
	while n % key == 0:
		cnt += 1
		n //= key
	return cnt
t = inp()
for _ in range(t):
	(p, q) = inpl()
	if p % q:
		print(p)
		continue
	b = prime_factorize(q)
	res = 0
	for key in list(b):
		df = a_count(key)
		cnt = df - (b[key] - 1)
		now = p // pow(key, cnt)
		res = max(res, now)
	print(res)


import sys
import math as m
t = int(input())
while t:
	t -= 1
	ans = 1
	(a, b) = map(int, input().split())
	if a < b:
		print(a)
	elif a % b != 0:
		print(a)
	else:
		for i in range(2, b):
			if i * i > b:
				break
			if b % i == 0:
				tem = a
				while tem % b == 0:
					tem //= i
				ans = max(ans, tem)
				while b % i == 0:
					b //= i
		if b != 1:
			while a % b == 0:
				a //= b
			ans = max(ans, a)
		print(int(ans))


for i in range(int(input())):
	(a, b) = map(int, input().split())
	t = []
	ans = 1
	i = 2
	n = b
	while i * i <= n:
		if n % i == 0:
			t.append(i)
			n //= i
		else:
			i = i + 1
	t.append(n)
	for i in t:
		k = a
		while k % b == 0:
			k //= i
		ans = max(ans, k)
	print(ans)


from math import gcd

def solve():
	(a, b) = map(int, input().split())
	if a % b != 0:
		print(a)
	else:
		k = a // b
		i = 2
		mi = a
		while i * i <= b:
			if b % i == 0:
				while b % i == 0:
					b //= i
				c = 0
				while k % i == 0:
					k //= i
					c += 1
				mi = min(mi, pow(i, c + 1))
			i += 1
		if b != 1:
			c = 0
			while k % b == 0:
				k //= b
				c += 1
			mi = min(mi, pow(b, c + 1))
		print(a // mi)

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()
main()


t = int(input())
for you in range(t):
	l = input().split()
	p = int(l[0])
	q = int(l[1])
	if p % q:
		print(p)
		continue
	l = []
	i = 2
	z = q
	while i * i <= q:
		if q % i == 0:
			count = 0
			while q % i == 0:
				q = q // i
				count += 1
			l.append((i, count))
		i += 1
	if q != 1:
		l.append((q, 1))
	ok = 10 ** 18
	for i in l:
		j = i[0]
		op = p
		count = 0
		while op % j == 0:
			count += 1
			op = op // j
		ok = min(ok, pow(j, count - i[1] + 1))
	print(p // ok)


for _ in range(int(input())):
	(a, b) = list(map(int, input().split()))
	if a % b != 0:
		print(a)
		continue
	bDevVal = []
	bDevAm = []
	iter = 2
	bc = b
	while iter * iter <= bc:
		if bc % iter == 0:
			bDevVal.append(iter)
			bDevAm.append(0)
		while bc % iter == 0:
			bDevAm[-1] += 1
			bc //= iter
		iter += 1
	if bc > 1:
		bDevVal.append(bc)
		bDevAm.append(1)
	m = 1
	for i in range(len(bDevVal)):
		ac = a
		while ac % bDevVal[i] == 0:
			ac //= bDevVal[i]
		ac *= bDevVal[i] ** (bDevAm[i] - 1)
		m = max(ac, m)
	print(m)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input()

def msi():
	return map(str, input().split())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x * y // gcd(x, y)
mod = 1000000007

def modInverse(b, m):
	g = gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def modu(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m
from math import log, sqrt, factorial, cos, tan, sin, radians, floor, ceil
import bisect
from decimal import *
getcontext().prec = 8
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def gcd1(a):
	if len(a) == 1:
		return a[0]
	ans = a[0]
	for i in range(1, len(a)):
		ans = gcd(ans, a[i])
	return ans

def main():
	for _ in range(ii()):
		(p, q) = mi()
		temp2 = q
		if p % q:
			print(p)
			continue
		ans = 1
		l = []
		if q % 2 == 0:
			l.append(2)
			while q % 2 == 0:
				q //= 2
		for i in range(3, ceil(sqrt(q)) + 1, 2):
			if q % i == 0:
				l.append(i)
				while q % i == 0:
					q //= i
		if q > 2:
			l.append(q)
		temp = p
		for i in range(len(l)):
			p = temp
			while p % l[i] == 0:
				p //= l[i]
				if p % temp2:
					break
			ans = max(ans, p)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from math import *
from random import *
from bisect import *
from math import *

def mn(v):
	i = 2
	t = v
	res = []
	while i * i <= v:
		if t % i == 0:
			cc = 0
			while t % i == 0:
				t //= i
				cc += 1
			res.append((i, cc))
		i += 1
	if t != 1:
		res.append((t, 1))
	return res
for t in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
		continue
	r = mn(q)
	mx = -1
	for i in r:
		v = p
		dd = i[0] ** i[1]
		while v % dd == 0:
			v //= i[0]
		mx = max(v, mx)
	print(mx)


import math
import sys
input = sys.stdin.readline

def inlist():
	return list(map(int, input().split()))

def Sieve():
	isp = []
	mxN = 100000
	for i in range(0, mxN):
		isp.append(0)
	for i in range(4, mxN, 2):
		isp[i] = 1
	sq = int(math.sqrt(mxN))
	for i in range(3, sq + 1, 2):
		if isp[i] == 0:
			for j in range(i * i, mxN, i + i):
				isp[j] = 1
	prime = []
	prime.append(2)
	for i in range(3, mxN, 2):
		if isp[i] == 0:
			prime.append(i)
	return prime

def main():
	prime = Sieve()
	t = int(input())
	for tc in range(t):
		pq = inlist()
		p = pq[0]
		q = pq[1]
		x = 1
		if p < q:
			x = p
		elif p % q != 0:
			x = p
		else:
			x = 1
			for i in prime:
				if q == 1:
					break
				if q % i == 0:
					tmp = p
					cnt = 0
					while q % i == 0:
						q //= i
						cnt += 1
					tot = 0
					while tmp % i == 0:
						tmp //= i
					cnt -= 1
					while cnt != 0:
						tmp *= i
						cnt -= 1
					x = max(x, tmp)
			if q != 1:
				while p % q == 0:
					p //= q
				x = max(x, p)
		sys.stdout.write(str(x) + '\n')
main()


def fun(i):
	if i == 1:
		return 1
	a = p
	while a % q == 0:
		a = a // i
	return a
t = int(input())
for _ in range(t):
	(p, q) = list(map(int, input().split()))
	if p % q != 0:
		print(p)
		continue
	ans = 1
	for i in range(1, q):
		if i * i > q:
			break
		if q % i == 0:
			ans = max(ans, fun(i))
			ans = max(ans, fun(q // i))
	print(int(ans))


import sys
import math
from collections import deque
I = sys.stdin.readline
IN = lambda x: map(int, x.split())
L = lambda x: list(IN(x))
Nmax = 31623

def seive(n):
	l = []
	prime = [1] * (n + 1)
	i = 2
	m = 0
	while i <= n:
		j = i * i
		l.append(i)
		m += 1
		while j <= n:
			prime[j] = i
			j += i
		i += 1
		while i <= n and prime[i] != 1:
			i += 1
	return (l, prime, m)
(prime, lis, leng) = seive(Nmax)

def li(q):
	l = []
	j = 0
	while q > Nmax:
		if j == leng:
			break
		num = prime[j]
		(a, b) = divmod(q, num)
		if b == 0:
			l.append(num)
			while b == 0:
				q = a
				(a, b) = divmod(q, num)
		j += 1
	if q == 1:
		return l
	if j == leng:
		l.append(q)
		return l
	while q > 1:
		t = lis[q]
		if t == 1:
			l.append(q)
			return l
		l.append(t)
		(a, b) = divmod(q, t)
		while b == 0:
			q = a
			(a, b) = divmod(q, t)
	return l

def find(p, q):
	l = li(q)
	j = 0
	c = 0
	res = 0
	z = p
	for i in l:
		if q % i == 0:
			a1 = p // i
			while a1 % q == 0:
				p = a1
				a1 = p // i
			(p, res) = (z, max(res, a1))
	return res
for _ in range(int(I())):
	(p, q) = IN(I())
	if p % q != 0:
		print(p)
		continue
	print(find(p, q))


import math, sys
t = int(input())
for i in range(t):
	(p, q) = list(map(int, sys.stdin.readline().strip().split()))
	if p % q != 0:
		print(p)
	else:
		p_fctr = []
		lim_fctr = int(math.sqrt(q)) + 1
		Q = q
		i = 2
		while Q > 1 and i <= lim_fctr:
			while Q % i == 0:
				if len(p_fctr) == 0 or p_fctr[-1] != i:
					p_fctr.append(i)
				Q //= i
			i += 1
		if Q > 1:
			p_fctr.append(Q)
		x = 1
		for i in range(len(p_fctr)):
			fctr = p_fctr[i]
			P = p
			while P % q == 0:
				P = P // fctr
			x = max(x, P)
		print(x)


import math
for i in range(int(input())):
	(p, q) = map(int, input().split())
	l = p
	if p % q != 0:
		print(p)
	else:
		g = []
		c = 1
		if q % 2 == 0:
			while q % 2 == 0:
				q = q // 2
				p = p // 2
			while p % 2 == 0:
				c = c * 2
				p = p // 2
			g.append(c * 2)
		for j in range(3, int(math.sqrt(q)) + 2, 2):
			if q % j == 0:
				c = 1
				while q % j == 0:
					q = q // j
					p = p // j
				while p % j == 0:
					c = c * j
					p = p // j
				g.append(c * j)
		if q > 2:
			c = 1
			while p % q == 0:
				c = c * q
				p = p // q
			g.append(c)
		print(l // min(g))


import math

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n

def prevPowerofK(n, k):
	p = int(math.log(n) / math.log(k))
	return int(math.pow(k, p))

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n // i
	if n > 2:
		l.append(n)
	return l
t = int(input())
while t:
	(n, x) = map(int, input().split())
	if n % x != 0:
		print(n)
	else:
		l2 = primeFactors(x)
		d2 = {}
		for j in l2:
			if j in d2:
				d2[j] += 1
			else:
				d2[j] = 1
		ans = 0
		for i in d2:
			if n % i == 0:
				power = 1
				number = i
				while n % number == 0:
					power += 1
					number *= i
				if power > 1:
					xxx = n // i ** (power - 1)
					xxx *= i ** (d2[i] - 1)
					ans = max(ans, xxx)
		print(ans)
	t -= 1


import sys
import itertools as it
import math as mt
import bisect as bi
import collections as cc
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(a, b) = I()
	if a % b != 0:
		print(a)
	else:
		now = b
		f = cc.defaultdict(int)
		while now % 2 == 0:
			f[2] += 1
			now //= 2
		for i in range(3, int(now ** 0.5) + 1, 2):
			while now % i == 0:
				f[i] += 1
				now //= i
		if now > 1:
			f[now] += 1
		temp = []
		ans = 0
		for i in f:
			cnt = 0
			now = a
			while now % i == 0:
				cnt += 1
				now //= i
			dif = cnt - f[i] + 1
			if dif < 0:
				continue
			ans = max(ans, a // pow(i, dif))
		print(ans)


import os
import sys
from io import BytesIO, IOBase
_str = str
BUFSIZE = 8192

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline().rstrip()

def mpint():
	return map(int, inp().split(' '))

def itg():
	return int(inp())
from collections import Counter

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def solve():
	(p, q) = mpint()
	if p % q:
		return p
	qf = prime_factors(q)
	pf = Counter()
	mx = 1
	for f in qf:
		p //= f ** qf[f]
		pf[f] = qf[f]
		while not p % f:
			p //= f
			pf[f] += 1
		mx *= f ** pf[f]
	ans = 1
	for (f, exp) in qf.items():
		ans = max(ans, mx // f ** (pf[f] - exp + 1))
	return ans * p

def main():
	for _ in range(itg()):
		print(solve())
DEBUG = 0
URL = 'https://codeforces.com/contest/1445/problem/C'
if DEBUG == 1:
	import requests
	from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
	runner = TestCaseRunner(main, URL)
	inp = runner.input_stream
	print = runner.output_stream
	runner.checking()
elif DEBUG == 2:
	main()
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	main()


t = int(input())
for _ in range(t):
	[p, q] = list(map(int, input().split(' ')))
	if p % q != 0:
		print(p)
		continue
	fact_q = {}
	f = 2
	while q > 1:
		if f * f > q:
			break
		c = 0
		while q % f == 0:
			q //= f
			c += 1
		if c > 0:
			fact_q[f] = c
		f += 1
	if q > 1:
		fact_q[q] = 1
	ans = p
	for f in fact_q:
		c = 0
		x = p
		while x % f == 0:
			x //= f
			c += 1
		val = f ** (c - fact_q[f] + 1)
		if val < ans:
			ans = val
	print(p // ans)


def find_prime(n):
	ans = []
	i = 2
	x = n
	while i * i <= n:
		if x % i == 0:
			ans.append(i)
			while x % i == 0:
				x //= i
		i += 1
	if x > 1:
		ans.append(x)
	return ans
for _ in range(int(input())):
	(p, q) = map(int, input().strip().split())
	if p % q != 0:
		print(p)
	else:
		ans = 1
		primes = find_prime(q)
		for x in primes:
			i = p
			while i % q == 0:
				i //= x
			ans = max(ans, i)
		print(ans)


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	tmp = q
	tmps = []
	i = 2
	while i ** 2 <= tmp:
		if tmp % i == 0:
			tmps.append(i)
			while tmp % i == 0:
				tmp //= i
		i += 1
	if tmp > 1:
		tmps.append(tmp)
		tmp = 1
	for i in tmps:
		t = p
		while t % q == 0:
			t //= i
		tmp = max(t, tmp)
	print(tmp)


import sys, math
input = sys.stdin.readline

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def prime_f(num):
	add = []
	if num % 2 == 0:
		add.append(2)
	while num % 2 == 0:
		num //= 2
	for i in range(3, int(math.sqrt(num)) + 1, 2):
		if num % i == 0:
			add.append(i)
		while num % i == 0:
			num //= i
	if num > 2:
		add.append(num)
	return add
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if p < q:
		print(p)
		continue
	if p % q == 0:
		a = prime_f(q)
		ans = 1
		for i in a:
			tmp = p
			while tmp % q == 0:
				tmp //= i
			ans = max(ans, tmp)
		print(ans if ans % q != 0 else 1)
	else:
		print(p)


from collections import Counter
nn = int(input())

def get_primes(up_to):
	res = []
	for p in range(2, up_to + 1):
		flag = False
		for q in res:
			if q * q > p:
				break
			if p % q == 0:
				flag = True
				break
		if not flag:
			res.append(p)
	return res
primes = get_primes(int(1000000000 ** 0.5))
primes_set = set(primes)

def get_prime_divisors(x):
	res = Counter()
	if x in primes_set:
		res[x] += 1
		return res
	for prime in primes:
		while x % prime == 0:
			x = x // prime
			res[prime] += 1
		if x in primes_set:
			res[x] += 1
			return res
	if x > 1:
		res[x] += 1
	return res

def get_fix_prime_divisors(x, fix_primes):
	res = Counter()
	for prime in fix_primes:
		while x % prime == 0:
			x = x // prime
			res[prime] += 1
	return res

def main(p, q):
	q_primes = get_prime_divisors(q)
	p_primes = get_fix_prime_divisors(p, q_primes)
	minim = 1e+19
	for prime in q_primes:
		diff = p_primes[prime] - q_primes[prime]
		if diff < 0:
			return p
		minim = min(minim, prime ** (diff + 1))
	return p // minim
for _ in range(nn):
	(p, q) = [int(s) for s in input().split()]
	print(main(p, q))


from sys import maxsize, stdout, stdin, stderr
import re
tup = lambda : map(int, stdin.readline().split())
I = lambda : int(stdin.readline())
lint = lambda : [int(x) for x in stdin.readline().split()]
S = lambda : stdin.readline().replace('\n', '').strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
stpr = lambda x: stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
from collections import *
from math import *
for _ in range(I()):
	(a, b) = tup()
	t = b
	if a % b != 0:
		print(a)
		continue
	d = defaultdict(int)
	while b % 2 == 0:
		b //= 2
		d[2] += 1
	for i in range(3, int(b ** 0.5) + 1, 2):
		while b % i == 0:
			b //= i
			d[i] += 1
	if b > 2:
		d[b] += 1
	ans = 1
	for i in d:
		p = a
		(cnta, cntb) = (0, 0)
		while p % i == 0:
			p //= i
			cnta += 1
		ans = max(ans, a // i ** (cnta - d[i] + 1))
	print(ans)


def solve(p, q):
	array = []
	init = q
	i = 2
	while i ** 2 <= init:
		if init % i == 0:
			array.append(i)
			while init % i == 0:
				init //= i
		i += 1
	if init > 1:
		array.append(init)
	ans = 0
	for c in array:
		init2 = p
		while init2 % q == 0:
			init2 //= c
		ans = max(ans, init2)
	return ans
t = int(input())
for test in range(t):
	(p, q) = map(int, input().split())
	print(solve(p, q))


import math
t = int(input())
for tc in range(t):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	b = q
	factors = []
	for i in range(2, int(math.sqrt(b)) + 1):
		while not b % i:
			b //= i
			factors.append(i)
	if b - 1:
		factors.append(b)
	res = 1
	for f in factors:
		c = p
		while not c % q:
			c //= f
		res = max(res, c)
	print(res)


def facts(n):
	r = [n]
	i = 2
	while i * i <= n:
		if n % i == 0:
			r.append(i)
			if n // i != i:
				r.append(n // i)
		i += 1
	return r
for i in range(int(input())):
	(a, b) = map(int, input().split())
	if a % b != 0:
		print(a)
	else:
		c = 1
		x = facts(b)
		for i in x:
			n = a
			while n % i == 0:
				n //= i
				if n % b != 0:
					c = max(c, n)
					break
		print(c)


import sys
input = sys.stdin.readline
for f in range(int(input())):
	(p, q) = list(map(int, input().split()))
	d = 2
	facs = []
	facsm = []
	while q >= d * d:
		if q % d == 0:
			facs.append(d)
			x = 0
			while q % d == 0:
				x += 1
				q //= d
			facsm.append(x - 1)
		d += 1
	if q > 1:
		facs.append(q)
		facsm.append(0)
	mc = p
	pc = p
	for i in range(len(facs)):
		x = 0
		while pc % facs[i] == 0:
			x += 1
			pc //= facs[i]
		mc = min(mc, facs[i] ** (x - min(x, facsm[i])))
	p //= mc
	print(p)


import collections
t = int(input())
result = []
for i in range(t):
	(p, q) = list(map(int, input().split()))
	if p % q != 0:
		result.append(p)
	else:
		qcopy = q
		delit = collections.defaultdict(int)
		i = 2
		for i in range(2, int(q ** 0.5) + 1):
			while qcopy % i == 0:
				qcopy //= i
				delit[i] += 1
		if qcopy > 1:
			delit[qcopy] += 1
		pdel = collections.defaultdict(int)
		pcopy = p
		for i in delit.keys():
			while pcopy % i == 0:
				pdel[i] += 1
				pcopy //= i
		mindel = p
		for i in delit.keys():
			mindel = min(mindel, i ** (pdel[i] - delit[i] + 1))
		result.append(p // mindel)
for i in result:
	print(i)


import os, sys
from io import BytesIO, IOBase
from collections import Counter

def main():
	for _ in range(int(input())):
		(p, q) = map(int, input().split())
		fac = Counter()
		while not q % 2:
			fac[2] += 1
			q //= 2
		z = int(q ** 0.5)
		for i in range(3, z + 1, 2):
			while not q % i:
				fac[i] += 1
				q //= i
		if q != 1:
			fac[q] += 1
		maxi = 1
		for x in fac:
			r = 0
			z = p
			while not z % x:
				z //= x
				r += 1
			if r >= fac[x]:
				maxi = max(maxi, p // pow(x, r - fac[x] + 1))
			else:
				maxi = p
				break
		print(maxi)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
from array import array
from typing import List, Tuple, TypeVar, Generic, Sequence, Union

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def get_primes(n: int):
	from itertools import chain
	from array import array
	primes = [2, 3]
	is_prime = array('b', (0, 0, 1, 1, 0, 1, 0)) + array('b', (1, 0, 0, 0, 1, 0)) * ((n - 1) // 6)
	for i in chain.from_iterable((range(5, n + 1, 6), range(7, n + 1, 6))):
		if is_prime[i]:
			primes.append(i)
			for j in range(i * 3, n + 1, i * 2):
				is_prime[j] = 0
	return (is_prime, primes)

def main():
	t = int(input())
	(_, primes) = get_primes(50000)
	for _ in range(t):
		(pi, qi) = map(int, input().split())
		if pi % qi:
			print(pi)
			continue
		ans = 1
		for p in primes:
			if qi % p == 0:
				cnt = 0
				while qi % p == 0:
					cnt += 1
					qi //= p
				x = pi
				while x % p == 0:
					x //= p
				ans = max(ans, x * p ** (cnt - 1))
		if qi > 1 and pi % qi == 0:
			while pi % qi == 0:
				pi //= qi
			ans = max(ans, pi)
		print(ans)
main()


from collections import defaultdict
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	d = defaultdict(int)
	dp = defaultdict(int)
	tmp = q
	f = True
	z = 2
	while tmp > 1 and f:
		f = False
		for i in range(z, int(tmp ** 0.5) + 1):
			while tmp % i == 0:
				tmp //= i
				d[i] += 1
				f = True
			z = i + 1
		if f == False:
			d[tmp] += 1
	if p % q != 0:
		print(p)
		continue
	else:
		for x in d:
			kek = x
			while p % kek == 0:
				dp[x] += 1
				kek *= x
		mi = p + 1
		for x in d:
			mi = min(mi, x ** (dp[x] - d[x] + 1))
		print(p // mi)


t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		d = 2
		while d * d <= q and q % d:
			d += 1
		if d * d > q:
			d = q
		primes = [d]
		deg_q = [1]
		r = q // d
		while r % d == 0:
			r //= d
			deg_q[0] += 1
		if d == 2:
			d = 3
		else:
			d += 2
		while r > 1:
			while d * d <= r and r % d:
				d += 2
			if d * d > r:
				d = r
			primes.append(d)
			deg_q.append(0)
			while r % d == 0:
				r //= d
				deg_q[-1] += 1
			d += 2
		deg_p = []
		r = p
		for pr in primes:
			deg_p.append(0)
			while r % pr == 0:
				r //= pr
				deg_p[-1] += 1
		rec = p
		for i in range(len(primes)):
			n = int(primes[i] ** (deg_p[i] - deg_q[i] + 1))
			if n < rec:
				rec = n
		print(p // rec)


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k, modulus=1):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
		res %= modulus
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(p, q) = int_array()
	if q > p or p % q != 0:
		print(p)
		continue
	fact = primefs(q)
	ans = NINF
	for i in fact:
		x = p
		while x % q == 0:
			x //= i
		ans = max(ans, x)
	print(ans)


import sys
input = sys.stdin.readline

def prime_factors(m):
	i = 2
	f = []
	while i * i <= m:
		b = False
		while m % i == 0:
			m //= i
			b = True
		if b:
			f.append(i)
		i += 1
	if m > 1:
		f.append(m)
	return f

def solve(p, q):
	f = prime_factors(q)
	c = []
	for g in f:
		x = p
		while x % q == 0 and x % g == 0:
			x //= g
		c.append(x)
	return max(c)
t = int(input())
for case in range(t):
	print(solve(*[int(x) for x in input().split(' ')]))


for s in [*open(0)][1:]:
	(p, q) = map(int, s.split())
	l = []
	j = 2
	a = q
	while j * j <= a:
		if a % j == 0:
			l += [j]
			while a % j == 0:
				a //= j
		j += 1
	if a > 1:
		l += [a]
		a = 1
	for j in l:
		k = p
		while k % q == 0:
			k //= j
		a = max(k, a)
	print(a)


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		dic = {}
		for i in range(2, int(q ** (1 / 2)) + 1):
			count = 0
			while q % i == 0:
				count += 1
				q //= i
			if count != 0:
				dic[i] = count
		if q >= 2:
			dic[q] = 1
		ans = []
		for i in dic:
			co = p
			while co % i == 0:
				co //= i
			co *= i ** (dic[i] - 1)
			ans.append(co)
		print(max(ans))


from collections import defaultdict

def factorize(n):
	d = defaultdict(int)
	count = 0
	while (n % 2 > 0) == False:
		n >>= 1
		count += 1
	if count > 0:
		d[2] += count
	for i in range(3, int(n ** 0.5) + 1):
		count = 0
		while n % i == 0:
			count += 1
			n //= i
		if count > 0:
			d[i] += count
		i += 2
	if n > 2:
		d[n] += 1
	return d
for i in range(int(input())):
	(p, q) = map(int, input().split())
	d2 = factorize(q)
	ans = 1
	for j in d2:
		c = j ** d2[j]
		if p % c:
			ans = p
			break
		a = p
		while a % c == 0:
			a //= j
		ans = max(ans, a)
	print(ans)


from math import pow

def pizda(n):
	i = 2
	primfac = []
	while i * i <= n:
		while n % i == 0:
			primfac.append(i)
			n = n // i
		i = i + 1
	if n > 1:
		primfac.append(n)
	return primfac
t = int(input())
for j in range(t):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		a = pizda(q)
		b = []
		f = [a[0]]
		for i in range(1, len(a)):
			if a[i] == a[i - 1]:
				f.append(a[i])
			else:
				b.append(f)
				f = [a[i]]
		b.append(f)
		a = []
		c = []
		for i in range(len(b)):
			a.append(b[i][0])
			c.append(len(b[i]))
		b = [0 for i in range(len(a))]
		p1 = p
		for i in range(len(a)):
			while p % a[i] == 0:
				p = p // a[i]
				b[i] += 1
		for i in range(len(a)):
			b[i] = max(0, b[i] - c[i] + 1)
		for i in range(len(a)):
			b[i] = a[i] ** b[i]
		b.sort()
		print(p1 // b[0])


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
		continue
	fp = []
	q1 = q
	for i in range(2, int(q ** 0.5) + 1):
		if q1 % i == 0:
			fp.append(i)
			while q1 % i == 0:
				q1 //= i
	if q1 > 1:
		fp.append(q1)
	orig_p = p
	mini = p
	for i in range(len(fp)):
		p1 = p
		q1 = q
		(cnt_a, cnt_b) = (0, 0)
		while p1 % fp[i] == 0:
			p1 //= fp[i]
			cnt_a += 1
		while q1 % fp[i] == 0:
			q1 //= fp[i]
			cnt_b += 1
		val = 1
		while cnt_a >= cnt_b:
			val *= fp[i]
			cnt_a -= 1
		mini = min(mini, val)
	print(orig_p // mini)


import math
for x in range(int(input())):
	(h, m) = list(map(int, input().split()))
	x = m
	if h < m:
		print(h)
	elif h % m != 0:
		print(h)
	else:
		a = []
		for i in range(2, int(math.sqrt(m)) + 1):
			if m % i == 0:
				a.append(i)
				while x % i == 0:
					x = x // i
		v = 1
		if x > 1:
			a.append(x)
		for i in a:
			t = h
			while t % i == 0:
				t //= i
				if t % m != 0:
					v = max(v, t)
		print(v)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 1000000007
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def BruteForce(a, b):
	for i in range(1, a + 1):
		if a % i == 0 and i % b:
			ans = i
	return ans

def PrimeFactors(n):
	factors = []
	if n < 0:
		factors.append(-1)
		n = abs(n)
	while n % 2 == 0:
		factors.append(2)
		n //= 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n //= i
	if n > 1:
		factors.append(n)
	return factors
for _ in range(Int()):
	(a, b) = value()
	if a % b:
		print(a)
	else:
		fac = sorted(PrimeFactors(b))
		ans = 1
		for i in fac:
			temp = a
			while temp % i == 0:
				temp //= i
				if temp % b:
					ans = max(ans, temp)
		print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	d = defaultdict(int)
	copy = q
	if p % q != 0:
		print(p)
		continue
	while q % 2 == 0:
		d[2] += 1
		q //= 2
	for i in range(3, int(math.sqrt(q)) + 1, 2):
		while q % i == 0:
			q //= i
			d[i] += 1
	if q > 2:
		d[q] += 1
	mx = 0
	for i in d:
		num = i ** d[i]
		copy = p
		while copy % num == 0:
			copy //= i
		mx = max(mx, copy)
	print(mx)


for test in range(int(input())):
	(p, q) = map(int, input().split())
	j = 2
	c = q
	a = []
	while j * j <= c:
		if c % j == 0:
			a.append(j)
			while c % j == 0:
				c //= j
		j += 1
	if c > 1:
		a.append(c)
		c = 1
	for i in a:
		t = p
		while t % q == 0:
			t //= i
		c = max(c, t)
	print(c)


import math
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	y = q
	if p > q and p % q != 0:
		print(p)
	elif p < q:
		print(p)
	else:
		r = int(math.sqrt(q)) + 1
		d = []
		for i in range(2, r):
			cnt = 0
			if q % i == 0:
				d.append(i)
				while q % i == 0:
					q //= i
		if q > 1:
			d.append(q)
		ans = 1
		for i in d:
			tmp = p
			while tmp % y == 0:
				tmp //= i
			ans = max(tmp, ans)
		print(ans)


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	c = q
	d = p
	i = 1
	factor = []
	while i * i <= q:
		if q % i == 0:
			factor.append(i)
			if q // i != i:
				factor.append(q // i)
		i += 1
	factor.sort(reverse=True)
	factor.pop()
	m = 1
	for i in factor:
		d = p
		while d % c == 0:
			d //= i
		m = max(m, d)
	print(m)


from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C

def pf(n):
	v = []
	d = dd(lambda : 0)
	while n % 2 == 0:
		d[2] += 1
		n //= 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			d[i] += 1
			n //= i
		else:
			i += 1
	if n > 2:
		d[n] += 1
	return dict(d)
for _ in range(it()):
	(p, q) = mp()
	d = pf(q)
	ans = q
	ans = 1
	for (i, j) in d.items():
		k = p
		while k % q == 0:
			k //= i
		ans = max(ans, k)
	print(ans)


import collections
import math
for t in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		temp = set([])
		for i in range(1, int(math.sqrt(q)) + 1):
			if q % i == 0:
				temp.add(i)
				temp.add(q // i)
		out = 1
		temp.remove(1)
		for i in temp:
			val = p
			while val % i == 0:
				val //= i
				if val % q != 0:
					out = max(out, val)
					break
		print(out)


from sys import stdin
input = lambda : stdin.readline().strip()
t = int(input())
for j in range(t):
	(p, q) = map(int, input().split())
	i = 2
	lol = {}
	q1 = q
	while i ** 2 <= q1:
		if q % i == 0:
			lol[i] = 0
			while q % i == 0:
				lol[i] += 1
				q //= i
		i += 1
	if q != 1:
		lol[q] = 1
	if p % q1 == 0:
		ans = 0
		for i in lol.keys():
			p1 = p
			popa = 0
			while p1 % i == 0:
				popa += 1
				p1 //= i
			ans = max(ans, p1 * i ** (lol[i] - 1))
		p = ans
	print(p)


t = int(input())
for tt in range(t):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		s = []
		xx = q
		for i in range(2, int(q ** 0.5) + 1):
			if q % i == 0:
				s.append(q // i)
				s.append(i)
		s.append(q)
		ans = 0
		for i in s:
			val = 1
			xx = p
			while xx % q == 0:
				xx //= i
				val = xx
			ans = max(ans, val)
		print(ans)


def prime(m, a):
	i = 2
	while i * i <= m:
		if m % i == 0:
			m = m // i
			a.append(i)
		else:
			i += 1
	a.append(m)
	pass
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if m > n or n % m != 0:
		print(n)
	else:
		ans = 1
		a = []
		prime(m, a)
		for i in set(a):
			t = n
			while t % i == 0:
				t = t // i
				if t % m != 0:
					ans = max(t, ans)
					break
		print(ans)


import sys
import math
input = sys.stdin.readline

def get_int():
	return int(input().strip())

def get_int_list():
	return list(map(int, input().strip().split()))

def get_char_list():
	return list(input().strip())

def get_prime_list(v):
	target = int(math.sqrt(v))
	prime = [1] * target
	prime[0] = 0
	for i in range(1, target):
		if prime[i]:
			for j in range(i + i + 1, target, i + 1):
				prime[j] = 0
	fp = []
	for (i, v) in enumerate(prime):
		if v:
			fp.append(i + 1)
	return fp

def get_factor(v, prime_list):
	factor = []
	p_index = 0
	while v != 1:
		if v % prime_list[p_index] == 0:
			count = 0
			while v % prime_list[p_index] == 0:
				count += 1
				v //= prime_list[p_index]
			factor.append((prime_list[p_index], count))
		p_index += 1
		if p_index >= len(prime_list) and v != 1:
			factor.append((v, 1))
			v = 1
	return factor

def combine(f1, f2):
	factor = []
	i1 = 0
	i2 = 0
	while i1 < len(f1) and i2 < len(f2):
		(v1, c1) = f1[i1]
		(v2, c2) = f2[i2]
		if v1 == v2:
			factor.append((v1, c1 + c2))
			i1 += 1
			i2 += 1
		elif v1 > v2:
			factor.append((v2, c2))
			i2 += 1
		else:
			factor.append((v1, c1))
			i1 += 1
	if i1 < len(f1):
		factor.extend(f1[i1:])
	if i2 < len(f2):
		factor.extend(f2[i2:])
	return factor
cases = get_int()
prime_list = get_prime_list(1000000000.0)
for _ in range(cases):
	(p, q) = get_int_list()
	if p % q != 0:
		print(p)
	else:
		orig_p = p
		q_factor = get_factor(q, prime_list)
		p_factor = []
		while p % q == 0:
			p_factor = combine(p_factor, q_factor)
			p //= q
		p_factor = combine(p_factor, get_factor(p, prime_list))
		p_i = 0
		q_i = 0
		minv = orig_p
		while p_i < len(p_factor) and q_i < len(q_factor):
			(pv, pc) = p_factor[p_i]
			(qv, qc) = q_factor[q_i]
			if pv == qv:
				tv = pow(pv, pc - (qc - 1))
				minv = min(tv, minv)
				p_i += 1
				q_i += 1
			elif pv > qv:
				q_i += 1
			else:
				p_i += 1
		print(orig_p // minv)


def primeFactors(n):
	import math
	factors = dict()
	while n % 2 == 0:
		if 2 not in factors:
			factors[2] = 0
		factors[2] += 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			if i not in factors:
				factors[i] = 0
			factors[i] += 1
			n = n // i
	if n > 2:
		if n not in factors:
			factors[n] = 0
		factors[n] += 1
	return factors

def testcase():
	(p, q) = list(map(int, input().split()))
	if p < q or p % q != 0:
		print(p)
		return
	qf = primeFactors(q)
	pf = dict()
	pt = p
	for f in qf:
		pf[f] = 0
		while pt % f == 0 and f > 0:
			pf[f] += 1
			pt //= f
	div = 10 ** 18
	for f in qf:
		if qf[f] <= pf[f]:
			val = f ** (pf[f] - qf[f] + 1)
			div = min(div, val)
	ans = p // div
	print(ans)
	return
import sys, os
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)
t = int(input())
for _ in range(t):
	testcase()


import sys
from copy import *
from bisect import *
from math import gcd, ceil, sqrt
from itertools import permutations as prm, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def string(s):
	return ''.join(s)

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	(p, q) = mi()
	d = {}
	if p % q:
		print(p)
	else:
		for j in range(2, ceil(sqrt(q)) + 3):
			if q % j == 0:
				d[j] = 0
				while q % j == 0:
					q //= j
					d[j] += 1
		if q >= 2:
			d[q] = 1
		mini = 10 ** 18
		n = p
		for i in d:
			c = 0
			if p % i == 0:
				while p % i == 0:
					p //= i
					c += 1
				mini = min(mini, i ** (c - d[i] + 1))
		print(n // mini)


import sys
input = sys.stdin.buffer.readline

def solution():
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		l = []
		for i in range(2, int(q ** 0.5) + 1):
			if q % i == 0:
				l.append(i)
				if i != q // i:
					l.append(q // i)
		l.append(q)
		ans = 1
		for x in l:
			z = p
			while z % x == 0:
				z //= x
				if z % q != 0:
					ans = max(ans, z)
					break
		print(ans)
t = int(input())
for _ in range(t):
	solution()


primes = [2, 3]
for j in range(3, 35000):
	for k in primes:
		if j % k == 0:
			break
	else:
		primes.append(j)
t = int(input())
for i in range(t):
	(p, q) = [int(k) for k in input().split()]
	v = 10 ** 20
	l = q
	zxc = p
	res = []
	d = {}
	for j in primes:
		d[j] = 0
		while l % j == 0:
			d[j] += 1
			l //= j
	if l != 1:
		d[l] = 1
		v = l
	primes.append(v)
	if p % q:
		print(p)
	else:
		c = {}
		for j in primes:
			if q % j == 0:
				c[j] = 0
				while p % j == 0:
					c[j] += 1
					p //= j
				if d[j] > 0:
					res.append(j ** (c[j] - d[j] + 1))
		print(zxc // min(res))


for _ in ' ' * int(input()):
	(n, m) = map(int, input().split())
	if n % m:
		print(n)
	else:
		lst = []
		x = 2
		while x * x <= m:
			if not m % x:
				lst.append(x)
				if x != m // x:
					lst.append(m // x)
			x += 1
		lst.append(m)
		ans = 1
		for i in lst:
			elm = n
			while not elm % i:
				elm = elm // i
				if elm % m:
					ans = max(ans, elm)
					break
		print(ans)


from collections import Counter

def prime_dict(num):
	ans = Counter()
	while num % 2 == 0:
		num //= 2
		ans[2] += 1
	for i in range(3, int(num ** 0.5) + 1, 2):
		while num % i == 0:
			num //= i
			ans[i] += 1
		if num == 1:
			break
	if num > 2:
		ans[num] += 1
	return ans

def get_pow(a, b):
	ans = 0
	while a % b == 0:
		ans += 1
		a //= b
	return ans
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		d = prime_dict(q)
		ans = 1
		for i in d:
			alp = get_pow(p, i)
			bet = d[i]
			ans = max(ans, p // i ** (alp - bet + 1))
		print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter, defaultdict
import math

def prime(n):
	l1 = []
	while n % 2 == 0:
		l1.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l1.append(i)
			n = n // i
	if n > 2:
		l1.append(n)
	return l1
for _ in range(int(input())):
	(a1, b) = map(int, input().split())
	a = a1
	if a < b:
		print(a)
	else:
		v = a % b
		if v != 0:
			print(a)
		else:
			kb = Counter(prime(b))
			l1 = defaultdict(int)
			for i in kb:
				while a % i == 0:
					l1[i] += 1
					a = a // i
			max1 = -pow(10, 7)
			for i in l1:
				v = kb[i] - 1
				v1 = a1 // i ** (l1[i] - v)
				max1 = max(max1, v1)
			print(max1)


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q == 0:
		i = 2
		out = []
		while i ** 2 <= q:
			if q % i == 0:
				c = 0
				k = q
				while k % i == 0:
					k = k // i
					c += 1
				c -= 1
				t = p
				while t % i == 0:
					t = t // i
				out.append(t * i ** c)
				c = 0
				k = q
				while k % (q // i) == 0:
					k = k // (q // i)
					c += 1
				c -= 1
				r = p
				while r % (q // i) == 0:
					r = r // (q // i)
				out.append(r * (q // i) ** c)
			i += 1
		try:
			print(max(out))
		except:
			while p % q == 0:
				p = p // q
			print(p)
	else:
		print(p)


def factors(x):
	d = {}
	temp = 0
	while x % 2 == 0:
		temp += 1
		x = x // 2
	if temp:
		d[2] = temp
	for i in range(3, int(x ** 0.5) + 1):
		temp = 0
		while x % i == 0:
			temp += 1
			x = x // i
		if temp:
			d[i] = d.get(i, 0) + temp
	if x > 1:
		d[x] = d.get(x, 0) + 1
	return d
for u in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		x = factors(q)
		ans = 10 ** 19
		for i in x:
			temp = 0
			k = p
			while k % i == 0:
				temp += 1
				k = k // i
			ans = min(ans, i ** (temp - x[i] + 1))
		print(p // ans)


import math
import sys
input = sys.stdin.readline

def pf(n):
	l = set()
	while n % 2 == 0:
		l.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.add(i)
			n = n // i
	if n > 2:
		l.add(n)
	return l
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
		continue
	l = list(pf(q))
	ans = 1
	for i in l:
		k = p
		while k % q == 0:
			k //= i
		ans = max(k, ans)
	ans = str(ans)
	sys.stdout.write(ans + '\n')


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]
from math import floor

def solve(p, q):
	if p < q:
		return p
	if p % q:
		return p
	factors = set()
	for i in range(2, floor(q ** 0.5) + 1):
		if q % i == 0:
			factors.add(i)
			factors.add(q // i)
	factors.add(q)
	factors = list(factors)
	if factors == []:
		return 1
	if p == q:
		return p // min(factors)
	count = []
	for i in factors:
		q1 = q
		cnt = 0
		while q1 % i == 0:
			q1 = q1 // i
			cnt += 1
		count.append(cnt)
	count1 = []
	for i in factors:
		p1 = p
		cnt = 0
		while p1 % i == 0:
			p1 = p1 // i
			cnt += 1
		count1.append(cnt)
	ans = float('-inf')
	for i in range(len(factors)):
		p1 = p
		for j in range(count1[i] - count[i] + 1):
			p1 = p1 // factors[i]
			if p1 % factors[i]:
				break
		ans = max(ans, p1)
	if ans == float('-inf'):
		return 1
	return ans
for _ in range(int(input())):
	(p, q) = list(map(int, input().split()))
	print(solve(p, q))


import sys
import random
input = sys.stdin.readline

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def main():
	for _ in range(int(input())):
		(p, q) = map(int, input().split())
		orp = p
		orq = q
		if p % q != 0:
			print(p)
		else:
			a = []
			for i in range(2, int(q ** 0.5) + 1):
				if q % i == 0:
					a.append(i)
					a.append(q // i)
			a.append(q)
			b = []
			for i in a:
				k = p
				while k % q == 0:
					k //= i
				b.append(k)
			print(max(b))
	return
main()


import sys
import time
import os
from math import sqrt, log, log2, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial
from copy import copy, deepcopy
from sys import exit, stdin, stdout
from collections import Counter, defaultdict, deque
from itertools import permutations
import heapq
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = 1000000007
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
intl = lambda : list(map(int, inp().split(' ')))
mint = lambda : map(int, inp().split())
flol = lambda : list(map(float, inp().split(' ')))
flush = lambda : stdout.flush()

def permute(nums):

	def fun(arr, nums, cur, v):
		if len(cur) == len(nums):
			arr.append(cur.copy())
		i = 0
		while i < len(nums):
			if v[i]:
				i += 1
				continue
			else:
				cur.append(nums[i])
				v[i] = 1
				fun(arr, nums, cur, v)
				cur.pop()
				v[i] = 0
				i += 1
		return arr
	res = []
	nums.sort()
	v = [0] * len(nums)
	return fun(res, nums, [], v)

def subsets(res, index, arr, cur):
	res.append(cur.copy())
	for i in range(index, len(arr)):
		cur.append(arr[i])
		subsets(res, i + 1, arr, cur)
		cur.pop()
	return res

def sieve(N):
	root = int(sqrt(N))
	primes = [1] * (N + 1)
	(primes[0], primes[1]) = (0, 0)
	for i in range(2, root + 1):
		if primes[i]:
			for j in range(i * i, N + 1, i):
				primes[j] = 0
	return primes

def bs(arr, l, r, x):
	if x < arr[0] or x > arr[len(arr) - 1]:
		return -1
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	p = int(sqrt(n))
	for i in range(5, p + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def solve():
	(p, q) = mint()
	if p % q:
		print(p)
		return
	s = set()
	i = 2
	while i ** 2 <= q:
		if q % i == 0:
			s.add(i)
			s.add(q // i)
		i += 1
	s.add(q)
	l = list(s)
	ans = [1]
	for i in l:
		tp = p
		while tp % i == 0:
			tp //= i
			if tp % q != 0:
				ans.append(tp)
				break
	print(max(ans))
start_time = time.time()
for _ in range(iinp()):
	solve()


import collections
import math
import collections
import sys
from functools import reduce
import decimal
sys.setrecursionlimit(10 ** 9)
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
graphDict = collections.defaultdict
queue = collections.deque

class Graphs:

	def __init__(self):
		self.graph = graphDict(list)

	def add_edge(self, u, v):
		self.graph[u].append(v)

	def dfs_utility(self, nodes, visited_nodes):
		visited_nodes.add(nodes)
		for neighbour in self.graph[nodes]:
			if neighbour not in visited_nodes:
				self.dfs_utility(neighbour, visited_nodes)
			else:
				return neighbour

	def dfs(self, node):
		Visited = set()
		self.dfs_utility(node, Visited)

	def bfs(self, node):
		visited = set()
		if node not in visited:
			queue.append(node)
			visited.add(node)
		while queue:
			parent = queue.popleft()
			print(parent)
			for item in self.graph[parent]:
				if item not in visited:
					queue.append(item)
					visited.add(item)

def rounding(n):
	return int(decimal.Decimal(f'{n}').to_integral_value())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def inp():
	return sys.stdin.readline().strip()

def map_inp(v_type):
	return map(v_type, inp().split())

def list_inp(v_type):
	return list(map_inp(v_type))
for _ in range(int(inp())):
	(p, q) = map_inp(int)
	temp_q = q
	if p < q:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		fac = []
		for i in range(2, int(q ** 0.5) + 1):
			if temp_q % i == 0:
				fac.append(i)
				while temp_q % i == 0:
					temp_q //= i
		if temp_q > 1:
			fac.append(temp_q)
		ans = 1
		for item in fac:
			temp = p
			while temp % q == 0:
				temp //= item
			ans = max(ans, temp)
		print(ans)


def sieve(n):
	prime = [-1] * (n + 1)
	for i in range(2, n + 1):
		if prime[i] == -1:
			for j in range(i, n + 1, i):
				prime[j] = i
	return prime
prime = sieve(100010)
for nt in range(int(input())):
	(p, q) = map(int, input().split())
	if p > q:
		if p % q != 0:
			print(p)
		else:
			primes = []
			for i in range(2, int(q ** 0.5) + 2):
				if q % i == 0:
					primes.append(i)
					if q // i != 1:
						primes.append(q // i)
			ans = 1
			if not primes:
				primes.append(q)
			for i in primes:
				k = 1
				while p % i ** k == 0:
					if p // i ** k % q != 0:
						ans = max(ans, p // i ** k)
						break
					k += 1
			print(ans)
	elif p == q:
		num = -1
		for i in range(2, int(p ** 0.5) + 1):
			if p % i == 0:
				num = i
				break
		if num == -1:
			print(1)
		else:
			print(p // num)
	else:
		print(p)


import random

def gcd(a, b):
	if a == 0:
		return b
	if a < 0:
		return gcd(-a, b)
	while b > 0:
		c = a % b
		(a, b) = (b, c)
	return a

def mod_mul(a, b, n):
	result = 0
	while b > 0:
		if b & 1 > 0:
			result = (result + a) % n
		a = (a + a) % n
		b = b >> 1
	return result

def mod_exp(a, b, n):
	result = 1
	while b > 0:
		if b & 1 > 0:
			result = mod_mul(result, a, n)
		a = mod_mul(a, a, n)
		b = b >> 1
	return result

def MillerRabinPrimeCheck(n):
	if n in {2, 3, 5, 7, 11}:
		return True
	elif n == 1 or n % 2 == 0 or n % 3 == 0 or (n % 5 == 0) or (n % 7 == 0) or (n % 11 == 0):
		return False
	(k, u) = (0, n - 1)
	while not u & 1 > 0:
		k += 1
		u = u >> 1
	random.seed(0)
	s = 5
	for i in range(s):
		x = random.randint(2, n - 1)
		if x % n == 0:
			continue
		x = mod_exp(x, u, n)
		pre = x
		for j in range(k):
			x = mod_mul(x, x, n)
			if x == 1 and pre != 1 and (pre != n - 1):
				return False
			pre = x
		if x != 1:
			return False
		return True

def Pollard_rho(x, c):
	(i, k) = (1, 2)
	x0 = random.randint(0, x)
	y = x0
	while 1:
		i += 1
		x0 = (mod_mul(x0, x0, x) + c) % x
		d = gcd(y - x0, x)
		if d != 1 and d != x:
			return d
		if y == x0:
			return x
		if i == k:
			y = x0
			k += k

def PrimeFactorsListGenerator(n):
	result = []
	if n <= 1:
		return [1]
	if MillerRabinPrimeCheck(n):
		return [n]
	p = n
	while p >= n:
		p = Pollard_rho(p, random.randint(1, n - 1))
	result.extend(PrimeFactorsListGenerator(p))
	result.extend(PrimeFactorsListGenerator(n // p))
	return result
from collections import defaultdict
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		factor_q = sorted(PrimeFactorsListGenerator(q))
		FQ = defaultdict(int)
		for fq in factor_q:
			FQ[fq] += 1
		Min = 9999999999999999999
		for fq in list(set(factor_q)):
			temp = 1
			temp_p = p
			while temp_p % fq == 0:
				if temp_p < fq:
					break
				temp_p //= fq
				temp *= fq
			Min = min(Min, temp // fq ** (FQ[fq] - 1))
		print(p // Min)


results = []
for i in range(int(input())):
	(x, y) = map(int, input().split())
	n = y
	prms = []
	if x % y:
		results.append(x)
		continue
	i = 2
	while i * i <= n:
		if n % i == 0:
			prms.append(i)
			while n % i == 0:
				n //= i
		i += 1
	if n > 1:
		prms.append(n)
	biggest = 0
	for fac in prms:
		m = x
		m //= fac
		while m % y == 0:
			m //= fac
		biggest = max(m, biggest)
	results.append(biggest)
for r in results:
	print(r)


import math

def primeFactors(n):
	d = {}
	while n % 2 == 0:
		d[2] = d.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			d[i] = d.get(i, 0) + 1
			n = n // i
	if n > 2:
		d[n] = 1
	return d

def b_c(number, n):
	if n == 0:
		return 1
	elif n % 2 == 0:
		return b_c(number, n // 2) * b_c(number, n // 2)
	else:
		return number * b_c(number, (n - 1) // 2) * b_c(number, (n - 1) // 2)

def main():
	t = int(input())
	for __ in range(t):
		(p, q) = map(int, input().split())
		if p % q != 0:
			print(p)
		else:
			d = primeFactors(q)
			maxm = None
			for i in d.keys():
				x = p
				while x % q == 0:
					x = x // i
				if maxm is None:
					maxm = x
				else:
					maxm = max(maxm, x)
			print(maxm)
main()


def factors(q):
	factors = []
	i = 2
	while i * i <= q:
		if q % i == 0:
			factors.append(i)
			if i != q // i:
				factors.append(q // i)
		i += 1
	factors.append(q)
	return factors

def ans(p, q):
	if p % q != 0:
		return p
	ans = 1
	for i in factors(q):
		max_fact = p
		while max_fact % i == 0:
			max_fact = max_fact // i
			if max_fact % q != 0:
				ans = max(ans, max_fact)
				break
	return ans
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	print(ans(p, q))


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def solve():
	(P, Q) = getInts()
	if P < Q:
		return P
	if P % Q > 0:
		return P
	facs = sorted(list(factors(Q)))
	best = 0
	for fac in facs:
		if fac == 1:
			continue
		power = 1
		while P % fac ** power == 0:
			tmp = P // fac ** power
			if tmp % Q > 0:
				best = max(best, tmp)
			power += 1
	return best
for _ in range(getInt()):
	print(solve())


from collections import Counter
import math
import sys

def primeFactors(n):
	a = []
	while n % 2 == 0:
		a.append(2)
		n //= 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			a.append(i)
			n //= i
	if n > 2:
		a.append(n)
	return Counter(a)
cases = int(input())
for t in range(cases):
	(p, q) = list(map(int, input().split()))
	if p % q != 0:
		print(p)
	else:
		qprime = primeFactors(q)
		pprime = {}
		for i in qprime:
			x = p
			c = 0
			while x % i == 0:
				c += 1
				x //= i
			pprime[i] = c
		mv = pow(10, 20)
		for i in qprime:
			v = pprime[i] - (qprime[i] - 1)
			mv = min(mv, pow(i, v))
		output = p // mv
		print(output)


import os
import sys
from io import BytesIO, IOBase
N = int(10 ** 4.5) + 3
Primes = []
for i in range(2, N):
	isPrime = True
	for j in range(2, int(i ** 0.5) + 1):
		if i % j == 0:
			isPrime = False
			break
	if isPrime:
		Primes.append(i)

def main():
	for i in range(int(input())):
		(p, q) = map(int, input().split())
		if p % q != 0:
			print(p)
		else:
			factors = {}
			for i in Primes:
				if q < i * i:
					break
				while q % i == 0:
					q //= i
					if i in factors:
						factors[i] += 1
					else:
						factors[i] = 1
			if q != 1:
				if q in factors:
					factors[q] += 1
				else:
					factors[q] = 1
			ansMax = 0
			for elem in factors:
				pCpy = p
				cnt = 0
				while pCpy % elem == 0:
					cnt += 1
					pCpy //= elem
				ans = pCpy * elem ** min(cnt, factors[elem] - 1)
				if ans > ansMax:
					ansMax = ans
			print(ansMax)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	res = []
	for (ch, g) in groupby(it):
		res.append((ch, len(list(g))))
	return res
t = N()
for i in range(t):
	(p, q) = RL()
	if p % q != 0:
		ans = p
	else:
		x = q
		res = []
		i = 2
		while i * i <= q:
			c = 0
			while q % i == 0:
				c += 1
				q //= i
			if c:
				res.append((i, c))
			i += 1
		if q > 1:
			res.append((q, 1))
		ans = 0
		for (a, c) in res:
			tmp = p // pow(a, c)
			while tmp % a == 0:
				tmp //= a
			ans = max(ans, pow(a, c - 1) * tmp)
	print(ans)


n = int(input())
for j in range(n):
	(p, q) = map(int, input().split())
	(i, x, lis) = (2, q, [])
	while i ** 2 <= q:
		if x % i == 0:
			lis.append(i)
			while x % i == 0:
				x = x // i
		i = i + 1
	if x > 1:
		lis.append(x)
		x = 1
	for o in lis:
		y = p
		while y % q == 0:
			y = y // o
		if y > x:
			x = y
	print(x)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')

def primeFactors(n):
	dic = dd(int)
	while n % 2 == 0:
		dic[2] += 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			dic[i] += 1
			n = n // i
	if n > 2:
		dic[n] += 1
	return dic
t = ip()
for _ in range(t):
	(p, q) = inp()
	if p < q:
		ans = p
	elif p % q != 0:
		ans = p
	else:
		g = gcd(p, q)
		if g == 1:
			ans = p
		else:
			prime = primeFactors(g)
			ch = sorted(list(prime.keys()))
			d = dd(int)
			for i in ch:
				temp = p
				while temp % i == 0:
					d[i] += 1
					temp = temp // i
			val = []
			for i in ch:
				val.append(pow(i, d[i] - prime[i] + 1))
			div = min(val)
			ans = p // div
	out(ans)


import sys
input = sys.stdin.buffer.readline
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().decode().rstrip('\n\r')
wrt = sys.stdout.write
pr = lambda *args, end='\n': wrt(' '.join([str(x) for x in args]) + end)
xrange = lambda args: reversed(range(args))
cdiv = lambda x, y: -(-x // y)
enum = enumerate
inf = float('inf')
mod = 10 ** 9 + 7
from collections import Counter

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def do(p, q, x):
	cnt = 0
	while q % x == 0:
		cnt += 1
		q //= x
	while p % x == 0:
		p //= x
	for i in range(cnt - 1):
		p *= x
	return p
from math import gcd
Cases = ri()
for Case in range(Cases):
	(p, q) = rl()
	if p % q:
		print(p)
	else:
		ans = 1
		for x in prime_factors(q):
			ans = max(ans, do(p, q, x))
		print(ans)


import math

def prime_factors(x):
	factors = set()
	for i in range(2, 10 ** 5 + 1):
		while not x % i:
			factors.add(i)
			x /= i
	if x != 1:
		factors.add(x)
	return factors
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	(factors, can) = (prime_factors(q), [])
	for factor in factors:
		res = p
		while not res % q:
			res //= factor
		can.append(res)
	print(int(max(can)))


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def primeFactors(n):
	ans = []
	while n % 2 == 0:
		ans.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			ans.append(i)
			n = n // i
	if n > 2:
		ans.append(n)
	return ans

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		(A, B) = In()
		if A % B != 0:
			print(A)
			return
		else:
			b = dict(primeFactors(B))
			ans = 0
			for x in b:
				a = A
				cnt = 0
				while a % x == 0:
					cnt += 1
					a //= x
				temp = a * x ** min(cnt, b[x] - 1)
				ans = max(ans, temp)
			print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	(p, q) = map(int, stdin.readline().split())
	if p % q != 0:
		print(p)
	else:
		factor = {}
		i = 2
		while i * i <= q:
			if q % i == 0:
				pcount = 1
				while q % i == 0:
					q = q // i
					pcount *= i
				factor[i] = pcount
			i += 1
		if q > 1:
			factor[q] = q
		maxi = 1
		for i in factor:
			tmp = p
			count = 1
			while tmp % i == 0:
				tmp = tmp // i
				count *= i
			m = factor[i]
			while count >= m:
				count = count // i
			c = tmp * count
			if c > maxi:
				maxi = c
		print(maxi)


import math

def primeFactors(n):
	primes = []
	fact = []
	if n % 2 == 0:
		primes.append(2)
		fact.append(1)
		n = n // 2
	while n % 2 == 0:
		fact[0] += 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		if n % i == 0:
			primes.append(i)
			fact.append(1)
			n = n // i
		while n % i == 0:
			fact[-1] += 1
			n = n // i
	if n > 2:
		primes.append(n)
		fact.append(1)
	return [primes, fact]
import sys
input = sys.stdin.readline
t = int(input())
for test in range(t):
	[p, q] = list(map(int, input().split(' ')))
	[primes, fact] = primeFactors(q)
	deli = p % q
	if deli == 0:
		k = []
		for i in range(len(primes)):
			prox = p
			fac = 0
			while prox % primes[i] == 0:
				fac += 1
				prox = prox // primes[i]
			k.append(primes[i] ** (fac - fact[i] + 1))
		m = min(k)
		res = p // m
		print(res)
	else:
		print(int(p))


def primefactorize(n):
	global Counter
	i = 2
	while i <= n ** (1 / 2):
		if n == 1:
			break
		if n % i == 0:
			n //= i
			if i in Counter:
				Counter[i] += 1
			else:
				Counter[i] = 1
		else:
			i += 1
	if n != 1:
		if n in Counter:
			Counter[n] += 1
		else:
			Counter[n] = 1
for i in range(int(input())):
	(p, q) = map(int, input().split())
	Counter = dict()
	primefactorize(q)
	ans = float('-inf')
	if p % q != 0:
		print(p)
		continue
	for i in Counter:
		x = p
		while x % q == 0:
			x //= i
		if x > ans:
			ans = x
	print(ans)


from collections import defaultdict
primes = {i for i in range(2, 10 ** 5)}
for i in range(2, 10 ** 5):
	if i in primes:
		for j in range(2 * i, 10 ** 5, i):
			if j in primes:
				primes.remove(j)
primes = list(primes)
primes.sort()

def solve():
	(p, q) = map(int, input().split())
	P = p
	if p % q != 0:
		print(p)
		return
	pdiv = defaultdict(int)
	qdiv = defaultdict(int)
	for i in range(2, q + 1):
		if i * i > q:
			break
		if q % i == 0:
			while q % i == 0:
				pdiv[i] += 1
				qdiv[i] += 1
				p //= i
				q //= i
			while p % i == 0:
				pdiv[i] += 1
				p //= i
	if q > 1:
		qdiv[q] += 1
		while p % q == 0:
			pdiv[q] += 1
			p //= q
	ans = -1
	for v in pdiv.keys():
		ans = max(ans, P // v ** (pdiv[v] - qdiv[v] + 1))
	print(ans)
	return

def main():
	testcase = int(input())
	for i in range(testcase):
		solve()
	return
main()


import math
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	ans = []
	nq = q
	for test in [2] + list(range(3, int(math.sqrt(q)) + 1, 2)):
		if q % test == 0:
			p2 = p
			while p2 % q == 0:
				p2 //= test
			ans.append(p2)
		while nq % test == 0:
			nq //= test
	if nq > 1:
		while p % q == 0:
			p //= nq
		ans.append(p)
	print(max(ans))


import math

def factorize(n):
	primes = {}
	for d in range(2, int(n ** 0.5) + 1):
		while n % d == 0:
			if d not in primes:
				primes[d] = 1
			else:
				primes[d] += 1
			n //= d
		if n == 1:
			break
	if n != 1:
		primes[n] = 1
	return primes

def main():
	t = int(input())
	for i in range(t):
		(p, q) = map(int, input().split())
		if p % q:
			print(p)
			continue
		q_primes = factorize(q)
		result = 1
		for prime in q_primes:
			temp = p
			while temp % prime == 0:
				temp //= prime
			x = temp * prime ** (q_primes[prime] - 1)
			result = max(result, x)
		print(result)
main()


from collections import Counter
t = int(input())
for __ in range(t):
	(a, b) = map(int, input().split())
	if a % b != 0:
		print(a)
		continue
	d = Counter()
	x = a
	for i in range(2, 50000):
		while b % i == 0:
			b = b // i
			d[i] += 1
	if b != 1:
		d[b] += 1
	m = 0
	for i in d:
		x = a
		while x % i == 0:
			x = x // i
		x = x * i ** (d[i] - 1)
		m = max(m, x)
	print(m)


import math

def primeFactors(n, s):
	while n % 2 == 0:
		try:
			s[2] += 1
		except:
			s[2] = 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			try:
				s[i] += 1
			except:
				s[i] = 1
			n = n // i
	if n > 2:
		s[n] = 1
	return s
for _ in range(int(input())):
	s = {}
	(a, b) = map(int, input().split())
	if a % b != 0:
		print(a)
	else:
		s = primeFactors(b, s)
		ans = []
		for i in s:
			temp = a
			cnt = 0
			while temp % i == 0 and temp > 0:
				cnt += 1
				temp //= i
			ans.append(i ** (cnt - s[i] + 1))
		print(int(a // min(ans)))


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(i) for i in st()]

def factorize(n):
	d = defaultdict(int)
	for i in range(2, int(n ** 0.5) + 1):
		while n % i == 0:
			d[i] += 1
			n = n // i
	if n - 1:
		d[n] = 1
	return d
for i in range(val()):
	(a, b) = li()
	if a % b:
		print(a)
		continue
	mydict = factorize(b)
	l = list(mydict)
	ans = a
	finans = 1
	for i in l:
		temp = ans
		while temp % i == 0:
			temp = temp // i
		temp *= i ** (mydict[i] - 1)
		finans = max(finans, temp)
	print(finans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *
from math import sqrt, pi, ceil, log, inf, gcd
from itertools import permutations
from copy import deepcopy
from heapq import *
from sys import setrecursionlimit

def main():
	for _ in range(int(input())):
		(p, q) = map(int, input().split())
		if p % q != 0:
			print(p)
		else:
			f = Counter()
			(i, ans) = (2, 0)
			while i * i <= q:
				if q % i:
					i += 1
				else:
					q = q // i
					f[i] += 1
			if q > 1:
				f[q] += 1
			for i in f:
				(x, c) = (p, 0)
				while x % i == 0:
					x = x // i
					c += 1
				ans = max(ans, p // pow(i, c - f[i] + 1))
			print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from math import floor, sqrt
t = int(input())
primes = []
powers = []

def factorization(n):
	for i in range(2, floor(sqrt(n) + 1)):
		if n % i == 0:
			power = 0
			while n % i == 0:
				power = power + 1
				n = n // i
			primes.append(i)
			powers.append(power)
	if n != 1:
		primes.append(n)
		powers.append(1)

def vp(x, prime):
	powers2 = []
	for i in prime:
		c = 1
		while x != 1 and x % i == 0:
			x = x // i
			c = c + 1
		powers2.append(i ** c)
	return powers2
for i in range(t):
	(p, q) = input().split()
	p = int(p)
	q = int(q)
	if p < q:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		primes = []
		powers = []
		factorization(q)
		s = p // q
		powers2 = vp(s, primes)
		print(p // min(powers2))


from math import sqrt

def pr(p):
	if p <= 3:
		return [[p, 1]]
	i = 2
	n = int(sqrt(p)) + 1
	res = []
	while i < n:
		if p % i == 0:
			res.append([i, 0])
			while p % i == 0:
				p = p // i
				res[-1][1] += 1
		i += 1
	if p > 1:
		res.append([p, 1])
	return res

def kkk(x, p):
	res = 0
	while x % p == 0:
		x = x // p
		res += 1
	return res
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	pp = pr(q)
	ans = 1
	for x in pp:
		a = x[0]
		k = x[1]
		h = kkk(p, a)
		temp = p
		if h < k:
			ans = p
		else:
			for j in range(h - k + 1):
				temp = temp // a
			ans = max(ans, temp)
	print(ans)


import sys, time
from copy import copy, deepcopy as dcopy
from os import path
from collections import *
from heapq import *
from math import *

def sieve(q):
	x = 2
	fact = []
	while x * x <= q:
		if q % x == 0:
			fact.append(x)
		while q % x == 0:
			q //= x
		x += 1
	if q > 1:
		fact.append(q)
	return fact

def calc(x, q):
	ans = 0
	while q % x == 0:
		ans += 1
		q //= x
	return ans

def get(c, p, q):
	det = calc(c, p) - calc(c, q) + 1
	return int(pow(c, det))

def solve(kkkk):
	(p, q) = inm()
	gc = gcd(p, q)
	ans = p
	if gc == q:
		pr = sieve(q)
		val = p
		for x in pr:
			val = min(val, get(x, p, q))
		ans = p // val
	out(ans)

def main():
	t = 1
	t = inp()
	for i in range(t):
		solve(i + 1)
localsys = 0
start_time = time.time()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	localsys = 1
cin = sys.stdin.readline
cout = sys.stdout.write

def inp():
	return int(cin().strip())

def ins():
	return list(cin().strip())

def inm():
	return map(int, cin().strip().split())

def ina():
	return list(map(int, cin().strip().split()))

def out(a):
	cout(str(a) + '\n')

def outa(a):
	cout(' '.join(map(str, a)) + '\n')

def outm(a, b, c=None, d=None):
	cout(str(a) + ' ' + str(b))
	if c != None:
		cout(' ' + str(c))
	if d != None:
		cout(' ' + str(d))
	cout('\n')
main()
if localsys:
	print('\n\nTime Elapsed :', time.time() - start_time, 'seconds')


import math
T = int(input())
for _ in range(0, T):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		n = q
		L = []
		if n % 2 == 0:
			L.append(2)
		while n % 2 == 0:
			n = n // 2
		for i in range(3, int(math.sqrt(n)) + 1, 2):
			if n % i == 0:
				L.append(i)
			while n % i == 0:
				n = n // i
		if n > 2:
			L.append(n)
		ans = 1
		for i in range(0, len(L)):
			num = p
			ele = L[i]
			while num % q == 0 and num % ele == 0:
				num = num // ele
			ans = max(ans, num)
		print(ans)


import math
for test in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
		continue
	factors = [q]
	for f in range(2, int(math.sqrt(q)) + 1):
		if not q % f:
			factors.append(f)
			factors.append(q // f)
	ans = 1
	for f in factors:
		tmp = p
		while not tmp % q and (not tmp % f):
			tmp = tmp // f
		if tmp % q:
			ans = max(ans, tmp)
	print(ans)


import math
t = int(input())
for _ in range(t):
	(p, q) = list(map(int, input().split()))
	t = []
	ans = 1
	i = 2
	n = q
	while i * i <= n:
		if n % i == 0:
			t.append(i)
			n //= i
		else:
			i += 1
	t.append(n)
	for i in t:
		k = p
		while k % q == 0:
			k //= i
		ans = max(ans, k)
	print(ans)


from math import sqrt
t = int(input())
for _ in range(t):
	(p, q) = (int(x) for x in input().split())
	if p % q:
		print(p)
	else:
		st = {1}
		(i, s) = (2, sqrt(q))
		c = p
		while c % q == 0:
			c //= q
		st.add(c)
		while i <= s:
			if q % i == 0:
				c = p
				while c % q == 0:
					c //= i
				st.add(c)
				c = p
				while c % q == 0:
					c //= q // i
				st.add(c)
			i += 1
		print(max(st))


import io
import os
from collections import Counter, defaultdict, deque
from math import gcd

def primeFactors(n):
	assert n >= 1
	factors = Counter()
	while n % 2 == 0:
		factors[2] += 1
		n //= 2
	x = 3
	while x * x <= n:
		while n % x == 0:
			factors[x] += 1
			n //= x
		x += 2
	if n != 1:
		factors[n] += 1
	return factors

def solve(P, Q):
	g = gcd(P, Q)
	if g != Q:
		return P
	else:
		best = float('inf')
		factors = primeFactors(Q)
		for (k, v) in factors.items():
			k2 = P
			v2 = 0
			while k2 % k == 0:
				k2 //= k
				v2 += 1
			best = min(best, k ** (v2 - v + 1))
		return P // best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(P, Q) = [int(x) for x in input().split()]
	ans = solve(P, Q)
	print(ans)


import sys
input = sys.stdin.readline

def inar():
	return [int(el) for el in input().split()]

def main():
	t = int(input())
	for _ in range(t):
		(p, q) = map(int, input().split())
		if p % q:
			print(p)
		else:
			new = q
			factors = [q]
			ans = 1
			for i in range(2, int(new ** 0.5) + 1):
				if new % i == 0:
					factors.append(i)
				if new != new // i:
					factors.append(new // i)
			for el in factors:
				take = p
				while take % el == 0:
					take //= el
					if take % q != 0:
						ans = max(ans, take)
						break
			print(ans)
main()


import sys
from math import *
from fractions import *

def main():
	(p, q) = map(int, sys.stdin.readline().split())
	if p % q:
		print(p)
	else:
		res = 1
		i = 1
		while i * i <= q:
			if q % i == 0:
				(a, s, d, f) = (p, q, 0, 0)
				if i != 1:
					while a % i == 0:
						a //= i
						d += 1
					while s % i == 0:
						f += 1
						s //= i
					a = p
					while d >= f:
						d -= 1
						a //= i
					res = max(res, a)
				(a, s, d, f) = (p, q, 0, 0)
				if q // i != 1:
					g = q // i
					while a % g == 0:
						a //= g
						d += 1
					while s % g == 0:
						f += 1
						s //= g
					a = p
					while d >= f:
						d -= 1
						a //= g
					res = max(res, a)
			i += 1
		print(res)
for i in range(int(input()) - 1):
	main()
main()


from sys import stdin, stdout
from math import gcd
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))

def fact(n):
	mp = {}
	while n % 2 == 0:
		mp[2] = mp.get(2, 0) + 1
		n //= 2
	div = 3
	while div * div <= n:
		while n % div == 0:
			mp[div] = mp.get(div, 0) + 1
			n //= div
		div += 2
	if n > 2:
		mp[n] = 1
	return mp
for _ in range(1):
	tc = nmbr()
	for _ in range(tc):
		(p, q) = lst()
		if p % q != 0:
			print(p)
		else:
			ans = float('inf')
			fq = fact(q)
			for (f, pow) in fq.items():
				n = p
				power = 0
				while n % f == 0:
					n //= f
					power += 1
				extra = power - (pow - 1)
				ans = min(ans, f ** extra)
			print(p // ans)


from collections import defaultdict as dd, deque as dq, Counter as c
from math import factorial as f, ceil, gcd, sqrt
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	d = dd(lambda : 0)
	p = dd(lambda : 0)
	ans = float('inf')
	if a % b != 0:
		print(a)
	else:
		co = 0
		while b % 2 == 0:
			d[2] += 1
			b //= 2
		temp = b
		for i in range(3, int(sqrt(temp)) + 1, 2):
			if b % i == 0:
				co = 0
				while b % i == 0:
					d[i] += 1
					b //= i
		if b > 2:
			d[b] = 1
		temp = a
		for i in d:
			while temp % i == 0:
				p[i] += 1
				temp //= i
			ans = min(ans, i ** (p[i] - d[i] + 1))
		print(a // ans)


T = int(input())
for _ in range(T):
	(p, q) = map(int, input().split())
	if p < q:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		s = set()
		i = 2
		while i * i <= q:
			if q % i == 0:
				s.add(i)
				s.add(q // i)
			i += 1
		l = list(s)
		l.append(q)
		ans = 1
		for i in l:
			k = p
			while k % q == 0:
				k //= i
			ans = max(ans, k)
		print(ans)


from math import gcd as _gcd

def main():
	t = int(input())
	for i_t in range(t):
		(p, q) = _read_ints()
		if p % q != 0:
			x = p
		else:
			q_factors = _represent_as_prime_factors(q)
			x = max((p // q_factor ** _count_factor(p, q_factor) * q_factor ** (q_factor_n - 1) for (q_factor, q_factor_n) in q_factors.items()))
		print(x)

def _read_ints():
	return map(int, input().split())

def _represent_as_prime_factors(x):
	if x < 1 or x != int(x):
		raise ValueError("can't represent as prime factors")
	x = int(x)
	result = dict()
	potential_factor = 2
	while potential_factor ** 2 <= x:
		if x % potential_factor == 0:
			result[potential_factor] = _count_factor(x, potential_factor)
			x //= potential_factor ** result[potential_factor]
		potential_factor += 1
	if x != 1:
		result[x] = 1
	return result

def _count_factor(x, factor):
	result = 0
	while x % factor == 0:
		x //= factor
		result += 1
	return result
main()


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for asdf in range(t):
	(p, q) = invr()
	prime = []
	if p % q != 0:
		print(p)
	else:
		tmpq = q
		for i in range(2, 1 + math.floor(math.sqrt(tmpq))):
			if q == 1:
				break
			if q % i == 0:
				q = q // i
				prime.append([i, 1])
			while q % i == 0:
				q = q // i
				prime[-1][1] += 1
		if q != 1:
			prime.append([q, 1])
		result = 1
		for a in prime:
			tmpp = p
			pr = a[0]
			qex = a[1]
			pex = 0
			while tmpp % pr == 0:
				tmpp = tmpp // pr
				pex += 1
			for j in range(qex - 1):
				tmpp *= pr
			if result < tmpp:
				result = tmpp
		print(result)


import os
import sys
from io import BytesIO, IOBase
import math
from collections import defaultdict
import random
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	x = k
	i = 2
	l = []
	while i * i <= x:
		while x % i == 0:
			l.append(i)
			x //= i
		i += 1
	if x > 1:
		l.append(x)
		x = 1
	for i in l:
		t = n
		while t % k == 0:
			t //= i
		x = max(x, t)
	print(x)


primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133, 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223, 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429, 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529, 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859, 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957, 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071, 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171, 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279, 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393, 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491, 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731, 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831, 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933, 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119, 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241, 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343, 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437, 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527, 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613, 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713, 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229, 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337, 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457, 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577, 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687, 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759, 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877, 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967, 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347, 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653, 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747, 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831, 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939, 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073, 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161, 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269, 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349, 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443, 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559, 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649, 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749, 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859, 15877, 15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959, 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069, 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187, 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301, 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421, 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529, 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649, 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747, 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883, 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981, 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077, 17093, 17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191, 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321, 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401, 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491, 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599, 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729, 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839, 17851, 17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939, 17957, 17959, 17971, 17977, 17981, 17987, 17989, 18013, 18041, 18043, 18047, 18049, 18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133, 18143, 18149, 18169, 18181, 18191, 18199, 18211, 18217, 18223, 18229, 18233, 18251, 18253, 18257, 18269, 18287, 18289, 18301, 18307, 18311, 18313, 18329, 18341, 18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439, 18443, 18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523, 18539, 18541, 18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691, 18701, 18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797, 18803, 18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18959, 18973, 18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079, 19081, 19087, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211, 19213, 19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309, 19319, 19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423, 19427, 19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483, 19489, 19501, 19507, 19531, 19541, 19543, 19553, 19559, 19571, 19577, 19583, 19597, 19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727, 19739, 19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841, 19843, 19853, 19861, 19867, 19889, 19891, 19913, 19919, 19927, 19937, 19949, 19961, 19963, 19973, 19979, 19991, 19993, 19997, 20011, 20021, 20023, 20029, 20047, 20051, 20063, 20071, 20089, 20101, 20107, 20113, 20117, 20123, 20129, 20143, 20147, 20149, 20161, 20173, 20177, 20183, 20201, 20219, 20231, 20233, 20249, 20261, 20269, 20287, 20297, 20323, 20327, 20333, 20341, 20347, 20353, 20357, 20359, 20369, 20389, 20393, 20399, 20407, 20411, 20431, 20441, 20443, 20477, 20479, 20483, 20507, 20509, 20521, 20533, 20543, 20549, 20551, 20563, 20593, 20599, 20611, 20627, 20639, 20641, 20663, 20681, 20693, 20707, 20717, 20719, 20731, 20743, 20747, 20749, 20753, 20759, 20771, 20773, 20789, 20807, 20809, 20849, 20857, 20873, 20879, 20887, 20897, 20899, 20903, 20921, 20929, 20939, 20947, 20959, 20963, 20981, 20983, 21001, 21011, 21013, 21017, 21019, 21023, 21031, 21059, 21061, 21067, 21089, 21101, 21107, 21121, 21139, 21143, 21149, 21157, 21163, 21169, 21179, 21187, 21191, 21193, 21211, 21221, 21227, 21247, 21269, 21277, 21283, 21313, 21317, 21319, 21323, 21341, 21347, 21377, 21379, 21383, 21391, 21397, 21401, 21407, 21419, 21433, 21467, 21481, 21487, 21491, 21493, 21499, 21503, 21517, 21521, 21523, 21529, 21557, 21559, 21563, 21569, 21577, 21587, 21589, 21599, 21601, 21611, 21613, 21617, 21647, 21649, 21661, 21673, 21683, 21701, 21713, 21727, 21737, 21739, 21751, 21757, 21767, 21773, 21787, 21799, 21803, 21817, 21821, 21839, 21841, 21851, 21859, 21863, 21871, 21881, 21893, 21911, 21929, 21937, 21943, 21961, 21977, 21991, 21997, 22003, 22013, 22027, 22031, 22037, 22039, 22051, 22063, 22067, 22073, 22079, 22091, 22093, 22109, 22111, 22123, 22129, 22133, 22147, 22153, 22157, 22159, 22171, 22189, 22193, 22229, 22247, 22259, 22271, 22273, 22277, 22279, 22283, 22291, 22303, 22307, 22343, 22349, 22367, 22369, 22381, 22391, 22397, 22409, 22433, 22441, 22447, 22453, 22469, 22481, 22483, 22501, 22511, 22531, 22541, 22543, 22549, 22567, 22571, 22573, 22613, 22619, 22621, 22637, 22639, 22643, 22651, 22669, 22679, 22691, 22697, 22699, 22709, 22717, 22721, 22727, 22739, 22741, 22751, 22769, 22777, 22783, 22787, 22807, 22811, 22817, 22853, 22859, 22861, 22871, 22877, 22901, 22907, 22921, 22937, 22943, 22961, 22963, 22973, 22993, 23003, 23011, 23017, 23021, 23027, 23029, 23039, 23041, 23053, 23057, 23059, 23063, 23071, 23081, 23087, 23099, 23117, 23131, 23143, 23159, 23167, 23173, 23189, 23197, 23201, 23203, 23209, 23227, 23251, 23269, 23279, 23291, 23293, 23297, 23311, 23321, 23327, 23333, 23339, 23357, 23369, 23371, 23399, 23417, 23431, 23447, 23459, 23473, 23497, 23509, 23531, 23537, 23539, 23549, 23557, 23561, 23563, 23567, 23581, 23593, 23599, 23603, 23609, 23623, 23627, 23629, 23633, 23663, 23669, 23671, 23677, 23687, 23689, 23719, 23741, 23743, 23747, 23753, 23761, 23767, 23773, 23789, 23801, 23813, 23819, 23827, 23831, 23833, 23857, 23869, 23873, 23879, 23887, 23893, 23899, 23909, 23911, 23917, 23929, 23957, 23971, 23977, 23981, 23993, 24001, 24007, 24019, 24023, 24029, 24043, 24049, 24061, 24071, 24077, 24083, 24091, 24097, 24103, 24107, 24109, 24113, 24121, 24133, 24137, 24151, 24169, 24179, 24181, 24197, 24203, 24223, 24229, 24239, 24247, 24251, 24281, 24317, 24329, 24337, 24359, 24371, 24373, 24379, 24391, 24407, 24413, 24419, 24421, 24439, 24443, 24469, 24473, 24481, 24499, 24509, 24517, 24527, 24533, 24547, 24551, 24571, 24593, 24611, 24623, 24631, 24659, 24671, 24677, 24683, 24691, 24697, 24709, 24733, 24749, 24763, 24767, 24781, 24793, 24799, 24809, 24821, 24841, 24847, 24851, 24859, 24877, 24889, 24907, 24917, 24919, 24923, 24943, 24953, 24967, 24971, 24977, 24979, 24989, 25013, 25031, 25033, 25037, 25057, 25073, 25087, 25097, 25111, 25117, 25121, 25127, 25147, 25153, 25163, 25169, 25171, 25183, 25189, 25219, 25229, 25237, 25243, 25247, 25253, 25261, 25301, 25303, 25307, 25309, 25321, 25339, 25343, 25349, 25357, 25367, 25373, 25391, 25409, 25411, 25423, 25439, 25447, 25453, 25457, 25463, 25469, 25471, 25523, 25537, 25541, 25561, 25577, 25579, 25583, 25589, 25601, 25603, 25609, 25621, 25633, 25639, 25643, 25657, 25667, 25673, 25679, 25693, 25703, 25717, 25733, 25741, 25747, 25759, 25763, 25771, 25793, 25799, 25801, 25819, 25841, 25847, 25849, 25867, 25873, 25889, 25903, 25913, 25919, 25931, 25933, 25939, 25943, 25951, 25969, 25981, 25997, 25999, 26003, 26017, 26021, 26029, 26041, 26053, 26083, 26099, 26107, 26111, 26113, 26119, 26141, 26153, 26161, 26171, 26177, 26183, 26189, 26203, 26209, 26227, 26237, 26249, 26251, 26261, 26263, 26267, 26293, 26297, 26309, 26317, 26321, 26339, 26347, 26357, 26371, 26387, 26393, 26399, 26407, 26417, 26423, 26431, 26437, 26449, 26459, 26479, 26489, 26497, 26501, 26513, 26539, 26557, 26561, 26573, 26591, 26597, 26627, 26633, 26641, 26647, 26669, 26681, 26683, 26687, 26693, 26699, 26701, 26711, 26713, 26717, 26723, 26729, 26731, 26737, 26759, 26777, 26783, 26801, 26813, 26821, 26833, 26839, 26849, 26861, 26863, 26879, 26881, 26891, 26893, 26903, 26921, 26927, 26947, 26951, 26953, 26959, 26981, 26987, 26993, 27011, 27017, 27031, 27043, 27059, 27061, 27067, 27073, 27077, 27091, 27103, 27107, 27109, 27127, 27143, 27179, 27191, 27197, 27211, 27239, 27241, 27253, 27259, 27271, 27277, 27281, 27283, 27299, 27329, 27337, 27361, 27367, 27397, 27407, 27409, 27427, 27431, 27437, 27449, 27457, 27479, 27481, 27487, 27509, 27527, 27529, 27539, 27541, 27551, 27581, 27583, 27611, 27617, 27631, 27647, 27653, 27673, 27689, 27691, 27697, 27701, 27733, 27737, 27739, 27743, 27749, 27751, 27763, 27767, 27773, 27779, 27791, 27793, 27799, 27803, 27809, 27817, 27823, 27827, 27847, 27851, 27883, 27893, 27901, 27917, 27919, 27941, 27943, 27947, 27953, 27961, 27967, 27983, 27997, 28001, 28019, 28027, 28031, 28051, 28057, 28069, 28081, 28087, 28097, 28099, 28109, 28111, 28123, 28151, 28163, 28181, 28183, 28201, 28211, 28219, 28229, 28277, 28279, 28283, 28289, 28297, 28307, 28309, 28319, 28349, 28351, 28387, 28393, 28403, 28409, 28411, 28429, 28433, 28439, 28447, 28463, 28477, 28493, 28499, 28513, 28517, 28537, 28541, 28547, 28549, 28559, 28571, 28573, 28579, 28591, 28597, 28603, 28607, 28619, 28621, 28627, 28631, 28643, 28649, 28657, 28661, 28663, 28669, 28687, 28697, 28703, 28711, 28723, 28729, 28751, 28753, 28759, 28771, 28789, 28793, 28807, 28813, 28817, 28837, 28843, 28859, 28867, 28871, 28879, 28901, 28909, 28921, 28927, 28933, 28949, 28961, 28979, 29009, 29017, 29021, 29023, 29027, 29033, 29059, 29063, 29077, 29101, 29123, 29129, 29131, 29137, 29147, 29153, 29167, 29173, 29179, 29191, 29201, 29207, 29209, 29221, 29231, 29243, 29251, 29269, 29287, 29297, 29303, 29311, 29327, 29333, 29339, 29347, 29363, 29383, 29387, 29389, 29399, 29401, 29411, 29423, 29429, 29437, 29443, 29453, 29473, 29483, 29501, 29527, 29531, 29537, 29567, 29569, 29573, 29581, 29587, 29599, 29611, 29629, 29633, 29641, 29663, 29669, 29671, 29683, 29717, 29723, 29741, 29753, 29759, 29761, 29789, 29803, 29819, 29833, 29837, 29851, 29863, 29867, 29873, 29879, 29881, 29917, 29921, 29927, 29947, 29959, 29983, 29989, 30011, 30013, 30029, 30047, 30059, 30071, 30089, 30091, 30097, 30103, 30109, 30113, 30119, 30133, 30137, 30139, 30161, 30169, 30181, 30187, 30197, 30203, 30211, 30223, 30241, 30253, 30259, 30269, 30271, 30293, 30307, 30313, 30319, 30323, 30341, 30347, 30367, 30389, 30391, 30403, 30427, 30431, 30449, 30467, 30469, 30491, 30493, 30497, 30509, 30517, 30529, 30539, 30553, 30557, 30559, 30577, 30593, 30631, 30637, 30643, 30649, 30661, 30671, 30677, 30689, 30697, 30703, 30707, 30713, 30727, 30757, 30763, 30773, 30781, 30803, 30809, 30817, 30829, 30839, 30841, 30851, 30853, 30859, 30869, 30871, 30881, 30893, 30911, 30931, 30937, 30941, 30949, 30971, 30977, 30983, 31013, 31019, 31033, 31039, 31051, 31063, 31069, 31079, 31081, 31091, 31121, 31123, 31139, 31147, 31151, 31153, 31159, 31177, 31181, 31183, 31189, 31193, 31219, 31223, 31231, 31237, 31247, 31249, 31253, 31259, 31267, 31271, 31277, 31307, 31319, 31321, 31327, 31333, 31337, 31357, 31379, 31387, 31391, 31393, 31397, 31469, 31477, 31481, 31489, 31511, 31513, 31517, 31531, 31541, 31543, 31547, 31567, 31573, 31583, 31601, 31607, 31627, 31643, 31649, 31657, 31663, 31667, 31687, 31699, 31721, 31723, 31727, 31729, 31741, 31751, 31769, 31771, 31793, 31799, 31817, 31847, 31849, 31859, 31873, 31883, 31891, 31907, 31957, 31963, 31973, 31981, 31991, 32003, 32009, 32027, 32029, 32051, 32057, 32059, 32063, 32069, 32077, 32083, 32089, 32099, 32117, 32119, 32141, 32143, 32159, 32173, 32183, 32189, 32191, 32203, 32213, 32233, 32237, 32251, 32257, 32261, 32297, 32299, 32303, 32309, 32321, 32323, 32327, 32341, 32353, 32359, 32363, 32369, 32371, 32377, 32381, 32401, 32411, 32413, 32423, 32429, 32441, 32443, 32467, 32479, 32491, 32497, 32503, 32507, 32531, 32533, 32537, 32561, 32563, 32569, 32573, 32579, 32587, 32603, 32609, 32611, 32621, 32633, 32647, 32653, 32687, 32693, 32707, 32713, 32717, 32719, 32749, 32771, 32779, 32783, 32789, 32797, 32801, 32803, 32831, 32833, 32839, 32843, 32869, 32887, 32909, 32911, 32917, 32933, 32939, 32941, 32957, 32969, 32971, 32983, 32987, 32993, 32999, 33013, 33023, 33029, 33037, 33049, 33053, 33071, 33073, 33083, 33091, 33107, 33113, 33119, 33149, 33151, 33161, 33179, 33181, 33191, 33199, 33203, 33211, 33223, 33247, 33287, 33289, 33301, 33311, 33317, 33329, 33331, 33343, 33347, 33349, 33353, 33359, 33377, 33391, 33403, 33409, 33413, 33427, 33457, 33461, 33469, 33479, 33487, 33493, 33503, 33521, 33529, 33533, 33547, 33563, 33569, 33577, 33581, 33587, 33589, 33599, 33601, 33613, 33617, 33619, 33623, 33629, 33637, 33641, 33647, 33679, 33703, 33713, 33721, 33739, 33749, 33751, 33757, 33767, 33769, 33773, 33791, 33797, 33809, 33811, 33827, 33829, 33851, 33857, 33863, 33871, 33889, 33893, 33911, 33923, 33931, 33937, 33941, 33961, 33967, 33997, 34019, 34031, 34033, 34039, 34057, 34061, 34123, 34127, 34129, 34141, 34147, 34157, 34159, 34171, 34183, 34211, 34213, 34217, 34231, 34253, 34259, 34261, 34267, 34273, 34283, 34297, 34301, 34303, 34313, 34319, 34327, 34337, 34351, 34361, 34367, 34369, 34381, 34403, 34421, 34429, 34439, 34457, 34469, 34471, 34483, 34487, 34499, 34501, 34511, 34513, 34519, 34537, 34543, 34549, 34583, 34589, 34591, 34603, 34607, 34613, 34631, 34649, 34651, 34667, 34673, 34679, 34687, 34693, 34703, 34721, 34729, 34739, 34747, 34757, 34759, 34763, 34781, 34807, 34819, 34841, 34843, 34847, 34849, 34871, 34877, 34883, 34897, 34913, 34919, 34939, 34949, 34961, 34963, 34981, 35023, 35027, 35051, 35053, 35059, 35069, 35081, 35083, 35089, 35099, 35107, 35111, 35117, 35129, 35141, 35149, 35153, 35159, 35171, 35201, 35221, 35227, 35251, 35257, 35267, 35279, 35281, 35291, 35311, 35317, 35323, 35327, 35339, 35353, 35363, 35381, 35393, 35401, 35407, 35419, 35423, 35437, 35447, 35449, 35461, 35491, 35507, 35509, 35521, 35527, 35531, 35533, 35537, 35543, 35569, 35573, 35591, 35593, 35597, 35603, 35617, 35671, 35677, 35729, 35731, 35747, 35753, 35759, 35771, 35797, 35801, 35803, 35809, 35831, 35837, 35839, 35851, 35863, 35869, 35879, 35897, 35899, 35911, 35923, 35933, 35951, 35963, 35969, 35977, 35983, 35993, 35999, 36007, 36011, 36013, 36017, 36037, 36061, 36067, 36073, 36083, 36097, 36107, 36109, 36131, 36137, 36151, 36161, 36187, 36191, 36209, 36217, 36229, 36241, 36251, 36263, 36269, 36277, 36293, 36299, 36307, 36313, 36319, 36341, 36343, 36353, 36373, 36383, 36389, 36433, 36451, 36457, 36467, 36469, 36473, 36479, 36493, 36497, 36523, 36527, 36529, 36541, 36551, 36559, 36563, 36571, 36583, 36587, 36599, 36607, 36629, 36637, 36643, 36653, 36671, 36677, 36683, 36691, 36697, 36709, 36713, 36721, 36739, 36749, 36761, 36767, 36779, 36781, 36787, 36791, 36793, 36809, 36821, 36833, 36847, 36857, 36871, 36877, 36887, 36899, 36901, 36913, 36919, 36923, 36929, 36931, 36943, 36947, 36973, 36979, 36997, 37003, 37013, 37019, 37021, 37039, 37049, 37057, 37061, 37087, 37097, 37117, 37123, 37139, 37159, 37171, 37181, 37189, 37199, 37201, 37217, 37223, 37243, 37253, 37273, 37277, 37307, 37309, 37313, 37321, 37337, 37339, 37357, 37361, 37363, 37369, 37379, 37397, 37409, 37423, 37441, 37447, 37463, 37483]

def myinput():
	while True:
		str = input()
		if len(str) == 0:
			continue
		return str
t = int(input())
for i in range(t):
	(p, q) = [int(x) for x in myinput().split()]
	if p % q != 0:
		print(p)
	else:
		n = p // q
		pr = 2
		bestdiv = p
		for pr in primes:
			if pr * pr > q or pr > bestdiv:
				break
			if q % pr == 0:
				while q % pr == 0:
					q //= pr
				d = pr
				while n % pr == 0 and d < bestdiv:
					n //= pr
					d *= pr
				if d < bestdiv:
					bestdiv = d
		if q != 1:
			d = q
			while n % q == 0:
				n //= q
				d *= q
			if d < bestdiv:
				bestdiv = d
		print(p // bestdiv)


import math

def solve(n):
	l.append(n)
	i = 2
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
		i = i + 1
t = int(input())
for er in range(t):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		l = []
		solve(q)
		ans = 1
		for val in l:
			cur = p
			while cur % val == 0:
				cur = cur // val
				if cur % q != 0:
					ans = max(ans, cur)
					break
		print(ans)


import sys
input = sys.stdin.buffer.readline

def solution():
	(p, q) = map(int, input().split())
	d = {}
	n = q
	if p % q != 0:
		print(p)
	else:
		while n % 2 == 0:
			d[2] = d.get(2, 0) + 1
			n = n // 2
		for i in range(3, int(n ** 0.5) + 1, 2):
			while n % i == 0:
				d[i] = d.get(i, 0) + 1
				n = n // i
		if n > 2:
			d[n] = d.get(n, 0) + 1
		m = -2e+18
		for i in d:
			x = p
			cnt = 0
			while x % i == 0:
				x //= i
				if x % q != 0:
					m = max(m, x)
		print(int(m))
t = int(input())
for _ in range(t):
	solution()


def count(n, p):
	r = 0
	while n % p == 0:
		n //= p
		r += 1
	return r

def candidate(p, q, d):
	ep = count(p, d)
	eq = count(q, d)
	assert ep >= eq
	return p // d ** (ep - eq + 1)

def solve(p, q):
	if p % q > 0:
		return p
	res = -1
	d = 2
	while d * d <= q:
		if q % d == 0:
			cur = candidate(p, q, d)
			if cur > res:
				res = cur
			while q % d == 0:
				q //= d
		d += 1
	if q > 1:
		cur = candidate(p, q, q)
		if cur > res:
			res = cur
	assert p % res == 0
	assert res != -1
	return res
tc = int(input())
for _ in range(tc):
	(p, q) = map(int, input().split())
	print(solve(p, q))


import math

def f(n):
	if n == 1:
		return 1
	semi_p = p
	while semi_p % q == 0:
		semi_p //= n
	return semi_p
t = int(input())
for i in range(t):
	(p, q) = map(int, input().split(' '))
	if p % q:
		print(p)
	else:
		maxi = 1
		for j in range(1, int(math.sqrt(q)) + 1):
			if q % j == 0:
				maxi = max(maxi, f(j), f(q // j))
		print(maxi)


import sys, os
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict
from functools import reduce
import math

def prime_factors(nr):
	i = 2
	factors = []
	while i <= nr:
		if i > math.sqrt(nr):
			i = nr
		if nr % i == 0:
			factors.append(int(i))
			nr = nr / i
		elif i == 2:
			i = 3
		else:
			i = i + 2
	return factors

def solve_(p, q):
	if p % q:
		return p
	q_fact = prime_factors(q)
	c_q_fact = Counter(q_fact)
	nr = p
	p_fact = []
	for (k, v) in c_q_fact.items():
		while nr % k == 0:
			p_fact.append(k)
			nr = nr // k
	if nr > 1:
		p_fact.append(nr)
	console(p_fact)
	console(q_fact)
	c_p_fact = Counter(p_fact)
	maxres = 1
	for (k1, v1) in c_q_fact.items():
		res = 1
		for (k2, v2) in c_p_fact.items():
			if k2 != k1:
				res *= k2 ** v2
			else:
				res *= k1 ** (v1 - 1)
		maxres = max(maxres, res)
	return maxres

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	pass
ONLINE_JUDGE = False
if os.path.exists('input.txt'):
	ONLINE_JUDGE = True
if ONLINE_JUDGE:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def console(*args):
		pass

def solve(*args):
	if not ONLINE_JUDGE:
		console('----- solving ------')
		console(*args)
		console('----- ------- ------')
	return solve_(*args)
if True:
	inp = iter(sys.stdin.buffer.readlines())
	input = lambda : next(inp)
else:
	input = sys.stdin.buffer.readline
for case_num in range(int(input())):
	(p, q) = list(map(int, input().split()))
	res = solve(p, q)
	print(res)


t = int(input())
for _ in range(t):
	(p, q) = list(map(int, input().split()))
	dels = []
	i = 2
	while i * i <= q:
		while q % i == 0:
			dels.append(i)
			q //= i
		i += 1
	if q > 1:
		dels.append(q)
	st = sorted(list(set(dels)))
	pws_p = dict()
	for val in st:
		pws_p[val] = 0
	pws_q = dict()
	for val in st:
		pws_q[val] = 0
	for val in dels:
		pws_q[val] += 1
	kek = p
	for val in st:
		pwr = 0
		while kek % val == 0:
			pwr += 1
			kek //= val
		pws_p[val] = pwr
	dlt = p
	for val in st:
		a = pws_p[val]
		b = pws_q[val]
		if b > a:
			dlt = 1
			continue
		rm = a - b + 1
		dlt = min(dlt, pow(val, rm))
	print(p // dlt)


t = int(input())

def abc(l, n, i, x):
	if i == n:
		return x
	return abc(l, n, i + 1, x) | abc(l, n, i + 1, x + l[i])
import random
for _ in range(t):
	(a, b) = map(int, input().split())
	fac = []
	if fac:
		print(a)
	else:
		ans = []
		for i in range(1, int(pow(b, 0.5)) + 1):
			if not b % i:
				fac.append(i)
				if i != b // i:
					fac.append(b // i)
		fac.sort()
		for i in fac:
			d = a
			while d % i == 0:
				d = d // i
				if d % b:
					ans.append(d)
					break
				if i == 1:
					break
			else:
				ans.append(a)
	print(max(ans))


import sys, math as mt
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(p, q) = I()
	if p % q:
		an = p
	else:
		an = 1
		ct = 0
		i = 2

		def f(a, b):
			an = 0
			while a % b == 0:
				a //= b
				an += 1
			return an
		while q % i == 0:
			q //= i
			ct += 1
		if ct:
			an = max(an, p // pow(i, f(p, i) - ct + 1))
		for i in range(3, int(q ** 0.5) + 1, 2):
			ct = 0
			while q % i == 0:
				q //= i
				ct += 1
			if ct:
				an = max(an, p // pow(i, f(p, i) - ct + 1))
		if q > 1:
			an = max(an, p // pow(q, f(p, q)))
	print(an)


import sys
import math
from collections import defaultdict, Counter
input = sys.stdin.readline

def print(x):
	sys.stdout.write(str(x) + '\n')

def find(n):
	d = defaultdict(int)
	while n % 2 == 0:
		n //= 2
		d[2] += 1
	for j in range(3, int(math.sqrt(n)) + 1, 2):
		while n % j == 0:
			n //= j
			d[j] += 1
	if n > 2:
		d[n] = 1
	return d
t = int(input())
for i in range(t):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		p1 = p
		d1 = find(q)
		ans = 10 ** 18
		for j in d1:
			c = 0
			while p % j == 0:
				p //= j
				c += 1
			cur = pow(j, c - (d1[j] - 1))
			ans = min(ans, cur)
		print(p1 // ans)


import math

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if p % q:
		print(p)
	else:
		f = getFactors(q)
		f.sort()
		ans = 1
		for i in f:
			if i != 1:
				cur = q // i
				rem = p // cur
				while rem % i == 0:
					rem //= i
				ans = max(ans, rem * cur)
		print(ans)


def quickPow(a, b):
	res = 1
	while b:
		if b & 1:
			res *= a
		b >>= 1
		a *= a
	return res
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		ans = 1
		i = 2
		m = pow(q, 0.5)
		while i <= m:
			if q % i == 0:
				cnt = 0
				while q % i == 0:
					q //= i
					cnt += 1
				reduced_p = p
				while reduced_p % i == 0:
					reduced_p //= i
				ans = max(ans, reduced_p * quickPow(i, cnt - 1))
			i += 1
		if q > 1:
			reduced_p = p
			while reduced_p % q == 0:
				reduced_p //= q
			ans = max(ans, reduced_p)
		print(ans)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	(p, q) = map(int, inputi().split())
	if p % q != 0:
		print(p)
		return
	qf = {}
	qr = q
	i = 2
	while i * i <= qr:
		while qr % i == 0:
			qf[i] = qf.get(i, 0) + 1
			qr //= i
		i += 1
	if qr != 1:
		qf[qr] = 1
	pf = {}
	pr = p
	for f in qf.keys():
		pf[f] = 0
		while pr % f == 0:
			pf[f] += 1
			pr //= f
	best = 0
	for f in qf.keys():
		best = max(p // f ** (pf[f] - qf[f] + 1), best)
	print(best)

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


import sys
readline = sys.stdin.readline
from collections import Counter

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrimeMR(n):
	d = n - 1
	d = d // (d & -d)
	L = [2]
	for a in L:
		t = d
		y = pow(a, t, n)
		if y == 1:
			continue
		while y != n - 1:
			y = y * y % n
			if y == 1 or t == n - 1:
				return 0
			t <<= 1
	return 1

def findFactorRho(n):
	m = 1 << n.bit_length() // 8
	for c in range(1, 99):
		f = lambda x: (x * x + c) % n
		(y, r, q, g) = (2, 1, 1, 1)
		while g == 1:
			x = y
			for i in range(r):
				y = f(y)
			k = 0
			while k < r and g == 1:
				ys = y
				for i in range(min(m, r - k)):
					y = f(y)
					q = q * abs(x - y) % n
				g = gcd(q, n)
				k += m
			r <<= 1
		if g == n:
			g = 1
			while g == 1:
				ys = f(ys)
				g = gcd(abs(x - ys), n)
		if g < n:
			if isPrimeMR(g):
				return g
			elif isPrimeMR(n // g):
				return n // g
			return findFactorRho(g)

def primeFactor(n):
	i = 2
	ret = {}
	rhoFlg = 0
	while i * i <= n:
		k = 0
		while n % i == 0:
			n //= i
			k += 1
		if k:
			ret[i] = k
		i += 1 + i % 2
		if i == 101 and n >= 2 ** 20:
			while n > 1:
				if isPrimeMR(n):
					(ret[n], n) = (1, 1)
				else:
					rhoFlg = 1
					j = findFactorRho(n)
					k = 0
					while n % j == 0:
						n //= j
						k += 1
					ret[j] = k
	if n > 1:
		ret[n] = 1
	if rhoFlg:
		ret = {x: ret[x] for x in sorted(ret)}
	return ret
T = int(readline())
Ans = [None] * T
for qu in range(T):
	(p, q) = map(int, readline().split())
	if p % q:
		Ans[qu] = p
	else:
		k = p // q
		cp = Counter(primeFactor(k))
		cq = Counter(primeFactor(q))
		for (k, v) in cq.items():
			cp[k] += v
		res = 1
		for (k, v) in cp.items():
			if cq[k] == 0:
				continue
			can = p // pow(k, cp[k] - cq[k] + 1)
			if can % q:
				res = max(res, can)
		Ans[qu] = res
print('\n'.join(map(str, Ans)))


from math import gcd

def facts(n):
	ans = []
	while n % 2 == 0:
		ans.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5 + 7), 2):
		while n % i == 0:
			ans.append(i)
			n = n // i
	if n > 2:
		ans.append(n)
	ans.sort()
	return ans
t = int(input())
for _ in range(t):
	(p, q) = [int(p) for p in input().split()]
	if p < q:
		print(p)
		continue
	elif p == q:
		ok = False
		if q % 2 == 0:
			print(p // 2)
			continue
		else:
			for i in range(3, int(q ** 0.5) + 1, 2):
				if q % i == 0:
					ok = True
					print(max(q // i, i))
					break
		if not ok:
			print(1)
	elif p % q != 0:
		print(p)
		continue
	elif p % q == 0:
		f1 = facts(q)
		t = p // q
		ans = 1
		for i in range(len(f1)):
			ele = p
			while ele % f1[i] == 0:
				ele = ele // f1[i]
				if ele % q:
					ans = max(ans, ele)
					break
		print(ans)


from sys import stdin, stdout
from collections import *
from math import gcd, floor, ceil
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
INF = float('inf')

def factors(n):
	l = []
	i = 2
	while i * i <= n:
		if n % i == 0:
			x = [i, 0]
			while n % i == 0:
				n //= i
				x[1] += 1
			l.append(x)
		i += 1
	if n > 1:
		l.append([n, 1])
	return l

def solve():
	(a, b) = mp()
	if a % b:
		pr(a)
		return
	fact = factors(b)
	mi = a
	for i in fact:
		p = a
		count = 0
		while p % i[0] == 0:
			count += 1
			p //= i[0]
		cur = 1
		while count >= i[1]:
			cur *= i[0]
			count -= 1
		mi = min(mi, cur)
	pr(a // mi)
for _ in range(inp()):
	solve()


import sys
input = sys.stdin.readline
from collections import *
import math

def prime(q, al):
	t = q
	for i in range(2, int(math.sqrt(t)) + 1):
		while q % i == 0:
			al[i] += 1
			q = q // i
	if q > 1:
		al[q] += 1
	q = t
	return

def power(p, x):
	t1 = p
	cnt = 0
	while p % x == 0:
		p = p // x
		cnt += 1
	p = t1
	return cnt
a = int(input())
for x in range(a):
	(p, q) = map(int, input().split())
	al = defaultdict(int)
	prime(q, al)
	new = defaultdict(int)
	for i in al:
		new[i] = al[i]
	for i in new:
		y = power(p, i)
		new[i] = y
	flag = 0
	for i in al:
		if al[i] > new[i]:
			flag = 1
			break
	if flag == 0:
		number = -1
		powers = -1
		val = math.inf
		for i in al:
			reduc = new[i] - (al[i] - 1)
			v1 = pow(i, reduc)
			if v1 < val:
				powers = al[i] - 1
				number = i
				val = v1
		new[number] = powers
	for i in new:
		alpha = power(p, i)
		p = p // pow(i, alpha - new[i])
	print(p)


def search_denominator(x):
	res = []
	for _ in range(2, int(x ** 0.5) + 1):
		k = 0
		while x % _ == 0:
			k += 1
			x //= _
		if k:
			res.append([_, k])
	if x > 1:
		res.append([x, 1])
	return res
t = int(input())
for w in range(t):
	(p, q) = list(map(int, input().split()))
	if p % q != 0 or q > p:
		print(p)
	else:
		variants = []
		for i in search_denominator(q):
			h = p
			qw = i[0] ** i[1]
			while h % qw == 0:
				h //= i[0]
			variants.append(h)
		print(max(variants))


from sys import stdin, stdout
from math import sqrt

def II():
	return int(stdin.readline())

def MI():
	return map(int, stdin.readline().split())
bigp = 10 ** 18 + 7
primes = []

def SieveOfEratosthenes(n, primes):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n):
		if prime[p]:
			primes.append(p)

def solve():
	(p, q) = MI()
	if p % q != 0:
		ans = p
	else:
		(x, y) = (q, p)
		mind = bigp
		sqrtq = int(sqrt(q))
		sp = [i for i in primes if i <= sqrtq] + [bigp]
		for i in sp:
			j = i
			if x == 1:
				break
			qe = 0
			while x % j == 0:
				qe += 1
				x = x // j
			if i == bigp:
				(qe, j) = (1, x)
			if qe > 0:
				pe = qe
				y = y // pow(j, qe)
				while y % j == 0:
					pe += 1
					y = y // j
				mind = min(mind, pow(j, pe - qe + 1))
		ans = p // mind
	stdout.write(str(ans) + '\n')
SieveOfEratosthenes(32000, primes)
t = II()
for _ in range(t):
	solve()


import math

def divisors(n):
	i = 2
	l = []
	l1 = []
	while i <= math.floor(math.sqrt(n)):
		x = 0
		if n % i == 0:
			l.append(i)
			x += 1
			n = n // i
			while n % i == 0:
				x += 1
				n = n // i
			l1.append(x)
		i += 1
	if n > 1:
		l.append(n)
		l1.append(1)
	return (l, l1)
for w in range(int(input())):
	(p, q) = tuple(map(int, input().split()))
	if q > p:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		n = p // q
		(l, l1) = divisors(q)
		ans = -1
		for i in range(len(l)):
			x = 0
			while n % l[i] == 0:
				x += 1
				n = n // l[i]
			if ans == -1:
				ans = pow(l[i], 1 + x)
			else:
				ans = min(ans, pow(l[i], x + 1))
		print(p // ans)


import sys
import math
from bisect import bisect_right as br
from statistics import mode
from itertools import combinations as cb

def int_arr():
	return list(map(int, sys.stdin.readline().strip().split()))

def str_arr():
	return list(map(str, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	(p, q) = int_arr()
	if p % q != 0:
		print(p)
		continue
	fact_q = set()
	(temp_p, temp_q) = (p, q)
	while temp_q > 1:
		for i in range(2, int(temp_q ** 0.5) + 1):
			if temp_q % i == 0:
				temp_q //= i
				fact_q.add(i)
				break
		else:
			fact_q.add(temp_q)
			break
	ans = 1
	for i in fact_q:
		sq = i
		while p % sq == 0:
			if p // sq % q != 0:
				ans = max(ans, p // sq)
				break
			sq *= i
	print(ans)


import math

def factorize(n):
	count = 0
	res = []
	while (n % 2 > 0) == False:
		n >>= 1
		count += 1
	if count > 0:
		res.append([2, count])
	for i in range(3, int(math.sqrt(n)) + 1):
		count = 0
		while n % i == 0:
			count += 1
			n = int(n / i)
		if count > 0:
			res.append([i, count])
		i += 2
	if n > 2:
		res.append([n, 1])
	return res
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if q > p:
		print(p)
	elif p % q != 0:
		print(p)
	else:
		r = p // q
		req1 = factorize(q)
		req1.sort()
		res = 1
		for i in range(len(req1)):
			cnt = 0
			j = req1[i][0]
			while r % j ** (cnt + 1) == 0:
				cnt += 1
			if cnt:
				if req1[i][1] > cnt:
					res = max(res, r * (q // req1[i][0] ** req1[i][1]))
					res = max(res, p // req1[i][0] ** (cnt + 1))
				else:
					res = max(res, r * q // req1[i][0] ** (cnt + 1))
					if len(req1) > 1:
						res = max(res, req1[i][0] ** (cnt + req1[i][1]))
			else:
				res = max(res, q // req1[i][0] * r)
		print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from collections import Counter

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def distinct_factors(n):
	factors = [1]
	for (p, exp) in prime_factors(n).items():
		factors += [p ** i * factor for factor in factors for i in range(1, exp + 1)]
	return factors

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small
for _ in range(int(input()) if True else 1):
	(p, q) = map(int, input().split())
	if p % q != 0 or p < q:
		print(p)
		continue
	ans = 0
	pq = prime_factors(q)
	for factor in pq:
		pc = p
		cnt = 0
		while pc % factor == 0:
			pc = pc // factor
			cnt += 1
		cnt2 = pq[factor]
		todiv = cnt - (cnt2 - 1)
		x = p // factor ** todiv
		if p % x == 0 and x % q != 0:
			ans = max(ans, x)
	print(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

def YES():
	print('YES')

def NO():
	print('NO')

def Yes():
	print('Yes')

def No():
	print('No')
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24
import random

def primesbelow(N):
	correction = N % 6 > 1
	N = {0: N, 1: N - 1, 2: N + 4, 3: N + 3, 4: N + 2, 5: N + 1}[N % 6]
	sieve = [True] * (N // 3)
	sieve[0] = False
	for i in range(int(N ** 0.5) // 3 + 1):
		if sieve[i]:
			k = 3 * i + 1 | 1
			sieve[k * k // 3::2 * k] = [False] * ((N // 6 - k * k // 6 - 1) // k + 1)
			sieve[(k * k + 4 * k - 2 * k * (i % 2)) // 3::2 * k] = [False] * ((N // 6 - (k * k + 4 * k - 2 * k * (i % 2)) // 6 - 1) // k + 1)
	return [2, 3] + [3 * i + 1 | 1 for i in range(1, N // 3 - correction) if sieve[i]]
smallprimeset = set(primesbelow(100000))
_smallprimeset = 100000

def isprime(n, precision=7):
	if n < 1:
		raise ValueError('Out of bounds, first argument must be > 0')
	elif n <= 3:
		return n >= 2
	elif n % 2 == 0:
		return False
	elif n < _smallprimeset:
		return n in smallprimeset
	d = n - 1
	s = 0
	while d % 2 == 0:
		d //= 2
		s += 1
	for repeat in range(precision):
		a = random.randrange(2, n - 2)
		x = pow(a, d, n)
		if x == 1 or x == n - 1:
			continue
		for r in range(s - 1):
			x = pow(x, 2, n)
			if x == 1:
				return False
			if x == n - 1:
				break
		else:
			return False
	return True

def pollard_brent(n):
	if n % 2 == 0:
		return 2
	if n % 3 == 0:
		return 3
	(y, c, m) = (random.randint(1, n - 1), random.randint(1, n - 1), random.randint(1, n - 1))
	(g, r, q) = (1, 1, 1)
	while g == 1:
		x = y
		for i in range(r):
			y = (pow(y, 2, n) + c) % n
		k = 0
		while k < r and g == 1:
			ys = y
			for i in range(min(m, r - k)):
				y = (pow(y, 2, n) + c) % n
				q = q * abs(x - y) % n
			g = gcd(q, n)
			k += m
		r *= 2
	if g == n:
		while True:
			ys = (pow(ys, 2, n) + c) % n
			g = gcd(abs(x - ys), n)
			if g > 1:
				break
	return g
smallprimes = primesbelow(1000)

def primefactors(n, sort=False):
	factors = []
	for checker in smallprimes:
		while n % checker == 0:
			factors.append(checker)
			n //= checker
		if checker > n:
			break
	if n < 2:
		return factors
	while n > 1:
		if isprime(n):
			factors.append(n)
			break
		factor = pollard_brent(n)
		factors.extend(primefactors(factor))
		n //= factor
	if sort:
		factors.sort()
	return factors

def factorization(n):
	factors = defaultdict(int)
	for p1 in primefactors(n):
		factors[p1] += 1
	return factors

def solve():
	(p, q) = sep()
	if p % q != 0:
		print(p)
		return
	else:
		fq = factorization(q)
		m = p
		for i in fq.keys():
			pt = 0
			temp = p
			while temp % i == 0:
				temp //= i
				pt += 1
			qt = fq[i]
			rem = i ** (pt - (qt - 1))
			m = min(rem, m)
		print(p // m)
testcase(int(inp()))


from math import gcd

def factors(n):
	i = 2
	blanck = []
	while i ** 2 <= n:
		if n % i == 0:
			blanck.append(i)
			while n % i == 0:
				n //= i
		else:
			i += 1
	if n > 1:
		blanck.append(n)
		n = 1
	return blanck

def finder(p, q):
	x = factors(q)
	temp = []
	for i in x:
		k = p
		while k % q == 0:
			k //= i
		temp.append(k)
	return max(temp)
t = int(input())
for i in range(t):
	(a, b) = map(int, input().strip().split())
	print(finder(a, b))


from collections import defaultdict
t = int(input())
for _ in range(t):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
		continue
	mp = defaultdict(int)
	ind = 2
	while ind * ind <= q:
		while q % ind == 0:
			mp[ind] += 1
			q //= ind
		ind += 1
	if q > 1:
		mp[q] += 1
	ans = -1
	for x in mp:
		temp = p
		while temp % x ** mp[x] == 0:
			temp //= x
		ans = max(ans, temp)
	print(ans)


INF = 10 ** 20

def f(n):
	d = 2
	a = {}
	counter = {}
	x = n
	while d * d <= n:
		while x % d == 0:
			if d in a:
				a[d] += 1
				counter[d] *= d
			else:
				a[d] = 1
				counter[d] = d
			x //= d
		d += 1
	if x != 1:
		a[x] = 1
		counter[x] = x
	return (a, counter)
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if a % b != 0:
		print(a)
	else:
		(l, count) = f(b)
		best = 2
		result = INF
		for key in l:
			num = 1
			while a % num == 0:
				num *= key
			k = num // count[key]
			if k < result:
				result = k
				best = key
		print(a // result)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
	return map(int, sys.stdin.readline().split())

def RLL():
	return list(map(int, sys.stdin.readline().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
from collections import defaultdict as dc

def getPrimes(n):
	if n < 2:
		return []
	else:
		output = [1] * n
		(output[0], output[1]) = (0, 0)
		for i in range(2, int(n ** 0.5) + 1):
			if output[i] == 1:
				output[i * i:n:i] = [0] * len(output[i * i:n:i])
	return [i for i in range(n) if output[i] == 1]
primes = getPrimes(33000)
pset = set(primes)
for _ in range(N()):
	(p, q) = RL()
	if p % q > 0:
		print(p)
	else:
		pp = p
		dic = dc(int)
		for t in primes:
			while q % t == 0:
				dic[t] += 1
				q //= t
			if q == 1:
				break
		if q > 1:
			dic[q] += 1
		dic2 = dc(int)
		for t in dic:
			while p % t == 0:
				dic2[t] += 1
				p //= t
		res = float('inf')
		for t in dic:
			res = min(res, t ** (dic2[t] - dic[t] + 1))
		print(pp // res)


def count(n, p):
	r = 0
	while n % p == 0:
		n //= p
		r += 1
	return r

def candidate(p, q, d):
	ep = count(p, d)
	eq = count(q, d)
	assert ep >= eq
	return p // d ** (ep - eq + 1)

def factorize(n):
	p = 2
	while p * p <= n:
		if n % p == 0:
			c = 0
			while n % p == 0:
				c += 1
				n //= p
			yield (p, c)
		p += 1
	if n > 1:
		yield (n, 1)

def solve(p, q):
	if p % q > 0:
		return p
	res = max((candidate(p, q, d) for (d, e) in factorize(q)))
	assert p % res == 0
	assert res % q > 0
	return res
tc = int(input())
for _ in range(tc):
	(p, q) = map(int, input().split())
	print(solve(p, q))


from collections import Counter
for _ in range(int(input())):
	(p, q) = map(int, input().split())
	if p % q != 0:
		print(p)
	else:
		f = Counter()
		(i, ans) = (2, 0)
		while i * i <= q:
			if q % i:
				i += 1
			else:
				q = q // i
				f[i] += 1
		if q > 1:
			f[q] += 1
		for i in f:
			(x, c) = (p, 0)
			while x % i == 0:
				x = x // i
				c += 1
			ans = max(ans, p // pow(i, c - f[i] + 1))
		print(ans)