#include <bits/stdc++.h>
const double pi = acos(-1.0);
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long ans = 0;
    int a, b;
    cin >> a >> b;
    while (a != 0 && b != 0) {
      if (a >= b) {
        ans += a / b;
        a %= b;
      } else {
        ans += b / a;
        b %= a;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
