#CDC12F. Forbidden Machine

Forbidden Machine

Finally Ronny could make it to the safe place, he left his family there and decided to go to this new planet and talk to their King. Ronny know that he can’t do this by himself, so he called every army of the Earth to have a backup in case that things turn out bad.

They made it to the new planet, and realize that it was like only rainbows and teddy bears, and just at the entrance there was a sign that said: ”Welcome to Rainbowland”. Ronny and his army went through this planet directly to their castle. At the entrance, a guard wearing a hat colored with rainbow colors, stopped Ronny. He said that Ronny could pass if and only if he could solve a puzzle: The Forbidden Machine Puzzle.

This puzzle consists on a machine that takes strings, and then says if the strings are correct. A string is considered correct if it follows the rules of the machine. This rules consists on a sequence of state transitions. Each transition tells the machine that if it was in a state X, it can go to a state Y with a character Z on the string. The machine starts reading from the first position of the string, and each transition move the string one position to the left, so the next position to read it’s the second one. It is important to add that the machine always start on an initial state and a string follows the rules of the machine if and only if this can made it to a final state of the machine, and the string has been read completely. Ronny has a hard time understanding this machine, so he would like you to simulate it, in order to have the correct answer for the guard.

Please note that a single state-character combination can lead to several different states.

Input

The first line contains an integer T, which specifies the number of test cases. Then, will follow the descriptions of T test cases.

Each test case will contain a line with 4 integers, N, M, F and Q. N is the number of states of the machine, M is the number of transitions, F is the number of final states of the machine, and Q it’s the initial state. Then F lines will follow with a single integer E on it, representing that the state E it’s a final state. Then M lines will follow, with 2 integers I and J and a character C; this represents that there exists a transition that goes from state I to state J reading the character C. Then will be a line with an integer S, that indicate the number of strings to process, and S lines will follow, each one with a string A that has to be evaluated by Ronny.

Output

For each input case you must "Scenario #i:" where i is the number of the test case that you are evaluating (Starting by 1). Then S lines, with the string and the answer of the machine for that string, if the string is correct, you should output "Accepted", and if it’s not, you should output "Rejected" (Quotes for clarity).

Sample

Input
2
4 8 1 0
0
0 1 a
1 0 a
1 2 b
2 1 b
0 3 b
3 0 b
3 2 a
2 3 a
3
ababbaa
abababab
aaaabbbb
6 8 2 0
2
5
0 0 a
0 1 a
1 1 b
1 2 c
1 3 c
3 4 d
4 4 d
4 5 d
5
aaabcccc
aabbbbcdc
abbcdddcc
acdddddd
abc

Output Scenario #1: ababbaa Rejected abababab Accepted aaaabbbb Accepted Scenario #2: aaabcccc Rejected aabbbbcdc Rejected abbcdddcc Rejected acdddddd Accepted abc Accepted

</p>

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 100
  • 1 ≤ F, Q, I, J, E ≤ N
  • 1 ≤ S ≤ 100
  • 1 ≤ |A| ≤ 1000