import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class medals_b3_slow2 {
  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][];
    int[][][] gw = new int[N][][];
    int[][] gr = new int[N][];
    int[] deg = new int[N];
    int[] di = new int[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 int[deg[i]][];
      gr[i] = new int[deg[i]];
    }
    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]] = new int[10];
      for (int k = 0; k < 10; k++)
        gw[0][di[0]][k] = 0;
      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]] = new int[10];
      for (int k = 0; k < 10; k++)
        gw[i + 1][di[i + 1]][k] = 0;
      di[0]++;
      di[i + 1]++;
      for (int j = 0; j < a[i].length; j++) {
        int x = a[i][j] + 1 + n;
        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]] = new int[10];
        for (int k = 0; k < 10; k++)
          gw[i + 1][di[i + 1]][k] = 0;
        gw[i + 1][di[i + 1]][b[i][j] - 1] = -1;
        gt[x][di[x]] = i + 1;
        gf[x][di[x]] = 0;
        gc[x][di[x]] = 0;
        gw[x][di[x]] = new int[10];
        for (int k = 0; k < 10; k++)
          gw[x][di[x]][k] = 0;
        gw[x][di[x]][b[i][j] - 1] = 1;
        di[i + 1]++;
        di[x]++;
      }
    }
    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]] = new int[10];
      for (int k = 0; k < 10; k++)
        gw[x][di[x]][k] = 0;
      gt[t][di[t]] = x;
      gf[t][di[t]] = 0;
      gc[t][di[t]] = 0;
      gw[t][di[t]] = new int[10];
      for (int k = 0; k < 10; k++)
        gw[t][di[t]][k] = 0;
      di[t]++;
      di[x]++;
    }

    int[] ans = new int[10];
    for (int k = 0; k < 10; k++)
      ans[k] = 0;
 
    int[] c = new int[N];
    int[] inq = new int[N];
    int[] p = new int[N];
    int[] pi = new int[N];
    int[] q = new int[N];
    int[][] d = new int[N][10];
    int[] w = new int[10];

    while (true) {
      int qs = 0, qz = 0;
      for (int i = 0; i < N; i++) {
        c[i] = 0;
        inq[i] = 0;
        p[i] = -1;
        pi[i] = -1;
        for (int j = 0; j < 10; j++)
          d[i][j] = 0;
      }
      c[s] = inq[s] = 1;
      q[(qs + qz++) % N] = s;
      while (qz > 0) {
        int u = q[(qs++) % N];
        inq[u] = 0;
        qz--;
        for (int i = 0; i < gt[u].length; i++) {
          if (gf[u][i] >= gc[u][i])
            continue;
          int x = gt[u][i];
          for (int k = 0; k < 10; k++)
            w[k] = d[u][k] + gw[u][i][k];
          int r = c[x] == 1 ? 0 : 1;
          for (int k = 0; k < 10 && r == 0; k++)
            if (w[k] < d[x][k])
              r = 1;
            else if (w[k] > d[x][k])
              r = -1;
          if (r <= 0)
            continue;
          for (int k = 0; k < 10; k++)
            d[x][k] = w[k];
          p[x] = u;
          pi[x] = i;
          if (inq[x] != 0)
            continue;
          c[x] = 1;
          inq[x] = 1;
          q[(qs + qz++) % N] = x;
        }
      }
      int r = c[t] == 0 ? -1 : 0;
      for (int k = 0; k < 10 && r == 0; k++)
        if (d[t][k] < 0)
          r = 1;
        else if (d[t][k] > 0)
          r = -1;
      if (r <= 0)
        break;
      for (int k = 0; k < 10; k++)
        ans[k] = ans[k] - d[t][k];
      for (int u = t; p[u] != -1; u = p[u]) {
        gf[p[u]][pi[u]]++;
        gf[u][gr[p[u]][pi[u]]]--;
      }
    }

    int[] ra = ans;
    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();

  }
}

