#include <bits/stdc++.h>
using namespace std;
inline long long fpow(long long n, long long k, int p = 998244353) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline long long inv(long long a, long long p = 998244353) {
  return fpow(a, p - 2, p);
}
inline long long addmod(long long a, long long val, long long p = 998244353) {
  {
    if ((a = (a + val)) >= p) a -= p;
  }
  return a;
}
inline long long submod(long long a, long long val, long long p = 998244353) {
  {
    if ((a = (a - val)) < 0) a += p;
  }
  return a;
}
inline long long mult(long long a, long long b, long long p = 998244353) {
  return (long long)a * b % p;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  vector<vector<int> > child(n, vector<int>(9, 0)), pizza(m, vector<int>(9, 0));
  vector<long long> cost(m, 0);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      int y;
      cin >> y;
      y--;
      child[i][y] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> cost[i];
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      int y;
      cin >> y;
      y--;
      pizza[i][y] = 1;
    }
  }
  vector<long long> stsfy((1ll << 9), 0);
  vector<pair<long long, long long> > minicost((1ll << 9), {-1, -1});
  long long lim = (1ll << 9);
  for (int i = 0; i < lim; i++) {
    int ctr = 0;
    for (int j = 0; j < n; j++) {
      bool check = true;
      for (int k = 0; k < 9; k++) {
        if (child[j][k] && !((i >> k) & 1)) {
          check = false;
          break;
        }
      }
      if (check) ctr++;
    }
    stsfy[i] = ctr;
    long long mini = LLONG_MAX, miniidx = -1;
    for (int j = 0; j < m; j++) {
      bool check = true;
      for (int k = 0; k < 9; k++) {
        if ((i >> k) & 1 && !pizza[j][k]) {
          check = false;
          break;
        }
      }
      if (check) {
        if (cost[j] < mini) {
          mini = cost[j];
          miniidx = j;
        }
      }
    }
    minicost[i].first = miniidx;
    mini = LLONG_MAX;
    miniidx = -1;
    for (int j = 0; j < m; j++) {
      bool check = true;
      if (j == minicost[i].first) continue;
      for (int k = 0; k < 9; k++) {
        if ((i >> k) & 1 && !pizza[j][k]) {
          check = false;
          break;
        }
      }
      if (check) {
        if (cost[j] < mini) {
          mini = cost[j];
          miniidx = j;
        }
      }
    }
    minicost[i].second = miniidx;
  }
  vector<pair<long long, pair<long long, long long> > > stsfycost(
      (1 << 9), {LLONG_MAX, {-1, -1}});
  for (int i = 0; i < lim; i++) {
    vector<int> setbit;
    for (int j = 0; j < 9; j++) {
      if ((i >> j) & 1) {
        setbit.push_back(j);
      }
    }
    int sz = setbit.size();
    int limi = (1 << sz);
    for (int j = 0; j < (limi); j++) {
      int mask1 = i, mask2 = 0;
      for (int k = 0; k < sz; k++) {
        if ((j >> k) & 1) {
          mask1 ^= (1ll << setbit[k]);
          mask2 |= (1ll << setbit[k]);
        }
      }
      pair<int, int> p1 = minicost[mask1], p2 = minicost[mask2];
      if (mask1 == i && p1.first == -1 || mask2 == i && p2.first == -1)
        continue;
      if (mask1 != i && mask2 != i && (p1.first == -1 || p2.first == -1))
        continue;
      else {
        if (p1.first == p2.first) {
          if (p2.second != -1) {
            if (cost[p1.first] + cost[p2.second] <= stsfycost[i].first) {
              stsfycost[i] = {cost[p1.first] + cost[p2.second],
                              {p1.first, p2.second}};
            }
          }
          if (p1.second != -1) {
            if (cost[p2.first] + cost[p1.second] <= stsfycost[i].first) {
              stsfycost[i] = {cost[p2.first] + cost[p1.second],
                              {p2.first, p1.second}};
            }
          }
          if (mask1 == i && p1.second != -1) {
            if (cost[p1.first] + cost[p1.second] <= stsfycost[i].first) {
              stsfycost[i] = {cost[p1.first] + cost[p1.second],
                              {p1.first, p1.second}};
            }
          }
          if (mask2 == i && p2.second != -1) {
            if (cost[p2.first] + cost[p2.second] <= stsfycost[i].first) {
              stsfycost[i] = {cost[p2.first] + cost[p2.second],
                              {p2.first, p2.second}};
            }
          }
        } else {
          if (cost[p1.first] + cost[p2.first] <= stsfycost[i].first) {
            stsfycost[i] = {cost[p1.first] + cost[p2.first],
                            {p1.first, p2.first}};
          }
          if (mask1 == i && p1.second != -1) {
            if (cost[p1.first] + cost[p1.second] <= stsfycost[i].first) {
              stsfycost[i] = {cost[p1.first] + cost[p1.second],
                              {p1.first, p1.second}};
            }
          }
          if (mask2 == i && p2.second != -1) {
            if (cost[p2.first] + cost[p2.second] <= stsfycost[i].first) {
              stsfycost[i] = {cost[p2.first] + cost[p2.second],
                              {p2.first, p2.second}};
            }
          }
        }
      }
    }
  }
  long long cldsatisfy = 0, costly = LLONG_MAX;
  long long ans = LLONG_MAX, ansidx = -1;
  for (int i = 0; i < lim; i++) {
    if (stsfy[i] > cldsatisfy && stsfycost[i].first != LLONG_MAX) {
      ans = stsfycost[i].first;
      ansidx = i;
      cldsatisfy = stsfy[i];
    } else if (stsfy[i] == cldsatisfy && stsfycost[i].first != LLONG_MAX &&
               stsfycost[i].first < ans) {
      ans = stsfycost[i].first;
      ansidx = i;
    }
  }
  if (ansidx == -1)
    cout << 1 << " " << 2 << "\n";
  else {
    if (stsfycost[ansidx].second.second != -1)
      cout << stsfycost[ansidx].second.first + 1 << " "
           << stsfycost[ansidx].second.second + 1 << "\n";
    else {
      int idx = stsfycost[ansidx].second.first;
      long long mini = LLONG_MAX, miniidx = -1;
      for (int i = 0; i < m; i++) {
        if (i == idx) continue;
        if (cost[i] < mini) {
          mini = cost[i];
          miniidx = i;
        }
      }
      cout << stsfycost[ansidx].second.first + 1 << " " << miniidx + 1 << "\n";
    }
  }
}
