// #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return max(low, min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { t = clamp(t, low, high); }

namespace init__ {

struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
    return V<decltype(rec)>(s, rec);
#undef rec
}

struct Node {
    Node *parent;
    V<pair<Node*, size_t>> children;
    bool just;
    Node(Node *parent) 
        : parent(parent),
          children(26, make_pair(nullptr, 0)),
          just(false) {}
};

Node root(nullptr);

void insert(const string &str, ll idx, Node *cur) {
    if(idx == str.size()) { 
        cur->just = true;
        return;
    }
    auto ch_idx = str[idx] - 'a';
    if(cur->children[ch_idx].first == nullptr) {
        auto child = new Node(cur);
        cur->children[ch_idx] = make_pair(child, 0);
    }
    cur->children[ch_idx].second++;
    insert(str, idx + 1, cur->children[ch_idx].first);
}

void count_smaller(VV<ll> &counts, ll &cnt_just, const string &str, ll idx, Node *cur) {
    cnt_just += cur->just;
    if(idx == str.size()) return;
    char cur_c = str[idx];
    for(ll i = 0; i < 26; i++) counts[cur_c - 'a'][i] += cur->children[i].second;
    return count_smaller(counts, cnt_just, str, idx + 1, cur->children[cur_c - 'a'].first);
}

// k, perm, idx
using Query = tuple<ll, V<ll>, ll>;

int main() {
    ll N;
    cin >> N;
    V<string> vs(N);
    for(auto &&ele : vs) {
        cin >> ele;
        insert(ele, 0, &root);
    }

    ll Q;
    cin >> Q;
    V<Query> query(Q);
    for(ll idx = 0; idx < Q; idx++) {
        V<ll> iperm(26);
        ll k;
        cin >> k;
        k--;
        for(ll i = 0; i < 26; i++) {
            char c;
            cin >> c;
            iperm[c - 'a'] = i;
        }
        query[idx] = make_tuple(k, move(iperm), idx);
    }
    sort(ALL(query), [](const Query &a, const Query &b) { return get<0>(a) < get<0>(b); });
    V<ll> ans_v(Q);
    ll pre_k = -1;
    ll cnt_just = 0;
    V<ll> iperm;
    auto counts = make_v<ll>(0, 26, 26);
    for(auto &&ele : query) {
        ll k, idx;
        tie(k, iperm, idx) = ele;
        if(k != pre_k) {
            for(auto &&ele : counts) for(auto &&e : ele) e = 0;
            cnt_just = 0;
            count_smaller(counts, cnt_just, vs[k], 0, &root);
        }
        pre_k = k;
        ll ans = cnt_just;
        for(ll i = 0; i < 26; i++) for(ll j = 0; j < 26; j++) if(iperm[i] < iperm[j]) ans += counts[j][i];
        ans_v[idx] = ans;
    }
    for(auto &&ele : ans_v) cout << ele << endl;
    return 0;
}


Measured memory_bytes: 215040