for _ in range(int(input())):
n = int(input())
parents = list(map(int, input().split()))
perm = list(map(int, input().split()))
root = [num for index, num in enumerate(parents, start=1) if num == index][0]
curr = 0
dist = [-1] * n
dist[root - 1] = 0
if perm[0] != root:
print(-1)
continue
ptotal = [-1] * n
ptotal[root - 1] = 0
for num in perm:
if dist[parents[num - 1] - 1] == -1:
print(-1)
break
dist[num - 1] = curr - ptotal[parents[num - 1] - 1]
ptotal[num - 1] = curr
curr += 1
else:
print(*dist)
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
#define rep(i,j,n) for(long long i=j;i<n;i++)
#define rrep(i,j) for(ll i=j;i>=0;i--)
#define DEB(x) cout<<"##"<<x<<"##"<<endl
#define see(x) cout<<x<<"\n";
#define ll long long
#define pb push_back
#define ft first
#define se second
#define vvi vector<vector<int>>
#define vi vector<int>
#define vll vector<ll>
#define all(v) v.begin(),v.end()
#pragma GCC optimize "trapv"// Detects overflow.....(RE)
template <typename Type>
istream &operator>>(istream &in, vector<Type> &vec) {
int n = vec.size();
for (int i = 0; i < n; i++)
in >> vec[i];
return in;
}
template <typename Type>
ostream &operator<<(ostream &out, vector<Type> &vec) {
for (auto val : vec)
out << val << " ";
return out;
}
void reduceFraction(ll &x, ll &y)
{
int d;
d = __gcd(x, y);
x = x / d;
y = y / d;
}
string str(int i) {
return i < 0 ? "" : str((i / 26) - 1) + (char)(97 + i % 26);
}
const int MX = 6e6 + 5;
// ll fact[MX];
// ll ifact[MX];//Inverse of factorial
ll bin_power(ll a,ll b,ll mod){
ll res=1;
while (b){
if (b&1)
res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
// ll ncr(ll n,ll r){
// if(r>n)
// return 0;
// return (fact[n]*ifact[n-r]%mod)*ifact[r]%mod;
// }
/*
fact[0]=1;
ifact[0]=1;
rep(i,1,MX-1){
fact[i]=(i*fact[i-1])%mod;
ifact[i]=bin_power(fact[i],mod-2,mod);
}
*/
#define int long long
// const int MAX_SIZE = 2800001;
// vector<int>isprime(MAX_SIZE , true);
// vector<int> idx(MAX_SIZE);
// vector<int> prime;
// vector<int>SPF(MAX_SIZE);//SPF[i]=smallest prime factor of number i
// void manipulated_seive(int N) {
// isprime[0] = isprime[1] = false ;
// for (int i = 2; i < N ; i++) {
// if (isprime[i]) {
// prime.push_back(i);
// SPF[i] = i;
// }
// for (int j = 0; j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) {
// isprime[i * prime[j]] = false;
// SPF[i * prime[j]] = prime[j] ;
// }
// }
// for (int i = 0; i < (int)prime.size(); i++) {
// idx[prime[i]] = i + 1;
// }
// }
// //With sieve
// set<int> primeFactors(int n) {
// set<int> factors;
// while (n > 1) {
// factors.insert(SPF[n]);
// n /= SPF[n];
// }
// if(n>1)factors.insert(n);
// return factors;
// }
// //Without sieve
// bool isprime_n(ll n){
// for(ll i=2;i*i<=n;i++){
// if(n%i==0)return false;
// }
// return true;
// }
// vector<int>divisors(1e6,0);
// void precompute_divisors(){
// for(int i=1;i<=1e6;i++){
// for(int j=i;j<=1e6;j+=i){
// divisors[j]++;
// }
// }
// }
// Try to do dry run on large no. of testcases (If not provided make some big tc)
// to observe the pattern.
// Edge cases : All elements 0, all -ve .........
// // ********************Think mathematically**************************
// class Pair{
// public:
// int x,y,d;
// Pair(int x,int y,int d){
// this->x=x;
// this->y=y;
// this->d=d;
// }
// };
int d1[8][2]={{1,2},{2,1},{-1,2},{-2,1},{1,-2},{2,-1},{-1,-2},{-2,-1}};
int d[4][2]={{0,-1},{-1,0},{1,0},{0,1}};
//count dearrangements
ll countDer(int n)
{
ll der[n + 1] = {0};
der[1] = 0;
der[2] = 1;
for (int i = 3; i <= n; ++i)
der[i] = (i - 1) * (der[i - 1] +
der[i - 2]);
return der[n];
}
// finding ncr of larger numbers upto 1000 iteratively
ll ncr1(int n , int r){
if(n<0 || r<0 || r>n) return 0;
if(r==0)return 1;
if(r==1)return n;
ll ans = 1 ;
ll k=1;
for(int i=n ; i>n-r ;i--){
ans *= i;
ans/=k;
k++;
}
return ans;
}
long long lcm(long long a,long long b){
return (a*b)/(__gcd(a,b));
}
set<int> find_factors(int x){
set<int>ans;
for(int i=2;i*i<=x;i++){
if(x%i==0){
ans.insert(i);
if(x/i!=i){
ans.insert(x/i);
}
}
}
return ans;
}
vector<int>prime_factor(int n){
vector<int>ans;
while(n%2==0){
n/=2;
ans.pb(2);
}
for(int i=3;i*i<=n;i++){
while(n%i==0){
ans.pb(i);
n/=i;
}
}
if(n>1)ans.pb(n);
return ans;
}
void solve(){
int n;
cin>>n;
vi par(n),p(n);
cin>>par>>p;
vi dist(n+1,-1);
vi path(n+1,0);
int root=-1;
for(int i=0;i<n;i++){
if(i+1==par[i]){
root=i+1;
}
}
if(p[0]!=root){
see(-1);
return;
}
dist[p[0]]=0;
int mn=-1;
for(int i=1;i<n;i++){
if(dist[par[p[i]-1]]==-1){
see(-1);
return;
}
int x=p[i];
int val=0;
val=path[par[p[i]-1]];
// while(par[x-1]!=x){
// x=par[x-1];
// val+=dist[x];
// }
// dist[p[i]]=max(dist[par[p[i]-1]]+1,mn+1);
if(val+1>mn){
dist[p[i]]=dist[par[p[i]-1]]+1;
}else{
dist[p[i]]=(mn+1)-val;
}
path[p[i]]=dist[p[i]]+val;
mn=max(mn,dist[p[i]]+val);
}
for(int i=1;i<=n;i++){
cout<<dist[i]<<" ";
}
see("");
}
int32_t main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
cin>>t;
// manipulated_seive(sqrt(1e9+50));
while(t--){
solve();
}
return 0;
}
996A - Hit the Lottery | MSNSADM1 Football |
MATCHES Playing with Matches | HRDSEQ Hard Sequence |
DRCHEF Doctor Chef | 559. Maximum Depth of N-ary Tree |
821. Shortest Distance to a Character | 1441. Build an Array With Stack Operations |
1356. Sort Integers by The Number of 1 Bits | 922. Sort Array By Parity II |
344. Reverse String | 1047. Remove All Adjacent Duplicates In String |
977. Squares of a Sorted Array | 852. Peak Index in a Mountain Array |
461. Hamming Distance | 1748. Sum of Unique Elements |
897. Increasing Order Search Tree | 905. Sort Array By Parity |
1351. Count Negative Numbers in a Sorted Matrix | 617. Merge Two Binary Trees |
1450. Number of Students Doing Homework at a Given Time | 700. Search in a Binary Search Tree |
590. N-ary Tree Postorder Traversal | 589. N-ary Tree Preorder Traversal |
1299. Replace Elements with Greatest Element on Right Side | 1768. Merge Strings Alternately |
561. Array Partition I | 1374. Generate a String With Characters That Have Odd Counts |
1822. Sign of the Product of an Array | 1464. Maximum Product of Two Elements in an Array |