#include <bits/stdc++.h>
using namespace std;
const long long N = 2e3 + 10, mod = 1e9 + 7, inf = 0x3f3f3f3f3f3f3f3f;
long long p[N], sp[N], in[N];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0, k, tep = 0; i < n; i++, tep = 0) {
    scanf("%d", &k);
    for (int j = 0, t; j < k; j++) {
      scanf("%d", &t);
      t--;
      tep |= (1 << t);
    }
    p[tep]++;
  }
  memset(sp, 0x3f, sizeof sp);
  for (int i = 0, k, tep = 0, cos; i < m; i++, tep = 0) {
    scanf("%d %d", &cos, &k);
    for (int j = 0, t; j < k; j++) {
      scanf("%d", &t);
      t--;
      tep |= (1 << t);
    }
    if (cos < sp[tep]) {
      sp[tep] = cos;
      in[tep] = i + 1;
    }
  }
  long long ans = 0, cos = 0x3f3f3f3f3f3f3f3f, a1 = 1, a2 = 2;
  for (int i = 1; i < (1 << 9); i++) {
    if (sp[i] == inf) continue;
    for (int j = i + 1, sum = 0; j < (1 << 9); j++, sum = 0) {
      if (sp[j] == inf) continue;
      for (int k = 1; k < (1 << 9); k++) {
        if (((i | j) & k) >= k && p[k]) {
          sum += p[k];
        }
      }
      if (sum > ans || (sum == ans && sp[i] + sp[j] <= cos)) {
        a1 = in[i];
        a2 = in[j];
        cos = sp[i] + sp[j];
        ans = sum;
      }
    }
  }
  if (a1 > a2) swap(a1, a2);
  printf("%lld %lld\n", a1, a2);
}
