#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char s1[10],s2[10];
int ans,a[10],b[10],c[10];
inline int sgn(int x)
{
if(x>0)return 1;
else if(x==0)return 0;
else return -1;
}
inline void cal(int &res,int l,int r)
{
/*
cout<<"cal "<<l<<" "<<r<<endl;
for(int i=l;i<=r;i++)
cout<<a[i]<<" ";
cout<<endl;
*/
if(l>r)return;
int mini=1000000000;
for(int i=l;i<=r;i++)
mini=min(mini,a[i]);
res+=mini;
for(int i=l;i<=r;i++)
a[i]-=mini;
for(int i=l;i<=r;)
{
int j=i;
if(!a[i]){ i++;continue; }
while(j<r&&sgn(a[j+1])==sgn(a[i]))j++;
cal(res,i,j);
i=j+1;
}
}
inline int sol()
{
int res=0;
for(int i=1;i<=4;)
{
if(!a[i]){ i++;continue; }
int j=i;
while(j<4&&sgn(a[j+1])==sgn(a[i]))j++;
for(int k=i;k<=j;k++)
a[k]=abs(a[k]);
cal(res,i,j);
i=j+1;
}
//cout<<res<<endl;
return res;
}
int main()
{
int T;
scanf("%d",&T);
for(int _=1;_<=T;_++)
{
scanf("%s%s",s1+1,s2+1);
for(int i=1;i<=4;i++)
a[i]=s1[i]-'0',b[i]=s2[i]-'0',a[i]=b[i]-a[i];
for(int i=1;i<=4;i++)
c[i]=a[i];
ans=1000000000;
//sol();
for(int i=0;i<16;i++)
{
for(int j=0;j<4;j++)
{
if(i>>j&1)a[j+1]=c[j+1];
else a[j+1]=sgn(c[j+1])*(-1)*(10-abs(c[j+1]));
}
//cout<<"i ";
ans=min(ans,sol());
}
/**/
if(_==73){printf("%s %s",s1+1,s2+1);
printf("%d\n",ans);
}
return 0;
}