Gudi trapped in the Room

4.2

69 votes
Depth First Search, Easy, Graphs
Problem

Gudi enters the castle, and moves along the main path. Suddenly, a block in the ground opens and she falls into it! Gudi slides down and lands in a dark room. A mysterious voice announces:

Intruders are not allowed inside the castle. To proceed, you must solve my puzzle. Here is a string S indexed from 1 to N, consisting of digits from 0-9. If you summon the spell "Sera", the string will be rotated clockwise by H positions. If you summon the spell "Xhaka", the number A will be added to all the even-indexed digits of the string. For example, if H = 1 A = 3 "Sera" and "Xhaka" on the string "781" will result in strings ""178" and "711" respectively i.e. digits post 9 are cycled back to 0. The objective is to obtain the lexicographically smallest string possible as a result of applying any of the two spells any number of times in any order. Find the string and I shall set you free


Input
The first line contains an integer T. T testcases follow.
First line of each test contains the string S.
The next line contains two space-separated integers A and H.

Output
Print the answer to each testcase in a new line.

Constraints
1T10
1N,H6
1A10

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For the first testcase, we can summon the spells as:
31 --(Sera)- -> 13 --(Xhaka)- -> 17 --(Xhaka)- -> 11, and it is the smallest possible answer.

Editor Image

?