#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long dp[(1LL << 12)];
long long a[100005];
vector<pair<long long, long long> > v[1LL << 12];
int main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    long long mask = 0;
    while (x--) {
      long long y;
      cin >> y;
      y--;
      mask |= (1LL << y);
    }
    a[i] = mask;
  }
  for (long long i = 0; i < (1LL << 9); i++) {
    for (long long j = 1; j <= n; j++) {
      if ((i | a[j]) == i) {
        dp[i]++;
      }
    }
  }
  for (long long i = 1; i <= m; i++) {
    long long c;
    cin >> c;
    long long x;
    cin >> x;
    long long mask = 0;
    while (x--) {
      long long y;
      cin >> y;
      y--;
      mask |= (1LL << y);
    }
    v[mask].push_back(make_pair(c, i));
  }
  long long lim = (1LL << 9);
  for (long long i = 0; i < lim; i++) {
    if (v[i].size() > 0) {
      sort(v[i].begin(), v[i].end());
    }
  }
  pair<long long, long long> ans = make_pair(-1, -1);
  long long id1 = -1;
  long long id2 = -1;
  for (long long i = 0; i < lim; i++) {
    if (v[i].size() == 0) continue;
    pair<long long, long long> cur = {-1, -1};
    long long ii = -1;
    long long jj = -1;
    if (v[i].size() >= 2) {
      cur = {dp[i], -v[i][0].first - v[i][1].first};
      ii = v[i][0].second;
      jj = v[i][1].second;
    }
    for (long long j = 0; j < lim; j++) {
      if (j == i) continue;
      if (v[j].size() == 0) continue;
      pair<long long, long long> ay =
          make_pair(dp[(i | j)], -1 * v[i][0].first - v[j][0].first);
      if (ay > cur) {
        cur = ay;
        ii = v[i][0].second;
        jj = v[j][0].second;
      }
    }
    if (cur > ans) {
      ans = cur;
      id1 = ii;
      id2 = jj;
    }
  }
  cout << id1 << " " << id2 << endl;
}
