#include <bits/stdc++.h>
using namespace std;
constexpr long long int inf = 1000000000000000000ll;
long long int costs[1 << 10];
long long int fs[1 << 10];
int main() {
  for (int i = 0; i < (1 << 10); i++) {
    costs[i] = inf;
  }
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  vector<int> likes;
  vector<int> pizzas;
  vector<int> pizzacost;
  int n, m;
  cin >> n >> m;
  likes.reserve(n);
  pizzas.reserve(m);
  pizzacost.reserve(m);
  for (int i = 0; i < n; i++) {
    int f;
    cin >> f;
    int pizza = 0;
    for (int j = 0; j < f; j++) {
      int b;
      cin >> b;
      pizza |= 1 << (b - 1);
    }
    likes.push_back(pizza);
  }
  for (int i = 0; i < m; i++) {
    int f;
    long long int cost;
    cin >> cost >> f;
    int pizza = 0;
    for (int j = 0; j < f; j++) {
      int b;
      cin >> b;
      pizza |= 1 << (b - 1);
    }
    pizzas.push_back(pizza);
    costs[pizza] = min(costs[pizza], cost);
    pizzacost.push_back(cost);
  }
  for (int p = 0; p < (1 << 9); p++) {
    int f = 0;
    for (int fr : likes) {
      if ((p & fr) == fr) {
        f++;
      }
    }
    fs[p] = f;
  }
  long long int bp1 = 0;
  long long int bp2 = 0;
  int bl = 0;
  for (int p1 = 0; p1 < (1 << 9); p1++) {
    for (int p2 = 0; p2 < (1 << 9); p2++) {
      long long int cost = costs[p1] + costs[p2];
      if (cost > inf) continue;
      int f = fs[p1 | p2];
      if (f > bl || (f == bl && cost < costs[bp1] + costs[bp2]) ||
          (f == bl && cost == costs[bp1] + costs[bp2] && p1 != p2)) {
        bl = f;
        bp1 = p1;
        bp2 = p2;
      }
    }
  }
  int b1i = -1;
  int b2i = -1;
  for (int i = 0; i < m; i++) {
    if (pizzas[i] == bp1 && costs[bp1] == pizzacost[i]) {
      b1i = i;
    }
    if (pizzas[i] == bp2 && costs[bp2] == pizzacost[i]) {
      b2i = i;
    }
  }
  if (b1i == b2i) {
    b2i = -1;
    for (int i = 0; i < m; i++) {
      if (i != b1i && (b2i == -1 || pizzacost[i] < pizzacost[b2i])) {
        b2i = i;
      }
    }
  }
  cout << b1i + 1 << " " << b2i + 1 << "\n";
}
