#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, a[maxn], t = 9, num[600], p[maxn], b[maxn];
int c[maxn], mn = 0, tmn = 0;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    int y;
    for (int j = 1; j <= x; j++) scanf("%d", &y), y--, a[i] |= (1 << y);
  }
  for (int i = 0; i < (1 << t); i++) {
    for (int j = 1; j <= n; j++)
      if ((i & a[j]) == a[j]) num[i]++;
  }
  c[0] = 1e9 + 2;
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d%d", &c[i], &x);
    int y;
    for (int j = 1; j <= x; j++) scanf("%d", &y), y--, b[i] |= (1 << y);
    for (int j = 0; j < (1 << t); j++) {
      if ((b[i] & j) == j) {
        if (p[j] == 0 || c[p[j]] > c[i]) p[j] = i;
      }
    }
    if (c[mn] > c[i])
      tmn = mn, mn = i;
    else if (c[tmn] > c[i])
      tmn = i;
  }
  int ans1 = 0;
  int t1 = 0, t2 = 0;
  for (int i = 0; i < (1 << t); i++) {
    for (int j = 1; j <= m; j++) {
      if ((b[j] & i) == b[j]) {
        int w = b[j] ^ i;
        if (p[w] != 0) {
          if (ans1 < num[i]) {
            ans1 = num[i], t1 = j, t2 = p[w];
            if (t1 == t2) t2 = tmn;
          } else if (ans1 == num[i] && c[t1] + c[t2] > c[j] + c[p[w]]) {
            t1 = j;
            t2 = p[w];
            if (t1 == t2) t2 = tmn;
          }
        }
      }
    }
  }
  if (t1 > t2) swap(t1, t2);
  printf("%d %d\n", t1, t2);
  return 0;
}
