Jumping Beans

Time limit1sMemory limit128 MB

Summary
Track how beans in a row are rearranged after T seconds of a wrap-around swapping process, printing the final order for each test case.
Level

Medium7 of 10

Topics
Simulation, Math, Implementation, Array
Solved
No attempts yet

Problem

NN jumping beans stand in a row. Every second, one bean jumps. Your task is to determine the final arrangement of the beans after a given number of seconds.

To describe the process, give each bean a distinct uppercase letter and assume the beans initially stand in alphabetical order: A, B, C, and so on. For example, if N=4N = 4 the beans start in the order ABCD.

  • At second 1, bean A jumps and swaps places with the bean on its right (B). The row becomes BACD.
  • At second 2 it is B's turn. This time B swaps twice: first with A, then with C, giving ACBD.

In general, at second ss the leftmost bean that has jumped the fewest times jumps, and it performs ss swaps in a row. Each swap exchanges the jumping bean with the bean immediately to its right. When the jumping bean is already in the rightmost position, its swap to the right wraps around: the bean moves to the leftmost position and every other bean shifts one place to the right.

Continuing from ACBD, the leftmost least-jumped bean is C. Because this is second 3, C swaps three times: ACBD → ABCD → ABDC → CABD. At second 4 it is D's turn. At second 5 every bean has jumped exactly once, so the bean that jumps is again the one standing in the leftmost position.

Input

The program is tested on one or more test cases. Each test case is given on a single line containing an integer TT and a string SS, where 0<T<1090 < T < 10^9 is the number of seconds and SS is the initial arrangement of the beans. SS is a non-empty string made of distinct uppercase letters ('A'...'Z').

The last test case is followed by a line containing a single 0.

Output

For each test case, print one line:

k. S

where kk is the test case number (starting at 1) and SS is the arrangement of the beans after they have jumped for TT seconds.

Examples2

  1. Example 1

    Input
    3 ABCD
    13 ACM
    0
    
    Expected output
    1. CABD
    2. CAM
    
  2. Example 2

    Input
    1 AB
    0
    
    Expected output
    1. BA