#include <bits/stdc++.h>
using namespace std;
int findMax(set<int> my_set) {
  int max_element;
  if (!my_set.empty()) max_element = *(my_set.rbegin());
  return max_element;
}
int count(int k) {
  int cnt = 0;
  for (int i(2); i * i <= k; i++) {
    if (k % i == 0) {
      cnt++;
      while (k % i == 0) {
        k /= i;
      }
    }
  }
  if (k > 1) cnt++;
  return pow(2, cnt);
}
int tull(int a, int b) {
  if (b >= a) {
    b = b % a;
    return (b / a);
  } else {
    a = a % b;
    return (a / b);
  }
}
void solve() {
  long long a, b;
  cin >> a >> b;
  long long sum = 0;
  while (a != 0 && b != 0) {
    if (b >= a) {
      sum += (b / a);
      b = b % a;
    } else {
      sum += (a / b);
      a = a % b;
    }
  }
  cout << sum << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
