#include<bits/stdc++.h>usingnamespace std;#definefifirst#definesesecondusing LL =longlong;using PII = pair<int,int>;constint N =2e6+10;
PII q[N];bool st[N], vi[N];voidsolve(){int A, B;
cin >> A >> B;memset(st,false,sizeof st);memset(vi,false,sizeof vi);auto check =[&](int x){for(int i =2; i * i <= x; i ++)if(x % i ==0)returnfalse;returntrue;};for(int i =1000; i <10000; i ++)if(check(i)) st[i]=true;int hh =0, tt =-1;
q[++ tt]={A,0};while(hh <= tt){auto t = q[hh ++];// cout << t.fi << endl;if(t.fi == B){
cout << t.se << endl;return;}if(vi[t.fi])continue;
vi[t.fi]=true;int one, two, thr, fou;
fou = t.fi %10, thr =(t.fi /10)%10;
two =(t.fi /100)%10, one = t.fi /1000;// cout << one << two << thr << fou << endl;for(int i =1; i <=9; i ++){int x = i *1000;
x += two *100+ thr *10+ fou;if(st[x]) q[++ tt]={x, t.se +1};}for(int i =0; i <=9; i ++){int x = one *1000;
x += i *100+ thr *10+ fou;if(st[x]) q[++ tt]={x, t.se +1};}for(int i =0; i <=9; i ++){int x = one *1000;
x += two *100+ i *10+ fou;if(st[x]) q[++ tt]={x, t.se +1};}for(int i =0; i <=9; i ++){int x = one *1000;
x += two *100+ thr *10+ i;if(st[x]) q[++ tt]={x, t.se +1};}}
cout <<"Impossible"<< endl;}signedmain(){int ___ =1;
cin >> ___;for(; ___ --;)solve();return0;}