//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} struct Sieve { int n; vector f, primes; Sieve(int n=1):n(n), f(n+1) { f[0] = f[1] = -1; for(int i=2; i<=n; ++i) { if(!f[i]) { primes.emplace_back(i); f[i] = i; } for(int p:primes) { int u = p*i; if(u > n or p > f[i]) break; f[u] = p; } } } //素数判定 bool isPrime(int x) {return f[x] == x;} //素数判定(1つ用)(宣言はsqrt(x)のサイズで) bool isPrime1(int64_t x) { bool m = true; for(int z:primes) { if(!(x%z)) { m = false; break; } } return m; } //素数列挙 vector factorList(int x) { assert(x <= n); vector res; while(x != 1) { res.emplace_back(f[x]); x /= f[x]; } return res; } //素因数分解 pair(素数, 素因数の数) vector > factor(int x) { vector fl = factorList(x); if(fl.size() == 0) return {}; vector > res(1, pair(fl[0], 0)); for(int p : fl) { if(res.back().first == p) { ++res.back().second; } else { res.emplace_back(p, 1); } } return res; } //素数列挙(1つ用)(宣言はsqrt(x)のサイズで) vector factorList1(int64_t x) { vector vec; for(int z:primes) { while(!(x%z)) { vec.emplace_back(z); x /= z; } } if(x != 1) vec.emplace_back(x); return vec; } //素数列挙(1つ用)(宣言はsqrt(x)のサイズで) pair(素数, 素因数の数) vector > factor1(int64_t x) { vector vec = factorList1(x); vector > vecc; for(vector::iterator itr = vec.begin(); itr != vec.end(); ++itr) { if(itr != vec.begin() && *itr == *(itr-1)) { (vecc.end()-1)->second++; } else { vecc.emplace_back(pair(*itr, 1)); } } return vecc; } //約数列挙 vector div1(int64_t x) { vector res(1, 1); vector > fac = factor1(x); for(int i = 0; i < fac.size(); ++i) { int si = res.size(); for(int j = 0; j < fac[i].second; ++j) { for(int k = 0; k < si; ++k) { res.emplace_back(res[k+j*si]*fac[i].first); } } } sort(res.begin(), res.end()); return res; } }pp(100000); //head int s; int x, y; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s; while(s--) { cin >> x >> y; if(x == y) { int ans = 0; if(~x&1) ans--; ans += x-1; auto v = pp.factor1(x); int u = 1; for(auto k:v) { u *= k.second+1; } ans += u-1; cout << ans << '\n'; } else { if(x > y) swap(x, y); auto v = pp.div1(y-x); int ans = 0; for(int u:v) { int b = y/(u+1); int c = y-(u+1)*b; if((u+1)*c+b == x) ans++; } cout << ans << '\n'; } } }