#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long INF = 1e18;
vector<pair<long long, int> > p[(1 << 9) + 5];
int cnt[(1 << 9) + 5];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int f, x = 0;
    cin >> f;
    for (int j = 1; j <= f; j++) {
      int y;
      cin >> y;
      x ^= (1 << (y - 1));
    }
    cnt[x]++;
  }
  for (int i = 1; i <= m; i++) {
    int cost, r, x = 0;
    cin >> cost >> r;
    for (int j = 1; j <= r; j++) {
      int y;
      cin >> y;
      x ^= (1 << (y - 1));
    }
    p[x].push_back({cost, i});
    sort(p[x].begin(), p[x].end());
    while (p[x].size() > 2) p[x].pop_back();
  }
  int most = 0;
  long long Mincost = INF;
  pair<int, int> ans;
  for (int i = 0; i < (1 << 9); i++)
    for (int j = 0; j < (1 << 9); j++) {
      long long cost;
      int nr = 0;
      pair<int, int> now;
      int mask = -1;
      if (i == j) {
        if (p[i].size() < 2) continue;
        mask = i;
        cost = p[i][0].first + p[i][1].first;
        now = {p[i][0].second, p[i][1].second};
      } else {
        if (p[i].empty() || p[j].empty()) continue;
        mask = (i | j);
        cost = p[i][0].first + p[j][0].first;
        now = {p[i][0].second, p[j][0].second};
      }
      if (mask != -1) {
        for (int k = 0; k < (1 << 9); k++)
          if ((k & mask) == k) nr += cnt[k];
        if (nr > most || (nr == most && Mincost > cost)) {
          most = nr;
          Mincost = cost;
          ans = now;
        }
      }
    }
  cout << ans.first << " " << ans.second;
}
