#include <bits/stdc++.h>
using namespace std;
int64_t build_msk(vector<int64_t> &v) {
  int64_t msk = 0;
  for (int64_t i = 0; i < v.size(); ++i) {
    msk |= 1 << (v[i] - 1);
  }
  return msk;
}
const int64_t INF = 1e18;
struct twop {
  int64_t cost = INF, a, b;
  twop() {}
  twop(int64_t cost, int64_t a, int64_t b) : cost(cost), a(a), b(b) {}
};
int32_t main() {
  ios_base::sync_with_stdio(false);
  int64_t n, m;
  cin >> n >> m;
  vector<int64_t> f(n);
  for (int64_t i = 0; i < n; ++i) {
    int64_t cnt;
    cin >> cnt;
    vector<int64_t> g(cnt);
    for (int64_t j = 0; j < cnt; ++j) cin >> g[j];
    f[i] = build_msk(g);
  }
  vector<pair<int64_t, int64_t> > p(1 << 9, {INF, (int64_t)0});
  vector<twop> tp(1 << 9);
  for (int64_t i = 0; i < m; ++i) {
    int64_t cost, cnt;
    cin >> cost >> cnt;
    vector<int64_t> g(cnt);
    for (int64_t j = 0; j < cnt; ++j) cin >> g[j];
    int64_t msk = build_msk(g);
    if (p[msk].first != INF) {
      int64_t cst = p[msk].first + cost;
      if (tp[msk].cost > cst) {
        tp[msk] = twop(cst, p[msk].second, i);
      }
    }
    if (p[msk].first > cost) {
      p[msk] = {cost, i};
    }
  }
  for (int64_t i = 0; i < 1 << 9; ++i)
    for (int64_t j = i + 1; j < (1 << 9); ++j) {
      int64_t msk = i | j;
      if (tp[msk].cost > p[i].first + p[j].first) {
        tp[msk] = twop(p[i].first + p[j].first, p[i].second, p[j].second);
      }
    }
  int64_t bst = 0, cost = INF, a, b;
  for (int64_t msk = 0; msk < 1 << 9; ++msk) {
    if (tp[msk].cost == INF) continue;
    int64_t cnt = 0;
    for (int64_t i = 0; i < n; ++i) {
      if ((f[i] & msk) == f[i]) ++cnt;
    }
    if (cnt > bst || (cnt == bst && tp[msk].cost < cost)) {
      cost = tp[msk].cost;
      bst = cnt;
      a = tp[msk].a;
      b = tp[msk].b;
    }
  }
  cout << a + 1 << ' ' << b + 1;
}
