#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline int chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2>
inline int chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);
const int MAXM = 500;
const int bound = (int)1e6;
const int MAGIC = 24;
int lp[MAXN];
vector<int> pr;
set<vector<int> > st;
void prepare() {
  for (int i = 2; i <= bound; i++) {
    if (lp[i] == 0) lp[i] = i, pr.push_back(i);
    for (int j = 0;
         j < (int)pr.size() && i * 1ll * pr[j] <= bound && pr[j] <= lp[i]; j++)
      lp[i * pr[j]] = pr[j];
  }
}
vector<vector<int> > li;
vector<int> get(int i) {
  int x = i;
  vector<int> curr;
  while (x != 1) {
    int d = lp[x], cnt = 0;
    while (x % d == 0) x /= d, cnt++;
    curr.push_back(cnt);
  }
  sort(curr.begin(), curr.end());
  return curr;
}
void read() {
  for (int i = 1; i <= bound; i++) {
    int x = i;
    vector<int> curr;
    while (x != 1) {
      int d = lp[x], cnt = 0;
      while (x % d == 0) x /= d, cnt++;
      curr.push_back(cnt);
    }
    sort(curr.begin(), curr.end());
    st.insert(curr);
  }
  for (auto it : st) li.push_back(it);
}
int N, K;
int dp[MAXM][1 << 16];
int get_id(const vector<vector<int> > &li, vector<int> p) {
  return lower_bound(li.begin(), li.end(), p) - li.begin();
}
vector<pair<int, int> > same;
vector<vector<int> > all_vec;
vector<int> curr_Vec;
void gen(int sum, int last = 1) {
  all_vec.push_back(curr_Vec);
  if (sum == 0) return;
  for (int curr = last; curr <= sum; curr++) {
    curr_Vec.push_back(curr);
    gen(sum - curr, curr);
    curr_Vec.pop_back();
  }
}
int dist[MAXN];
vector<int> adj[MAXN];
vector<int> possible_divs;
int divs(vector<int> &l) {
  int r = 1;
  for (int i : l) r *= (i + 1);
  return r;
}
int get_pos(int x) {
  return lower_bound(possible_divs.begin(), possible_divs.end(), x) -
         possible_divs.begin();
}
int D[MAXN];
void solve() {
  gen(MAGIC);
  N = li.size();
  K = all_vec.size();
  for (int i = 0; i < K; i++) {
    for (int p = 0; p < (int)all_vec[i].size(); p++) {
      {
        vector<int> nw = all_vec[i];
        nw[p]--;
        sort(nw.begin(), nw.end());
        if (*nw.begin() == 0) nw.erase(nw.begin());
        int id = get_id(all_vec, nw);
        if (id != K && all_vec[id] == nw) {
          adj[i].push_back(id);
          adj[id].push_back(i);
        }
      }
      {
        vector<int> nw = all_vec[i];
        nw[p]++;
        sort(nw.begin(), nw.end());
        int id = get_id(all_vec, nw);
        if (id != K && all_vec[id] == nw) {
          adj[i].push_back(id);
          adj[id].push_back(i);
        }
      }
    }
    {
      vector<int> nw = all_vec[i];
      nw.push_back(1);
      sort(nw.begin(), nw.end());
      int id = get_id(all_vec, nw);
      if (id != K && all_vec[id] == nw) {
        adj[i].push_back(id);
        adj[id].push_back(i);
      }
    }
  }
  for (int i = 0; i < K; i++) possible_divs.push_back(divs(all_vec[i]));
  sort(possible_divs.begin(), possible_divs.end());
  possible_divs.erase(unique(possible_divs.begin(), possible_divs.end()),
                      possible_divs.end());
  for (int i = 0; i < K; i++) D[i] = get_pos(divs(all_vec[i]));
  for (int ii = 0; ii < N; ii++) {
    int root = get_id(all_vec, li[ii]);
    for (int i = 0; i < K; i++) dist[i] = -1;
    queue<int> q;
    q.push(root);
    dist[root] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int v : adj[u])
        if (dist[v] == -1) {
          dist[v] = dist[u] + 1;
          q.push(v);
        }
    }
    for (int iii = 0; iii < (int)possible_divs.size(); iii++)
      dp[ii][iii] = MAXN;
    for (int j = 0; j < K; j++) chkmin(dp[ii][D[j]], dist[j]);
  }
  int t;
  cin >> t;
  while (t--) {
    int a, b;
    cin >> a >> b;
    vector<int> A = get(a), B = get(b);
    a = lower_bound(li.begin(), li.end(), A) - li.begin();
    b = lower_bound(li.begin(), li.end(), B) - li.begin();
    int answer = MAXN;
    for (int i = 0; i < (int)possible_divs.size(); i++)
      chkmin(answer, dp[a][i] + dp[b][i]);
    cout << answer << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  prepare();
  read();
  solve();
  return 0;
}
