/*
分析:
和hdu1495一样,推状态就行了。
2012-07-24
*/
分析:
和hdu1495一样,推状态就行了。
2012-07-24
*/
#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
int map[10][10][10][10];
char s[11];
char ans[11];
struct node
{
int a,b,c,d;
int step;
};
int judge(int a,int b,int c,int d)
{
return map[a][b][c][d];
}
int BFS()
{
queue<node>q;
node cur,next;
int t;
cur.a=s[0];
cur.b=s[1];
cur.c=s[2];
cur.d=s[3];
cur.step=0;
map[cur.a][cur.b][cur.c][cur.d]=1;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.a==ans[0] && cur.b==ans[1] && cur.c==ans[2] && cur.d==ans[3]) return cur.step;
if(cur.a==1) next.a=9;
else next.a=cur.a-1;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
if(cur.a==9) next.a=1;
else next.a=cur.a+1;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
next.a=cur.a;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
t=next.a;
next.a=next.b;
next.b=t;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
if(cur.b==1) next.b=9;
else next.b=cur.b-1;
next.a=cur.a;
next.c=cur.c;
next.d=cur.d;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
if(cur.b==9) next.b=1;
else next.b=cur.b+1;
next.a=cur.a;
next.c=cur.c;
next.d=cur.d;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
next.a=cur.a;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
t=next.a;
next.a=next.b;
next.b=t;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
next.a=cur.a;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
t=next.c;
next.c=next.b;
next.b=t;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
if(cur.c==1) next.c=9;
else next.c=cur.c-1;
next.b=cur.b;
next.a=cur.a;
next.d=cur.d;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
if(cur.c==9) next.c=1;
else next.c=cur.c+1;
next.b=cur.b;
next.a=cur.a;
next.d=cur.d;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
next.a=cur.a;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
t=next.b;
next.b=next.c;
next.c=t;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
next.a=cur.a;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
t=next.d;
next.d=next.c;
next.c=t;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
if(cur.d==1) next.d=9;
else next.d=cur.d-1;
next.b=cur.b;
next.c=cur.c;
next.a=cur.a;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
if(cur.d==9) next.d=1;
else next.d=cur.d+1;
next.b=cur.b;
next.c=cur.c;
next.a=cur.a;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
next.a=cur.a;
next.b=cur.b;
next.c=cur.c;
next.d=cur.d;
t=next.d;
next.d=next.c;
next.c=t;
if(!judge(next.a,next.b,next.c,next.d))
{
next.step=cur.step+1;
map[next.a][next.b][next.c][next.d]=1;
q.push(next);
}
}
return -1;
}
int main()
{
int T;
int i;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",s,ans);
for(i=0;i<4;i++) {s[i]-='0';ans[i]-='0';}
memset(map,0,sizeof(map));
printf("%d\n",BFS());
}
return 0;
}