#include <bits/stdc++.h>
using namespace std;
const int B = 9;
const long long inf = 1e18;
long long cost[2][1 << B];
int dp[1 << B], who[2][1 << B];
template <typename T>
void min_self(T& a, T b) {
  a = min(a, b);
}
template <typename T>
void max_self(T& a, T b) {
  a = max(a, b);
}
void upd(int mask, long long c, int i) {
  if (c <= cost[0][mask]) {
    cost[1][mask] = cost[0][mask];
    who[1][mask] = who[0][mask];
    cost[0][mask] = c;
    who[0][mask] = i;
  } else if (c < cost[1][mask]) {
    cost[1][mask] = c;
    who[1][mask] = i;
  }
}
long long get(int s1, int s2, int& i, int& j) {
  if (s1 == s2) {
    if (cost[1][s1] == inf) {
      return inf;
    }
    i = who[0][s1], j = who[1][s1];
    return cost[0][s1] + cost[1][s1];
  } else {
    if (cost[0][s1] == inf || cost[0][s2] == inf) {
      return inf;
    }
    i = who[0][s1], j = who[0][s2];
    return cost[0][s1] + cost[0][s2];
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < int(n); i++) {
    int S;
    cin >> S;
    for (int j = 0; j < int(S); j++) {
      int x;
      cin >> x;
      --x;
      a[i] |= 1 << x;
    }
    ++dp[a[i]];
  }
  for (int b = 0; b < int(B); b++) {
    for (int s = 0; s < int(1 << B); s++) {
      if (s >> b & 1) {
        dp[s] += dp[s ^ 1 << b];
      }
    }
  }
  fill((long long*)cost, (long long*)cost + (1 << B), inf);
  for (int i = 0; i < int(m); i++) {
    long long c;
    int S;
    cin >> c >> S;
    int mask = 0;
    for (int j = 0; j < int(S); j++) {
      int x;
      cin >> x;
      --x;
      mask |= 1 << x;
    }
    upd(mask, c, i + 1);
  }
  int most = 0;
  for (int s1 = 0; s1 < int(1 << B); s1++) {
    for (int s2 = 0; s2 < int(1 << B); s2++) {
      int z, zz;
      if (get(s1, s2, z, zz) < inf) {
        max_self(most, dp[s1 | s2]);
      }
    }
  }
  long long ans = inf;
  int i = -1, j = -1;
  for (int s1 = 0; s1 < int(1 << B); s1++) {
    for (int s2 = 0; s2 < int(1 << B); s2++) {
      if (dp[s1 | s2] == most) {
        int maybe_i, maybe_j;
        long long total_cost = get(s1, s2, maybe_i, maybe_j);
        if (total_cost < ans) {
          ans = total_cost;
          i = maybe_i, j = maybe_j;
        }
      }
    }
  }
  assert(ans < inf);
  cout << i << " " << j << endl;
}
