#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define ls rt<<1
#define rs rt<<1|1
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define Inf 0x3f3f3f3f
#define debug(a) cout<<#a<<"="<<a<<"\n"
using namespace std;
inline void ckmin(int& a,int b){if(a>b) a=b;}
inline void ckmax(int& a,int b){if(a<b) a=b;}
const int mod=998244353;
const int maxn=2e5+5;
vector<int>g[maxn]; 
int n,k;
int d[maxn],ans[maxn],num[maxn];
vector<pii>qu[maxn];
void dfs(int x,int fa){
	d[x]=d[fa]+1;
	for(pii i:qu[x]){
		ans[i.se]=-num[i.fi];
	}
	++num[d[x]];
	for(int i:g[x]){
		dfs(i,x);
	}
	for(pii i:qu[x]){
		ans[i.se]+=num[i.fi];
	}
}
int main()
{	
	scanf("%d",&n);
	for(int x,i=2;i<=n;i++){
		scanf("%d",&x);
		g[x].push_back(i);
	} 
	d[0]=-1;
	int q,u,c;
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d%d",&u,&c);
		qu[u].push_back({c,i});
	}
	dfs(1,0);
	for(int i=1;i<=q;i++){
		printf("%d\n",ans[i]);
	}
	return 0;
}

#include <bits/stdc++.h>
const int N = 200005;
int T, n; char A[N], B[N], C[N];
int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n); n *= 2;
		scanf("%s%s%s", A, B, C);
		int cnt1 = (A[0] == '1') + (B[0] == '1') + (C[0] == '1');
		int cnt2 = (A[n - 1] == '1') + (B[n - 1] == '1') + (C[n - 1] == '1');
		if (cnt1 == 3 || cnt2 == 3) {
			if (cnt1 == 3) printf("1");
			for (int i = 1; i <= n; i++) printf("0");
			if (cnt1 != 3) printf("1");
		}
		else if (cnt1 == 0 || cnt2 == 0) {
			if (cnt1 == 0) printf("0");
			for (int i = 1; i <= n; i++) printf("1");
			if (cnt1 != 0) printf("0");
		}
		else if (cnt1 != cnt2) {
			int ok = cnt1 > 1;
			printf("%d", ok);
			for (int i = 1; i <= n/2; i++) printf("%d", ok ^ 1);
			for (int i = 1; i <= n/2; i++) printf("%d", ok);
		} else {
			int ok = cnt1 > 1;
			for (int i = 1; i <= n/2; i++) printf("%d", ok);
			for (int i = 1; i <= n/2; i++) printf("%d", ok ^ 1);
			printf("%d", ok);
		}
		puts("");
	}
	return 0;
}