Even after changing the code multiple times and testing with different inputs still i get wrong answer. But it works find locally for a bunch of inputs i tried. Could anyone check and let me know whats wrong here ?

This answer is for next palindrome problem.

import java.io.*;

public class Main {

```
static int size;
static char[] n;
public static void main(String[] args) {
try
{
Main m=new Main();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
n = new char[1000002];
int t;
t = Integer.parseInt(input.readLine());
while(t-- > 0)
{
n[0]='0';
size=input.read(n,1,1000001);
m.nextPalin();
String temp= (n[0]=='0' ? String.valueOf(n,1,size) : String.valueOf(n,0,size+1)) ;
System.out.print(temp);
}
}
catch(Exception e)
{
}
}
void addNumber(int start, int end)
{
int x,k;
boolean needCarry=true;
for(x=start;x>=end && needCarry ;x--)
{
if(n[x] == '9')
{
n[x]='0';
needCarry=true;
}
else if(n[x] == '0')
{
n[x]='1';
break;
}
else
{
k=Character.getNumericValue((n[x])+1);
n[x]= (char)(k+'0');
break;
}
}
}
void checkPalin()
{
int i,j;
for(i=1,j=size-1;i<j;i++,j--)
{
if(n[i]!=n[j])
break;
}
if(i>=j)
addNumber(size-1,0);
}
void nextPalin()
{
checkPalin();
int i,j;
i = n[0]=='0' ? 1: 0;
for(j=size-1; i<j ;i++,j--)
{
if (n[i] < n[j])
{
addNumber(j-1,i);
n[j] = n[i];
}
else if (n[i] > n[j])
{
n[j] = n[i];
}
}
}
```

}