#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 1 << 17;
string s;
VI g[N];
VI dp[N];
int c0[N], c1[N];
int ans[N];
VI transform(VI a, int to, int m)
{
VI b(m, N);
deque<PII> dq1, dq2;
FOR(k, 0, m)
{
int i = k - c1[to];
if (i >= 0 && i < SZ(a))
{
int val = a[i] - i;
while (!dq1.empty() && dq1.back().S >= val)
dq1.pop_back();
dq1.PB({i, val});
}
if (k < SZ(a))
{
int val = a[k] + k;
while (!dq2.empty() && dq2.back().S >= val)
dq2.pop_back();
dq2.PB({k, val});
}
if (!dq1.empty())
b[k] = min(b[k], k - c1[to] + dq1.front().S);
if (!dq2.empty())
b[k] = min(b[k], c1[to] - k + dq2.front().S);
if (!dq1.empty() && k - c1[to] - c0[to] == dq1.front().F)
dq1.pop_front();
if (!dq2.empty() && k - c1[to] == dq2.front().F)
dq2.pop_front();
}
return b;
}
int dfs(int v)
{
if (g[v].empty())
{
c0[v] = s[v] == '0';
c1[v] = s[v] == '1';
dp[v] = {0};
ans[v] = 0;
return v;
}
if (SZ(g[v]) == 1)
{
int to = g[v][0];
int j = dfs(to);
c0[v] = c0[to] + (s[v] == '0');
c1[v] = c1[to] + (s[v] == '1');
ans[v] = ans[to];
return j;
}
int m = N;
VI indices;
for (int to : g[v])
{
indices.PB(dfs(to));
m = min(m, SZ(dp[indices.back()]) + c0[to] + c1[to]);
}
FOR(i, 0, SZ(g[v]))
{
int to = g[v][i];
int j = indices[i];
while (SZ(dp[j]) > m)
dp[j].pop_back();
dp[j] = transform(dp[j], to, m);
}
VI dpSons(m);
for (int j : indices)
FOR(k, 0, SZ(dp[j]))
dpSons[k] += dp[j][k];
dp[v].resize(m + 1, N);
FOR(k, 0, m + 1)
{
if (k < m)
dp[v][k] = dpSons[k] + (s[v] == '1');
if (k > 0)
dp[v][k] = min(dp[v][k], dpSons[k - 1] + (s[v] == '0'));
}
ans[v] = *min_element(ALL(dp[v]));
return v;
}
void solve()
{
int n;
cin >> n >> s;
FOR(i, 1, n)
{
int p;
cin >> p;
p--;
g[p].PB(i);
}
dfs(0);
FOR(i, 0, n)
{
if (i > 0)
cout << " ";
cout << ans[i];
}
cout << "\n";
FOR(i, 0, n)
{
g[i].clear();
dp[i].clear();
c0[i] = c1[i] = 0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}