1 #include<stdio.h>
2 #include<math.h>
3 int Prime(int x)
4 {
5 int i=2, flag=1;
6 if(x==1) return 0;
7 while(i<=sqrt(x))
8 {
9 if(x%i == 0) flag = 0;
10 i++;
11 }
12 return flag;
13 }
14
15 int main()
16 {
17 int ncases,m,i,j,distl,distr,temp1,temp2;
18 scanf("%d", &ncases);
19 while(ncases--)
20 {
21 distl=distr=0;
22 scanf("%d", &m);
23 if(m == 1)
24 printf("2 1\n");
25 else if(Prime(m) && m!=1)
26 printf("%d %d\n",m,0);
27 else
28 {
29 for(i=m+1; ; i++)//刚开始这加了i<10000000,WA让我快疯掉了,最后我把他去了结果竟然过了,我晕啊!
30 {
31 if(Prime(i))
32 {
33 temp1 = i;
34 distr = i-m;
35 break;
36 }
37 }
38
39 for(j=m-1; ; j--)
40 {
41 if(Prime(j))
42 {
43 temp2 = j;
44 distl = m-j;
45 break;
46 }
47 }
48 if(distl <= distr)
49 printf("%d %d\n",temp2,distl);
50 else
51 printf("%d %d\n",temp1,distr);
52 }
53 }
54 return 0;
55 }