#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <memory>

const int ALPHA_SIZE = 26;

using namespace std;

template<typename T, typename S>
istream& operator >> (istream& is, pair<T, S>& p) {
    is >> p.first >> p.second;
    return is;
}

template<typename T>
istream& operator >> (istream& is, vector<T>& v) {
    for (auto& x : v) {
        is >> x;
    }
    return is;
}

template<typename T>
ostream& operator << (ostream& os, const vector<T>& v) {
    for (auto& x : v) {
        os << x << " ";
    }
    os << "\n";
    return os;
}

struct Trie {
    struct Node {
        map<char, unique_ptr<Node>> c_to_next;
        optional<int> idx;
        int size;

        Node(): idx(nullopt), size(1) {}

        void calc_size() {
            size = 0;
            if (idx) size = 1;
            for (auto& [c, ptr] : c_to_next) {
                ptr->calc_size();
                size += ptr->size;
            }
        }

        void add(const string& s, int term_idx, size_t start) {
            if (start >= s.size()) {
                idx = term_idx;
                return;
            }
            char c = s[start];
            if (!c_to_next.count(c)) {
                c_to_next[c] = make_unique<Node>();
            }
            c_to_next[c]->add(s, term_idx, start + 1);
        }

        void calc_substr(int term_cnt, vector<int>& substr_cnt) {
            if (idx) {
                substr_cnt[*idx] = term_cnt;
                term_cnt++;
            }
            for (auto& [c, ptr] : c_to_next) {
                ptr->calc_substr(term_cnt, substr_cnt);
            }
        }

        void calc_char_char_diff_count(vector<vector<int>>& branches, vector<vector<vector<int>>>& result) {
            if (idx) {
                result[*idx] = branches;
            }
            for (auto& [c_next, ptr_next] : c_to_next) {
                for (auto& [c_alt, ptr_alt]: c_to_next) {
                    if (c_next == c_alt) continue;
                    branches[c_alt - 'a'][c_next - 'a'] += ptr_alt->size;
                }

                ptr_next->calc_char_char_diff_count(branches, result);

                for (auto& [c_alt, ptr_alt]: c_to_next) {
                    if (c_next == c_alt) continue;
                    branches[c_alt - 'a'][c_next - 'a'] -= ptr_alt->size;
                }
            }
        }
    };

    unique_ptr<Node> root;
    int N;
    Trie() {
        root = make_unique<Node>();
        N = 1;
    }

    void add(const string& s, int i) {
        N = max(N, i);
        root->add(s, i, 0);
    };

    vector<int> count_substr() {
        vector<int> result(N + 1);
        root->calc_substr(0, result);
        return result;
    }

    vector<vector<vector<int>>> get_char_char_diff_count() {
        vector<vector<vector<int>>> result(N + 1, vector<vector<int>>(ALPHA_SIZE, vector<int>(ALPHA_SIZE, 0)));
        vector<vector<int>> branches(ALPHA_SIZE, vector<int>(ALPHA_SIZE, 0));
        root->calc_size();
        root->calc_char_char_diff_count(branches, result);
        return result;
    }
};

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);

#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int N;
    cin >> N;
    Trie t;
    for (int i = 0; i < N; ++i) {
        string s;
        cin >> s;
        t.add(s, i + 1);
    }
    vector<int> num_substr = t.count_substr();
    // cout << num_substr;
    vector<vector<vector<int>>> i_to_char_char_count = t.get_char_char_diff_count();
    // cout << i_to_char_char_count;

    int Q;
    cin >> Q;
    for (int q = 0; q < Q; ++q) {
        int k;
        string order;
        cin >> k >> order;
        int ans = num_substr[k];
        for (int i1 = 0; i1 < ALPHA_SIZE; ++i1) {
            for (int i2 = i1 + 1; i2 < ALPHA_SIZE; ++i2) {
                char c1 = order[i1], c2 = order[i2];
                ans += i_to_char_char_count[k][c1 - 'a'][c2 - 'a'];
            }
        }
        cout << ans + 1<< "\n";
    }
}


Measured memory_bytes: 369196