#include <bits/stdc++.h>
using namespace std;
struct node {
int to[26];
int cnt;
node() {
memset (to, 0, sizeof to);
cnt = 0;
}
};
vector<node> t(1);
map<string, int> ans;
int go (string s, bool add) {
int v = 0;
int sum = 0;
for (int i = 0; i < s.size(); i++) {
int c = s[i] - 'a';
if (!t[v].to[c]) {
t[v].to[c] = t.size();
t.push_back(node());
}
v = t[v].to[c];
sum += t[v].cnt;
if (add)
t[v].cnt++;
}
return sum;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
int v = go(s, 1);
if (!ans.count(s))
ans[s] = v + s.length()+i;
//cout << ans[s] << endl;
}
int q;
cin >> q;
while (q--) {
string s;
cin >> s;
if (ans.count (s))
cout<< ans[s]<<endl;
else
{
cout << go(s, 0) + n << endl;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3Qgbm9kZSB7CglpbnQgdG9bMjZdOwoJaW50IGNudDsKCW5vZGUoKSB7CgkJbWVtc2V0ICh0bywgMCwgc2l6ZW9mIHRvKTsKCQljbnQgPSAwOwoJfQp9Owp2ZWN0b3I8bm9kZT4gdCgxKTsKbWFwPHN0cmluZywgaW50PiBhbnM7CmludCBnbyAoc3RyaW5nIHMsIGJvb2wgYWRkKSB7CglpbnQgdiA9IDA7CglpbnQgc3VtID0gMDsKCWZvciAoaW50IGkgPSAwOyBpIDwgcy5zaXplKCk7IGkrKykgewoJCgkJaW50IGMgPSBzW2ldIC0gJ2EnOwoJCWlmICghdFt2XS50b1tjXSkgewoJCQl0W3ZdLnRvW2NdID0gdC5zaXplKCk7CgkJCXQucHVzaF9iYWNrKG5vZGUoKSk7CgkJfQoJCXYgPSB0W3ZdLnRvW2NdOwoJCXN1bSArPSB0W3ZdLmNudDsKCQlpZiAoYWRkKQoJCQl0W3ZdLmNudCsrOwoJfQoJcmV0dXJuIHN1bTsKfQppbnQgbWFpbigpIHsKC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