#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> b(n, 0), a(m, 0), c(m);
  long long ans = 0, cost = 1e15;
  pair<long long, long long> best;
  for (long long i = 0; i < n; ++i) {
    long long x, z = 0;
    cin >> x;
    for (long long j = 0; j < x; ++j) {
      long long k;
      cin >> k;
      z |= (1 << (k - 1));
    }
    b[i] = z;
  }
  for (long long i = 0; i < m; ++i) {
    cin >> c[i];
    long long x, z = 0;
    cin >> x;
    for (long long j = 0; j < x; ++j) {
      long long k;
      cin >> k;
      z |= (1 << (k - 1));
    }
    a[i] = z;
  }
  vector<set<pair<long long, long long>>> val(1 << 9);
  for (long long j = 0; j < m; ++j) val[a[j]].insert(make_pair(c[j], j));
  for (long long i = 0; i < (1 << 9); ++i) {
    long long z = 1e15;
    pair<long long, long long> l;
    for (long long j = 0; j < val.size(); ++j) {
      if ((j & i) == j && val[j].size()) {
        for (long long k = j; k < val.size(); ++k) {
          if ((k & i) == k && val[k].size()) {
            if ((j | k) == i) {
              if (j == k) {
                if (val[j].size() > 1) {
                  auto x = val[j].begin();
                  auto y = val[j].begin();
                  ++y;
                  long long x1 = x->first, i1 = x->second;
                  long long y1 = y->first, i2 = y->second;
                  if (z > x1 + y1) {
                    z = x1 + y1;
                    l = make_pair(i1, i2);
                  }
                }
              } else {
                auto x = val[j].begin();
                auto y = val[k].begin();
                long long x1 = x->first, i1 = x->second;
                long long y1 = y->first, i2 = y->second;
                if (z > x1 + y1) {
                  z = x1 + y1;
                  l = make_pair(i1, i2);
                }
              }
            }
          }
        }
      }
    }
    long long c = 0;
    for (long long j = 0; j < n; ++j) {
      if ((b[j] & i) == b[j]) {
        ++c;
      }
    }
    if (z != 1e15) {
      if (c > ans) {
        ans = c;
        best = l;
        cost = z;
      } else if (ans == c && z < cost) {
        cost = z;
        best = l;
      }
    }
  }
  cout << best.first + 1 << " " << best.second + 1 << "\n";
}
