#include<bits/stdc++.h>
using namespace std;
int t,n;
struct node{
long long val,b,depth,fa;
vector<int>son;
void clear(){val=b=depth=fa=0;son.clear();}
void input(){clear();cin>>val;}
void input2();
void b_b(){b=val-depth;}
void erase_this();
bool operator>(const node nd){return b>nd.b;}
void operator=(const node nd){val=nd.val,b=nd.b,depth=nd.depth;}
}a[100010];
priority_queue<node,vector<node>,greater<node>> pq;
void node::input2(){cin>>node::fa;a[node::fa].son.push_back(this-a);}
void node::erase_this(){a[node::fa].son.clear();}
void depth(int x){
if(x==1)a[x].depth=0;
for(int i:a[x].son){
a[i].depth=a[x].depth+1;
a[i].b_b();
depth(i);
}
}
template<typename key,typename value>
class map_sort_with_value{
unordered_map<key,value>mp;
vector<pair<key,value>>vct;
bool cmp(value vala,value valb){return vala>valb;}
value operator[](int __val){
for(auto i:mp){
vct.push_back(i);
}
sort(vct.begin(),vct.end(),cmp);
}
};
string solve(){
cin>>n;
for(int i=1;i<=n;i++)a[i].input();
for(int i=1;i<=n;i++)a[i].input2();
depth(1);
for(int i=1;i<=n;i++)pq.push(a[i]);
a[1]=pq.top();pq.pop();
int x[100010];
int cnt=0;
for(auto i:a[1].son){
x[++cnt]=i;
while(a[x[cnt]].son.size()){
x[cnt]=a[x[cnt]].son[0];
}
}
}
int main(){
for(cin>>t;t--;cout<<solve()<<"\n");
return 0;
}