#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair PII; typedef vector VI; #define fi first #define se second #define MP make_pair int read() { int v = 0, f = 1; char c = getchar(); while (c < 48 || 57 < c) {if (c == '-') f = -1; c = getchar();} while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar(); return v * f; } int f[1010][1010]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int g(int i, int j) { return gcd(i, j) == 1; } int main() { f[1][1] = 1; for (int i = 1; i <= 1000; i++) for (int j = 1; j <= 1000; j++) { if (i > 1) f[i][j] = max(f[i][j], f[i - 1][j] + g(i, j)); if (j > 1) f[i][j] = max(f[i][j], f[i][j - 1] + g(i, j)); } int cas = read(); while (cas--) { int a = read(), b = read(); printf("%d\n", f[a][b]); } }