#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int N = 1e5+7;
int n, q;
struct Str {
    string s;
    int i;    
};

int cnt[N][26][26];
int pref[N];

const int L = 4e5+7;
vector <int> lenp[L];

struct Quer {
    int i, l, r;
};  
vector <Quer> d[L]; // (i, len)

vector <int> p[26];

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(20); 
    #endif
    cin >> n;
    vector <Str> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].s;
        a[i].i = i;        
    }
    auto comp = [&] (Str a, Str b) {
        return a.s < b.s;
    };  
    sort(all(a), comp);

    vector <int> pos(n);
    for (int i = 0; i < n; ++i) {
        pos[a[i].i] = i;
        lenp[a[i].s.size()].app(i);
    }

    vector <int> lcp(n - 1);
    for (int i = 0; i + 1 < n; ++i) {
        int j = i + 1;
        while (lcp[i] < a[i].s.size() && lcp[i] < a[j].s.size() && a[i].s[lcp[i]] == a[j].s[lcp[i]]) {
            lcp[i]++;
        }   
    }   

    {
    vector <ii> s;
    for (int i = 1; i < n; ++i) {
        while (s.size() && s.back().f >= lcp[i - 1]) {
            s.pop_back();
        }   
        s.app(mp(lcp[i - 1], i - 1));

        int last = -1;
        for (int j = (int)s.size() - 1; j >= 0; --j) {            
            int to = 0;
            if (j)
                to = s[j - 1].s + 1;
            last = s[j].f;

            int L = to;
            int R = s[j].s;
            if (L <= R) {
                pref[i] += upper_bound(all(lenp[last]), R) - lower_bound(all(lenp[last]), L);
                if (last < a[i].s.size()) {
                    d[last].app({i, L, R});
                }
            }   
        }   
    }   
    }
    
    {
    vector <ii> s; // (lcp, i)
    for (int i = n - 2; i >= 0; --i) {
        while (s.size() && s.back().f >= lcp[i]) {
            s.pop_back();
        }   
        s.app(mp(lcp[i], i));

        int last = -1;
        for (int j = (int)s.size() - 1; j >= 0; --j) {
            int to = n - 1;
            if (j)
                to = s[j - 1].s;
            last = s[j].f;

            int L = s[j].s + 1;
            int R = to;
            if (L <= R) {
                pref[i] += upper_bound(all(lenp[last]), R) - lower_bound(all(lenp[last]), L);
                if (last < a[i].s.size()) {
                    d[last].app({i, L, R});
                }
            }   

        }   
    }   
    }
    
    vector <int> ord;
    for (int i = 0; i < n; ++i)
        ord.app(i);

    auto comp_len = [&] (int i, int j) {
        return a[i].s.size() > a[j].s.size();
    };

    sort(all(ord), comp_len);        
    for (int len = 0; len < L; ++len) {

        for (int c = 0; c < 26; ++c)
            p[c].clear();
        
        for (int i : ord) {
            if (len >= a[i].s.size())
                break;
            p[a[i].s[len] - 'a'].app(i);
        }   

        for (int c = 0; c < 26; ++c)
            sort(all(p[c]));

        for (auto e : d[len]) {
            for (int c = 0; c < 26; ++c) {
                cnt[e.i][a[e.i].s[len]-'a'][c] += upper_bound(all(p[c]), e.r) - lower_bound(all(p[c]), e.l);
            }
        }   
    }   

    int q;
    cin >> q;
    while (q--) {
        int num;
        cin >> num;
        int i = pos[num - 1];

        string al;
        cin >> al;

        vector <int> cpos(26);
        for (int i = 0; i < 26; ++i)
            cpos[al[i]-'a'] = i;

        int ans = pref[i];
        for (int x = 0; x < 26; ++x)
            for (int y = 0; y < 26; ++y)    
                if (cpos[x] > cpos[y]) {
                    ans += cnt[i][x][y];
                }
        cout << ans + 1 << endl;
    }   
}


Measured memory_bytes: 301732