#include <bits/stdc++.h>
using namespace std;
int calc(int a, int b) {
  if (b == 0) return 0;
  return a / b + calc(b, a % b);
}
int main() {
  int n, a, b;
  cin >> n;
  while (n--) {
    cin >> a >> b;
    cout << calc(a, b) << endl;
  }
  return 0;
}
