#include <bits/stdc++.h>
using namespace std;
long long int binpowmod(long long int a, long long int b, long long int m) {
  a %= m;
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long int modInverse(long long int n, long long int p) {
  return binpowmod(n, p - 2, p);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int i, j, r, f, e, x, y, mid, z, w, h, b, c, d;
  long long int t2, t3, tc, t, t4, k, m, t1, ans = 0;
  long long int l, q, n;
  cin >> n >> m;
  long long int val[n];
  long long int ing[n][9];
  for (i = 0; i < n; ++i) {
    cin >> x;
    val[i] = 0;
    for (j = 0; j < x; ++j) {
      cin >> ing[i][j];
      ing[i][j]--;
      val[i] += (1 << ing[i][j]);
    }
  }
  long long int cost[m];
  long long int val1[m];
  long long int ing1[m][9];
  for (i = 0; i < m; ++i) {
    cin >> cost[i] >> x;
    val1[i] = 0;
    for (j = 0; j < x; ++j) {
      cin >> ing1[i][j];
      ing1[i][j]--;
      val1[i] += (1 << ing1[i][j]);
    }
  }
  t1 = 1 << 9;
  pair<long long int, pair<long long int, long long int>> dp[t1][3];
  for (i = 0; i < t1; ++i) {
    for (j = 0; j < 3; ++j) {
      dp[i][j] = {1e18, {-1, -1}};
    }
  }
  dp[0][0] = {0, {-1, -1}};
  long long int res1 = 0, res2 = -1;
  for (i = 0; i <= t1 - 1; ++i) {
    t2 = 0;
    for (j = 0; j < n; ++j) {
      t3 = val[j] ^ i;
      t4 = val[j] & i;
      if (t3 + t4 == i) t2++;
    }
    for (j = 0; j < m; ++j) {
      t3 = i - (i & val1[j]);
      if (t3 == 0 && dp[i][1].first > cost[j]) {
        dp[i][1].first = cost[j];
        dp[i][1].second = {j, -1};
      }
      if (dp[i][2].first > dp[t3][1].first + cost[j] &&
          dp[t3][1].second.first != j) {
        dp[i][2].first = dp[t3][1].first + cost[j];
        dp[i][2].second = {dp[t3][1].second.first, j};
      }
    }
    if (dp[i][2].first != 1e18) {
      if (t2 > res1) {
        res1 = t2;
        res2 = i;
      } else if (res1 == t2) {
        if (res2 == -1) {
          res2 = i;
        } else if (dp[res2][2].first > dp[i][2].first)
          res2 = i;
      }
    }
  }
  cout << dp[res2][2].second.first + 1 << " " << dp[res2][2].second.second + 1;
  return 0;
}
