#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 c;
        int fail;
        vector<ll> next;
        vector<ll> matched;
        node() : c(-1), fail(-1), next(types, -1) {}
        node(int c) : c(c), fail(-1), next(types, -1) {}
    };

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

    // 初期化
    Trie() {}
    Trie(vector<string> pattern, F f = [](char c){return c-'a';})
    : prefix(pattern.size()), nodes(1), 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.push_back(node(trans(c)));
                    nodes[now].next[trans(c)] = nodes.size() - 1;
                }
                now = nodes[now].next[trans(c)];
            }
            nodes[now].matched.push_back(i);
        }
    }

    vector<ll> dfs(ll v) {
        vector<ll> all(nodes[v].matched);
        vector<ll> ch_c;
        vector<vector<ll>> ch_s;
        REP(j, 26) {
            if(nodes[v].next[j] == -1) continue;
            const int to = nodes[v].next[j];
            auto v = dfs(to);
            ch_c.emplace_back(nodes[to].c);
            ch_s.emplace_back(v);
            if(all.size() < v.size()) swap(v, all);
            for(auto i: v) all.push_back(i);
        }
        REP(i, ch_s.size()) {
            for(auto j: ch_s[i]) {
                prefix[j] += nodes[v].matched.size();
                REP(k, ch_s.size()) {
                    if(i == k) continue;
                    const ll c1 = ch_c[i], c2 = ch_c[k];
                    table[c2][c1][j] += ch_s[k].size();
                }
            }
        }
        return all;
    }
};

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);
    tree.dfs(tree.root);

    // 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: 314412