# Ninja and Numbers

Contributed by
Smit Mistry
Medium
0/80
25 mins
75 %

## Problem Statement

#### Your task is to find the smallest number which is stable and whose value is greater than or equal to βAβ. Zero-based indexing is used everywhere.

##### Example :
``````β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.
``````
Detailed explanation ( Input/output format, Notes, Images )
##### Constraints :
``````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
``````
##### Sample Input 1 :
``````2
4 2
6825
3 3
420
``````
##### Sample Output 1 :
``````6868
420
``````
##### Explanation For Sample Input 1 :
``````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.

For test case 2:
The given number itself is stable, and hence it is the best possible answer.
``````
##### Sample Input 2 :
``````2
5 1
40369
4 2
8516
``````
##### Sample Output 2 :
``````44444
8585
``````
Autocomplete
Console