#include /** * @title Sieve of Eratosthenes * @docs eratosthenes_sieve.md */ template struct EratosthenesSieve{ static std::bitset is_prime; static void init(){ is_prime.flip(); is_prime[0] = is_prime[1] = false; for(int i = 2; i <= MAX; ++i){ if(is_prime[i]){ for(int j = 2*i; j <= MAX; j += i){ is_prime[j] = false; } } } } }; template std::bitset EratosthenesSieve::is_prime; using E = EratosthenesSieve<5000000>; int main(){ E::init(); std::cin.tie(0); std::ios::sync_with_stdio(false); int T; std::cin >> T; while(T--){ int64_t A, P; std::cin >> A >> P; if(E::is_prime[P]){ if(A % P == 0){ std::cout << 0 << "\n"; }else{ std::cout << 1 << "\n"; } }else{ std::cout << -1 << "\n"; } } return 0; }