#include<stdio.h> #include<stdlib.h> #include<string.h> #include<queue> #include<math.h> #define N 10010 using namespace std; int vis[N]; char s1[5], s2[5], s3[5]; struct node { int step; char st[5]; }; int prime(int y) { int i, k = (int)sqrt(y); for(i = 2 ; i <= k ; i++) if(y % i == 0) return 0; return 1; } int BFS(char s[]) { queue<node>Q; node now, next; int i, j, h, x = 0; memset(vis, 0, sizeof(vis)); memset(s3, 0, sizeof(s3)); for(i = 0 ; i < 4 ; i++) x = x * 10 + (s[i] - '0'); vis[x] = 1; strcpy(now.st, s); now.step = 0; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); if(strcmp(now.st, s2) == 0) return now.step; for(i = 0 ; i < 4 ; i++)//各个位(即个位,十位,百位,千位) { for(j = 0 ; j < 10 ; j++)//0到9十个数 { if(i == 0 && j == 0)//千位不为0 continue; if(now.st[i] == j + '0')//原有的数不需要替换 continue; strcpy(s3, now.st); now.st[i] = j + '0';//0到9其中的一个数去替换四位数气中的一位 x = 0; for(h = 0 ; h < 4 ; h++) x = x * 10 + (now.st[h] - '0'); if(prime(x) == 1 && !vis[x]) { vis[x] = 1; next.step = now.step + 1; strcpy(next.st, now.st); i = 0;/***/ j = 0;/***/ Q.push(next); } strcpy(now.st, s3); } } } return -1; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%s%s", s1, s2); printf("%d ", BFS(s1)); } return 0; }