#include<iostream>
#include<queue>
#include<string>
#include<cstdio>
using namespace std;
typedef struct node
{
char data[5];
int step;
}Node;
bool mark[11][11][11][11];
char a[5];
char b[5];
void Bfs()
{
int i;
Node p,q;
queue<Node> Q;
memset(mark,0,sizeof(mark));
strcpy(p.data,a);
p.step = 0;
Q.push(p);
mark[p.data[0]-'0'][p.data[1]-'0'][p.data[2]-'0'][p.data[3]-'0'] = 1;
while(!Q.empty())
{
q = Q.front();
Q.pop();
for(i=0;i<4;i++)
{
strcpy(p.data,q.data);
p.step=0;
p.data[i] = q.data[i]+1;
if(p.data[i] == '9' +1 )
p.data[i] = '1';
p.step = q.step+1;
if(mark[p.data[0]-'0'][p.data[1]-'0'][p.data[2]-'0'][p.data[3]-'0'] == 0)
{
if(strcmp(p.data,b) == 0)
{
cout<<p.step<<endl;
return ;
}
else
{
mark[p.data[0]-'0'][p.data[1]-'0'][p.data[2]-'0'][p.data[3]-'0'] = 1;
Q.push(p);
}
}
strcpy(p.data,q.data);
p.step=0;
p.data[i] = q.data[i]-1;
if(p.data[i] == '0' )
p.data[i] = '9';
p.step = q.step+1;
if(mark[p.data[0]-'0'][p.data[1]-'0'][p.data[2]-'0'][p.data[3]-'0'] == 0)
{
if(strcmp(p.data,b) == 0)
{
cout<<p.step<<endl;
return ;
}
else
{
mark[p.data[0]-'0'][p.data[1]-'0'][p.data[2]-'0'][p.data[3]-'0'] = 1;
Q.push(p);
}
}
if(i<3)
{
strcpy(p.data,q.data);
p.step=0;
p.data[i] = q.data[i+1];
p.data[i+1] = q.data[i];
p.step = q.step+1;
if(mark[p.data[0]-'0'][p.data[1]-'0'][p.data[2]-'0'][p.data[3]-'0'] == 0)
{
if(strcmp(p.data,b) == 0)
{
cout<<p.step<<endl;
return ;
}
else
{
mark[p.data[0]-'0'][p.data[1]-'0'][p.data[2]-'0'][p.data[3]-'0'] = 1;
Q.push(p);
}
}
}
}
}
}
int main()
{
int text;
cin>>text;
while(text--)
{
scanf("%s",a);
scanf("%s",b);
if(strcmp(a,b) == 0)
{
cout<<0<<endl;
continue;
}
Bfs();
}
return 0;
}