#include<iostream>
#include<set>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
ll a, b, x, y;
ll res = -1;
ll f = 1;
void fun(ll x)
{
string s = to_string(x);
ll ans = -1;
for (ll i = 0; i < s.size(); i++)
{
ans = max(ans, (ll)s[i] - '0');
}
if (res == 9) {
cout << res << endl;
f = 0;
}
else res = ans;
}
signed main()
{
ll t;
cin >> t;
while (t--)
{
f = 1;
res = -1;
cin >> a >> b >> x >> y;
ll ans = 0;
for (ll i = a; i <= b; i++)
{
for (ll j = x; j <= y; j++)
{
ans = i + j;
fun(ans);
if (!f) break;
}
if (!f) break;
}
if (f == 1) cout << res << endl;
}
}