#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int tof = 1e8;
int E[N * N], val[N], dis[N][N], P[8] = {19, 17, 13, 11, 7, 5, 3, 2}, tot, ttl;
vector<int> vec, Vec[N], tmp;
map<vector<int>, int> id;
map<int, int> Id;
void bt(int x = 1, int lst = 0, int n = 0) {
  if (n == 8) {
    val[tot] = 1;
    for (int y : vec) val[tot] *= y;
    if (Id.find(val[tot]) == Id.end()) Id[val[tot]] = ttl++;
    Vec[tot] = vec;
    id[vec] = tot++;
    return;
  }
  for (int i = 0; i < lst; i++) {
    if (x > tof / P[n]) return;
    x *= P[n];
  }
  while (true) {
    vec.push_back(lst + 1);
    bt(x, lst, n + 1);
    vec.pop_back();
    if (x > tof / P[n]) return;
    x *= P[n];
    lst++;
  }
}
void BFS(int s) {
  queue<int> q;
  for (int i = 0; i < tot; i++) {
    if (Id[val[i]] == s) {
      dis[s][i] = 0;
      q.push(i);
    }
  }
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < 8; i++) {
      tmp = Vec[v];
      tmp[i]--;
      sort(tmp.begin(), tmp.end());
      if (id.find(tmp) != id.end()) {
        int u = id[tmp];
        if (dis[s][v] + 1 < dis[s][u]) {
          dis[s][u] = dis[s][v] + 1;
          q.push(u);
        }
      }
      tmp = Vec[v];
      tmp[i]++;
      sort(tmp.begin(), tmp.end());
      if (id.find(tmp) == id.end()) continue;
      int u = id[tmp];
      if (dis[s][v] + 1 < dis[s][u]) {
        dis[s][u] = dis[s][v] + 1;
        q.push(u);
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  E[1] = 1;
  for (int i = 2; i < N; i++) {
    if (!E[i]) {
      E[i] = i;
      for (int j = i * i; j < N * N; j += i) E[j] = i;
    }
  }
  for (int i = 1; i < N * N; i++)
    if (!E[i]) E[i] = i;
  bt();
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) dis[i][j] = N;
  for (int i = 0; i < ttl; i++) BFS(i);
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    vector<int> vx, vy;
    while (1 < x) {
      int p = E[x], t = 0;
      while (x % p == 0) x /= p, t++;
      vx.push_back(t + 1);
    }
    while ((int)(vx.size()) < 8) vx.push_back(1);
    while (1 < y) {
      int p = E[y], t = 0;
      while (y % p == 0) y /= p, t++;
      vy.push_back(t + 1);
    }
    while ((int)(vy.size()) < 8) vy.push_back(1);
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    int i1 = id[vx], i2 = id[vy], ans = N;
    for (int i = 0; i < ttl; i++) ans = min(ans, dis[i][i1] + dis[i][i2]);
    cout << ans << '\n';
  }
  return 0;
}
