#include <bits/stdc++.h>
const int inf = (1ll << 31) - 1;
using namespace std;
int get(int a, int b) {
  if (a == 0) return 0;
  return get(b % a, a) + b / a;
}
void solve() {
  int a, b;
  cin >> a >> b;
  if (a > b) swap(a, b);
  cout << get(a, b) << endl;
}
int main() {
  int n;
  cin >> n;
  while (n--) solve();
  return 0;
}
