#include using namespace std; using ll = int; constexpr char newl = '\n'; ll solve(ll x, ll y) { // x = ab+c, y = ac+b // x + y = (a + 1)(b + c) ll sum = x + y; ll res = 0; for (ll i = 2; i * i <= sum; i++) { if (sum % i != 0) continue; ll j = sum / i; // a + 1 == i, b + c == j // (i - 1) c + (j - c) == y // (i - 2) c + j == y // c == (y - j) / (i - 2) if (i == 2 && x == y) { res += j - 1; } else if (i > 2 && y > j && (y - j) % (i - 2) == 0) { ll a = i - 1; ll c = (y - j) / (i - 2); ll b = j - c; res += (a > 0 && b > 0 && c > 0); } if (i == j) continue; // b + c == i, a + 1 == j // c == (y - i) / (j - 2) if (j == 2 && x == y) { res += i - 1; } else if (j > 2 && y > i && (y - i) % (j - 2) == 0) { ll a = j - 1; ll c = (y - i) / (j - 2); ll b = i - c; res += (a > 0 && b > 0 && c > 0); } } return res; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { ll x, y; cin >> x >> y; cout << solve(x, y) << newl; } return 0; }