#K61252. Rotate String

    ID: 31268 Type: Default 1000ms 256MiB

Rotate String

Rotate String

You are given a string S and an integer n. Your task is to rotate the string based on the value of n as follows:

  • If n is positive, rotate the string to the right by n positions.
  • If n is negative, rotate the string to the right by n \mod |S| positions (which is equivalent to a left rotation by |n| positions).

Formally, let \( |S| \) be the length of the string. Compute the effective shift as \[ n_{eff} = n \mod |S| \] and then construct the result as: \[ S' = S[(|S|-n_{eff}):] \; + \; S[0:(|S|-n_{eff})] \]

For example:

  • Input: S = "abcdef", n = 2 → Output: efabcd
  • Input: S = "abcdef", n = -2 → Output: cdefab
  • Input: S = "encyclopedia", n = 3 → Output: diaencyclope

You will be given multiple test cases. The first line of input contains an integer T denoting the number of test cases. Each of the subsequent T lines contains a test case where a string and an integer are provided separated by a space.

inputFormat

The input is read from stdin and has the following format:

T
S1 n1
S2 n2
... 
ST nT

Where T is the number of test cases, and each subsequent line contains a non-empty string S and an integer n separated by a space.

outputFormat

For each test case, output the rotated string on a new line on stdout.

## sample
3
abcdef 2
abcdef -2
encyclopedia 3
efabcd

cdefab diaencyclope