#include<iostream>
#include<vector>
#include<string>
using namespace std;
///Function to print the array
void printArray(vector<int>& v)
{
for(int i=0;i<v.size();++i)
{
cout<<v[i];
}
cout<<endl;
}
///Function to convert string to number array
void convertToIntArray(vector<int>& v, string& s)
{
for(int i=0;i<s.size();++i)
{
int no = s[i] - 48;
v.push_back(no);
}
}
///Funtion to check if all 9s
bool isAll9(vector<int>& v)
{
for(int i=0;i<v.size();++i)
{
if(v[i]!=9)
return false;
}
return true;
}
///Funtion for type 2 and 3
void genrateNextPalindromeUtil(vector<int>& v)
{
int n = v.size();
int mid = n/2;
///A bool variable to check if copy of left side to right side is sufficient or not
bool leftsmaller = false;
int i = mid - 1;
int j = (n % 2)? mid + 1 : mid;
while(i>=0 && v[i] == v[j])
i--,j++;
if(i < 0 || v[i] < v[j])
leftsmaller = true;
///Copy the mirror of left to right
while(i>=0){
v[j] = v[i];
++j;
--i;
}
if(leftsmaller)
{
int carry = 1;
i = mid - 1;
if(n%2 == 1)
{
v[mid] += carry;
carry = v[mid]/10;
v[mid] = v[mid]%10;
j = mid + 1;
}
else
j = mid;
while(i>=0)
{
v[i] += carry;
carry = v[i] /10;
v[i] = v[i] % 10;
v[j++] = v[i--];
}
}
}
///Funtion to print next palindrome
void generateNextPalindrome(vector<int>& v)
{
int n = v.size();
///Input type 1
if(isAll9(v))
{
cout<<"1";
for(int i=0;i<n;++i)
{
cout<<"0";
}
cout<<"1"<<endl;
}
else{
genrateNextPalindromeUtil(v);
printArray(v);
}
}
int main()
{
int t;
cin>>t;
vector<int> v;
while(t--){
string s;
cin>>s;
convertToIntArray(v,s);
generateNextPalindrome(v);
v.clear();
}
return 0;
}