#include <bits/stdc++.h>
using namespace std;
const long long oo = 100000000000000000LL;
const double PI = 3.1415931;
const double eps = 1e-9;
const int maxMask = 1024;
const int maxN = 100000 + 10;
int cnt[maxMask], price[maxMask], price2[maxMask];
bool use[maxMask];
int P[maxN], a[maxN];
int satisfy(int tk, int mask) {
  tk = 511 - tk;
  if (mask & tk) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    int mask = 0;
    for (int i = 1; i <= t; i++) {
      int x;
      cin >> x;
      mask |= (1 << (x - 1));
    }
    cnt[mask]++;
  }
  vector<int> check;
  for (int i = 1; i <= m; i++) {
    cin >> P[i];
    int t;
    cin >> t;
    int mask = 0;
    for (int j = 1; j <= t; j++) {
      int x;
      cin >> x;
      mask |= (1 << (x - 1));
    }
    if (price[mask] == 0)
      price[mask] = i;
    else if (price2[mask] == 0) {
      price2[mask] = i;
      if (P[price2[mask]] < P[price[mask]]) {
        swap(price2[mask], price[mask]);
      }
    } else if (P[price[mask]] > P[i]) {
      price2[mask] = price[mask];
      price[mask] = i;
    } else if (P[price2[mask]] > P[i]) {
      price2[mask] = i;
    }
    a[i] = mask;
    if (!use[mask]) {
      check.push_back(i);
      use[mask] = true;
    }
  }
  int ans = 0, cost = 2 * 1000000000 + 1;
  int p1 = 1, p2 = 2;
  for (int i = 0; i < check.size(); i++) {
    if (price2[a[check[i]]] == 0) continue;
    int j = i;
    int cur = 0;
    int tk = a[check[i]] | a[check[j]];
    for (int mask = 0; mask < (1 << 9); mask++) {
      if (cnt[mask] > 0 && satisfy(tk, mask)) {
        cur += cnt[mask];
      }
    }
    if (cur > ans) {
      cost = P[price[a[check[i]]]] + P[price2[a[check[j]]]];
      ans = cur;
      p1 = price[a[check[i]]];
      p2 = price2[a[check[j]]];
    } else if (cur == ans &&
               cost > P[price[a[check[i]]]] + P[price2[a[check[j]]]]) {
      cost = P[price[a[check[i]]]] + P[price2[a[check[j]]]];
      ans = cur;
      p1 = price[a[check[i]]];
      p2 = price2[a[check[j]]];
    }
  }
  for (int i = 0; i < check.size(); i++) {
    for (int j = i + 1; j < check.size(); j++) {
      int cur = 0;
      int tk = a[check[i]] | a[check[j]];
      for (int mask = 0; mask < (1 << 9); mask++) {
        if (cnt[mask] > 0 && satisfy(tk, mask)) {
          cur += cnt[mask];
        }
      }
      if (cur > ans) {
        cost = P[price[a[check[i]]]] + P[price[a[check[j]]]];
        ans = cur;
        p1 = price[a[check[i]]];
        p2 = price[a[check[j]]];
      } else if (cur == ans &&
                 cost > P[price[a[check[i]]]] + P[price[a[check[j]]]]) {
        cost = P[price[a[check[i]]]] + P[price[a[check[j]]]];
        ans = cur;
        p1 = price[a[check[i]]];
        p2 = price[a[check[j]]];
      }
    }
  }
  assert(p1 != p2);
  cout << p1 << " " << p2 << endl;
  return 0;
}
