#include <bits/stdc++.h>
using namespace std;
struct P {
  int x, y, z;
};
vector<int> v;
int i, n, m, k, a, d, b, c;
int e;
int o[112222];
int l[113222][2];
int j[1];
int dx[10] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[10] = {1, 0, -1, 0, 1, -1, 1, -1},
    dz[10] = {0, 0, 0, 0, 1, -1};
int px[10] = {-1, 1, 1, -1, 1, 1, -1, -1},
    py[10] = {1, 1, -1, -1, 1, -1, 1, -1};
long long mod = 1000000007, mod2 = 1000000009, mod3 = 2017;
long long x, z, y;
double pi = 3.14159265;
P u[1151], u1[1111];
stack<long long> s;
queue<P> q;
char r[2211];
map<int, int> p;
int main() {
  scanf("%d %d", &a, &b);
  for (int t = 1; t <= a; t++) {
    scanf("%d", &n);
    for (int w = 1; w <= n; o[t] |= (1 << (i - 1)), w++) scanf("%d", &i);
  }
  for (int t = 1; t <= b; t++) {
    scanf("%d %d", &l[t][0], &n);
    for (int w = 1; w <= n; l[t][1] |= (1 << (i - 1)), w++) scanf("%d", &i);
    if (u1[l[t][1]].x == 0 || u1[l[t][1]].x > l[t][0])
      u1[l[t][1]] = {l[t][0], t};
  }
  for (int t = 1; t <= b; t++) {
    for (int w = 0; w <= 1023; w++)
      if (u1[w].y != t && u1[w].y &&
          (u[l[t][1] | w].x == 0 || u[l[t][1] | w].x > l[t][0] + u1[w].x)) {
        u[l[t][1] | w] = {l[t][0] + u1[w].x, u1[w].y, t};
      }
  }
  n = 0;
  m = 987654321;
  i = 987654321;
  for (int t = 0; t <= 1023; t++)
    if (u[t].x) {
      int k = 0;
      for (int w = 1; w <= a; w++)
        if ((t & o[w]) == o[w]) k++;
      if (k > n || (k == n && m > u[t].x)) n = k, m = u[t].x, i = t;
    }
  if (i == 987654321) {
    n = 1987654321;
    m = 1987654321;
    for (int t = 1; t <= b; t++)
      if (n > l[t][0])
        m = n, d = c, n = l[t][0], c = t;
      else if (m > l[t][0])
        m = l[t][0], d = t;
    printf("%d %d", c, d);
  } else
    printf("%d %d", u[i].y, u[i].z);
}
