//teja349
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
using namespace std;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<ll,pll>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
ll prim =(727999983);
//std::ios::sync_with_stdio(false);
vector<vi> adj1(123456),adj2(123456);
ll cntable[123456],subtree[123456],ans2[123456];
int dfs2(int cur,int par){
vl vec1;
ll i;
subtree[cur]=1;
rep(i,adj2[cur].size()){
dfs2(adj2[cur][i],cur);
subtree[cur]+=subtree[adj2[cur][i]];
}
if(adj2[cur].empty()){
ans2[cur]=1997;
return 0;
}
rep(i,adj2[cur].size()){
vec1.pb(ans2[adj2[cur][i]]);
}
sort(vec1.begin(),vec1.end());
ll haha=0;
ll val=173;
rep(i,vec1.size()){
haha+=vec1[i]*val;
val*=prim;
haha*=vec1[i];
}
ans2[cur]=haha;
return 0;
}
int dfs1(int cur,int par){
vl vec1;
ll i;
set< tri > vec2;
set< tri >::iterator it;
subtree[cur]=1;
rep(i,adj1[cur].size()){
dfs1(adj1[cur][i],cur);
subtree[cur]+=subtree[adj1[cur][i]];
}
if(adj1[cur].empty()){
ans2[cur]=1997;
cntable[cur]=1;
return 0;
}
rep(i,adj1[cur].size()){
vec1.pb(ans2[adj1[cur][i]]);
vec2.insert(mp(cntable[adj1[cur][i]],mp(subtree[adj1[cur][i]],ans2[adj1[cur][i]])));
}
sort(vec1.begin(),vec1.end());
ll haha=0;
ll val=173;
rep(i,vec1.size()){
haha+=vec1[i]*val;
val*=prim;
haha*=vec1[i];
}
ans2[cur]=haha;
cntable[cur]=0;
for(it=vec2.begin();it!=vec2.end();it++){
cntable[cur]+=it->ff;
}
return 0;
}
int main(){
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
int n1,n2;
cin>>n1>>n2;
int i,u;
rep(i,n1){
adj1[i].clear();
}
rep(i,n2){
adj2[i].clear();
}
f(i,1,n1){
cin>>u;
u--;
adj1[u].pb(i);
//adj1[i].pb(u);
}
f(i,1,n2){
cin>>u;
u--;
adj2[u].pb(i);
//adj2[i].pb(u);
}
dfs2(0,-1);
set<pll> seti;
rep(i,n2){
seti.insert(mp(subtree[i],ans2[i]));
}
ll distinct=seti.size();
dfs1(0,-1);
distinct*=cntable[0];
cout<<distinct<<"\n";
}
return 0;
}
Ly90ZWphMzQ5CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGNsaW1pdHM+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8aW9tYW5pcD4gCi8vc2V0YmFzZSAtIGNvdXQgPDwgc2V0YmFzZSAoMTYpOyBjb3V0IDw8IDEwMCA8PCBlbmRsOyBQcmludHMgNjQKLy9zZXRmaWx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