#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 9 + 1;
int n, m, a[N], b[N], mn[1 << M];
pair<int, int> dp[1 << M];
long long c[N];
inline void read_input() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    while (k--) {
      int x;
      cin >> x;
      a[i] |= 1 << --x;
    }
  }
  for (int i = 0; i < m; i++) {
    int k;
    cin >> c[i] >> k;
    while (k--) {
      int x;
      cin >> x;
      b[i] |= 1 << --x;
    }
  }
}
inline void solve() {
  c[m] = 2e9 + 1;
  for (int i = 511; ~i; i--) dp[i] = {m, mn[i] = m};
  for (int i = 0; i < m; i++) {
    for (int j = 511; ~j; j--)
      if (c[i] + c[mn[j]] < c[dp[b[i] | j].first] + c[dp[b[i] | j].second])
        dp[b[i] | j] = {i, mn[j]};
    if (c[i] < c[mn[b[i]]]) mn[b[i]] = i;
  }
}
inline void write_output() {
  pair<pair<int, int>, pair<int, int>> ans = {{-1, 0}, {}};
  for (int i = 511; i; i--) {
    if (dp[i].second == m) continue;
    int good = 0;
    for (int j = 0; j < n; j++) good += (i & a[j]) == a[j];
    ans = max(ans, {{good, -c[dp[i].first] - c[dp[i].second]}, dp[i]});
  }
  cout << ++ans.second.second << ' ' << ++ans.second.first;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  read_input(), solve(), write_output();
  return 0;
}
