#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

const int MAX_C = 26;

struct TNode {
    int Next[MAX_C];
    int UpValue;
    int Count;

    int Sum[MAX_C];
};

const int MAX_NODE_COUNT = 4e5 + 1;

TNode nodes[MAX_NODE_COUNT];
int nodesCount;

void InitMemory() {
    nodesCount = 0;
}

void ClearNode(int index) {
    for (int i = 0; i < MAX_C; ++i) {
        nodes[index].Next[i] = -1;
    }
    nodes[index].Count = 0;
    nodes[index].UpValue = false;
}

int GetNewNode() {
    ClearNode(nodesCount);
    return nodesCount++;
}

int AddString(const string& s, int root) {
    for (int i = 0; i < (int) s.length(); ++i) {
        ++nodes[root].Count;

        int c = s[i] - 'a';

        if (nodes[root].Next[c] == -1) {
            nodes[root].Next[c] = GetNewNode();
        }

        root = nodes[root].Next[c];
    }
    ++nodes[root].Count;
    nodes[root].UpValue = true;
    return root;
}

void BuildUpValueDfs(int vert, int pr) {
    if (pr != -1) {
        nodes[vert].UpValue += nodes[pr].UpValue;
    }
    for (int i = 0; i < MAX_C; ++i) {
        if (nodes[vert].Next[i] != -1) {
            BuildUpValueDfs(nodes[vert].Next[i], vert);
        }
    }
}

void BuildUpValue(int root) {
    BuildUpValueDfs(root, -1);
}

void BuildSumDfs(int vert, int pr, int prChar, int ci) {
    if (pr != -1) {
        for (int j = 0; j < MAX_C; ++j) {
            nodes[vert].Sum[j] = nodes[pr].Sum[j];
        }
        if (prChar == ci) {
            for (int j = 0; j < MAX_C; ++j) {
                int nxt = nodes[pr].Next[j];
                if (nxt != -1) {
                    nodes[vert].Sum[j] += nodes[nxt].Count;
                }
            }
        }
    } else {
        for (int j = 0; j < MAX_C; ++j) {
            nodes[vert].Sum[j] = 0;
        }
    }
    for (int i = 0; i < MAX_C; ++i) {
        if (nodes[vert].Next[i] != -1) {
            BuildSumDfs(nodes[vert].Next[i], vert, i, ci);
        }
    }
}

void BuildSum(int root, int ci) {
    BuildSumDfs(root, -1, -1, ci);
}

int main() {
    // freopen("input.txt", "r", stdin);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    bool first = true;
    while (true) {
        InitMemory();

        int root = GetNewNode();

        int n;
        if (!(cin >> n)) {
            break;
        }

        if (!first) {
            cout << endl;
        } else {
            first = false;
        }

        vector<int> ids(n);

        vector<string> s(n);
        for (int i = 0; i < n; ++i) {
            cin >> s[i];

            ids[i] = AddString(s[i], root);
        }

        BuildUpValue(root);

        int tests;
        cin >> tests;

        vector<vector<int>> charValues(tests, vector<int>(MAX_C));
        vector<int> ks(tests);
        for (int test = 0; test < tests; ++test) {
            cin >> ks[test];
            --ks[test];

            string t;
            cin >> t;

            for (int i = 0; i < MAX_C; ++i) {
                charValues[test][t[i] - 'a'] = i;
            }
        }

        vector<int> result(tests, 0);

        for (int i = 0; i < MAX_C; ++i) {
            BuildSum(root, i);
            for (int t = 0; t < tests; ++t) {
                for (int j = 0; j < MAX_C; ++j) {
                    if (charValues[t][i] > charValues[t][j]) {
                        const int trieVertex = ids[ks[t]];
                        result[t] += nodes[trieVertex].Sum[j];
                    }
                }
            }
        }

        for (int t = 0; t < tests; ++t) {
            const int trieVertex = ids[ks[t]];
            result[t] += nodes[trieVertex].UpValue;
        }

        for (int i : result) {
            cout << i << '\n';
        }
        cout.flush();
    }

    return 0;
}

Measured memory_bytes: 118044