// tested by Hightail: https://github.com/dj3500/hightail
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

const int N = 303;
vector<pii> v[N];

int main () {
   wez(n)
   FOR(i,n) {
      wez(ile)
      map<int,int> m;
      while (ile--) {
         wez(x)
         m[x]++;
      }
      FOREACH(it,m) v[i].pb(*it);
   }
   TESTS {
      wez(ty)
      wez(K)
      map<int,int> m;
      FOR(u,K) {
         wez(x)
         m[x]++;
      }
      int res = 0;
      FOR(i,n) {
         int found = 0;
         FOREACH(it,m) {
            vector<pii>::iterator f = lower_bound(ALL(v[i]), mp(it->fi, 0));
            if (f != v[i].end() && f->fi == it->fi) {
               found += min(f->se, it->se);
            }
         }
         if (ty == 1) {
            if (found == K) ++res;
         } else if (ty == 2) {
            if (found > 0) ++res;
         } else { // ty == 3
            if (found > 0 && found < K) ++res;
         }
      }
      pisz(res);
   }
}