#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t, i, j;
  cin >> t;
  while (t--) {
    long long a, b, sum = 0;
    cin >> a >> b;
    while (a > 0) {
      if (a < b) swap(a, b);
      sum += a / b;
      a %= b;
    }
    cout << sum << endl;
  }
}
