import java.math.BigInteger;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class medals_b3_wa4 {
  private static class Node implements Comparable<Node> {
    private final BigInteger d;
    private final int i;
    public Node( BigInteger d, int i ) {
      this.d = d;
      this.i = i;
    }
    public int compareTo( Node b ) {
      if (!d.equals(b.d))
        return d.compareTo(b.d);
      return Integer.valueOf(i).compareTo(b.i);
    }
    public int getI() {
      return i;
    }
  }

  public static final void main( String[] args ) throws FileNotFoundException {
    Scanner in = new Scanner(new File("medals.in"));
    PrintWriter out = new PrintWriter("medals.out");

    int n = in.nextInt(), m = 0;
    int[][] a = new int[n][];
    int[][] b = new int[n][];
    for (int i = 0; i < n; i++) {
      int k = in.nextInt();
      a[i] = new int[k];
      b[i] = new int[k];
      for (int j = 0; j < k; j++) {
        a[i][j] = in.nextInt() - 1;
        b[i][j] = in.nextInt();
        m = Math.max(m, a[i][j] + 1);
      }
    }

    int N = n + m + 2;
    int s = 0, t = n + m + 1;
    int[][] gt = new int[N][];
    int[][] gf = new int[N][];
    int[][] gc = new int[N][];
    BigInteger[][] gw = new BigInteger[N][];
    int[][] gr = new int[N][];
    int[] deg = new int[N];
    int[] di = new int[N];
    BigInteger[] pt = new BigInteger[N];
    for (int i = 0; i < N; i++)
      deg[i] = di[i] = 0;
    deg[s] = n;
    deg[t] = m;
    for (int i = 0; i < n; i++) {
      deg[i + 1] = a[i].length + 1;
      for (int j = 0; j < a[i].length; j++)
        deg[a[i][j] + n + 1]++;
    }
    for (int i = 0; i < m; i++)
      deg[i + n + 1]++;
    for (int i = 0; i < N; i++) {
      gt[i] = new int[deg[i]];
      gf[i] = new int[deg[i]];
      gc[i] = new int[deg[i]];
      gw[i] = new BigInteger[deg[i]];
      gr[i] = new int[deg[i]];
      pt[i] = BigInteger.ZERO;
    }
    for (int i = 0; i < n; i++) {
      gr[0][di[0]] = di[i + 1];
      gr[i + 1][di[i + 1]] = di[0];
      gt[0][di[0]] = i + 1;
      gf[0][di[0]] = 0;
      gc[0][di[0]] = 1;
      gw[0][di[0]] = BigInteger.ZERO;
      gt[i + 1][di[i + 1]] = 0;
      gf[i + 1][di[i + 1]] = 0;
      gc[i + 1][di[i + 1]] = 0;
      gw[i + 1][di[i + 1]] = BigInteger.ZERO;
      di[0]++;
      di[i + 1]++;
      for (int j = 0; j < a[i].length; j++) {
        int x = a[i][j] + 1 + n;
        BigInteger y = BigInteger.ONE;
        for (int k = b[i][j]; k < 10; k++)
          y = y.multiply(BigInteger.valueOf(1024));
        gr[i + 1][di[i + 1]] = di[x];
        gr[x][di[x]] = di[i + 1];
        gt[i + 1][di[i + 1]] = x;
        gf[i + 1][di[i + 1]] = 0;
        gc[i + 1][di[i + 1]] = 1;
        gw[i + 1][di[i + 1]] = y.negate();
        gt[x][di[x]] = i + 1;
        gf[x][di[x]] = 0;
        gc[x][di[x]] = 0;
        gw[x][di[x]] = y;
        di[i + 1]++;
        di[x]++;
        pt[x] = pt[x].min(y.negate());
        pt[t] = pt[t].min(y.negate());
      }
    }
    for (int i = 0; i < m; i++) {
      int x = i + n + 1;
      gr[x][di[x]] = di[t];
      gr[t][di[t]] = di[x];
      gt[x][di[x]] = t;
      gf[x][di[x]] = 0;
      gc[x][di[x]] = 1;
      gw[x][di[x]] = BigInteger.ZERO;
      gt[t][di[t]] = x;
      gf[t][di[t]] = 0;
      gc[t][di[t]] = 0;
      gw[t][di[t]] = BigInteger.ZERO;
      di[t]++;
      di[x]++;
    }

    BigInteger ans = BigInteger.ZERO;
    int[] c = new int[N];
    int[] p = new int[N];
    int[] pi = new int[N];
    BigInteger[] d = new BigInteger[N];
    Node[] nd = new Node[N];
    while (true) {
      TreeSet <Node> tr = new TreeSet<Node>();
      for (int i = 0; i < N; i++) {
        c[i] = 0;
        p[i] = -1;
        pi[i] = -1;
        d[i] = null;
        nd[i] = null;
      }
      d[s] = BigInteger.ZERO;
      nd[s] = new Node(d[s], s);
      tr.add(nd[s]);
      while (tr.size() != 0) {
        int u = tr.first().getI();
        tr.remove(tr.first());
        if (c[u] != 0)
          continue;
        c[u] = 1;
        for (int i = 0; i < gt[u].length; i++) {
          if (gf[u][i] >= gc[u][i])
            continue;
          int x = gt[u][i];
          BigInteger w = d[u].add(gw[u][i]).add(pt[u]).subtract(pt[x]);
          if (d[x] != null && w.compareTo(d[x]) >= 0)
            continue;
          // help me, how to delete from TreeSet?
          p[x] = u;
          pi[x] = i;
          d[x] = w;
          tr.add(new Node(d[x], x));
        }
      }
      if (c[t] == 0)
        break;
      BigInteger dd = d[t].add(pt[t]).subtract(pt[s]);
      if (dd.compareTo(BigInteger.ZERO) >= 0)
        break;
      ans = ans.subtract(dd);
      for (int u = t; p[u] != -1; u = p[u]) {
        gf[p[u]][pi[u]]++;
        gf[u][gr[p[u]][pi[u]]]--;
      }
    }

    int[] ra = new int[10];
    for (int i = 9; i >= 0; i--) {
      ra[i] = ans.remainder(BigInteger.valueOf(1024)).intValue();
      ans = ans.divide(BigInteger.valueOf(1024));
    }
    int[] rb = new int[n];
    for (int i = 0; i < n; i++) {
      int x = i + 1;
      rb[i] = 0;
      for (int j = 0; j < gt[x].length; j++) {
        if (gf[x][j] <= 0)
          continue;
        rb[i] = gt[x][j] - n;
      }
    }

    for (int i = 0; i < ra.length; i++) {
      if (i > 0)
        out.print(" ");
      out.print(ra[i]);
    }
    out.println();
    for (int i = 0; i < rb.length; i++) {
      if (i > 0)
        out.print(" ");
      out.print(rb[i]);
    }
    out.println();
    out.close();
  }
}

