import java.util.*;
import java.io.*;

public class scavenger_verf {

	static double[] dp;
	static int[] bonus;
	static ArrayList<Integer>[] graph;
	public static void main(String[] args) throws FileNotFoundException{
		Scanner scan = new Scanner(new File("scavenger.in"));
		PrintWriter out = new PrintWriter(new File("scavenger_verf.out"));
		int t = scan.nextInt();
		for(int q = 1; q <= t; q++){
			int n = scan.nextInt();
			if(!(n >= 2 && n <= 200)) problem(q);
			dp = new double[n];
			Arrays.fill(dp, -1);
			bonus = new int[n];
			int b = scan.nextInt();
			if(!(b >= 0 && b <= n)) problem(q);
			Arrays.fill(bonus, 1);
			for(int i = 0; i < b; i++){
				int v = scan.nextInt()-1;
				int m = scan.nextInt();
				if(v >= n || v < 0) problem(q);
				if(!(m >= 2 && m <= 10)) problem(q);
				bonus[v] = m;
			}
			int[][] floyd = new int[n][n];
			graph = new ArrayList[n];
			int[] indeg = new int[n];
			for(int i = 0; i < n; i++) graph[i] = new ArrayList<Integer>();
			for(int i = 0; i < n; i++){
				int v = scan.nextInt();
				if(!(v >= 0 && v < n)) problem(q);
				for(int j = 0; j < v; j++){
					int to = scan.nextInt()-1;
					if(!(to >= 0 && to < n)) problem(q);
					graph[i].add(to);
					floyd[i][to] = 1;
					indeg[to]++;
				}
			}
			// Verifying no cycles
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					for(int k = 0; k < n; k++){
						floyd[j][k] |= floyd[j][i]&floyd[i][k];
					}
				}
			}
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j) continue;
					if(floyd[i][j] == 1 && floyd[j][i] == 1) problem(q);
				}
			}

			out.printf("%.2f\n", solve(0));
		}
		out.flush();
	}

	static double solve(int i){
		if(graph[i].size() == 0) return bonus[i];
		if(dp[i] > -0.5) return dp[i];
		double res = 0;
		for(int j : graph[i])
			res += bonus[i]*(solve(j)+1);
		return dp[i] = res/graph[i].size();
	}

	static void problem(int caseID){
		System.out.println("Issue with case "+caseID);
	}
}