// Arup Guha
// 4/8/2020
// Solution to COP 4516 Team Final Exam Question: Scavenger Hunt

import java.util.*;

public class scavenger {

	public static int n;
	public static ArrayList[] g;
	public static int[] memo;
	public static int[] bonus;

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process each case.
		for (int loop=0; loop<nC; loop++) {
		
			n = stdin.nextInt();
			bonus = new int[n];
			Arrays.fill(bonus, 1);
			
			// Update non-one bonuses.
			int numB = stdin.nextInt();
			for (int i=0; i<numB; i++) {
				int idx = stdin.nextInt()-1;
				int mult = stdin.nextInt();
				bonus[idx] = mult;
			}
			
			// Form graph.
			g = new ArrayList[n];
			for (int i=0; i<n; i++)
				g[i] = new ArrayList<Integer>();
			
			// Go through each vertex, form graph.
			for (int i=0; i<n; i++) {
				int next = stdin.nextInt();
				for (int j=0; j<next; j++)
					g[i].add(stdin.nextInt()-1);
			}
		
			// Set up memo.
			memo = new int[n];
			Arrays.fill(memo, -1);
			
			// Ta da!
			System.out.println(go(0));
		}
	}
	
	public static int go(int v) {
	
		// Did this before.
		if (memo[v] != -1) return memo[v];
		
		// End of hunt.
		if (g[v].size() == 0) return bonus[v];
		
		int best = 0;
		
		// Try each path.
		for (int i=0; i<g[v].size(); i++) {
		
			// Best score for this path.
			int tmp = go((Integer)g[v].get(i));
			
			// Update our path, if this is better.
			best = Math.max(best, bonus[v]*(1 + tmp));
		}
		
		// Store and return.
		return memo[v] = best;
	}
}