#include <bits/stdc++.h>
using namespace std;
int n, m;
int cnt[1 << 9], num[1 << 9];
pair<int, int> MIN[1 << 9][2];
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int x, S = 0;
    cin >> x;
    for (int j = 1; j <= x; j++) {
      int y;
      cin >> y;
      S |= (1 << (y - 1));
    }
    cnt[S]++;
  }
  for (int i = 0; i < 512; i++)
    for (int j = i; j; j = (j - 1) & i) num[i] += cnt[j];
  for (int i = 0; i < 512; i++)
    MIN[i][0] = MIN[i][1] = pair<int, int>(2147483647, 0);
  for (int i = 1; i <= m; i++) {
    int c, x, S = 0;
    cin >> c >> x;
    for (int j = 1; j <= x; j++) {
      int y;
      cin >> y;
      S |= (1 << (y - 1));
    }
    if (c <= MIN[S][0].first) {
      MIN[S][1] = MIN[S][0];
      MIN[S][0] = pair<int, int>(c, i);
    } else if (c < MIN[S][1].first)
      MIN[S][1] = pair<int, int>(c, i);
  }
  pair<int, int> ans = pair<int, int>(0, 2147483647), f_ans;
  for (int i = 0; i < 512; i++)
    if (MIN[i][1].first != 2147483647) {
      int cost = MIN[i][0].first + MIN[i][1].first;
      if (num[i] > ans.first || (num[i] == ans.first && cost < ans.second)) {
        ans = pair<int, int>(num[i], cost);
        f_ans = pair<int, int>(MIN[i][0].second, MIN[i][1].second);
      }
    }
  for (int i = 0; i < 512; i++)
    for (int j = i + 1; j < 512; j++)
      if (MIN[i][0].first != 2147483647 && MIN[j][0].first != 2147483647) {
        int cost = MIN[i][0].first + MIN[j][0].first;
        if (num[i | j] > ans.first ||
            (num[i | j] == ans.first && cost < ans.second)) {
          ans = pair<int, int>(num[i | j], cost);
          f_ans = pair<int, int>(MIN[i][0].second, MIN[j][0].second);
        }
      }
  cout << f_ans.first << ' ' << f_ans.second << endl;
  return 0;
}
