#include <bits/stdc++.h>
using namespace std;
 
// We'll compute the cycle decomposition of the original permutation p (1-indexed)
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int t; 
    cin >> t;
    while(t--){
        int n; 
        cin >> n;
        vector<int> p(n+1);
        for (int i = 1; i <= n; i++){
            cin >> p[i];
        }
        vector<int> d(n+1);
        for (int i = 1; i <= n; i++){
            cin >> d[i];
        }
 
        // --- Step 1. Compute cycle decomposition of p.
        // cycleId[i] = the id (1-indexed) of the cycle to which index i belongs.
        // cycleSize[id-1] will store the size of that cycle.
        vector<int> cycleId(n+1, 0);
        vector<int> cycleSize;
        int cid = 0;
        vector<bool> visited(n+1, false);
        for (int i = 1; i <= n; i++){
            if(!visited[i]){
                cid++;
                int cur = i;
                int cnt = 0;
                while(!visited[cur]){
                    visited[cur] = true;
                    cycleId[cur] = cid;
                    cnt++;
                    cur = p[cur];
                }
                cycleSize.push_back(cnt);
            }
        }
 
        // --- Step 2. Reverse simulation.
        // In the forward process we “remove” entries one by one.
        // In the reverse process we start with an all-zero array and “restore” entries in the reverse order.
        // In the final fixed array (the original permutation) the cost (i.e. minimal operations needed)
        // is 0. In the reverse simulation, if we have restored k indices then:
        //     cost = (n - k) + (for each cycle C: if (restored in C) < |C| then (restored in C) else 0).
        //
        // Let F[k] be the cost when exactly k indices are restored.
 
        vector<long long> F(n+1, 0);
        int activeCount = 0; // number of restored indices
        long long currCost = n; // initially, no index is restored so cost = n (all indices missing)
        F[0] = currCost;
 
        // For each cycle (by id) we track how many indices have been restored.
        vector<int> cntCycle(cid+1, 0);
 
        // Process “adding back” indices in the reverse order of removals.
        // d[1..n] gives the removal order; so we add in reverse: d[n], d[n-1], ..., d[1].
        for (int i = 1; i <= n; i++){
            int pos = d[n - i + 1]; // index we are restoring
            activeCount++;
            int cyc = cycleId[pos];
 
            // In cycle cyc, let old = (number restored before) and new = old+1.
            int oldCount = cntCycle[cyc];
            int newCount = oldCount + 1;
 
            // When we add one element:
            //    (i) the missing count (n - activeCount) decreases by 1,
            //    (ii) and the contribution for cycle cyc changes from:
            //         prev = (oldCount if oldCount < cycleSize else 0)
            //         to new  = (newCount if newCount < cycleSize else 0).
            currCost -= 1; // decrease in missing count
            int cycleTotal = cycleSize[cyc - 1];
            int prevContribution = (oldCount < cycleTotal ? oldCount : 0);
            int newContribution  = (newCount < cycleTotal ? newCount : 0);
            currCost += (newContribution - prevContribution);
 
            cntCycle[cyc] = newCount;
 
            F[activeCount] = currCost;
        }
 
        // --- Step 3. Answering queries.
        // In the reverse simulation, F[k] is the answer (minimal operations needed)
        // when k indices are restored. But note that if k indices are restored,
        // then (n-k) indices were removed in the forward process.
        // So for query i (i from 1 to n: i removals) the answer equals F[n-i].
        vector<long long> ans(n+1, 0);
        for (int i = 1; i <= n; i++){
            int ac = n - i; // number of restored indices in the corresponding reverse state
            ans[i] = F[ac];
        }
 
        // Output the answer for the current test case.
        // (We output n numbers: the i-th is the answer after i removals.)
        for (int i = 1; i <= n; i++){
            cout << ans[i] << (i==n ? "\n" : " ");
        }
    }
    return 0;
}
