#include <iostream> #include <unordered_map> #include <algorithm> using namespace std; int T, n, sz; unordered_map <int, int> v[310], now; int Count1(int &ind) { unordered_map <int, int>::iterator it; for(it = now.begin(); it != now.end(); ++it) if(!v[ind].count((*it).first) || v[ind][(*it).first] < (*it).second) return 0; return 1; } int Count2(int &ind) { unordered_map <int, int>::iterator it; for(it = now.begin(); it != now.end(); ++it) if(v[ind].count((*it).first)) return 1; return 0; } int Count3(int &ind) { int nr = 0; unordered_map <int, int>::iterator it; for(it = now.begin(); it != now.end(); ++it) if(v[ind].count((*it).first)) nr += min((*it).second, v[ind][(*it).first]); if(nr == 0 || nr == sz) return 0; return 1; } int main() { std::ios_base::sync_with_stdio(false); int i, tip, x; cin >> n; for(i = 1; i <= n; ++i) { cin >> sz; while(sz--) { cin >> x; v[i][x]++; } } cin >> T; while(T--) { cin >> tip >> sz; now.clear(); for(i = 1; i <= sz; ++i) { cin >> x; now[x]++; } int sol = 0; if(tip == 1) { for(i = 1; i <= n; ++i) sol += Count1(i); } if(tip == 2) { for(i = 1; i <= n; ++i) sol += Count2(i); } if(tip == 3) { for(i = 1; i <= n; ++i) sol += Count3(i); } cout << sol << "\n"; } return 0; }