#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


template<typename C>
struct Trie {
    struct Node {
        int exist;
        vector<int> accept;
        map<C, int> ch;
        Node() : exist(0) {}  
    };
 
    vector<Node> node;
 
    Trie() {node.emplace_back();}
 
    template<typename S>
    void add (const S &A, int id) {
        int cur = 0;
        for (int i = 0; i < (int)A.size(); i++) {
            if (!node[cur].ch.count(A[i])) {
                int nex = node.size();
                node.emplace_back();
                node[cur].ch.emplace(A[i],nex);
            }
            node[cur].exist++;
            cur = node[cur].ch[A[i]];
        }
        node[cur].exist++;
        node[cur].accept.emplace_back(id);
    }
 
    template<typename S>
    int find(const S &A) {
        int cur = 0;
        for (int i = 0; i < (int)A.size(); i++) {
            if (!node[cur].ch.count(A[i])) return -1;
            cur = node[cur].ch[A[i]];
        }
        return cur;
    }
 
    int count() const {return node[0].eixst;}
 
    int size() const {return(int)node.size();}
};
 
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    vector<string> S(N);
    Trie<char> T;
    rep(i,N) {
        cin >> S[i];
        T.add(S[i],i);
    }
    vector<vector<pair<int, char>>> point(N);
    vector<int> cnt(N);
    rep(i,N) {
        int K = S[i].size();
        int cur = 0;
        rep(j,K) {
            if (T.node[cur].ch.size()!=1) {
                point[i].emplace_back(cur,S[i][j]);
            }
            cnt[i] += T.node[cur].accept.size();
            cur = T.node[cur].ch[S[i][j]];
        }
    }
    int Q; cin >> Q;
    vector<int> k(Q);
    vector<string> p(Q);
    rep(i,Q) {
        cin >> k[i];
        k[i]--;
        cin >> p[i];
    }
    vector<int> idx(Q);
    iota(all(idx),0);
    sort(all(idx),[&](int i, int j){return k[i] < k[j];});
    int l = 0, r = 0;
    vector<int> ans(Q);
    while (l < Q) {
        while (r < Q and k[idx[l]]==k[idx[r]]) r++;
        int num = k[idx[l]];
        int K = S[num].size();
        for (auto [pos,s] : point[num]) {
            for (int i = l; i < r; i++) {
                int u = idx[i];
                rep(j,26) {
                    char c = p[u][j];
                    if (c==s) break;
                    if (T.node[pos].ch.count(c)) {
                        int v = T.node[pos].ch[c];
                        ans[u] += T.node[v].exist;
                    }  
                }
            }
        }
        for (int i = l; i < r; i++) {
            int u = idx[i];
            ans[u] += cnt[num];
        }
        l = r;
    }
 
    rep(i,Q) cout << ans[i]+1 << ln;
}

Measured memory_bytes: 62540