#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100 * 1000 + 5;
int n, m, cnt[MAX_N], c[MAX_N];
vector<int> pizza[MAX_N];
void read_input() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int k, x = 0;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int a;
      cin >> a;
      a--;
      x |= 1 << a;
    }
    cnt[x]++;
  }
  for (int i = 0; i < m; i++) {
    int k, x = 0;
    cin >> c[i] >> k;
    for (int j = 0; j < k; j++) {
      int a;
      cin >> a;
      a--;
      x |= 1 << a;
    }
    pizza[x].push_back(i);
  }
}
bool cmp(int i, int j) { return c[i] < c[j]; }
bool ok(int a, int b, int msk) {
  a |= b;
  return (a & msk) == msk;
}
void solve() {
  for (int i = 0; i < 1 << 9; i++) sort(pizza[i].begin(), pizza[i].end(), cmp);
  pair<int, int> best = {0, -2e9}, ans;
  for (int msk = 0; msk < 1 << 9; msk++) {
    pair<int, int> x = {0, 2e9}, ind;
    for (int sub = msk; sub; sub = (sub - 1) & msk) x.first += cnt[sub];
    for (int m1 = 0; m1 < 1 << 9; m1++)
      for (int m2 = 0; m2 < m1; m2++)
        if ((int)pizza[m1].size() > (m1 == m2) && !pizza[m2].empty() &&
            ok(m1, m2, msk)) {
          int i = pizza[m1][0], j = pizza[m2][m1 == m2];
          if (c[i] + c[j] < x.second) {
            x.second = c[i] + c[j];
            ind = {i, j};
          }
        }
    x.second *= -1;
    if (x > best && x.second != -2e9) best = x, ans = ind;
  }
  if (ans.first == ans.second)
    cout << "1 2\n";
  else
    cout << ans.first + 1 << ' ' << ans.second + 1 << endl;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  read_input();
  solve();
}
