#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int MAXN = 2e5 + 10;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) is >> *i;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "\n" : " ");
  return os;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> many(512, 0);
  for (int i = 0, ilen = (int)(n); i < ilen; ++i) {
    int k, sum = 0;
    cin >> k;
    for (int j = 0, jlen = (int)(k); j < jlen; ++j) {
      int x;
      cin >> x;
      sum |= (1 << (x - 1));
    }
    for (int j = 0, jlen = (int)(512); j < jlen; ++j)
      if ((j & sum) == sum) ++many[j];
  }
  vector<pii> pizza[512];
  for (int i = 0, ilen = (int)(m); i < ilen; ++i) {
    int k, c, sum = 0;
    cin >> c >> k;
    for (int j = 0, jlen = (int)(k); j < jlen; ++j) {
      int x;
      cin >> x;
      sum |= (1 << (x - 1));
    }
    pizza[sum].push_back(make_pair(c, i + 1));
  }
  int maxhappy = 0, mincost = 1 << 30;
  int a = 1, b = 2;
  for (int i = 0; i < 512; ++i) {
    sort(begin(pizza[i]), end(pizza[i]));
    if (pizza[i].size() >= 2 &&
        (many[i] > maxhappy ||
         (many[i] == maxhappy &&
          pizza[i][0].first + pizza[i][1].first < mincost)))
      maxhappy = many[i], mincost = pizza[i][0].first + pizza[i][1].first,
      a = pizza[i][0].second, b = pizza[i][1].second;
  }
  for (int i = 0; i < 512; ++i) {
    for (int j = i + 1; j < 512; ++j) {
      if (pizza[i].size() && pizza[j].size() &&
          (many[i | j] > maxhappy ||
           (many[i | j] == maxhappy &&
            pizza[i][0].first + pizza[j][0].first < mincost)))
        maxhappy = many[i | j], mincost = pizza[i][0].first + pizza[j][0].first,
        a = pizza[i][0].second, b = pizza[j][0].second;
    }
  }
  cout << a << " " << b << '\n';
  return 0;
}
