#include<stdio.h>
int main()
{
int i,j,t,c;
scanf("%d",&t);//for no of test cases
int arr[t];
for(i=0;i<t;i++)
{
scanf("%d",&arr[i]);
if(arr[i]>1000000)
exit(0);
}
for(i=0;i<t;i++)
{
for(c=arr[i]+1;c<1000000;c++)
{
j=c;
int temp=0,sum=0;
while(j!=0)
{
temp=j%10;
j=j/10;
sum=(sum*10)+temp;
}
if(sum==c)
{
printf("\n %d",c);//printing next palindrome
break;
}
}
}
}