#P0106. Sakurako's Hobby

Sakurako's Hobby

Problem

For a certain permutation pp∗ Sakurako calls an integer jj reachable from an integer ii if it is possible to make ii equal to jj by assigning i=pii=pi a certain number of times.

If p=[3,5,6,1,2,4],p=[3,5,6,1,2,4], then, for example,44 is reachable from 1,1, because: i=1i=p1=3i=p3=6i=p6=4.i=1 →i=p1=3→i=p3=6→i=p6=4. Now i=4,i=4, so 44 is reachablefrom 1.1.

Each number in the permutation is colored either black or white.

Sakurako defines the function F(i)F(i) as the number of black integers that are reachable from i.i.

Sakurako is interested in F(i)F(i) for each 1in,1≤i≤n, but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this.

A∗A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (the number 22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation ( n=3,n=3, but the array contains 44 ).

Input

The first line contains a single integer tt (1t104)(1≤t≤10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1n105)(1≤n≤10^5) — the number of elements in the array.

The second line of each test case contains nn integers p1,p2,,pn(1pin)p1,p2,…,pn (1≤pi≤n) — the elements of the permutation.

The third line of each test case contains a string ss of length n,n, consisting of '0' and '1'. If si=0,si=0, then the number pipi is colored black; if si=1,si=1, then the number pipi is colored white.

It is guaranteed that the sum of nn across all test cases does not exceed 51055⋅10^5 .

Output

For each test case, output nn integers F(1),F(2),,F(n).F(1),F(2),…,F(n).

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