#include using namespace std; typedef long long ll; typedef pair pii; #define sz(a) ((int)a.size()) #define pb push_back #define lson (rt << 1) #define rson (rt << 1 | 1) #define gmid (l + r >> 1) const int maxn = 1e3 + 5; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; int dp[maxn][maxn]; int n; int gcd(int a, int b){ return b ? gcd(b, a % b) : a; } int main(){ ios::sync_with_stdio(0); cin.tie(0); for(int i = 1; i <= 1000; ++i){ for(int j = 1; j <= 1000; ++j){ dp[i][j] = (gcd(i, j) == 1) + max(dp[i - 1][j], dp[i][j - 1]); } } int t; cin >> t; while(t--){ int a, b; cin >> a >> b; cout << dp[a][b] << "\n"; } return 0; }