#include <bits/stdc++.h>
using namespace std;
int n, m, clients[520], good_for[520];
int best_pizza[520];
int cost[100005];
int main() {
  int i, j, k, b, f;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &f);
    b = 0;
    for (j = 0; j < f; j++) {
      scanf("%d", &k);
      k--;
      b ^= (1 << k);
    }
    clients[b]++;
  }
  for (i = 0; i < (1 << 9); i++) {
    for (j = 0; j < (1 << 9); j++)
      if ((j & i) == i) good_for[j] += clients[i];
  }
  for (i = 0; i < (1 << 9); i++) best_pizza[i] = 520 - 1;
  cost[520 - 1] = 1 << 30;
  int solmax = -1;
  int cost_sol = 0, index1, index2;
  for (i = 1; i <= m; i++) {
    scanf("%d", &cost[i]);
    scanf("%d", &f);
    b = 0;
    for (j = 0; j < f; j++) {
      scanf("%d", &k);
      k--;
      b ^= (1 << k);
    }
    if (best_pizza[b] != 520 - 1) {
      if (good_for[b] > solmax ||
          (good_for[b] == solmax && cost[best_pizza[b]] + cost[i] < cost_sol)) {
        solmax = good_for[b];
        cost_sol = cost[best_pizza[b]] + cost[i];
        index1 = best_pizza[b];
        index2 = i;
      }
    }
    if (cost[i] < cost[best_pizza[b]]) best_pizza[b] = i;
  }
  for (i = 0; i < (1 << 9); i++)
    if (best_pizza[i] != 520 - 1)
      for (j = i + 1; j < (1 << 9); j++)
        if (best_pizza[j] != 520 - 1)
          if (good_for[i | j] > solmax ||
              (good_for[i | j] == solmax &&
               cost[best_pizza[i]] + cost[best_pizza[j]] < cost_sol)) {
            solmax = good_for[i | j];
            cost_sol = cost[best_pizza[i]] + cost[best_pizza[j]];
            index1 = best_pizza[i];
            index2 = best_pizza[j];
          }
  cout << index1 << ' ' << index2 << '\n';
  return 0;
}
