#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
template <class T, class U>
inline void checkmin(T &x, U y) {
  if (y < x) x = y;
}
template <class T, class U>
inline void checkmax(T &x, U y) {
  if (y > x) x = y;
}
template <class T, class U>
inline bool ifmax(T &x, U y) {
  if (y > x) return x = y, true;
  return false;
}
template <class T, class U>
inline bool ifmin(T &x, U y) {
  if (y < x) return x = y, true;
  return false;
}
template <class T>
inline void sort(T &a) {
  sort(a.begin(), a.end());
}
template <class T>
inline void rsort(T &a) {
  sort(a.rbegin(), a.rend());
}
template <class T>
inline void reverse(T &a) {
  reverse(a.begin(), a.end());
}
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
  return str >> p.first >> p.second;
}
template <class T>
inline istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) str >> i;
  return str;
}
int readtype() {
  int n;
  cin >> n;
  int ans = 0;
  while (n--) {
    int t;
    cin >> t;
    ans |= (1 << (t - 1));
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  srand(94385);
  int n, m;
  cin >> n >> m;
  vector<int> friends(512);
  for (int i = 0; i < n; ++i) ++friends[readtype()];
  for (int i = signed(friends.size()) - 1; i >= 0; --i)
    for (int j = 0; j < i; ++j)
      if ((j & i) == j) friends[i] += friends[j];
  vector<vector<pair<int, int>>> pizza(512);
  for (int i = 0; i < m; ++i) {
    int c;
    cin >> c;
    pizza[readtype()].push_back({c, i});
  }
  vector<pair<int, pair<int, int>>> pizzas;
  for (int i = 0; i < signed(pizza.size()); ++i) {
    sort(pizza[i]);
    while (signed(pizza[i].size()) > 2) pizza[i].pop_back();
    for (auto j : pizza[i]) pizzas.push_back({i, j});
  }
  int mxcnt = 0;
  int ans = 2e9 + 10;
  pair<int, int> answer = {-1, -1};
  for (int i = 0; i < signed(pizzas.size()); ++i)
    for (int j = i + 1; j < signed(pizzas.size()); ++j) {
      if (ifmax(mxcnt, friends[(pizzas[i].first | pizzas[j].first)])) {
        ans = pizzas[i].second.first + pizzas[j].second.first;
        answer = {pizzas[i].second.second, pizzas[j].second.second};
      }
      if (mxcnt == friends[(pizzas[i].first | pizzas[j].first)])
        if (ifmin(ans, pizzas[i].second.first + pizzas[j].second.first)) {
          answer = {pizzas[i].second.second, pizzas[j].second.second};
        }
    }
  cout << answer.first + 1 << ' ' << answer.second + 1;
  return 0;
}
