#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> a(1 << 9, 0);
  for (long long i = (0); i < ((n)); i++) {
    long long x, v = 0;
    cin >> x;
    for (long long _ = (0); _ < ((x)); _++) {
      long long k;
      cin >> k;
      k--;
      v |= 1 << k;
    }
    a[v]++;
  }
  vector<vector<long long>> b(1 << 9, vector<long long>(2, (1LL << 60))),
      in(1 << 9, vector<long long>(2, -1));
  for (long long j = (0); j < ((m)); j++) {
    long long x, y, v = 0;
    cin >> x >> y;
    for (long long i = (0); i < ((y)); i++) {
      long long k;
      cin >> k;
      k--;
      v |= 1 << k;
    }
    if (b[v][0] > x) {
      b[v][1] = b[v][0];
      b[v][0] = x;
      in[v][1] = in[v][0];
      in[v][0] = j + 1;
    } else if (b[v][1] > x)
      b[v][1] = x, in[v][1] = j + 1;
  }
  pair<long long, long long> ans,
      mx = pair<long long, long long>(-(1LL << 60), (1LL << 60));
  for (long long i = (0); i < ((1 << 9)); i++) {
    for (long long j = (i); j < (1 << 9); j++) {
      long long x = b[i][0], y = i == j ? b[j][1] : b[j][0];
      long long id1 = in[i][0], id2 = i == j ? in[j][1] : in[j][0];
      if (x == (1LL << 60) || y == (1LL << 60)) continue;
      pair<long long, long long> cur = pair<long long, long long>(0, -x - y);
      long long tmp = i | j;
      for (long long k = (0); k < ((1 << 9)); k++)
        if ((tmp & k) == k) cur.first += a[k];
      if (mx < cur) mx = cur, ans = pair<long long, long long>(id1, id2);
    }
  }
  cout << ans.first << " " << ans.second << endl;
  return 0;
}
