#include <bits/stdc++.h>
using namespace std;
int n, m;
const int MX = 1e5 + 5;
int a[MX];
int b[MX];
int c[MX];
int q = 1, w = 2;
int ans;
int d[MX];
int e[MX];
int main() {
  int i, j;
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (j = 0; j < k; j++) {
      int x;
      cin >> x;
      x--;
      a[i] += (1 << x);
    }
    for (j = 1; j < (1 << 9); j++) {
      if ((a[i] & j) == a[i]) b[j]++;
    }
  }
  for (i = 1; i <= m; i++) {
    int k;
    cin >> e[i];
    cin >> k;
    for (j = 0; j < k; j++) {
      int x;
      cin >> x;
      x--;
      c[i] += (1 << x);
    }
    for (j = 1; j < (1 << 9); j++) {
      if ((c[i] & j) == j) {
        if (d[j] == 0) d[j] = i;
        if (e[i] < e[d[j]]) d[j] = i;
      }
    }
  }
  for (i = 1; i < (1 << 9); i++) {
    for (j = 1; j <= m; j++) {
      if ((c[j] & i) == c[j])
        ;
      else
        continue;
      if (d[i - c[j]]) {
        if (b[i] > ans) {
          q = j;
          w = d[i - c[j]];
          ans = b[i];
        }
        if (b[i] == ans) {
          if (e[d[i - c[j]]] + e[j] < e[q] + e[w]) {
            q = d[i - c[j]];
            w = j;
          }
        }
      }
    }
  }
  cout << q << " " << w;
  return 0;
}
