#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define mset multiset
#define pq priority_queue
#define ull unsigned long long
#define i128 __int128
const int maxn=1000+10;
int n,m,res,cnt,vis[maxn],fl[maxn];
vector<int> g[maxn];
map<string,int> mp;
int id(string str){
// cout<<str<<" "<<dep<<endl;
if(!mp[str]) mp[str]=++cnt;
return mp[str];
}
void dfs(int u){
bool fl=1;
for(int v:g[u]) fl=0,dfs(v);
if(fl) return ;
for(int v:g[u]) if(vis[v]==-1) return vis[u]=-1,void();
vis[u]=1;
}
void Dfs(int u){
if(fl[u]) return ;
if(vis[u]==1&&u!=1) return fl[u]=1,res++,void();
for(int v:g[u]) Dfs(v);
}
void solve(){
cin>>n>>m,cnt=1,res=0,mp.clear();
for(int i=1;i<=n;i++){
string s;
cin>>s;
string t,lst="-";
for(char c:s){
if(c=='/'){
if(lst!="-") g[id(lst)].pb(id(t));
else g[1].pb(id(t));
// cout<<lst<<" "<<t<<endl;
// cout<<id(t)<<endl;
lst=t;
}
t+=c;
}
if(lst!="-") g[id(lst)].pb(id(t));
else g[1].pb(id(t));
// cout<<lst<<" "<<t<<endl;
vis[id(t)]=1;
}
for(int i=1;i<=m;i++){
string s;
cin>>s;
string t,lst="-";
for(char c:s){
if(c=='/'){
dep++;
if(lst!="-") g[id(lst)].pb(id(t));
else g[1].pb(id(t));
lst=t;
}
t+=c;
}
if(lst!="-") g[id(lst)].pb(id(t));
else g[1].pb(id(t));
// cout<<t<<endl;
vis[id(t)]=-1;
}
dfs(1),Dfs(1);
// for(int i=1;i<=cnt;i++) cout<<vis[i]<<" ";
// cout<<endl;
cout<<res<<endl;
for(int i=1;i<=cnt;i++) vis[i]=fl[i]=0,g[i].clear();
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
/*
Samples
input:
output:
THINGS TODO:
检查freopen,尤其是后缀名
检查空间
检查调试语句是否全部注释
*/