#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, n, m, p;
  int cnt;
  cin >> t;
  while (t--) {
    cnt = 0;
    cin >> n >> m;
    while (n != 0 && m != 0) {
      if (n < m) swap(n, m);
      cnt += n / m;
      n %= m;
    }
    cout << cnt << endl;
  }
}
