Description

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

Translation

给两个四位数质数a,ba,b,要求每一次操作改变aa中四位数字的一个数,要求改变后仍为质数,且第一位不能改成00,问至少进行几次操作将aa改成bb

Idea

每一次操作选择四位数字中的一位改成[0,9][0,9]之间的整数,由于第一位不能是00,因此每次操作共有C41×(90+1)1=39C_4^1\times (9-0+1)-1=39种。
BFSBFS枚举每一次操作的情况,判断操作后的aa是否为质数(预处理[0,10000][0,10000]之间的质数),并且是否在之前的操作中出现过。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<queue>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
const int N=10004;
bool isprime[N];
int prime[N>>1];
int cnt=0;
struct state
{
//记录一个状态
int val;//该状态下的a值
int step;//该状态下的步数
};
void OlaSieve()//欧拉筛预处理质数
{
memset(isprime,true,sizeof(isprime));
int i,j;
for(i=2;i<N;i++)
{
if(isprime[i]) prime[++cnt]=i;
for(j=1;j<=cnt&&i*prime[j]<N;j++)
{
isprime[i*prime[j]]=false;
if(i%prime[j]==0) break;
}
}
}
queue<state>q;
int a,b;
bool flag;
bool vis[N];
void init()//初始化
{
flag=0;
memset(vis,0,sizeof(vis));
queue<state>e;
swap(e,q);
}
void check(state x)//判断新情况是否入队
{
if(vis[x.val]||!isprime[x.val]) return;
else
{
if(x.val==b)
{
cout<<x.step<<endl;
flag=1;//到达终点标志
return;
}
else
{
vis[x.val]=1;
q.push(x);
}
}
}
void bfs()
{
//初始状态入队
state start;
start.step=0;
start.val=a;
q.push(start);
while(!q.empty())
{
state now=q.front();
state nxt;
nxt.step=now.step+1;
int i,j;
//枚举四位
for(i=1;i<=4;i++)
{
//lp_q表示第p位到第q位的数字
if(i==1)//更改第一位
{
int l2_4=now.val%1000;
for(j=1;j<=9;j++)
{
nxt.val=l2_4+j*1000;
check(nxt);
if(flag) return;
}
}
else if(i==2)//更改第二位
{
int l1=now.val/1000;
int l3_4=now.val%100;
for(j=0;j<=9;j++)
{
nxt.val=l1*1000+j*100+l3_4;
check(nxt);
if(flag) return;
}
}
else if(i==3)//更改第三位
{
int l1_2=now.val/100;
int l_4=now.val%10;
for(j=0;j<=9;j++)
{
nxt.val=l1_2*100+j*10+l_4;
check(nxt);
if(flag) return;
}
}
else if(i==4)//更改第四位
{
int l1_3=now.val/10;
for(j=0;j<=9;j++)
{
nxt.val=l1_3*10+j;
check(nxt);
if(flag) return;
}
}
}
q.pop();//出队
}
}
void solve()
{
init();
cin>>a>>b;
if(a==b) puts("0");//特判
else bfs();
}
int main()
{
OlaSieve();
int t;
cin>>t;
while(t--) solve();
return 0;
}