#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, len, p, g1 = -1, g2 = -1, min1 = 10000000000, min2 = min1;
  cin >> n >> m;
  vector<long long> fav(n), act(m), cost(m);
  for (int i = 0; i < n; ++i) {
    cin >> len;
    for (int j = 0; j < len; ++j) {
      cin >> p;
      fav[i] += 1ll << (p - 1);
    }
  }
  for (int i = 0; i < m; ++i) {
    cin >> cost[i] >> len;
    if (cost[i] < min1) {
      min2 = min1;
      min1 = cost[i];
      g2 = g1;
      g1 = i;
    } else if (cost[i] < min2) {
      min2 = cost[i];
      g2 = i;
    }
    for (int j = 0; j < len; ++j) {
      cin >> p;
      act[i] += 1ll << (p - 1);
    }
  }
  vector<long long> pl(512), opt(512, -1);
  for (long long i = 0; i < 512; ++i) {
    for (auto j : fav) {
      if ((i & j) == j) ++pl[i];
    }
    for (int j = 0; j < m; ++j) {
      if ((i & act[j]) == i) {
        if (opt[i] == -1 || cost[opt[i]] > cost[j]) opt[i] = j;
      }
    }
  }
  long long ans = 100000000000000000, please = -1, x1 = -1, x2 = -1;
  for (int i = 0; i < 512; ++i) {
    for (int j = 0; j < m; ++j) {
      long long req = i ^ (act[j]);
      if (opt[req] > -1 and please <= pl[i]) {
        if (please == pl[i]) {
          if (ans > cost[j] + cost[opt[req]]) {
            ans = cost[j] + cost[opt[req]];
            x1 = j;
            x2 = opt[req];
          }
        } else {
          please = pl[i];
          ans = cost[j] + cost[opt[req]];
          x1 = j;
          x2 = opt[req];
        }
      }
    }
  }
  if (x1 == x2) {
    if (cost[x1] == min1)
      cout << x1 + 1 << " " << g2 + 1;
    else
      cout << x1 + 1 << " " << g1 + 1;
  } else
    cout << x1 + 1 << " " << x2 + 1;
}
