#include <bits/stdc++.h>
using namespace std;
int a[100100];
int c[100100];
int cnt[1 << 9];
int mn[1 << 9][2];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int k, first;
    scanf("%d", &k);
    while (k--) {
      scanf("%d", &first);
      a[i] |= 1 << first - 1;
    }
  }
  c[0] = 0x3f3f3f3f;
  for (int i = 1; i <= m; i++) {
    int k, first;
    scanf("%d %d", c + i, &k);
    int b = 0;
    while (k--) {
      scanf("%d", &first);
      b |= 1 << first - 1;
    }
    if (c[mn[b][1]] > c[i]) mn[b][1] = i;
    if (c[mn[b][0]] > c[mn[b][1]]) swap(mn[b][0], mn[b][1]);
  }
  for (int i = 0; i < 1 << 9; i++)
    for (int j = 0; j < n; j++) {
      if ((i & a[j]) == a[j]) cnt[i]++;
    }
  int mx = -1, A, B;
  for (int i = 1; i < 1 << 9; i++)
    if (mn[i][0]) {
      if (mn[i][1]) {
        if (mx == -1 || mx < cnt[i]) {
          mx = cnt[i];
          A = mn[i][0];
          B = mn[i][1];
        } else if (mx == cnt[i] && c[A] + c[B] > c[mn[i][0]] + c[mn[i][1]]) {
          A = mn[i][0];
          B = mn[i][1];
        }
      }
      for (int j = i + 1; j < 1 << 9; j++)
        if (mn[j][0]) {
          if (mx == -1 || mx < cnt[i | j]) {
            mx = cnt[i | j];
            A = mn[i][0];
            B = mn[j][0];
          } else if (mx == cnt[i | j] &&
                     c[A] + c[B] > c[mn[i][0]] + c[mn[j][0]]) {
            A = mn[i][0];
            B = mn[j][0];
          }
        }
    }
  printf("%d %d\n", A, B);
  return 0;
}
