#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double EPS = 1e-9;
const int INF = 1 << 29;
int rec(int a, int b) {
  if (a > b) swap(a, b);
  if (a == 0) return 0;
  return b / a + rec(a, b % a);
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    int a, b;
    cin >> a >> b;
    int ans = rec(a, b);
    cout << ans;
    putchar('\n');
  }
  return 0;
}
