include <bits/stdc++.h>
using namespace std;
// Compare numeric strings by length then lex bool numLess(const string &a, const string &b) { if (a.size() != b.size()) return a.size() < b.size(); return a < b; }
int main(){ ios::sync_with_stdio(false); cin.tie(nullptr);
// 1) Precompute all special palindromes
vector<string> pals;
for(int mask = 1; mask < (1<<9); ++mask){
// count how many odd digits are selected
int oddCount = 0, oddDigit = -1;
for(int d = 1; d <= 9; ++d){
if (mask & (1<<(d-1))){
if (d % 2 == 1) {
oddCount++;
oddDigit = d;
}
}
}
if (oddCount > 1) continue; // can't form palindrome
// build half-string: for each selected d, add floor(d/2) copies of '0'+d
string half;
for(int d = 1; d <= 9; ++d){
if (mask & (1<<(d-1))){
half.append(d/2, char('0' + d));
}
}
// build the full palindrome
string rev = half;
reverse(rev.begin(), rev.end());
string pal = half;
if (oddCount == 1)
pal.push_back(char('0' + oddDigit));
pal += rev;
pals.push_back(pal);
}
// 2) Sort by numeric value
sort(pals.begin(), pals.end(), numLess);
// 3) Answer queries
int T;
cin >> T;
while(T--){
string n;
cin >> n;
// find first palindrome strictly > n
auto it = upper_bound(
pals.begin(), pals.end(), n,
[](const string &a, const string &b){
return numLess(a,b);
}
);
// it is guaranteed to exist because the largest precomputed is > 10^17
cout << *it << "\n";
}
return 0;}