#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

// 文字の種類数をtemplate引数で渡す
template <int types = 26>
struct Trie {
    // trie木のnode
    struct node {
        int fail, num;
        vector<ll> next;
        vector<ll> matched;
        node() : fail(-1), num(0), next(types, -1) {}
    };

    vector<int> prefix;
    vector<int> table[26][26];
    // node の集合
    ll itr;
    vector<node> nodes;
    // 辞書の種類数, trie木の根
    int sz, root;
    // 文字と数字の対応付けをする関数
    using F = function<int(char)>;
    F trans;

    // 初期化
    Trie() {}
    Trie(vector<string> pattern, ll n = 1000000, F f = [](char c){return c-'a';}) : prefix(pattern.size()), itr(1), nodes(n), sz(pattern.size()), root(0), trans(f) {
        REP(i, 26) REP(j, 26) table[i][j].assign(sz, 0);
        build(pattern);
    }
    // 文字列集合patternからtrie木を構築
    void build(vector<string> pattern) {
        int now;
        nodes[root].fail = root;
        REP(i, pattern.size()) {
            now = root;
            for(const auto &c: pattern[i]) {
                if(nodes[now].next[trans(c)] == -1) {
                    nodes[now].next[trans(c)] = itr++;
                }
                nodes[now].num++;
                now = nodes[now].next[trans(c)];
            }
            nodes[now].num++;
            nodes[now].matched.push_back(i);
        }
    }

    void dfs(ll v, ll pf, vector<vector<ll>>& tab) {
        if(nodes[v].matched.size() > 0) {
            prefix[nodes[v].matched[0]] = pf;
            REP(i, 26) REP(j, 26) {
                table[i][j][nodes[v].matched[0]] = tab[i][j];
            }
            pf++;
        }
        REP(i, 26) {
            if(nodes[v].next[i] == -1) continue;
            REP(j, 26) {
                if(i == j || nodes[v].next[j] == -1) continue;
                tab[j][i] += nodes[nodes[v].next[j]].num;
            }
            const ll to = nodes[v].next[i];
            dfs(to, pf, tab);
            REP(j, 26) {
                if(i == j || nodes[v].next[j] == -1) continue;
                tab[j][i] -= nodes[nodes[v].next[j]].num;
            }
        }
    }
};

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<string> s(n);
    REP(i, n) cin >> s[i];

    Trie<26> tree(s, 400005);
    vector<vector<ll>> tab(26, vector<ll>(26));
    tree.dfs(tree.root, 0, tab);

    // cout << tree.prefix << endl;
    // REP(i, n) {
    //     cout << "i=" << i << " " << s[i] << endl;
    //     REP(j, 2) {
    //         REP(k, 2) {
    //             cout << tree.table[j][k][i] << " ";
    //         }
    //         cout << endl;
    //     }
    //     cout << endl;
    // }

    ll q;
    cin >> q;
    while(q--) {
        ll k;
        string s;
        cin >> k >> s;
        k--;

        ll ret = 0;
        ret += tree.prefix[k];
        REP(i, 26) FOR(j, i+1, 26) {
            ret += tree.table[s[i]-'a'][s[j]-'a'][k];
        }
        cout << ret+1 << endl;
    }

    return 0;
}


Measured memory_bytes: 390752