/*The next palindrome*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#ifndef __cplusplus
#define bool unsigned
#define true 1
#define false 0
#endif //__cplusplus
bool palin_more_than_org(const char* str,unsigned n)
//str[i] gives palin, str[j] gives org
{
int i=n/2-1,j=(n+1)/2;
for(;i>=0 && j<n;--i,++j)
{
if(str[j]>str[i])return false;
else if(str[j]<str[i])return true;
}
return false;
}
bool is_9s(const char* str,unsigned n)
{
unsigned i;
for(i=0;i<n;++i)if(str[i]!='9')return false;
return true;
}
void next_palin(char* str,unsigned n)
{
unsigned i,j;
if(is_9s(str,n))
{
str[0]='1';
for(i=1;i<n;++i)str[i]='0';
str[n]='1';
str[n+1]='\0';
return;
}
if(!palin_more_than_org(str,n))
{
//increase first half
for(i=(n-1)/2;str[i]=='9';--i)str[i]='0';
++str[i];
}
for(i=0,j=n-1;i<j;++i,--j)str[j]=str[i];
}
int main()
{
unsigned i,t;
char* str;
scanf("%u",&t);
//fflush(stdin);
str=(char*)malloc(10000002);
for(i=0;i<t;++i)
{
gets(str);
next_palin(str,strlen(str));
puts(str);
}
free(str);
return 0;
}
LypUaGUgbmV4dCBwYWxpbmRyb21lKi8KI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RyaW5nLmg+CiNpbmNsdWRlPHN0ZGxpYi5oPgojaW5jbHVkZTxtYWxsb2MuaD4KCiNpZm5kZWYgX19jcGx1c3BsdXMKI2RlZmluZSBib29sIHVuc2lnbmVkCiNkZWZpbmUgdHJ1ZSAxCiNkZWZpbmUgZmFsc2UgMAojZW5kaWYgICAgLy9fX2NwbHVzcGx1cwoKYm9vbCBwYWxpbl9tb3JlX3RoYW5fb3JnKGNvbnN0IGNoYXIqIHN0cix1bnNpZ25lZCBuKQovL3N0cltpXSBnaXZlcyBwYWxpbiwgc3RyW2pdIGdpdmVzIG9yZwp7CglpbnQgaT1uLzItMSxqPShuKzEpLzI7Cglmb3IoO2k+PTAgJiYgajxuOy0taSwrK2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