#P1932C. LR-remainders
LR-remainders
Description
You are given an array of length , a positive integer , and a string of commands of length . Each command is either the character 'L' or the character 'R'.
Process all commands in the order they are written in the string . Processing a command is done as follows:
- First, output the remainder of the product of all elements of the array when divided by .
- Then, if the command is 'L', remove the leftmost element from the array , if the command is 'R', remove the rightmost element from the array .
Note that after each move, the length of the array decreases by , and after processing all commands, it will be empty.
Write a program that will process all commands in the order they are written in the string (from left to right).
The first line contains an integer () — the number of test cases in the input. Then descriptions of test cases follow.
Each test case of the input is given by three lines.
The first line contains two integers and () — the initial length of the array and the value to take the remainder by.
The second line contains integers () — the elements of the array .
The third line contains a string consisting of characters 'L' and 'R'.
It is guaranteed that the sum of the values of for all test cases in a test does not exceed .
For each test case, output integers , where is the remainder when dividing the product of all elements of the current state of the array by at the beginning of the execution of the -th command.
Input
The first line contains an integer () — the number of test cases in the input. Then descriptions of test cases follow.
Each test case of the input is given by three lines.
The first line contains two integers and () — the initial length of the array and the value to take the remainder by.
The second line contains integers () — the elements of the array .
The third line contains a string consisting of characters 'L' and 'R'.
It is guaranteed that the sum of the values of for all test cases in a test does not exceed .
Output
For each test case, output integers , where is the remainder when dividing the product of all elements of the current state of the array by at the beginning of the execution of the -th command.
Note
In the first test case of the example:
- ;
- , so we remove the first element and get the array ;
- ;
- , so we remove the last element and get the array ;
- ;
- , so we remove the last element and get the array ;
- ;
- , so we remove the first element and get an empty array.