#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
priority_queue<int, vector<int>, greater<int> > pq;
vector<int> v;
int cnt[512];
pair<int, int> vec[512];
pair<int, int> sec[512];
int main() {
  memset(vec, 0x3f, sizeof(vec));
  memset(vec, 0x3f, sizeof(vec));
  int n, m;
  scanf("%d %d", &n, &m);
  int t;
  for (int i = 0; i < n; i++) {
    scanf("%d", &t);
    vector<int> kk;
    int k, bit = 0;
    while (t--) {
      scanf("%d", &k);
      k--;
      bit |= 1 << k;
    }
    for (int j = 0; j < 512; j++)
      if ((bit & j) == bit) cnt[j]++;
  }
  for (int i = 0; i < m; i++) {
    int p, t, bit = 0;
    scanf("%d %d", &p, &t);
    while (t--) {
      int k;
      scanf("%d", &k);
      k--;
      bit |= (1 << k);
    }
    if (vec[bit].first > p) {
      sec[bit] = vec[bit];
      vec[bit] = {p, i + 1};
    } else if (sec[bit].first > p) {
      sec[bit] = {p, i + 1};
    }
  }
  int mav = -1, mivp = 1e9, u, v;
  for (int i = 0; i < 512; i++) {
    if (vec[i].first > 1e9) continue;
    for (int j = i + 1; j < 512; j++) {
      if (i == j || vec[j].first > 1e9) continue;
      int k = i | j;
      if (mav < cnt[k]) {
        mav = cnt[k];
        mivp = vec[i].first + vec[j].first;
        u = vec[i].second;
        v = vec[j].second;
      } else if (mav == cnt[k] && mivp > vec[i].first + vec[j].first) {
        mivp = vec[i].first + vec[j].first;
        u = vec[i].second;
        v = vec[j].second;
      }
    }
    if (sec[i].first > 1e9) continue;
    if (mav < cnt[i]) {
      mav = cnt[i];
      mivp = vec[i].first + sec[i].first;
      u = vec[i].second, v = sec[i].second;
    } else if (mav == cnt[i] && mivp > vec[i].first + sec[i].first) {
      mivp = vec[i].first + sec[i].first;
      u = vec[i].second, v = sec[i].second;
    }
  }
  printf("%d %d", u, v);
}
