Problem of the day
‘B’ = 4, ‘S’ = “4321”, ‘K’ = 3.
The given number is not stable as ‘S[3]’ is not the same as ‘S[0]’ but 3%3 = 0 same as 0%3. ‘S[3] = 1’ and ‘S[0] = 4’. But the number “4324” is stable. As, for all ‘i’, ‘S[i]’ = ‘S[i%K]’ and “4324” is also greater than the given number. It can be proved that this is the best possible answer.
Hence, the answer is “4324”.
The first line contains a single integer ‘T’ denoting the number of test cases, then the test case follows.
The first line of each test case contains two single space-separated integers, ‘B’ and ‘K'.
The second line contains a string with ‘B’ characters representing the string ‘S’.
For each test case, return the minimum stable number whose value is the same or exceeds the value of ‘A’.
Output for each test case will be printed on a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ B ≤ 10^5
1 ≤ K ≤ 10^9
It’s guaranteed that sum of ‘B’ over all cases does not exceed 10^5.
The given number will not contain leading zeros.
Time limit: 1 sec
2
4 2
6825
3 3
420
6868
420
For test case 1:
6868 is the minimum possible, stable number. We can see that it is stable because ‘S[0] = S[2]’ because 0%2 = 2%2, and ‘S[1] = S[3]’ because 1%2 = 3%2. All the conditions are satisfied.
Hence, 6868 is the answer.
For test case 2:
The given number itself is stable, and hence it is the best possible answer.
2
5 1
40369
4 2
8516
44444
8585