#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 9) + 1, maxp = 1e5 + 1, mod = 1e9 + 7, inf = 2e9;
int n, m, a[maxn], c[maxp], pizza[maxn], id[maxn], ans = inf, id1, id2,
                                                   maxit = -1, maxic;
vector<pair<int, int> > ran;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < maxn; i++) {
    pizza[i] = inf;
  }
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int t, res = 0;
    cin >> t;
    for (int j = 1; j <= t; j++) {
      int tt;
      cin >> tt;
      res += (1 << (tt - 1));
    }
    a[res]++;
  }
  for (int i = 1; i <= m; i++) {
    int t, res = 0;
    cin >> c[i] >> t;
    for (int j = 1; j <= t; j++) {
      int tt;
      cin >> tt;
      res += (1 << (tt - 1));
    }
    if (pizza[res] > c[i]) {
      pizza[res] = c[i];
      id[res] = i;
    }
    ran.push_back({c[i], i});
  }
  sort(ran.begin(), ran.end());
  for (int i = 0; i < (1 << 9); i++) {
    for (int j = i; j > 0; j = (j - 1) & i) {
      if (pizza[j] > pizza[i]) {
        pizza[j] = pizza[i];
        id[j] = id[i];
      }
    }
  }
  pizza[0] = ran[0].first;
  id[0] = ran[0].second;
  for (int i = 0; i < (1 << 9); i++) {
    int ted = 0, t1 = 0, t2 = 0, cost = inf + 1;
    for (int j = i; j > 0; j = (j - 1) & i) {
      ted += a[j];
      if (pizza[j] != inf && pizza[i - j] != inf) {
        if (cost > pizza[j] + pizza[i - j]) {
          cost = pizza[j] + pizza[i - j];
          t1 = id[j];
          t2 = id[i - j];
        }
      }
    }
    if (cost == inf + 1) {
      continue;
    }
    if (ted > maxit) {
      maxit = ted;
      id1 = t1;
      id2 = t2;
      maxic = cost;
    }
    if (ted == maxit && maxic > cost) {
      id1 = t1;
      id2 = t2;
      maxic = cost;
    }
  }
  if (id1 == id2) {
    if (ran[0].second != id1) {
      cout << ran[0].second << " " << id1 << endl;
    } else {
      cout << ran[1].second << " " << id1 << endl;
    }
    return 0;
  }
  cout << id1 << " " << id2 << endl;
  return 0;
}
