1 /*
2 宽度优先搜索 BFS
3 */
4 // include file
5 #include <cstdio>
6 #include <cstdlib>
7 #include <cstring>
8 #include <cmath>
9 #include <cctype>
10 #include <ctime>
11
12 #include <iostream>
13 #include <sstream>
14 #include <fstream>
15 #include <iomanip>
16 #include <bitset>
17
18 #include <algorithm>
19 #include <string>
20 #include <vector>
21 #include <queue>
22 #include <set>
23 #include <list>
24 #include <functional>
25
26 using namespace std;
27
28 // typedef
29 typedef long long LL;
30 typedef unsigned long long ULL;
31 typedef __int64 Bint;
32
33 //
34 #define read freopen("in.txt","r",stdin)
35 #define write freopen("out.txt","w",stdout)
36 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
37 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
38 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
39 #define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
40 #define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
41 #define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
42 #define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)
43
44 #define FF(i,a) for(int i=0;i<(a);i++)
45 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
46
47 #define Z(a) (a<<1)
48 #define Y(a) (a>>1)
49
50 const double eps = 1e-6;
51 const double INFf = 1e10;
52 const int INFi = 1000000000;
53 const double Pi = acos(-1.0);
54
55 template<class T> inline T sqr(T a){return a*a;}
56 template<class T> inline T TMAX(T x,T y)
57 {
58 if(x>y) return x;
59 return y;
60 }
61 template<class T> inline T TMIN(T x,T y)
62 {
63 if(x<y) return x;
64 return y;
65 }
66 template<class T> inline void SWAP(T &x,T &y)
67 {
68 T t = x;
69 x = y;
70 y = t;
71 }
72 template<class T> inline T MMAX(T x,T y,T z)
73 {
74 return TMAX(TMAX(x,y),z);
75 }
76
77
78 // code begin
79 #define MAXN 10010
80 int N,S,E,ans;
81 bool pr[MAXN];
82 int used[MAXN];
83 struct node
84 {
85 node(){}
86 node(int a,int b)
87 {
88 tm=a;
89 val=b;
90 }
91 int tm;
92 int val;
93 };
94 queue<node> qi;
95
96 bool BFS()
97 {
98 //memset(used,INFi,sizeof(int)*MAXN);
99 FORi(0,MAXN,1) used[i] = INFi;
100 used[S] = 0;
101 while(!qi.empty()) qi.pop();
102 qi.push(node(0,S));
103 int n,t;
104 while(!qi.empty())
105 {
106 node cur = qi.front();qi.pop();
107 //printf("%d\n",cur.val);
108 t = cur.val;
109 n = cur.val%10;
110 cur.val -= n;
111 FORi(0,10,1)
112 {
113 //printf("第一批 %d %d\n",cur.val+i,pr[cur.val+i]);
114 if( !pr[cur.val+i] && cur.tm+1<used[cur.val+i] )
115 {
116
117 if(cur.val+i==E)
118 {
119 ans=cur.tm;
120 return true;
121 }
122 used[cur.val+i] = cur.tm+1;
123 qi.push(node(cur.tm+1,cur.val+i));
124 }
125 }
126 //printf("开始 %d\n",cur.val);
127 cur.val = t;
128 n = (cur.val/10)%10;
129 cur.val -= n*10;
130 FORi(0,10,1)
131 {
132 //printf("第二批 %d %d\n",cur.val+i*10,pr[cur.val+i*10]);
133 if(!pr[cur.val+i*10] && cur.tm+1<used[cur.val+i*10])
134 {
135 if(cur.val+i*10==E)
136 {
137 ans=cur.tm;
138 return true;
139 }
140 used[cur.val+i*10] = cur.tm+1;
141 qi.push(node(cur.tm+1,cur.val+i*10));
142 }
143 }
144 //printf("开始 %d\n",cur.val);
145 cur.val = t;
146 n = (cur.val/100)%10;
147 cur.val -= n*100;
148 FORi(0,10,1)
149 {
150 //printf("第三批 %d %d\n",cur.val+i*100,pr[cur.val+i*100]);
151 if( !pr[cur.val+i*100] && cur.tm+1<used[cur.val+i*100])
152 {
153 if(cur.val+i*100==E)
154 {
155 ans=cur.tm;
156 return true;
157 }
158 used[ cur.val+i*100 ] = cur.tm+1;
159 qi.push(node(cur.tm+1,cur.val+i*100));
160 }
161 }
162 //printf("第四批开始 %d\n",cur.val);
163 cur.val = t;
164 n = (cur.val/1000)%10;
165 cur.val -= (n-1)*1000;
166 FORi(0,9,1)
167 {
168 //printf("第四批 %d %d\n",cur.val+i*1000,pr[cur.val+i*1000]);
169 if( !pr[cur.val+i*1000] && cur.tm+1<used[cur.val+i*1000])
170 {
171 if(cur.val+i*1000==E)
172 {
173 ans=cur.tm;
174 return true;
175 }
176 used[ cur.val+i*1000 ] = cur.tm+1;
177 qi.push(node(cur.tm+1,cur.val+i*1000));
178 }
179 }
180 }
181 return false;
182 }
183
184 int main()
185 {
186 read;
187 write;
188 memset(pr,0,sizeof(pr));
189 FORi(2,MAXN/2,1)
190 {
191 if(!pr[i])
192 {
193 FORj(i+i,MAXN,i)
194 {
195 pr[j]=true;
196 }
197 }
198 }
199 while(scanf("%d",&N)!=-1)
200 {
201 while(N--)
202 {
203 scanf("%d %d",&S,&E);
204 if(S==E)
205 {
206 printf("0\n");
207 continue;
208 }
209 if(BFS())
210 printf("%d\n",ans+1);
211 else
212 printf("Impossible\n");
213 }
214 }
215 return 0;
216 }