#include #include #include #include #include #define LL long long using namespace std; const int N = 1; int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d%d", &n, &m); int ans = 0; while (1) { if (n == 1) { ans += m; break; } if (m == 1) { ans += n; break; } ++ans; if (n == m) break; else if (n < m) m -= n; else n -= m; } printf("%d\n", ans); } return 0; }