#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }


struct Trie {
    vector<array<int, 26> > trie_nodes = {{}};
    vector<int> sz = {0}, cnt = {0}, next = {-1}, depth = {0};

    void push(string s){
        int node = 0;
        sz[node]++;
        for (char c : s){
            int x = c - 'a';
            if (!trie_nodes[node][x]){
                trie_nodes[node][x] = trie_nodes.size();
                trie_nodes.push_back({});
                sz.push_back(0);
                cnt.push_back(0);
                next.push_back(-1);
                depth.push_back(depth[node] + 1);
            }
            node = trie_nodes[node][x];
            sz[node]++;
        }
        cnt[node]++;
    }

    int lcp(string s){
        int node = 0, ret = 0;
        for (char c : s){
            int x = c - 'a';
            if (!trie_nodes[node][x]) return ret;
            node = trie_nodes[node][x];
            ret++;
        }
        return ret;
    }

    bool ismatch(string s){
        return lcp(s) == s.length();
    }

    void shorten(int node){
        int c = 0;
        next[node] = node;
        REP(i,26){
            if (!trie_nodes[node][i]) continue;
            shorten(trie_nodes[node][i]);
            c++;
            next[node] = next[trie_nodes[node][i]];
        }
        if (c >= 2 || cnt[node]) next[node] = node;
    }

    int query(string s, string t){
        int node = 0, ret = 0;
        REP(i,s.length()){
            char c = s[i];
            for (char d : t){
                if (d == c) break;
                if (trie_nodes[node][d - 'a']){
                    ret += sz[trie_nodes[node][d - 'a']];
                }
            }
            ret += cnt[node];
            // cout << c << " " << ret << endl;
            if (next[node] == node) node = trie_nodes[node][c - 'a'];
            else {
                node = next[node];
                i = depth[node] - 1;
            }
        }
        return ret;
    }
};

int main() {
    int n, q;
    cin >> n;
    vector<string> s(n);
    Trie trie;
    REP(i,n){
        cin >> s[i];
        trie.push(s[i]);
    }
    trie.shorten(0);
    // for (int x : trie.next){
    //     cout << x << endl;
    // }
    cin >> q;
    while (q--){
        int k;
        string t;
        cin >> k >> t;
        k--;
        int x = trie.query(s[k], t);
        cout << x + 1 << endl;
    }

    return 0;
}


Measured memory_bytes: 69888