#include<bits/stdc++.h>
#define ll long long
#define endl “\n”
#define pb push_back
#define fr(a,b) for(int i = a; i < b; i++)
#define rep(i,a,b) for(int i = a; i < b; i++)
#define MOD 1000000007
#define inf (1LL<<60)
#define all(x) (x).begin(), (x).end()
#define prDouble(x) cout << fixed << setprecision(10) << x
#define triplet pair<ll,pair<ll,ll>>
#define goog(tno) cout << “Case #” << tno <<": "
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define read(x) int x; cin >> x
using namespace std;
ll mod_add(ll a, ll b, ll m)
{
a = a % m;
b = b % m;
return (((a + b) % m) + m) % m;
}
ll mod_mul(ll a, ll b, ll m)
{
a = a % m;
b = b % m;
return (((a * b) % m) + m) % m;
}
ll mod_sub(ll a, ll b, ll m)
{
a = a % m;
b = b % m;
return (((a - b) % m) + m) % m;
}
int poweru(long long x, long long int y, long long p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns factorial of n
ll gcd(int a, int b)
{
if (b == 0)
{
return (a);
}
return (gcd(b, a % b));
}
ll pw(ll a, ll b)
{
ll t = 1;
for (; b > 0; a = (a * a) % MOD, b >>= 1)
if (b & 1)
t = (t * a) % MOD;
return t;
}
ll findPow(ll i, ll p, ll m)
{
if (p == 0)
{
return (1);
}
int res = findPow(i, p / 2, m);
if (p % 2 == 0)
{
return ((res * res) % m);
}
else
{
return ((((res * res) % m) * i) % m);
}
}
ll fact(ll n);
ll nCr(ll n, ll r)
{
return fact(n) / (fact® * fact(n - r));
}
ll fact(ll n)
{
ll res = 1;
for (ll i = 2; i <= n; i++)
res = res * i;
return res;
}
int modInverse(int val, int m)
{
return (findPow(val, m - 2, m));
}
bool cmp(pair<int, int> &a,
pair<int, int> &b)
{
return a.second < b.second;
}
ll sumDigits(ll n)
{
ll sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
bool isPalindrome(string str)
{
int low = 0;
int high = str.length() - 1;
while (low < high)
{
// if a mismatch happens
if (str[low] != str[high])
{
return false;
}
low++;
high--;
}
return true;
}
bool isPowerOfTwo(int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x & (x - 1)));
}
/------------------------------------------------------------------------------------------------/
ll ans= 0;
vector<vector> adj(100001);
ll maxx[100001]; ll minn[100001];
ll W[100001] ; ll P[100001];
int dfs (ll i, ll parent)
{
for( auto it : adj[i])
{
dfs(it,i);
if(it != parent)
{
maxx[i]= max(maxx[i], maxx[it]);
minn[i]= max(minn[i], minn[it]);
}
}
maxx[i]= max(maxx[i], W[i]);
minn[i]= min( minn[i], W[i]);
ans= max( ans, abs( W[i]- maxx[i]) );
ans= max( ans, abs( W[i]- minn[i]) );
return ans;
}
void solve()
{
ll n;
cin >>n;
ll root=0;
for(ll i; i <n ; i++)
{
maxx[i]= INT_MIN;
minn[i]= INT_MAX;
}
for(ll i; i<n ; i++)
{
cin>> W[i];
}
for(ll i; i <n ; i++)
{
cin>> P[i];
if(P[i]== -1)
{
root= i;
}
else
adj[P[i]].push_back(i);
}
ans= dfs(root,0); // initial root taken as 0
cout<< ans<< “\n”;
}
void init_code(){
fast_io;
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
}
int main() {
fast_io;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1; cin >> t;
while(t–){
solve();
}
return 0;
}