#include <bits/stdc++.h>
using namespace std;
using namespace std;
int a[512], a1[512], w1[512], v1[512], w2[512], v2[512];
int main() {
  int n, m, k, msk, t1, c, ans1 = 0, ans2 = 0, wm = 0x7f7f7f7f, ans = -1;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &k);
    msk = 0;
    for (int j = 0; j < k; j++) {
      scanf("%d", &t1);
      msk |= (1 << (t1 - 1));
    }
    a[msk]++;
  }
  for (int i = 0; i < 512; i++) {
    for (int j = 0; j < 512; j++) {
      if ((i & j) == j) a1[i] += a[j];
    }
  }
  memset(w1, 127, sizeof(w1));
  memset(w2, 127, sizeof(w2));
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &c, &k);
    msk = 0;
    for (int j = 0; j < k; j++) {
      scanf("%d", &t1);
      msk |= (1 << (t1 - 1));
    }
    if (c < w1[msk]) {
      w2[msk] = w1[msk];
      v2[msk] = v1[msk];
      w1[msk] = c;
      v1[msk] = i + 1;
    } else if (c < w2[msk]) {
      w2[msk] = c;
      v2[msk] = i + 1;
    }
  }
  for (int i = 0; i < 512; i++) {
    if (w1[i] >= 0x7f7f7f7f) continue;
    if (w2[i] < 0x7f7f7f7f) {
      t1 = w1[i] + w2[i];
      if (a1[i] > ans || a1[i] == ans && t1 < wm) {
        ans = a1[i];
        wm = t1;
        ans1 = v1[i];
        ans2 = v2[i];
      }
    }
    for (int j = i + 1; j < 512; j++) {
      if (w1[j] >= 0x7f7f7f7f) continue;
      t1 = w1[i] + w1[j];
      if (a1[i | j] > ans || a1[i | j] == ans && t1 < wm) {
        ans = a1[i | j];
        wm = t1;
        ans1 = v1[i];
        ans2 = v1[j];
      }
    }
  }
  printf("%d %d\n", ans1, ans2);
  return 0;
}
