#include <bits/stdc++.h>
int main() {
  int n;
  unsigned int a, b, res;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    res = 0;
    while (a && b) {
      if (a > b) {
        res += a / b;
        a %= b;
      } else {
        res += b / a;
        b %= a;
      }
    }
    printf("%d\n", res);
  }
  return 0;
}
