#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 cnt[101010][26][26];
 
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);
    }

    rep(i,N) {
        int K = S[i].size();
        int cur = 0;
        rep(j,K) {
            cnt[i][0][0] += T.node[cur].accept.size();
            int n = S[i][j] - 'a';
            for (auto [c,other] : T.node[cur].ch) {
                if (c==S[i][j]) continue;
                int k = c - 'a';
                cnt[i][k][n] += T.node[other].exist;
            }
            cur = T.node[cur].ch[S[i][j]];
        }
    }

    int Q; cin >> Q;
    while (Q--) {
        int k; cin >> k;
        --k;
        string p; cin >> p;
        int ans = cnt[k][0][0]+1;
        for (int i = 0; i < 25; i++) {
            int s = p[i] - 'a';
            for (int j = i+1; j < 26; j++) {
                int l = p[j] - 'a';
                ans += cnt[k][s][l];
            }
        }

        cout << ans << ln;
    }
}

Measured memory_bytes: 288244