// Danny Wasserman
// 7/23/2014
// Solution to 2014 SI@UCF Contest Problem: Choose Your Own Adventure 2

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class adventure {

  // Store happiness and graph here.
  static int[] h;
  static ArrayList<Integer>[] g;

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int cases = in.nextInt();

    // Go through each case.
    for (int co = 1; co <= cases; co++) {

      // Read in the graph.
      int n = in.nextInt();
      g = new ArrayList[n];
      h = new int[n];
      for (int i = 0; i < n; i++) {
        g[i] = new ArrayList<Integer>();
        h[i] = in.nextInt();
        int adj = in.nextInt();
        for (int j = 0; j < adj; j++) {
          int v = in.nextInt() - 1;
          g[i].add(v);
        }
      }

      // Set up the memo array and solve.
      memo = new int[n];
      Arrays.fill(memo, -1);
      System.out.printf("%d\n", go(0));
    }
  }

  static int[] memo;

  // Solve the problem starting at page at.
  static int go(int at) {

  	// We did this case already...
    if (memo[at] != -1) return memo[at];

    // Try each forwarding page and do the best.
    int ret = 0;
    for (int adj : g[at])
      ret = Math.max(ret, go(adj));
    return memo[at] = ret + h[at];
  }
}
