#P0106. Sakurako's Hobby
Sakurako's Hobby
Problem
For a certain permutation Sakurako calls an integer reachable from an integer if it is possible to make equal to by assigning a certain number of times.
If then, for example, is reachable from because: Now so is reachablefrom
Each number in the permutation is colored either black or white.
Sakurako defines the function as the number of black integers that are reachable from
Sakurako is interested in for each but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this.
permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation (the number appears twice in the array), and is also not a permutation ( but the array contains ).
Input
The first line contains a single integer — the number of test cases.
The first line of each test case contains a single integer — the number of elements in the array.
The second line of each test case contains integers — the elements of the permutation.
The third line of each test case contains a string of length consisting of '0' and '1'. If then the number is colored black; if then the number is colored white.
It is guaranteed that the sum of across all test cases does not exceed .
Output
For each test case, output integers
Example : in
5
1
1
0
5
1 2 4 5 3
10101
5
5 4 1 3 2
10011
6
3 5 6 1 2 4
010000
6
1 2 3 4 5 6
100110
Example : out
1
0 1 1 1 1
2 2 2 2 2
4 1 4 4 1 4
0 1 1 0 0 1
相关
在下列比赛中: