#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 20;
const int INF = 2e9 + 15;
int n, m, cnt[N], mask[N], p[N], c[N];
pair<int, int> can[N];
vector<int> res = {-1, INF, -1, -1};
int main() {
  ios::sync_with_stdio(false);
  for (int i = 0; i < 1 << 9; i++) can[i] = {INF, INF};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int f;
    cin >> f;
    while (f--) {
      int x;
      cin >> x;
      x--;
      mask[i] = mask[i] | (1 << x);
    }
  }
  for (int i = 0; i < m; i++) {
    int f;
    cin >> c[i] >> f;
    while (f--) {
      int x;
      cin >> x;
      x--;
      p[i] = p[i] | (1 << x);
    }
    can[0] = min(can[0], {c[i], i});
  }
  for (int i = 0; i < 1 << 9; i++)
    for (int j = 0; j < n; j++)
      if ((mask[j] & i) == mask[j]) cnt[i]++;
  for (int i = 0; i < m; i++)
    for (int s = p[i]; s; s = (s - 1) & p[i]) can[s] = min(can[s], {c[i], i});
  for (int i = 0; i < 1 << 9; i++) {
    for (int s = i; s; s = (s - 1) & i) {
      if (can[s].first < INF && can[i ^ s].first < INF) {
        res = max(res, {cnt[i], -(can[s].first + can[i ^ s].first),
                        can[s].second, can[i ^ s].second});
      }
    }
  }
  vector<pair<int, int>> v;
  for (int i = 0; i < m; i++) v.push_back({c[i], i});
  sort(v.begin(), v.end());
  if (res[2] == -1)
    for (auto it : v)
      if (it.second != res[3]) {
        res[2] = it.second;
        break;
      }
  if (res[3] == -1)
    for (auto it : v)
      if (it.second != res[2]) {
        res[3] = it.second;
        break;
      }
  if (res[2] == res[3])
    for (auto it : v)
      if (it.second != res[2]) {
        res[3] = it.second;
        break;
      }
  cout << res[2] + 1 << " " << res[3] + 1 << endl;
}
