#include <bits/stdc++.h>
const int N = 515;
int a[N], b[N], id[N], b2[N], xid[N], id1, id2, ans, cost;
inline int gi() {
  char c = getchar();
  int x = 0;
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
  return x;
}
int main() {
  int n = gi(), m = gi();
  for (int i = 1; i <= n; ++i) {
    int x = gi(), s = 0;
    while (x--) s |= (1 << gi() - 1);
    ++a[s];
  }
  memset(b, 0x3f, sizeof(b));
  memset(b2, 0x3f, sizeof(b2));
  for (int i = 1; i <= m; ++i) {
    int c = gi(), x = gi(), s = 0;
    while (x--) s |= (1 << gi() - 1);
    if (c < b[s])
      b[s] = c, id[s] = i;
    else if (c < b2[s])
      b2[s] = c, xid[s] = i;
  }
  cost = 2e9 + 5;
  for (int s1 = 0; s1 < 512; ++s1)
    for (int s2 = 0; s2 < 512; ++s2) {
      if (!id[s1] || !id[s2]) continue;
      int tid1, tid2, tcost;
      if (s1 == s2)
        tid1 = id[s1], tid2 = xid[s1], tcost = b[s1] + b2[s1];
      else
        tid1 = id[s1], tid2 = id[s2], tcost = b[s1] + b[s2];
      int ret = 0;
      for (int s3 = 0; s3 < 512; ++s3)
        if (((s1 | s2) & s3) == s3) ret += a[s3];
      if (ret > ans || (ret == ans && cost > tcost))
        cost = tcost, ans = ret, id1 = tid1, id2 = tid2;
    }
  printf("%d %d", id1, id2);
}
