#include <iostream>
#include <vector>
using namespace std;


typedef vector<string> Fila;
typedef vector<Fila> Matriu;


int nombre_de_paraules(const Matriu& m, char c, int k);


int main () {
  int n;
  while (cin >> n) {
    Matriu v(n);
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m;
      v[i] = Fila(m);
      for (int j = 0; j < m; ++j) {
        cin >> v[i][j];
        if (v[i][j] == "@") v[i][j] = "";
      }
    }

    int q;
    cin >> q;
    while (q--) {
      char c;
      int k;
      cin >> c >> k;
      cout << nombre_de_paraules(v, c, k) << endl;
    }
    cout << endl;
  }
  return 0;
}
