#include <bits/stdc++.h>
long long int const MOD = 1000000007;
long long int const N = 1000005;
long long int const LN = 20;
long long int const inf = 8e18;
using namespace std;
long long int n, m, a[N];
long long int nof[N];
long long int tot[N];
long long int first[N];
vector<pair<long long int, long long int> > p[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) {
    long long int d;
    cin >> d;
    long long int temp = 0;
    for (long long int j = 1; j <= d; j++) {
      long long int k;
      cin >> k;
      k--;
      temp += (1LL << k);
    }
    nof[temp]++;
  }
  for (long long int i = 1; i <= m; i++) {
    long long int pp;
    cin >> pp;
    long long int d;
    cin >> d;
    long long int temp = 0;
    for (long long int j = 1; j <= d; j++) {
      long long int k;
      cin >> k;
      k--;
      temp += (1LL << k);
    }
    p[temp].push_back({pp, i});
  }
  long long int fi = (1 << 9);
  for (long long int i = 0; i < fi; i++) sort(p[i].begin(), p[i].end());
  for (long long int i = 0; i < fi; ++i) first[i] = nof[i];
  for (long long int i = 0; i < 9; ++i)
    for (long long int mask = 0; mask < fi; ++mask) {
      if (mask & (1 << i)) first[mask] += first[mask ^ (1 << i)];
    }
  long long int mxx = 0;
  long long int pri = inf;
  long long int i1 = 0;
  long long int i2 = 0;
  for (long long int i = 1; i < fi; i++) {
    for (long long int j = 1; j < fi; j++) {
      long long int tot = first[i | j];
      if (p[i].size() && p[j].size()) {
        if (mxx < tot) {
          if (i == j) {
            if (p[i].size() >= 2) {
              pri = p[i][0].first + p[i][1].first;
              i1 = p[i][0].second;
              i2 = p[i][1].second;
              mxx = tot;
            }
          } else
            mxx = tot, pri = p[i][0].first + p[j][0].first, i1 = p[i][0].second,
            i2 = p[j][0].second;
        } else if (mxx == tot) {
          if (i == j) {
            if (p[i].size() >= 2) {
              if (pri > p[i][0].first + p[i][1].first) {
                pri = p[i][0].first + p[i][1].first;
                i1 = p[i][0].second;
                i2 = p[i][1].second;
              }
            }
          } else if (pri > p[i][0].first + p[j][0].first) {
            pri = p[i][0].first + p[j][0].first;
            i1 = p[i][0].second;
            i2 = p[j][0].second;
          }
        }
      }
    }
  }
  cout << i1 << " " << i2;
  return 0;
}
