#include <bits/stdc++.h>
using namespace std;
long long buc[512], w1[512], w2[512];
int ind1[512], ind2[512];
int n, m;
int read() {
  int n, x, v = 0;
  for (scanf("%d", &n); n; n--) {
    scanf("%d", &x);
    v |= 1 << (x - 1);
  }
  return v;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) buc[read()]++;
  for (int i = 511; i >= 0; i--)
    for (int j = 0; j < i; j++)
      if ((i | j) == i) buc[i] += buc[j];
  for (int i = 0; i < 512; i++) w1[i] = w2[i] = 2e9;
  for (int i = 1, v; i <= m; i++) {
    scanf("%d", &v);
    int w = read();
    if (v < w1[w]) {
      w2[w] = w1[w], w1[w] = v;
      ind2[w] = ind1[w], ind1[w] = i;
    } else if (v < w2[w]) {
      w2[w] = v;
      ind2[w] = i;
    }
  }
  int mxnum = -1;
  long long ans = 1e10;
  int id1 = 1, id2 = 2;
  for (int i = 0; i < 512; i++)
    if (w1[i] <= 1.1e9)
      for (int j = 0; j < 512; j++)
        if (w1[j] <= 1.1e9) {
          if (i == j && w2[i] > 1.1e9) continue;
          if (buc[i | j] > mxnum) mxnum = buc[i | j], ans = 1e10;
          if (buc[i | j] == mxnum) {
            long long tmp = w1[i] + (i == j ? w2[j] : w1[j]);
            if (tmp < ans) {
              ans = tmp;
              id1 = ind1[i], id2 = i == j ? ind2[j] : ind1[j];
            }
          }
        }
  printf("%d %d\n", id1, id2);
}
