// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 10e-7
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
// mylittlepony
using namespace std;
struct node {
int son[26];
int sum;
};
struct trie {
vector<node> T;
int t;
node n0;
trie() {
n0.sum =0;
for(int i =0; i < 26; i++) n0.son[i] =-1;
T.resize(1000000,n0);
t =1;}
void put(string &s) {
int L =s.length(), akt =0;
for(int i =0; i < L; i++) {
T[akt].sum++;
if(T[akt].son[s[i]-'a'] == -1) T[akt].son[s[i]-'a'] =t++;
akt =T[akt].son[s[i]-'a'];}
T[akt].sum++;}
int get(string &s) {
int L =s.length(), akt =0, ret =0;
for(int i =0; i < L; i++) {
ret +=T[akt].sum;
if(T[akt].son[s[i]-'a'] == -1) break;
akt =T[akt].son[s[i]-'a'];
if(i == L-1) ret +=T[akt].sum;}
return ret;}
};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N,Q;
cin >> N;
map<string,int> M;
vector<string> A(N);
for(int i =0; i < N; i++) {
string s;
cin >> s;
M[s] =i;
A[i] =s;}
cin >> Q;
vector<int> ans(Q);
vector<string> B(Q);
vector< vector<int> > query(N+1);
for(int i =0; i < Q; i++) {
string s;
cin >> s;
B[i] =s;
auto it =M.find(s);
if(it == M.end()) query[N].push_back(i);
else query[it->ss].push_back(i);}
trie tr;
for(int i =0; i <= N; i++) {
if(i < N) tr.put(A[i]);
int b =(i == N)?0:tr.get(A[i]);
ALL_THE(query[i],it) {
if(i < N) ans[*it] =b;
else ans[*it] =tr.get(B[*it]);}
}
for(int i =0; i < Q; i++) cout << ans[i] << "\n";
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW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nlbTl0ucHVzaF9iYWNrKGkpOwogICAgCWVsc2UgcXVlcnlbaXQtPnNzXS5wdXNoX2JhY2soaSk7fQogICAgCiAgICB0cmllIHRyOwogICAgZm9yKGludCBpID0wOyBpIDw9IE47IGkrKykgewogICAgCWlmKGkgPCBOKSB0ci5wdXQoQVtpXSk7CiAgICAJaW50IGIgPShpID09IE4pPzA6dHIuZ2V0KEFbaV0pOwogICAgCUFMTF9USEUocXVlcnlbaV0saXQpIHsKICAgIAkJaWYoaSA8IE4pIGFuc1sqaXRdID1iOwogICAgCQllbHNlIGFuc1sqaXRdID10ci5nZXQoQlsqaXRdKTt9CiAgICAJfQogICAgCiAgICBmb3IoaW50IGkgPTA7IGkgPCBROyBpKyspIGNvdXQgPDwgYW5zW2ldIDw8ICJcbiI7CiAgIAlyZXR1cm4gMDt9CiAgICAgICAgCi8vIGxvb2sgYXQgbXkgY29kZQovLyBteSBjb2RlIGlzIGFtYXppbmc=