#include <bits/stdc++.h>
using namespace std;
int pizza[1 << 10], id[1 << 10], cnt[1 << 10];
int dp[1 << 10], dp_id[2][1 << 10];
int val[123456];
bool ok[1 << 10], exist[1 << 10];
int main() {
  ios::sync_with_stdio();
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int t, res = 0, x;
    cin >> t;
    while (t--) {
      cin >> x;
      res += (1 << (x - 1));
    }
    cnt[res]++;
  }
  for (int i = 0; i < (1 << 10); i++) dp[i] = 2e9 + 7;
  for (int i = 1; i <= m; i++) {
    int t, x, res = 0;
    cin >> val[i] >> t;
    while (t--) {
      cin >> x;
      res += (1 << (x - 1));
    }
    for (int j = 0; j < (1 << 9); j++) {
      if (exist[j]) {
        int tot = val[id[j]] + val[i];
        if (tot < dp[j | res]) {
          dp[j | res] = tot;
          dp_id[0][j | res] = id[j];
          dp_id[1][j | res] = i;
        }
        ok[j | res] = true;
      }
    }
    if (exist[res]) {
      if (pizza[res] > val[i]) {
        pizza[res] = val[i];
        id[res] = i;
      }
    } else {
      pizza[res] = val[i];
      id[res] = i;
      exist[res] = true;
    }
  }
  int tot_cnt = 0, tot = 2e9 + 7, ans1 = 0, ans2 = 0;
  for (int i = 0; i < (1 << 9); i++) {
    if (!ok[i]) continue;
    int cur_cnt = 0;
    for (int j = 0; j < (1 << 9); j++)
      if ((i & j) == j) cur_cnt += cnt[j];
    if (cur_cnt > tot_cnt) {
      tot_cnt = cur_cnt;
      tot = dp[i];
      ans1 = dp_id[0][i];
      ans2 = dp_id[1][i];
    } else if (cur_cnt == tot_cnt && tot > dp[i]) {
      tot = dp[i];
      ans1 = dp_id[0][i];
      ans2 = dp_id[1][i];
    }
  }
  cout << ans1 << ' ' << ans2 << endl;
}
