#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
freopen("absurd.in", "r", stdin);
freopen("absurd.out", "w", stdout);
int n;
cin >> n;
for(int k = 1; k <= n; k ++) {
int l, r;
cin >> l >> r;
int minn = 1e12, ans;
while(l <= r) {
int pos = l, cnt = 0, y, k, sum = 0;
while(pos % 10 == 0) {
cnt ++;
pos /= 10;
}
y = pos;
k = y % 10;
while(y != 0) {
sum ++;
y /= 10;
}
sum *= 2;
if(k == 5) {
sum --;
}
if(sum < minn) {
minn = sum;
ans = l;
}
l += pow(10, cnt);
}
cout << ans << endl;
}
return 0;
}