#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
typedef double db;
const int N=200010;
const ll mod=998244353;
//const ll mod=1e9+7;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define vi vector <int>
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
#define uint unsigned int
#define ull unsigned long long
#define i128 __int128
#define pb emplace_back
#define pii pair<int,int>
#define debug(x) std::cout<<x<<endl
#define endl '\n'
#define mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
template<typename type>
inline void read(type &x)
{
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x,bool mode=1)//0为空格,1为换行
{
x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10; while(x);
while(top) putchar(Stack[top--]|48);
mode?putchar('\n'):putchar(' ');
}
//head~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//begin~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//end~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int a[4],b[4],dis[20][20][2];
string x,y;
void init(){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
//dis[i][j]=min(abs(i-j),i+1+9-j);
if(i==j){
dis[i][j][0]=0;dis[i][j][1]=0;
continue;
}
if(i<j){
dis[i][j][0]=j-i;
dis[i][j][1]=i+9-j+1;
}
if(i>j){
dis[i][j][0]=9-i+1+j;
dis[i][j][1]=i-j;
}
}
}
}
int solve(vector<int> q){
vector<int> l,r;
if(q.size()==0){
return 0;
}
if(q.size()==1){
return q[0];
}
int mincnt=0;
for(int i=0;i<q.size();i++){
if(q[i]<q[mincnt]){
mincnt=i;
}
}
int node=q[mincnt];
for(int i=0;i<q.size();i++){
q[i]-=node;
}
for(int i=0;i<mincnt;i++){
l.push_back(q[i]);
}
for(int i=mincnt+1;i<q.size();i++){
r.push_back(q[i]);
}
return solve(l)+solve(r)+node;
}
void solve(){
cin>>x>>y;
int node=0;
for(int i=0;i<4;i++){
a[i]=x[i]-'0';b[i]=y[i]-'0';
}
int ans=100;
for(int s=0;s<16;s++){
vector<int> q;
int cnt=0;
for(int i=0;i<4;i++){
if(i==0){
q.push_back(dis[a[i]][b[i]][(s>>i)&1]);
continue;
}
if(((s>>i)&1)==((s>>(i-1))&1)){
q.push_back(dis[a[i]][b[i]][(s>>i)&1]);
}else{
cnt+=solve(q);
q.clear();
q.push_back(dis[a[i]][b[i]][(s>>i)&1]);;
}
}
cnt+=solve(q);
ans=min(ans,cnt);
}
cout<<ans<<endl;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int _=1;
cin>>_;
init();
while(_--){
solve();
}
return 0;
}