#if defined(_USE_PCH_)
#include "pch.hpp"
#else
#include <bits/stdc++.h>
#endif
#define RNG(V_, A_, B_, ...) for(int V_=(A_), V_##_END=(B_) __VA_OPT__(,) __VA_ARGS__; V_<=V_##_END; V_++)
#define IRNG(V_, A_, B_) for(int V_=(A_), V_##_END=(B_); V_>=V_##_END; V_--)
#ifdef _WIN32
#define long __int64
#endif
using namespace std;
const int MAXN=1e6+10,INF=1e9;
int n,m,ans; vector<pair<int,int>> G[MAXN];
int S[MAXN][2],T[MAXN][2];
int vis[MAXN];
void dfs1(int u,int fai,int& nc,int& ec){
++nc;
assert(!vis[u]); vis[u]=1;
for(auto [v,i]:G[u]){
if(fai==i) continue;
++ec;
if(!vis[v]) dfs1(v,i,nc,ec);
}
}
vector<tuple<int,int,int>> cyl; vector<tuple<int,int,int>> stk;
void dfs2(int u,int fai){
assert(vis[u]!=2); vis[u]=2;
for(auto [v,i]:G[u]){
if(fai==i) continue;
stk.push_back({i,u,v});
if(vis[v]==2){
if(!cyl.size()){
if(v==u){ cyl.push_back({i,u,u}); }
else{
IRNG(j,int(stk.size())-1,0){
if(j!=int(stk.size())-1&&get<2>(stk[j])==v) break;
cyl.push_back(stk[j]);
}
}
}
}else dfs2(v,i);
stk.pop_back();
}
}
bool test(int i,int u){ return S[i][0]==u||S[i][1]==u; }
void dfs3(int u,int fai,int& res){
assert(vis[u]!=3); vis[u]=3;
for(auto [v,i]:G[u]){
if(fai==i) continue;
res+=test(i,v);
if(vis[v]==3) continue;
dfs3(v,i,res);
}
}
int F[MAXN],H[MAXN],H1[MAXN];
void dfs4(int u,int fai,int h){
F[u]=0;
for(auto [v,i]:G[u]){
if(fai==i) continue;
dfs4(v,i,h+(test(i,u)&&!test(i,v)));
F[u]+=F[v]+(test(i,v));
}
H[u]=h;
for(auto [v,i]:G[u]){
if(fai==i) continue;
H[u]=min(H[u],(H1[v]=H[v]-(F[v]+test(i,v))));
}
H[u]+=F[u];
}
void dfs5(int u,int fai,int f,int h,int& res){
assert(h>=0);
res=max(res,min(F[u]+h,H[u]+f));
pair<int,int> mn{INF,0},sc{INF,0};
for(auto [v,i]:G[u]){
if(fai==i) continue;
auto tmp=pair<int,int>{H1[v],i};
if(tmp<mn) sc=mn,mn=tmp;
else if(tmp<sc) sc=tmp;
}
for(auto [v,i]:G[u]){
if(fai==i) continue;
int f1=F[u]-F[v]-test(i,v),h1=min(h+f1,(i==mn.second?sc:mn).first+f+f1);
dfs5(v,i,f+f1+(test(i,u)&&test(i,v)),h1+(test(i,v)&&!test(i,u)),res);
}
}
void case_main(){
cin>>n>>m;
ans=0;
RNG(i,1,n,k){
cin>>k>>S[i][0];
if(k==2) cin>>S[i][1];
else S[i][1]=0;
}
RNG(i,1,n,k){
cin>>k>>T[i][0];
if(k==2) cin>>T[i][1];
else T[i][1]=0;
}
RNG(i,1,n){
cerr<<T[i][0]<<" "<<T[i][1]<<"\n";
}
RNG(i,1,n){
cerr<<T[i][0]<<" "<<test(i,T[i][0])<<" "<<T[i][1]<<" "<<test(i,T[i][1])<<"\n";
}
RNG(i,1,m) G[i].clear();
RNG(i,1,n){
if(!T[i][1]) G[T[i][0]].push_back({T[i][0],i});
else G[T[i][0]].push_back({T[i][1],i}),G[T[i][1]].push_back({T[i][0],i});
}
fill_n(vis+1,m,0);
RNG(r,1,m){
if(vis[r]) continue;
int nc=0,ec=0;
dfs1(r,0,nc,ec);
if(ec>nc+1){ cout<<-1<<"\n"; return; }
int res=0;
if(ec>=nc){
cyl.clear();
dfs2(r,0);
assert(cyl.size());
if(cyl.size()==1) dfs3(get<1>(cyl[0]),get<0>(cyl[0]),res),res++;
else{
int c0=0,c1=0,c2=0;
for(auto [i,u,v]:cyl){
if(test(i,u)&&test(i,v)) c0++;
else if(test(i,u)) c1++;
else if(test(i,v)) c2++;
}
if(c1<c2){
// if(c0<(c2-c1)) res+=c0+c1;
// else res+=(c0+c1+c2)/2;
dfs3(get<1>(cyl[0]),get<0>(cyl[0]),res);
}else{
// if(c0<(c1-c2)) res+=c0+c2;
// else res+=(c0+c1+c2)/2;
dfs3(get<2>(cyl[0]),get<0>(cyl[0]),res);
}
}
}else{
dfs4(1,0,0);
dfs5(1,0,0,INF,res);
}
ans+=res;
}
cout<<ans<<"\n";
}
int main(){
#if defined(_LOCAL_)
freopen("in","r",stdin);
freopen("out","w",stdout);
#else
// freopen("pack.in","r",stdin);
// freopen("pack.out","w",stdout);
#endif
ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
int cas; cin>>cas;
RNG(_,1,cas) case_main();
}