#include <bits/stdc++.h>
using namespace std;
ostringstream oss;
const long long maxprize = 1e9 + 1;
int mm1 = 1e9 + 1, mm2 = 1e9 + 1, mmi1 = -1, mmi2;
vector<pair<long long, int>> mprize(512, {2 * maxprize, -1});
pair<long long, pair<int, int>> f(int x, int d, int t) {
  if (x < d || x == 0) {
    if (x == 0 || x == t) return {maxprize * 2, {-1, -1}};
    if (mprize[x].second != mprize[t - x].second)
      return {mprize[x].first + mprize[t - x].first,
              {mprize[x].second, mprize[t - x].second}};
    if (mprize[x].second == mmi1)
      return {mprize[x].first + mm2, {mprize[x].second, mmi2}};
    else
      return {mprize[x].first + mm1, {mprize[x].second, mmi1}};
  }
  if (x & d)
    return min(f(x - d, d << 1, t), f(x, d << 1, t));
  else
    return f(x, d << 1, t);
}
void g(int s, int d, int c, int ind) {
  if (s < d) {
    mprize[s] = min(mprize[s], {c, ind});
  } else if (s & d) {
    g(s, d << 1, c, ind);
    g(s - d, d << 1, c, ind);
  } else
    g(s, d << 1, c, ind);
}
int main() {
  int n, m;
  vector<int> d(9);
  d[0] = 1;
  for (int i = 1; i < 9; i++) d[i] = d[i - 1] * 2;
  cin >> n >> m;
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    int x, tmp;
    cin >> tmp;
    int s = 0;
    while (tmp--) {
      cin >> x;
      s += d[x - 1];
    }
    b[i] = s;
  }
  for (int i = 0; i < m; i++) {
    int tmp, x, s = 0, c;
    cin >> c;
    if (c < mm1) {
      mm2 = mm1;
      mmi2 = mmi1;
      mm1 = c;
      mmi1 = i + 1;
    } else if (c < mm2) {
      mm2 = c;
      mmi2 = i + 1;
    }
    cin >> tmp;
    while (tmp--) {
      cin >> x;
      s += d[x - 1];
    }
    g(s, 1, c, i + 1);
  }
  vector<pair<long long, pair<int, int>>> a(512);
  for (int i = 1; i <= 511; i++) {
    if (mprize[i].second == mmi1)
      a[i] = {mprize[i].first + mm2, {mprize[i].second, mmi2}};
    else
      a[i] = {mprize[i].first + mm1, {mprize[i].second, mmi1}};
    a[i] = min(a[i], f(i, 1, i));
  }
  int maxk = 0;
  int prize, ind1 = -1, ind2 = -1;
  for (int i = 1; i <= 511; i++) {
    int k = 0;
    for (int j = 0; j < n; j++) {
      if ((b[j] & i) == b[j]) k++;
    }
    if (k && k >= maxk && a[i].first < 2LL * maxprize) {
      if (k > maxk || a[i].first < prize) {
        maxk = k;
        prize = a[i].first;
        ind1 = a[i].second.first;
        ind2 = a[i].second.second;
      }
    }
  }
  if (ind1 == -1)
    cout << mmi1 << ' ' << mmi2 << '\n';
  else
    cout << ind1 << ' ' << ind2 << '\n';
}
