#include <iostream>
#include <cstring>
#include <cstdlib>
#define max_len 1000002
using namespace std;
char *str = new char[max_len];
char *rev_str = new char[max_len];
char *residue = new char[max_len];
char *xrev = new char[max_len];
char *a = new char[3];
char* str_rev(char *s) {
int len=strlen(s);
for(int i=0; i<len/2; i++)
swap(s[i], s[len-i-1]);
return s;
}
char* trim(char *s) {
int i;
for(i=0; s[i] && s[i]=='0'; i++);
if(s[i]) strcpy(s, s+i);
else strcpy(s, "0");
return s;
}
int compare(char *a, char *b) {
trim(a); trim(b);
int alen=strlen(a), blen=strlen(b);
if(alen>blen) return 1;
if(alen<blen) return -1;
for(int i=0; i<alen; i++) {
if(a[i]>b[i]) return 1;
if(a[i]<b[i]) return -1;
}
return 0;
}
char* add_by_1(char *n) {
int len = strlen(n);
if(n[len-1] < '9') {
n[len-1]++;
return n;
}
int carry=0, p, q=1, sum;
for(int i=len-1; i>=0; i--) {
p = n[i]-'0';
sum = p+q+carry;
carry = sum/10;
sum %= 10;
n[i] = sum+'0';
if(i==len-1) q=0;
}
if(carry) {
for(int i=len-1; i>=0; i--)
n[i+1] = n[i];
n[0] = carry+'0';
n[len+1] = 0;
}
return n;
}
char* get_palin(char *n) {
int len=strlen(n);
if(len==1 && n[0]!='9') {
n[0]++;
return n;
}
if(len==1 && n[0]=='9') {
strcpy(a, "11");
return a;
}
strncpy(rev_str, n, len/2);
rev_str[len/2] = 0;
if(!(len%2)) strncpy(residue, n+len/2, len/2);
else strcpy(residue, n+len/2+1);
residue[len/2] = 0;
strcpy(xrev, rev_str);
if(compare(residue,str_rev(xrev))<0) {
strcpy(str, rev_str);
if(len%2) {
str[len/2] = n[len/2];
str[len/2+1] = 0;
}
strcat(str, xrev);
}
else {
if(!(len%2)) {
add_by_1(rev_str);
strcpy(str, rev_str);
if(rev_str[len/2]) {
strncpy(xrev, rev_str, len/2);
xrev[len/2] = 0;
strcat(str, str_rev(xrev));
}
else strcat(str, str_rev(rev_str));
}
else {
rev_str[len/2] = n[len/2];
rev_str[len/2+1] = 0;
add_by_1(rev_str);
strcpy(str, rev_str);
strncpy(xrev, rev_str, len/2);
xrev[len/2] = 0;
strcat(str, str_rev(xrev));
}
}
return str;
}
char input[max_len];
int main() {
long long int t;
cin>>t;
while(t--) {
cin>>input;
cout<<get_palin(input)<<endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNkZWZpbmUgbWF4X2xlbiAxMDAwMDAyCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjaGFyICpzdHIgPSBuZXcgY2hhclttYXhfbGVuXTsKY2hhciAqcmV2X3N0ciA9IG5ldyBjaGFyW21heF9sZW5dOwpjaGFyICpyZXNpZHVlID0gbmV3IGNoYXJbbWF4X2xlbl07CmNoYXIgKnhyZXYgPSBuZXcgY2hhclttYXhfbGVuXTsKY2hhciAqYSA9IG5ldyBjaGFyWzNdOwoKY2hhciogc3RyX3JldihjaGFyICpzKSB7CglpbnQgbGVuPXN0cmxlbihzKTsKCWZvcihpbnQgaT0wOyBpPGxlbi8yOyBpKyspCgkJc3dhcChzW2ldLCBzW2xlbi1pLTFdKTsKCXJldHVybiBzOwp9CgpjaGFyKiB0cmltKGNoYXIgKnMpIHsKCWludCBpOwoJZm9yKGk9MDsgc1tpXSAmJiBzW2ldPT0nMCc7IGkrKyk7CglpZihzW2ldKSBzdHJjcHkocywgcytpKTsKCWVsc2Ugc3RyY3B5KHMsICIwIik7CglyZXR1cm4gczsKfQoKaW50IGNvbXBhcmUoY2hhciAqYSwgY2hhciAqYikgewoJdHJpbShhKTsgdHJpbShiKTsKCWludCBhbGVuPXN0cmxlbihhKSwgYmxlbj1zdHJsZW4oYik7CglpZihhbGVuPmJsZW4pIHJldHVybiAxOwoJaWYoYWxlbjxibGVuKSByZXR1cm4gLTE7Cglmb3IoaW50IGk9MDsgaTxhbGVuOyBpKyspIHsKCQlpZihhW2ldPmJbaV0pIHJldHVybi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