#include <cassert>
#include <cstdio>
#include <cstring>

#include <algorithm>
#include <vector>

using namespace std;

struct Weight {
  int a[4];
  Weight( int k = 0 ) {
    memset(a, 0, sizeof(a));
    if (k != 0) {
      a[(abs(k) - 1) / 3] = k > 0 ? 1 : -1;
      k = 2 - (abs(k) - 1) % 3;
    }
    while (k --> 0) {
      for (int i = 0; i < 4; i++)
        a[i] *= 1024;
    }
  }
  Weight operator+( Weight b ) {
    Weight w;
    for (int i = 0; i < 4; i++)
      w.a[i] = a[i] + b.a[i];
    return w;
  }
  Weight operator-( Weight b ) {
    Weight w;
    for (int i = 0; i < 4; i++)
      w.a[i] = a[i] - b.a[i];
    return w;
  }
  bool operator<( Weight b ) {
    for (int i = 0; i < 4; i++)
      if (a[i] != b.a[i])
        return a[i] < b.a[i];
    return false;
  }
  static Weight inf() {
    Weight w;
    w.a[0] = 0x7fffffff;
    return w;
  }
};

static const Weight INF = Weight::inf();

void it_set( int *it, int max_it, Weight *d, int x ) {
  int i = x + max_it;
  it[i] = x;
  for (i >>= 1; i; i >>= 1)
    it[i] = d[it[2 * i]] < d[it[2 * i + 1]] ? it[2 * i] : it[2 * i + 1];
}

int main() {
  assert(freopen("medals.in", "r", stdin));
  assert(freopen("medals.out", "w", stdout));

  int n, m = 0;
  assert(scanf("%d", &n) == 1);
  vector <int> a[n], b[n];
  for (int i = 0; i < n; i++) {
    int k;
    assert(scanf("%d", &k) == 1);
    a[i].resize(k);
    b[i].resize(k);
    for (int j = 0; j < k; j++) {
      assert(scanf("%d%d", &a[i][j], &b[i][j]) == 2);
      m = max(m, a[i][j]--);
    }
  }

  int N = n + m + 2, s = 0, t = N - 1;
  vector <int> gt[N], gf[N], gc[N], gr[N];
  vector <Weight> gw[N];
  Weight pt[N];
  for (int i = 0; i < N; i++)
    pt[i] = 0;
  for (int i = 0; i < n; i++) {
    gr[s].push_back(gt[i + 1].size());
    gr[i + 1].push_back(gt[s].size());
    gt[s].push_back(i + 1);
    gt[i + 1].push_back(s);
    gf[s].push_back(0);
    gf[i + 1].push_back(0);
    gc[s].push_back(1);
    gc[i + 1].push_back(0);
    gw[s].push_back(0);
    gw[i + 1].push_back(0);
    for (int j = 0; j < (int)a[i].size(); j++) {
      int x = a[i][j] + n + 1;
      gr[x].push_back(gt[i + 1].size());
      gr[i + 1].push_back(gt[x].size());
      gt[x].push_back(i + 1);
      gt[i + 1].push_back(x);
      gf[x].push_back(0);
      gf[i + 1].push_back(0);
      gc[x].push_back(0);
      gc[i + 1].push_back(1);
      gw[x].push_back(b[i][j]);
      gw[i + 1].push_back(-b[i][j]);
      if (Weight(-b[i][j]) < pt[x])
        pt[x] = Weight(-b[i][j]);
      if (Weight(-b[i][j]) < pt[t])
        pt[t] = Weight(-b[i][j]);
    }
  }
  for (int i = 0; i < m; i++) {
    gr[t].push_back(gt[i + n + 1].size());
    gr[i + n + 1].push_back(gt[t].size());
    gt[t].push_back(i + n + 1);
    gt[i + n + 1].push_back(t);
    gf[t].push_back(0);
    gf[i + n + 1].push_back(0);
    gc[t].push_back(0);
    gc[i + n + 1].push_back(1);
    gw[t].push_back(0);
    gw[i + n + 1].push_back(0);
  }

  int c[N], it_len = 1, r[N], ri[N], it[3 * N];
  while (it_len < N)
    it_len *= 2;
  Weight d[N + 1], dd[N], ans = 0;
  d[N] = INF;
  while (1) {
    for (int i = 0; i < 3 * N; i++) {
      it[i] = N;
    }
    for (int i = 0; i < N; i++) {
      c[i] = 0;
      d[i] = INF;
      it_set(it, it_len, d, i);
    }
    d[s] = 0;
    r[s] = ri[s] = -1;
    it_set(it, it_len, d, 0);
    while (1) {
      int u = it[1];
      if (!(d[u] < INF))
        break;
      Weight du = d[u] + pt[u];
      c[u] = 1;
      dd[u] = d[u];
      d[u] = INF;
      it_set(it, it_len, d, u);
      for (int i = 0; i < (int)gt[u].size(); i++) {
        if (c[gt[u][i]] || gf[u][i] >= gc[u][i])
          continue;
        Weight td = du + gw[u][i] - pt[gt[u][i]];
        assert(!(pt[u] + gw[u][i] - pt[gt[u][i]] < 0));
        if (!(td < d[gt[u][i]]))
          continue;
        d[gt[u][i]] = td;
        it_set(it, it_len, d, gt[u][i]);
        r[gt[u][i]] = u;
        ri[gt[u][i]] = i;
      }
    }
    if (!c[t] || Weight(0) < dd[t] + pt[t] - pt[s])
      break;
    ans = ans - (dd[t] + pt[t] - pt[s]);
    for (int i = 0; i < N; i++) {
      if (c[i])
        pt[i] = pt[i] + dd[i];
    }
    for (int u = t; r[u] != -1; u = r[u]) {
      int a = r[u], i = ri[u];
      Weight w = pt[a] + gw[a][i] - pt[u];
      assert(!(w < 0 || Weight(0) < w));
      gf[a][i]++;
      gf[u][gr[a][i]]--;
    }
  }
  
  int ans_r[12];
  for (int i = 2; i >= 0; i--) {
    for (int j = 0; j < 4; j++) {
      ans_r[j * 3 + i] = ans.a[j] % 1024;
      ans.a[j] /= 1024;
    }
  }

  for (int i = 0; i < 10; i++)
    printf("%d%c", ans_r[i], "\n "[i < 9]);
  for (int i = 0; i < n; i++) {
    int r = 0;
    for (int j = 0; j < (int)gt[i + 1].size(); j++) {
      if (gf[i + 1][j] <= 0)
        continue;
      assert(r == 0);
      r = gt[i + 1][j] - n;
    }
    printf("%d%c", r, "\n "[i < n - 1]);
  }

  return 0;
}

