#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; bool b[5000010] = { false }; int main() { for (int i = 2; i <= 5000000; i++) { if (!b[i]) { for (int j = i + i; j <= 5000000; j += i) { b[j] = true; } } } int t; cin >> t; long long a, p; for (int i = 0; i < t; i++) { cin >> a >> p; if (b[p]) { cout << "-1" << endl; } else { if (a % p != 0 && p % a != 0) { cout << "1" << endl; } else { cout << "0" << endl; } } } return 0; }