#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, mod = 998244353, INF = 1e9 + 7;
const long long BIG = 1e18;
const int block = 316;
const int tox[] = {0, 0, 1, -1}, toy[] = {1, -1, 0, 0};
int n, m, a[15][N], p[15], c[15], b[N], sz = 1;
map<int, int> go[10 * N];
long long ans, suff[N];
bool bad[N];
vector<int> order;
void add() {
  int v = 1;
  for (int i = 1; i <= n; i++) {
    int cur = b[i];
    if (!go[v].count(cur)) {
      go[v][cur] = ++sz;
    }
    v = go[v][cur];
  }
}
void dfs(int v, int lvl, long long s) {
  int mx = c[lvl];
  for (auto e : go[v]) {
    int to = e.second, ind = e.first;
    int val = a[lvl][ind];
    dfs(to, lvl + 1, s + val);
  }
  for (auto e : go[v]) {
    int ind = e.first;
    bad[ind] = 1;
    while (bad[mx]) {
      mx--;
    }
  }
  for (auto e : go[v]) {
    int ind = e.first;
    bad[ind] = 0;
  }
  if (mx > 0) {
    ans = max(ans, s + suff[lvl] - a[lvl][c[lvl]] + a[lvl][mx]);
  }
}
void trace(int v, int lvl, long long s) {
  int mx = c[lvl];
  for (auto e : go[v]) {
    int to = e.second, ind = e.first;
    int val = a[lvl][ind];
    order.push_back(ind);
    trace(to, lvl + 1, s + val);
    order.pop_back();
  }
  for (auto e : go[v]) {
    int ind = e.first;
    bad[ind] = 1;
    while (bad[mx]) {
      mx--;
    }
  }
  for (auto e : go[v]) {
    int ind = e.first;
    bad[ind] = 0;
  }
  if (mx > 0) {
    if (ans == s + suff[lvl] - a[lvl][c[lvl]] + a[lvl][mx]) {
      for (auto e : order) {
        cout << e << ' ';
      }
      cout << mx << ' ';
      for (int i = lvl + 1; i <= n; i++) {
        cout << c[i] << ' ';
      }
      cout << endl;
      exit(0);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    for (int j = 1; j <= c[i]; j++) {
      cin >> a[i][j];
    }
    p[i] = c[i];
  }
  for (int i = n; i >= 1; i--) {
    suff[i] = suff[i + 1] + a[i][c[i]];
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> b[j];
    }
    add();
  }
  dfs(1, 1, 0);
  trace(1, 1, 0);
}
