```go
package main
import (
"bufio"
"os"
"strconv"
)
func main() {
const maxBufSize = 2 * 1024 * 1024
scanner := bufio.NewScanner(os.Stdin)
buf := make([]byte, maxBufSize)
scanner.Buffer(buf, maxBufSize)
scanner.Split(bufio.ScanWords)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
if !scanner.Scan() {
return
}
t, _ := strconv.Atoi(scanner.Text())
for i := 0; i < t; i++ {
if !scanner.Scan() {
break
}
n, _ := strconv.Atoi(scanner.Text())
p := make([]int, n)
for j := 0; j < n; j++ {
scanner.Scan()
val, _ := strconv.Atoi(scanner.Text())
p[j] = val - 1
}
scanner.Scan()
s := scanner.Text()
ans := make([]int, n)
visited := make([]bool, n)
for j := 0; j < n; j++ {
if !visited[j] {
var cycle []int
curr := j
for !visited[curr] {
visited[curr] = true
cycle = append(cycle, curr)
curr = p[curr]
}
cnt := 0
for _, node := range cycle {
if s[node] == '0' {
cnt++
}
}
for _, node := range cycle {
ans[node] = cnt
}
}
}
for j := 0; j < n; j++ {
if j > 0 {
writer.WriteByte(' ')
}
writer.WriteString(strconv.Itoa(ans[j]))
}
writer.WriteByte('\n')
}
}
```