#include <bits/stdc++.h>
using namespace std;
bool check_nines(string check)
{
for(int i = 0; i < check.length(); i++)
{
if(check[i] != '9')
{
return false;
}
}
return true;
}
int main()
{
long t, num, len;
cin>>t;
string pal,nextpal;
while(t > 0)
{
cin>>num;
pal = to_string(num);
if(check_nines(pal))
{
nextpal = "1";
for(int z = 0; z < pal.length(); z++)
{
nextpal += "0";
}
nextpal += "1";
}
else
{
if(pal.length() % 2 == 0)
{
len = pal.length() / 2;
nextpal = pal.substr(0,len);
string rev = nextpal;
reverse(rev.begin(), rev.end());
nextpal = nextpal + rev;
if(stol(nextpal) <= num)
{
nextpal = pal.substr(0,len);
long temp2 = stol(nextpal);
temp2 += 1;
nextpal = to_string(temp2);
rev = nextpal;
reverse(rev.begin(), rev.end());
nextpal = nextpal + rev;
}
}
else
{
len = pal.length() / 2;
nextpal = pal.substr(0,len);
string rev = nextpal;
reverse(rev.begin(), rev.end());
int i = pal[len] - '0';
if( stol(nextpal + to_string(i) + rev) > num)
{
nextpal = nextpal + to_string(i) + rev;
}
else
{
long temp2 = stol(nextpal + pal[len]);
temp2 += 1;
nextpal = to_string(temp2);
rev = nextpal.substr(0,len);
reverse(rev.begin(), rev.end());
nextpal += rev;
}
}
}
cout<<nextpal<<endl;
t--;
}
}
why am i getting wrong answer for the following code??