#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char now[1 << 16], *S, *T;
  if (S == T) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    (c == '-') ? (f = 0) : 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f ? x : ((~x) + 1);
}
const int N = 100010;
int n, m, a[N], b[N], c[N], id[N], tot = 0;
vector<pair<int, int> > con[522];
int num[522];
bool vis[522];
int main() {
  n = read();
  m = read();
  for (int(i) = (1); (i) <= (n); ++(i)) {
    int cnt = read();
    for (int(j) = (1); (j) <= (cnt); ++(j)) {
      a[i] |= (1 << (read() - 1));
    }
    num[a[i]]++;
  }
  for (int(i) = (1); (i) <= (m); ++(i)) {
    c[i] = read();
    int cnt = read();
    for (int(j) = (1); (j) <= (cnt); ++(j)) {
      b[i] |= (1 << (read() - 1));
    }
    con[b[i]].push_back(make_pair(c[i], i));
  }
  for (int(i) = (0); (i) <= (511); ++(i)) {
    sort(con[i].begin(), con[i].end());
    for (int j = 0; j < 2 && j < ((int)((con[i]).size())); ++j) {
      id[++tot] = con[i][j].second;
    }
  }
  pair<int, int> ans = make_pair(0, 0x7fffffff);
  int ans1 = 0, ans2 = 0;
  for (int(i) = (1); (i) <= (tot); ++(i)) {
    for (int(j) = (1); (j) <= (i - 1); ++(j)) {
      int cost = c[id[i]] + c[id[j]], ss = 0;
      int s1 = b[id[i]], s2 = b[id[j]];
      int s = s1 | s2;
      for (int x = s; x; x = (x - 1) & s) {
        ss += num[x];
      }
      ss += num[0];
      if (ans.first < ss)
        ans = make_pair(ss, cost), ans1 = id[i], ans2 = id[j];
      else if (ans.first == ss) {
        if (ans.second > cost) {
          ans.second = cost;
          ans1 = id[i], ans2 = id[j];
        }
      }
    }
  }
  printf("%d %d\n", ans1, ans2);
  return 0;
}
