#include <bits/stdc++.h>
using namespace std;
const int LOG = 9;
const int N = 1 << LOG;
const int INF = 1e9;
struct Pizza {
  int mask, cost, id;
  Pizza(int _mask, int _cost, int _id) : mask(_mask), cost(_cost), id(_id){};
  Pizza(){};
  bool operator==(Pizza& X) { return X.id == id; }
};
vector<pair<int, int> > g[N];
vector<Pizza> v;
int fr[N];
int cnt[N];
int getMask() {
  int mask = 0;
  static int k;
  scanf("%d", &k);
  while (k--) {
    static int bit;
    scanf("%d", &bit);
    mask ^= (1 << (bit - 1));
  }
  return mask;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int mask = 0; mask < N; ++mask) fr[mask] = 0;
  for (int i = 0; i < n; ++i) ++fr[getMask()];
  for (int i = 0; i < m; ++i) {
    static int c;
    scanf("%d", &c);
    g[getMask()].push_back({c, i});
  }
  for (int mask = 0; mask < N; ++mask) {
    sort(g[mask].begin(), g[mask].end());
    for (int i = 0; i < min(2, (int)g[mask].size()); ++i)
      v.push_back(Pizza(mask, g[mask][i].first, g[mask][i].second));
  }
  for (int maskPizza = 0; maskPizza < N; ++maskPizza)
    for (int maskPeople = 0; maskPeople < N; ++maskPeople)
      if ((maskPizza | maskPeople) == maskPizza)
        cnt[maskPizza] += fr[maskPeople];
  pair<int, int> ans = {1, 2};
  int maxPeople = -1, minCost = INF;
  for (Pizza& X : v) {
    for (Pizza& Y : v) {
      if (X == Y) continue;
      if (cnt[X.mask | Y.mask] > maxPeople) {
        minCost = X.cost + Y.cost;
        maxPeople = cnt[X.mask | Y.mask];
        ans.first = X.id;
        ans.second = Y.id;
      } else if (cnt[X.mask | Y.mask] == maxPeople) {
        if (X.cost + Y.cost < minCost) {
          minCost = X.cost + Y.cost;
          maxPeople = cnt[X.mask | Y.mask];
          ans.first = X.id;
          ans.second = Y.id;
        }
      }
    }
  }
  printf("%d %d", ans.first + 1, ans.second + 1);
}
