#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct edge{
    int to, cost;
};

template<int char_size>
struct Trie_Node{
    vector<int> next;
    vector<int> accept;
    int exist;
    vector<edge> es;

    Trie_Node() : exist(0){
        next.assign(char_size, -1);
    }
};

template<int char_size, char base>
struct Trie{
    using Node = Trie_Node<char_size>;
    vector<Node> nodes;
    int root;

    Trie() : root(0){
        nodes.pb(Node());
    }

    void insert(const string &s, int id){
        int now = 0;
        rep(i, sz(s)){
            int c = s[i]-base;
            int &next = nodes[now].next[c];
            if(next == -1){
                next = sz(nodes), nodes.pb(Node());
            }
            nodes[now].exist++, now = next;
        }
        nodes[now].exist++, nodes[now].accept.pb(id);
    }

    void insert(const string &s){
        insert(s, nodes[0].exist);
    }

    void arrange(){
        for(auto &e: nodes){
            rep(j, char_size){
                if(e.next[j] != -1) e.es.eb((edge){j, 1});
            }
        }
        rep(i, sz(nodes)){
            Node &e = nodes[i];
            for(auto &u: e.es){
                int now = i, dir = u.to;
                while(true){
                    e.next[u.to] = nodes[now].next[dir];
                    now = nodes[now].next[dir];
                    if(sz(nodes[now].es) != 1 || sz(nodes[now].accept)) break;
                    u.cost++;
                    dir = nodes[now].es.front().to;
                    nodes[now].es.clear();
                }
            }
        }
    }

    int query(string s, vector<int> ord){
        int now = 0, ret = 0, n = sz(s);
        for(int i = 0; i < n;){
            int j = s[i]-'a';
            ret += sz(nodes[now].accept);
            for(auto &e: nodes[now].es){
                if(ord[e.to] < ord[j]) ret += nodes[nodes[now].next[e.to]].exist;
                if(e.to == j) i += e.cost;
            }
            now = nodes[now].next[j];
        }
        return ret;
    }
};

int main(){
    int N;
    cin >> N;
    Trie<26, 'a'> trie;
    string S[N];
    rep(i, N){
        cin >> S[i];
        trie.insert(S[i]);
    }
    trie.arrange();
    int Q; cin >> Q;
    rep(i, Q){
        int k; cin >> k; k--;
        string p; cin >> p;
        vector<int> ord(26);
        rep(j, 26) ord[p[j]-'a'] = j;
        cout << trie.query(S[k], ord)+1 << endl;
    }
}

Measured memory_bytes: 91428