#include using namespace std; using ll = long long; bool isPrime(int n) { // Corner case if (n <= 1) return false; if (n == 2 || n == 3)return true; if (n % 2 == 0 || n % 3 == 0)return false; // Check from 2 to n-1 for (int i = 5; i * i <= n; i += 6) if (n % i == 0) return false; for (int i = 7; i * i <= n; i += 6) if (n % i == 0) return false; return true; } int main() { int T;cin >> T; while(T--){ ll A, P;cin >> A >> P; if(isPrime(P))cout << (!!(A%P)) << '\n'; else cout << -1 << '\n'; } }