#include <bits/stdc++.h>
using namespace std;
int n, x, y;
int gcd(int x, int y) {
  if (x == 0 || y == 0) return 0;
  if (x > y)
    return gcd(x % y, y) + x / y;
  else
    return gcd(y % x, x) + y / x;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y;
    cout << gcd(x, y) << endl;
  }
  return 0;
}
