public static void main ( String [ ] args ) {
  PrintWriter out = new PrintWriter ( System . out ) ;
  int t = 1 ;
  t = sc . nextInt ( ) ;
  while ( t -- > 0 ) {
    long p = sc . nextLong ( ) ;
    long q = sc . nextLong ( ) ;
    if ( p < q || p % q != 0 ) {
      out . write ( p + "\n" ) ;
      continue ;
    }
    long j = 2L ;
    ArrayList < Long > a = new ArrayList < > ( ) ;
    long ans = q ;
    while ( j * j <= q ) {
      if ( ans % j == 0 ) {
        a . add ( j ) ;
        while ( ans % j == 0 ) ans /= j ;
      }
      j ++ ;
    }
    if ( ans > 1 ) {
      a . add ( ans ) ;
      ans = 1 ;
    }
    for ( long l : a ) {
      long k = p ;
      while ( k % q == 0 ) k /= l ;
      ans = Math . max ( k , ans ) ;
    }
    out . write ( ans + "\n" ) ;
  }
  out . close ( ) ;
}

public static void main ( String [ ] args ) { PrintWriter out = new PrintWriter ( System . out ) ; int t = 1 ; t = sc . nextInt ( ) ; while ( t -- > 0 ) { long p = sc . nextLong ( ) ; long q = sc . nextLong ( ) ; if ( p < q || p % q != 0 ) { out . write ( p + " \n " ) ; continue ; } long j = 2L ; ArrayList < Long > a = new ArrayList < > ( ) ; long ans = q ; while ( j * j <= q ) { if ( ans % j == 0 ) { a . add ( j ) ; while ( ans % j == 0 ) ans /= j ; } j ++ ; } if ( ans > 1 ) { a . add ( ans ) ; ans = 1 ; } for ( long l : a ) { long k = p ; while ( k % q == 0 ) k /= l ; ans = Math . max ( k , ans ) ; } out . write ( ans + " \n " ) ; } out . close ( ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (while_statement condition: (parenthesized_expression (binary_expression left: (update_expression (identifier)) right: (decimal_integer_literal))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal)))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (string_literal))))) (continue_statement))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) body: (expression_statement (assignment_expression left: (identifier) right: (identifier)))))) (expression_statement (update_expression (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))))) (enhanced_for_statement type: (integral_type) name: (identifier) value: (identifier) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) body: (expression_statement (assignment_expression left: (identifier) right: (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (string_literal))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))