Capeta is working part-time for an animal shipping company. He needs to pick up animals from various zoos and drop them to other zoos. The company ships four kinds of animals: elephants, dogs, cats, and mice.
There are zoos, numbered to . Also, there are animals. For each animal , Capeta knows its type (E
for elephant, D
for dog, C
for cat and M
for mouse), source zoo where Capeta has to pick it up from, and destination zoo where Capeta needs to deliver it to.
Capeta is given a truck with a huge capacity where animals can easily fit. He is also given additional instructions:
He must visit the zoos in increasing order. He also cannot skip zoos.
Dogs are scared of elephants, so he is not allowed to bring them together at the same time.
- Cats are scared of dogs, so he is not allowed to bring them together at the same time.
- Mice are scared of cats, so he is not allowed to bring them together at the same time.
- Elephants are scared of mice, so he is not allowed to bring them together at the same time.
Also, loading and unloading animals are complicated, so once an animal is loaded onto the truck, that animal will only be unloaded at its destination.
Because of these reasons, Capeta might not be able to transport all animals. He will need to ignore some animals. Which ones? The company decided to leave that decision for Capeta. He is asked to prepare a report and present it at a board meeting of the company.
Capeta needs to report the minimum number of zoos that must be reached so that she is able to transport animals, for each from to .
Complete the function minimumZooNumbers
and return an integer array where the integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport animals, or if it is impossible to transport animals.
He is good at driving, but not so much at planning. Hence, he needs your help.
Input Format
The first line contains a single integer , the number of test cases.
Each test case consists of four lines. The first line contains two space-separated integers and . The second line contains space-separated characters . The third line contains space-separated integers . The fourth line contains space-separated integers .
, and are the details for the th animal, as described in the problem statement.
Constraints
- is either
E
,D
,C
orM
Subtasks
- For of the total score,
Output Format
For each case, print a single line containing space-separated integers, where the integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport animals. If it is not possible to transport animals at all, then put instead.
Sample Input 0
2
10 3
E D C
4 1 4
7 5 8
10 6
E D C M E D
1 1 1 2 9 7
2 2 2 4 10 10
Sample Output 0
5 8 -1
2 2 4 10 -1 -1
Explanation 0
First Test Case
Capeta can transport one animal by traveling up to zoo number . Just drop the dog there. Next, in order to transport animals (elephant and cat), Capeta has to go up to zoo number .
Second Test Case
- Animal: Drop the elephant to zoo .
- Animal: Drop the elephant and cat to zoo .
- Animal: Drop the elephant and cat to zoo . Then drop the mouse to zoo .
- Animal: Drop the elephant and cat to zoo . Then drop the mouse to zoo . Finally, drop either the elephant or the dog to .
- It is impossible to transport or animals.