#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define pb push_back
#define fi first
#define se second
#define ll long long
#define sz(x) (int)(x).size()
#define pll pair<long long,long long>
#define pii pair<int,int>
#define pq priority_queue
const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-9,e=exp(1),PI=acos(-1.);
vector<ll>G[N];
ll a[N],x;
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int T,n;
cin>>T;
while(T--)
{
cin>>n;
for(int i=2; i<=n; i++)cin>>a[i];
for(int i=1; i<=n; i++)cin>>x,G[a[i]].push_back(x);
ll ma=0,mi=0,minx=1e9,maxx=-1e9;
for(int i=0; i<=n; i++)
{
sort(G[i].begin(),G[i].end());
int len=G[i].size();
if(len)
{
ma=max(ma,ma+G[i][len-1]),mi=min(mi,mi+G[i][0]);
if(len>1)minx=min(minx,G[i][1]),maxx=max(maxx,G[i][len-2]);
}
}
mi=min({mi,mi+minx,0LL}),ma=max({ma,maxx+ma,0LL});
cout<<ma<<" "<<mi<<"\n";
for(int i=0; i<=n; i++)G[i].clear();
}
return 0;
}