#include <bits/stdc++.h>
using namespace std;
int len1,len2;
int n1[15],n2[15];
int pw[15]={1};
int main(){
freopen ("absurd.in","r",stdin);
freopen ("absurd.out","w",stdout);
int T;scanf("%d",&T);
for (int i=1;i<=9;i++)pw[i]=pw[i-1]*10;
while(T--){
int l,r,x,y;scanf("%d%d",&l,&r);
x=l,y=r;
len1=len2=0;
while(l!=0){
n1[++len1]=l%10;l/=10;
}
while(r!=0){
n2[++len2]=r%10;r/=10;
}
if (len1!=len2){
if (x<=5*pw[len1-1])printf("%d\n",5*pw[len1-1]);
else if (5*pw[len1]<=y)printf("%d\n",5*pw[len1]);
else printf("%d\n",(n1[len1]+(x%pw[len1-1]!=0))*pw[len1-1]);
}
else{
int lw=0;
for (int i=len1;i>=1;i--){
if (n1[i]!=n2[i]){
lw=i;break;
}
}
for (int i=len1;i>lw;i--)printf("%d",n1[i]);
if (lw!=0){
if (x%pw[lw]==0)printf("0");
else if (n2[lw]>=5&&x%pw[lw]<=5*pw[lw-1])printf("5");
else if (lw>1)printf("%d",n1[lw]+(x%pw[lw-1]!=0));
else printf("%d",n1[lw]);
for (int i=lw-1;i>=1;i--)printf("0");
}
printf("\n");
}
}
return 0;
}