#include "iostream" #include "cmath" #include "queue" using namespace std; int mark[9999]; int pri[9999]; int lev[9999]; int IsPrime(int n) { int i=2; while(i<=sqrt((float)n)) { if(n%i==0) return 0; else i++; } return 1; } int compare(int a, int b) { int num=0; if(a/1000==b/1000) num++; a%=1000;b%=1000; if(a/100==b/100) num++; a%=100;b%=100; if(a/10==b/10) num++; a%=10;b%=10; if(a==b) num++; if(num==3) return 1; return 0; } int BFS(int s, int e) { int front,i; memset(mark,0,sizeof(mark)); memset(lev,0,sizeof(lev)); queue <int> seq; seq.push(s); mark[s]=1; while(!seq.empty()) { front=seq.front(); if(front==e) { while(!seq.empty()) seq.pop(); return lev[front]; } seq.pop(); for(i=0;pri[i]!='/0';i++) { if(compare(front,pri[i])&&!mark[pri[i]]) { // printf("%d ",pri[i]); mark[pri[i]]=1; lev[pri[i]]=lev[front]+1; seq.push(pri[i]); } } } return -1; } int main() { int a,b; int i,k=0,t; for(i=1000;i<=9999;i++) { if(IsPrime(i)) pri[k++]=i; } scanf("%d",&t); while(t--) { scanf("%d %d",&a,&b); if(BFS(a,b)>=0) printf("%d/n",BFS(a,b)); else printf("Impossible/n"); } return 0; }