#include <bits/stdc++.h>
using namespace std;
struct Trie {
    array<Trie*, 26> next;
    array<int, 26> count;
    int end;
};
int main() {
    int N; cin >> N;
    vector<string> S(N); for (auto &s : S) cin >> s;
    Trie *trie = new Trie();
    for (int i = 0; i < N; i++) {
        const string &s = S[i];
        Trie *t = trie;
        for (int j = 0; j < s.size(); j++) {
            t->count[s[j] - 'a']++;
            if (!t->next[s[j] - 'a']) t->next[s[j] - 'a'] = new Trie();
            t = t->next[s[j] - 'a'];
        }
        t->end++;
    }
    vector<array<array<int, 26>, 26>> counts(N);
    vector<int> ends(N);
    for (int i = 0; i < N; i++) {
        const string &s = S[i];
        Trie *t = trie;
        for (int j = 0; j < s.size(); j++) {
            ends[i] += t->end;
            for (int k = 0; k < 26; k++) {
                counts[i][s[j] - 'a'][k] += t->count[k];
            }
            t = t->next[s[j] - 'a'];
        }
    }
    int Q; cin >> Q;
    while (Q--) {
        int i; cin >> i; i--;
        string p; cin >> p;
        int cnt = ends[i];
        for (int j = 0; j < 26; j++) {
            for (int k = 0; k < 26; k++) {
                if (p[k] == j + 'a') break;
                cnt += counts[i][j][p[k] - 'a'];
            }
        }
        cout << cnt + 1 << '\n';
    }
    return 0;
}
