///#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
///#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,popcnt,avx,avx2,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

/*
    zet:
        find_by_order(k): k-stat [k > 0]
        order_of_key(k) : {el < k}

    gp_hash_table<key, val, custom_hash>

    rope<T>:
        iterators: mutable_begin, ..
        methods  : erase, insert, substr, ..
*/

#define all(x) (x).begin(), (x).end()
#define size(x) (int)((x).size())
#define em_back emplace_back

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<typename T>
using zet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

mt19937 eng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 4;
const int S = 4e5 + 4;
const int ABC = 'z' - 'a' + 1;

struct Node {
    int to[ABC];
    int leaf;
    int cnt;

    Node() {
        memset(to, -1, sizeof to);
        leaf = -1;
        cnt = 0;
    }
};

int n, it;
Node bor[S];
string b[N];

void add(string& s, int num) {
    int u = 0;
    for (char c : s) {
        int& to = bor[u].to[c - 'a'];
        if (to == -1) {
            to = ++it;
        }
        u = to;
    }
    bor[u].leaf = num;
}

void read() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
        add(b[i], i + 1);
    }
}

void dfs(int u) {
    int& res = bor[u].cnt;
    res += (bor[u].leaf != -1);
    for (int to = 0; to < ABC; ++to) {
        int v = bor[u].to[to];
        if (v != -1) {
            dfs(v);
            res += bor[v].cnt;
        }
    }
}

int way[N];
int cmp[N][ABC][ABC];

void precalc(int k) {
    int u = 0;
    for (char c : b[k]) {
        int v = bor[u].to[c - 'a'];

        for (int i = 0; i < ABC; ++i) if (i != c - 'a') {
            int w = bor[u].to[i];
            if (w != -1) {
                cmp[k][c - 'a'][i] += bor[w].cnt;
            }
        }

        way[k] += (bor[u].leaf != -1);
        u = v;
    }
}

int pos[ABC];

int calc(int k, string& p) {
    int res = way[k];
    for (int i = 0; i < ABC; ++i) {
        pos[p[i] - 'a'] = i;
    }

    for (int i = 0; i < ABC; ++i) {
        for (int j = 0; j < ABC; ++j) {
            if (pos[i] <= pos[j]) {
                continue;
            }
            res += cmp[k][i][j];
        }
    }

    return res + 1;
}

void solve() {
    read();
    dfs(0);
    for (int i = 0; i < n; ++i) {
        precalc(i);
    }

    int q;
    cin >> q;
    while (q--) {
        int k;
        string p;
        cin >> k >> p;
        cout << calc(k - 1, p) << "\n";
    }
}

int main() {
    ios::sync_with_stdio(0);
    cout.tie(0), cin.tie(0);

    int z = 1;
    /// cin >> z;

    while (z--) {
        solve();
    }

    return 0;
}


Measured memory_bytes: 315000