public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  int t = sc . nextInt ( ) ;
  while ( t -- > 0 ) {
    pi = sc . nextLong ( ) ;
    qi = sc . nextLong ( ) ;
    long tmpp = pi ;
    long tmpq = qi ;
    long ans = 0 ;
    for ( int i = 2 ;
    i <= ( long ) ( Math . sqrt ( tmpq ) ) ;
    i ++ ) {
      tmpp = pi ;
      while ( tmpq % i == 0 ) {
        if ( tmpp % qi != 0 ) {
          ans = Math . max ( ans , tmpp ) ;
        }
        else if ( tmpp % i != 0 ) {
        }
        else {
          tmpp /= i ;
          continue ;
        }
        tmpq /= i ;
      }
    }
    if ( tmpq >= 2 ) {
      long x = pi ;
      while ( true ) {
        if ( x % qi != 0 ) {
          ans = Math . max ( ans , x ) ;
          break ;
        }
        if ( x % tmpq != 0 ) {
          break ;
        }
        else x /= tmpq ;
      }
    }
    System . out . println ( ans ) ;
  }
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int t = sc . nextInt ( ) ; while ( t -- > 0 ) { pi = sc . nextLong ( ) ; qi = sc . nextLong ( ) ; long tmpp = pi ; long tmpq = qi ; long ans = 0 ; for ( int i = 2 ; i <= ( long ) ( Math . sqrt ( tmpq ) ) ; i ++ ) { tmpp = pi ; while ( tmpq % i == 0 ) { if ( tmpp % qi != 0 ) { ans = Math . max ( ans , tmpp ) ; } else if ( tmpp % i != 0 ) { } else { tmpp /= i ; continue ; } tmpq /= i ; } } if ( tmpq >= 2 ) { long x = pi ; while ( true ) { if ( x % qi != 0 ) { ans = Math . max ( ans , x ) ; break ; } if ( x % tmpq != 0 ) { break ; } else x /= tmpq ; } } System . out . println ( ans ) ; } }
(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: (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 (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (cast_expression type: (integral_type) value: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))))) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (identifier) right: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier)))))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) consequence: (block) alternative: (block (expression_statement (assignment_expression left: (identifier) right: (identifier))) (continue_statement)))) (expression_statement (assignment_expression left: (identifier) right: (identifier))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (true)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))) (break_statement))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) consequence: (block (break_statement)) alternative: (expression_statement (assignment_expression left: (identifier) right: (identifier)))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))))