/** * author: boutarou * created: 02.08.2020 12:43:46 **/ #include using namespace std; #define rep(i,n) for(int i = 0; i < int(n); i++) using ll = long long; using P = pair; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a;} int main() { int t; cin >> t; vector a(t); vector p(t); int max_p = 1; rep(i, t) cin >> a[i] >> p[i]; rep(i, t) max_p = max(max_p, p[i]); vector check(max_p + 1, false); check[1] = true; for (int i = 2; i <= max_p; i++) { if (check[i]) continue; for(int j = 2 * i; j <= max_p; j += i) { check[j] = true; } } rep(i, t) { if (!check[p[i]]) { if (a[i] == 1 && p[i] == 1) cout << 0 << endl; else if (a[i] == 1) cout << 1 << endl; else if (a[i] % (ll)p[i] == 0 || (ll)p[i] % a[i] == 0) cout << 0 << endl; else cout << 1 << endl; } else cout << -1 << endl; } return 0; }