#include <cstdio>
#include <queue>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int nz,nr;
int idz[10000];
int mz[10000];
int adj[10000][10];
int need[10000],nrneed;

int d[10000];
#define ADD(to,cost) if(d[to]==-1) q.push(to),d[to]=cost;
int go(int s) {
	queue<int> q;
	memset(d,-1,sizeof(d));
	ADD(s,1);
	int left=nrneed;
	while(!q.empty()) {
		int at=q.front(); q.pop();
		if(need[at]) if(--left==0) return d[at];
		REP(j,mz[at]) ADD(adj[at][j],d[at]+1);
	}
	return -1;
}

void run() {
	scanf("%d%d",&nz,&nr);
	REP(i,nz) { int id; scanf("%d",&id); idz[i]=id; scanf("%d",&mz[id]); REP(j,mz[id]) scanf("%d",&adj[id][j]); }
	memset(need,0,sizeof(need)); nrneed=0;
	REP(i,nr) {
		int mr; scanf("%d",&mr);
		REP(j,mr) { int id; scanf("%d",&id); if(++need[id]==1) ++nrneed; }
	}
	int ret=-1,center=-1; REP(i,nz) { int cur=go(idz[i]); if(cur!=-1&&(ret==-1||cur<ret)) ret=cur,center=idz[i]; }
//	REP(i,10000) if(reach[i]==cur-1) printf("%d: %d\n",i,res[i]);
	printf("%d %d\n",ret,center);
}

int main() {
	int n; scanf("%d",&n); REP(i,n) run();
	return 0;
}
