Hide

Problem B
Integer Lists

The programming language Better And Portable Code (BAPC) is a language for working with lists of integers. The language has two built-in functions: ‘R’ (reverse) and ‘D’ (drop).

The function ‘R’ reverses its input list, and ’D’ drops the first element of its input and returns the rest, or gives an error in case its input is an empty list. To get more advanced behavior, functions can be composed: “AB" is the function that first applies ‘A’ to its input and then ‘B’ to the resulting list. For example, “RDD" is a function that reverses a list and then drops the first two elements.

Unfortunately, our BAPC interpreter has bit rotted, so we ask you to write a new one. Given a BAPC program and its input, return its output or “error" in case ‘D’ is applied to an empty list. Lists are represented as the character ‘[’ followed by a comma-separated list of integers followed by the character ‘]’. Notice that the input and output lists can be quite long.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with a string $p$ $(1\le \text {length}(p) \le 100\, 000)$: a BAPC program, consisting of the characters ‘R’ and ‘D’.

  • one line with an integer $n$ $(0\le n \le 100\, 000)$: the number of elements in the input.

  • one line with a list of $n$ integers in the form $[x_1, \ldots ,x_ n]$ $(1\le x_ i \le 100)$: the input list.

Output

Per test case:

  • one line with the resulting integer list or “error" in case of an error.

Sample Input 1 Sample Output 1
4
RDD
4
[1,2,3,4]
DD
1
[42]
RRD
6
[1,1,2,3,5,8]
D
0
[]
[2,1]
error
[1,2,3,5,8]
error

Please log in to submit a solution to this problem

Log in