package arupsClassFinalTeam;

import java.util.ArrayList;
import java.util.Scanner;
/*
2
3 2
2 4
3 3
2 2 3
1 3
0
4 0
1 3
2 3 4
0
0

 */
public class ScavengerDavidSol {

	public static void main(String[] args) {
		Scanner fs=new Scanner(System.in);
		int T=fs.nextInt();
		for (int tt=0; tt<T; tt++) {
			int n=fs.nextInt();
			int nBonus=fs.nextInt();
			Node[] nodes=new Node[n];
			for (int i=0; i<n; i++) nodes[i]=new Node();
			for (int i=0; i<nBonus; i++) {
				int node=fs.nextInt()-1;
				int bonus=fs.nextInt();
				if (nodes[node].mult!=1) throw null;
				nodes[node].mult=bonus;
			}
			for (int i=0; i<n; i++) {
				int nAdj=fs.nextInt();
				for (int j=0; j<nAdj; j++) {
					int b=fs.nextInt()-1;
					nodes[i].adj.add(nodes[b]);
				}
			}
			System.out.println(nodes[0].go());
		}
	}
	
	static class Node {
		int mult=1;
		ArrayList<Node> adj=new ArrayList<>();
		int dp=-1;
		int go() {
			if (dp!=-1) return dp;
			int ans=mult;
			for (Node nn:adj)
				ans=Math.max(ans, mult*(1+nn.go()));
			return dp=ans;
		}
	}

}
