#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
long long qpow(long long x, long long k) {
  return k == 0 ? 1
                : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
const int N = 7;
const int maxst = 333;
int pfac[8] = {2, 3, 5, 7, 11, 13, 17};
int Tn;
int tot;
int st[maxst];
map<int, int> stid;
int getst(int x) {
  int seq[10];
  int sn = 0;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      int cnt = 0;
      while (x % i == 0) {
        x /= i;
        cnt++;
      }
      seq[sn++] = cnt;
    }
  }
  if (x > 1) seq[sn++] = 1;
  sort(seq, seq + sn);
  reverse(seq, seq + sn);
  x = 1;
  for (int i = 0; i < sn; i++) {
    for (int j = 0; j < seq[i]; j++) x = x * pfac[i];
  }
  return x;
}
int getfn(int x) {
  int seq[10];
  int sn = 0;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      int cnt = 0;
      while (x % i == 0) {
        x /= i;
        cnt++;
      }
      seq[sn++] = cnt;
    }
  }
  if (x > 1) seq[sn++] = 1;
  int ret = 1;
  for (int i = 0; i < sn; i++) ret *= seq[i] + 1;
  return ret;
}
const int cost_bound = 10;
const int nfn_bound = 2333;
int dis[maxst][nfn_bound];
map<int, int> mp;
void dfs(int i, int x, int mx, int cost, int fn, long long nfn) {
  if (i == N) {
    if (stid.find(x) == stid.end()) {
      stid[x] = tot++;
    }
    int id = stid[x];
    dis[id][nfn] = min(dis[id][nfn], cost);
    return;
  }
  for (int c = 0; c <= mx; c++) {
    if (x <= 1000000) {
      for (int j = 0; j <= c + 10; j++) {
        if (abs(j - c) + cost <= cost_bound) {
          if (nfn * (j + 1) <= nfn_bound) {
            dfs(i + 1, x, c, abs(j - c) + cost, fn * (c + 1), nfn * (j + 1));
          }
        }
      }
    } else
      break;
    x = x * pfac[i];
  }
}
int getid(int x) {
  if (stid.find(x) == stid.end())
    return tot;
  else
    return stid[x];
}
int g[maxst], tab[maxst][maxst];
map<int, int> fid;
int main() {
  memset(dis, 20, sizeof(dis));
  dfs(0, 1, 100, 0, 1, 1);
  if (0) cout << "tot=" << tot << endl;
  int q;
  q = getnum();
  while (q--) {
    int x, y;
    x = getnum(), y = getnum();
    x = getst(x);
    y = getst(y);
    x = getid(x);
    y = getid(y);
    assert(x < tot);
    assert(y < tot);
    int ans = 1e9;
    for (int i = 1; i < nfn_bound; i++) ans = min(ans, dis[x][i] + dis[y][i]);
    putnum(ans), putchar('\n');
  }
  return 0;
}
