#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;typedef pair<ll, ll>PII;constint N =2e5+10;constint MOD =998244353;constint INF =0X3F3F3F3F;constint dx[]={-1,1,0,0,-1,-1,+1,+1};constint dy[]={0,0,-1,1,-1,+1,-1,+1};constint M =1e6+10;intmain(){int t;
cin >> t;while(t --){
ll l1, l2, r1, r2;
cin >> l1 >> r1 >> l2 >> r2;//求这种得可以先确定总体得范围
ll l = l1 + l2, r = r1 + r2;
string a =to_string(l), b =to_string(r);if(a.size()< b.size()){reverse(a.begin(), a.end());while(a.size()< b.size()) a +='0';reverse(a.begin(), a.end());}//让它们得大小一样
ll ans =0, res =0;//贪心for(int i =0; i <(int)a.size(); i ++){
res +=(b[i]-'0');if(a[i]< b[i]){
ans =max(ans, res -1+(int)(b.size()- i -1)*9);}}
ans =max(ans, res);
cout << ans << endl;}}