#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)
#define NN 100010
ll s[NN];
ll ans[NN];
vl adj[NN];
struct info {
ll c[2] = {}; // reds, blacks
vl dp; // dp vector, dp[i] = minimum amount to get dp[i]
};
info dfs(ll i) {
if (!adj[i].size()) {
ans[i] = 0;
info ret;
ret.dp.assign(2, 0);
ret.dp[!s[i]] = 1;
return ret;
} else if (adj[i].size() == 1) {
ll only = adj[i][0];
auto ret = move(dfs(only));
ans[i] = ans[only];
ret.c[s[i]]++;
return ret;
}
vector<info> children;
ll dep = 1e9;
for (auto x: adj[i]) {
children.push_back(dfs(x));
auto &bck = children.back();
dep = min(dep, bck.c[0] + bck.c[1] + ssize(bck.dp));
}
info here;
here.dp.resize(dep);
for (const auto &[c, dp]: children) {
ll potentialOffset = c[0] + c[1];
vl ndp(dep, 1e9);
F(i, 0, ssize(dp)) {
F(j, 0, potentialOffset+1) if (i + j < dep) {
ndp[i + j] = min(ndp[i + j], dp[i] + abs(c[1] - j));
}
}
F(i, 0, dep) here.dp[i] += ndp[i];
}
here.dp.push_back(1e9); // cost of making everything black has to be included here
// simulate shifting up/down depending on red/black
{
vl move[2] {here.dp, here.dp};
move[1].insert(move[1].begin(), 1e9); // shift everything up by one
for (auto &x: move[!s[i]]) ++x;
F(i, 0, ssize(here.dp)) here.dp[i] = min(move[0][i], move[1][i]);
}
ans[i] = *min_element(A(here.dp));
return here;
}
int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(20);
G(t) while(t--) {
G(n)
GS(str)
F(i, 0, n) s[i] = str[i] - '0';
F(i, 0, n) adj[i].clear();
F(i, 1, n) {
G(x) adj[x-1].push_back(i);
}
dfs(0);
F(i, 0, n) cout << ans[i] << " "; cout << '\n';
}
}