#include "bits/stdc++.h"
const int INF = INT_MAX;
int q;
int power[10];
int calc(int x) {
while (x % 10 == 0) {
x /= 10;
}
int back = x % 10;
int len = 0;
while (x) {
x /= 10, ++ len;
}
if (back == 5) {
return 2 * len - 1;
} else {
return 2 * len;
}
}
int zeros(int x) {
int ret = 0;
while (x % 10 == 0) {
++ ret, x /= 10;
}
return ret;
}
int main() {
freopen("absurd.in", "r", stdin);
freopen("absurd.out", "w", stdout);
power[0] = 1;
for (int i = 1; i <= 9; ++ i) {
power[i] = power[i - 1] * 10;
}
std::cin >> q;
while (q --) {
int l, r, min = INF;
std::cin >> l >> r;
int ans = 0;
for (int i = l; i <= r; i += power[zeros(i)]) {
if (min > calc(i)) {
min = calc(i), ans = i;
}
}
std::cout << ans << '\n';
}
return 0;
}