


You are given a number 'N' in the form of a string 'S', your task is to find the smallest number strictly greater than the given number 'N' which is a palindrome.
Note:
1) A palindrome is a word, number, phrase, or another sequence of characters that reads the same backward as forward, such as 'naman', 'abcba', '1234321', etc
2) The numerical value of the given string 'S' will be greater than 0.
3) A single-digit number is also considered as a palindrome.
4) Note that the length of the string 'S' is nothing but the number of digits in the number 'N'.
The first line of the input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains an integer, denoting the number of digits in the number 'N' (i.e. the length of the string).
The second line of each test case contains the string 'S'.
Output Format:
The only line of output of each test case consists of the next greater palindromic number(as described in the problem statement) returned in the form of a string.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= len(S) <= 10^4
Time Limit : 1 sec
1
4
1221
1331
The next smaller palindrome to 1221 is 1331, as it is strictly greater than 1221 and it reads the same from the front and back both.
1
3
999
1001
The next smaller palindrome to 999 is 1001, as it is strictly greater than 999 and it reads the same from the front and back both.
Try copying the left half of the string to the right half and then making cases.
In this method, Before we start let us define what is the right half (or right part) and what is left half (or left part) in the string. There can be two cases based on the length of the string:
This number can also be represented as:
1234567= 1*10^6 + 2*10^5 + 3*10^4 + 4*10^3 + 5*10^2 + 6*10^1 + 7*10^0.
From this, we can derive a conclusion that the left part contains more significant digits (digits with greater place value) as compared to the right part.
So The algorithm for the problem would be as follows:
O(N), Where ‘N’ is the length of the given string ‘S’.
Here, since we are iterating over all the characters in the number string ‘S’ which is of length ‘N’. Therefore the time complexity grows by order O(N).
O(1),
Since we are using constant space here so the space complexity becomes O(1).