#include <bits/stdc++.h>
using namespace std;
int f[10000005];
int calc(int x){
int t=x,len=0;
while(t!=0){
len++;t/=10;
}
int p=0;
while(x%10==0){
p++;x/=10;
}
return 2*(len-p)-(x%10==5);
}
int main(){
freopen ("absurd.in","r",stdin);
freopen ("absurd.out","w",stdout);
int T;scanf("%d",&T);
while(T--){
int l,r;scanf("%d%d",&l,&r);
int mn=10000,ans;
for (int i=l;i<=r;i++){
int x=calc(i);
if (x<mn){
mn=x;ans=i;
}
}
printf("%d\n",ans);
}
return 0;
}