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 ≤ t ≤ 50) — the number of pairs.

Each of the following t lines contains two integers p_i and q_i (1 ≤ p_i ≤ 10^{18}; 2 ≤ q_i ≤ 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.
import java.io.*;
import java.util.*;
import java.lang.Math;

public class Main{
	static BufferedReader reader;
	static StringTokenizer tokenizer;
	static PrintWriter writer;

	static int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	static String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static String nexts() throws IOException {
	  	  tokenizer = new StringTokenizer(reader.readLine());
	   	 String s="";
	   	 while (tokenizer.hasMoreTokens()) {
			s+=tokenizer.nextElement()+" ";
		}
		return s;
	}
	public static int gcd(int x, int y){
		if (y == 0) return x; else return gcd(y, x % y);
	}

	public static boolean isPrime(int nn) 
	    { 
	         if(nn<=1){
		 return false; }
	        if(nn<=3){
		 return true; }
	      if (nn%2==0||nn%3==0){
		 return false; }
	        for (int i=5;i*i<=nn;i=i+6){ 
	            if(nn%i==0||nn%(i+2)==0){
	            	return false;  }
	            }
	         return true; 
	    } 

	public static void shuffle(int[] A) {
		for (int i = 0; i < A.length; i++) {
			int j = (int)(i * Math.random());
			int tmp = A[i];
			A[i] = A[j];
			A[j] = tmp;
		}
	}
	public static void shufflel(Long[] A) {
		for (int i = 0; i < A.length; i++) {
			int j = (int)(i * Math.random());
			long tmp = A[i];
			A[i] = A[j];
			A[j] = tmp;
		}
	}

	public static String reverse(String input)
 	   {
	        StringBuilder input2=new StringBuilder();
	        input2.append(input);
	        input2 = input2.reverse();
	        return input2.toString();
	    }


	 public static long power(int x, long n)
	{
	//    long mod = 1000000007;
		if (n == 0) {
			return 1;
		}

		long pow = power(x, n / 2);

		if ((n & 1) == 1) {
			return (x * pow * pow);
		}
		return (pow * pow);
	}
	public static long power(long x, long y, long p)     //x^y%p
	    { 
  	       long res = 1; 
	       x = x % p; 
  	       while (y > 0) { 
 	 	 if (y % 2 == 1) {
      	        	        res = (res * x) % p; 
		 }
  		 y = y >> 1; // y = y/2 
     	     	 x = (x * x) % p; 
     	         } 
  	         return res; 
    	} 

	 static long ncr(int n, int k)  
   	 { 
           		 long res = 1; 
  		   // Since C(n, k) = C(n, n-k)  
 		       if (k > n - k) { 
     		       k = n - k; 
      		  } 
  		  // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]  
     		   for (int i = 0; i < k; ++i) { 
        		    res *= (n - i); 
         		   res /= (i + 1); 
     		   } 
  		  return res; 
 	   }
	public static int inv(int a, int m)     //Returns modulo inverse of a with respect to m using extended euclid algorithm
	    { 
	        int m0=m,t,q; 
	        int x0=0,x1=1; 
      	        if(m==1){
	                return 0; }
                 	         while(a>1) 
     	        {  
   	                q=a/m; 
    	                t=m; 
                                 m=a%m;a=t; 
    	                t=x0; 
     	                x0=x1-q*x0; 
      	                x1=t; } 
    	          if(x1<0){
 	   	 x1+=m0; }
   	           return x1; 
	    } 

	static int modInverse(int n, int p) 
  	{ 
    	          return (int)power((long)n, (long)(p - 2),(long)p); 
	 } 
	static int ncrmod(int n, int r, int p) 
   	 { 
	        if (r == 0)        // Base case 
	            return 1; 
  	        int[] fac = new int[n + 1];         // Fill factorial array so that we can find all factorial of r, n and n-r 
	        fac[0] = 1; 
	        for (int i = 1; i <= n; i++) 
                                     fac[i] = fac[i - 1] * i % p; 
   	         return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;     //IMPORTANT
    	}	 

	static int lower(int a[], int x)   //Returns rightest index of x in sorted arr else -1
  	  {                                                //If not present returns index of just smaller element
  	            int n=a.length;
    	        int l=0;
                int r=n-1;
                int ans=-1;
                while(l<=r){
                    int m=(r-l)/2+l;
                    if(a[m]<=x){
                        ans=m;
                        l=m+1;
                    }
                    else{
                        r=m-1;
                    }
                }
                 return ans;  
                } 
	static int upper(int a[], int x)     //Returns leftest index of x in sorted arr else n
 	   {                                                  //If not present returns index of just greater element
 	            int n=a.length;
   	            int l=0;
                int r=n-1;
                int ans=n;
                while(l<=r){
                    int m=(r-l)/2+l;
                    if(a[m]>=x){
                        ans=m;
                        r=m-1;
                    }
                    else{
                         l=m+1;
                    }
                }
                 return ans; 
                 } 

	public static long stringtoint(String s){ // Convert bit string to number
		long a = 1;
		long ans = 0;
		StringBuilder sb = new StringBuilder();
		sb.append(s);  
		sb.reverse();
		s=sb.toString();
		for(int i=0;i<s.length();i++){
			ans = ans + a*(s.charAt(i)-'0');
			a = a*2;
		}
		return ans;
	}
 
	String inttostring(long a,long m){        // a is number and m is length of string required
		String res = "";                     // Convert a number in bit string representation
		for(int i=0;i<(int)m;i++){
			if((a&(1<<i))==1){
				res+='1';
			}else
				res+='0';
		}
		StringBuilder sb = new StringBuilder();
		sb.append(res);  
		sb.reverse();
		res=sb.toString();
		return res;
    }
	
	 static class R implements Comparable<R>{
	        int x, y;
	        public  R(int x, int y) {           //a[i]=new R(x,y);
	            this.x = x;
	            this.y = y;
	        }
	        public int compareTo(R o) {
            		return x-o.x;     //Increasing order(Which is usually required)
        		}
	    }   

	//double d=Math.sqrt(Math.pow(Math.abs(x2-x1),2)+Math.pow(Math.abs(y2-y1),2));

	public static void main(String[] args) throws IOException {
		reader = new BufferedReader(new InputStreamReader(System.in));
		tokenizer = null;
		writer = new PrintWriter(System.out);
		solve();
		reader.close();
		writer.close();
	}
	
   
	private static void solve() throws IOException {
	    // sieve1(); 
		int t = nextInt();
		while(t-->0){
			//String s= nextToken();
			//String[] s = str.split("\\s+");
			//ArrayList<Integer> arp=new ArrayList<Integer>();
		//	ArrayList<Integer> arq=new ArrayList<Integer>();
			//HashSet<Long> set=new HashSet<Long>();
			//HashSet<Long> set1=new HashSet<Long>();
		//	HashMap<Long,Integer> set=new HashMap<Long,Integer>();
			HashMap<Long,Integer> set1=new HashMap<Long,Integer>();
			//R[] a1=new R[n];
			//char[] c=nextToken().toCharArray();

			
			long p = nextLong();
			long q = nextLong();
            if(p%q!=0){
                writer.println(p);
                continue;
            }
		long qq=q;
   //set1.clear();
    while (q%2==0) 
        { 
            
             set1.put(2L,set1.getOrDefault(2L,0)+1);
            q /= 2; 
            // writer.println("20");
        } 
    for (Long i = 3L; i <= Math.sqrt(q); i+= 2L) 
        { 
           while (q%i == 0) 
            { 
                
                set1.put(i,set1.getOrDefault(i,0)+1);
                q /= i; 
                // writer.println(i+"q");
            } 
        } 
   if (q > 2) {
      
       set1.put(q,1);
   }       
   long ans=1L;
 /*  for (Map.Entry<Long, Integer> e : set.entrySet()) {
       writer.println(e.getKey()+" "+e.getValue());
   }*/
 /*  for (Map.Entry<Long, Integer> e1 : set1.entrySet()) {
       writer.println(e1.getKey()+" "+e1.getValue());
   }*/
   for (Map.Entry<Long, Integer> e : set1.entrySet()) {
       
       long ans1=1L;
       long p1=p;
       int c=0;
       while(p1%e.getKey()==0){
           p1/=e.getKey();
           c++;
       }
       
               ans=Math.max(ans,p1*(long)Math.pow(e.getKey(),e.getValue()-1));
       
       //12 - 2,2,3
       //6 - 2,3
   }
   
            writer.println(ans);
         }
	}
}