Problem:
Score : 1100 points

Problem Statement

There are N strings of lowercase alphabet only. The i-th string is S_i. Every string is unique.

Provide answers for the Q queries below. The i-th query has the following format:

Query: An integer k_i and a string p_{i,1}p_{i,2}...p_{i,26} that results from permuting {a,b,...,z} are given. Output the sequence of the string S_{k_i} among the N strings in lexicographical order when the literal sequence is p_{i,1}<p_{i,2}<...<p_{i,26}.

Constraints

1 ≦ N,Q ≦ 100000

1 ≦ |S_i| (1 ≦ i ≦ N)

S_i (1 ≦ i ≦ N) is a string of lowercase alphabet.

The sum of |S_i| is no more than 400000.

Every S_i is unique.

1 ≦ k_i ≦ N (1 ≦ i ≦ Q)

For all 1 ≦ i ≦ Q, p_{i,1}p_{i,2}...p_{i,26} is a permutation of abcd...z.

Input

Inputs are provided from standard inputs in the following form.

N
S_1
:
S_N
Q
k_1 p_{1,1}p_{1,2}...p_{1,26}
:
k_Q p_{Q,1}p_{Q,2}...p_{Q,26}

Output

Output Q lines.

On line i, for the i-th query, output an integer indicating the sequence of the string S_{k_i} among the N strings in lexicographical order.

Sample Input 1

5
aa
abbaa
abbba
aaab
aaaaaba
5
1 abcdefghijklmnopqrstuvwxyz
2 bacdefghijklmnopqrstuvwxyz
3 abcdefghijklmnopqrstuvwxyz
4 bacdefghijklmnopqrstuvwxyz
5 abcdefghijklmnopqrstuvwxyz

Sample Output 1

1
2
5
4
2

When the literal sequence is a < b, sorting the input strings in lexicographical order yields aa, aaaaaba, aaab, abbaa, abbba. The answers to queries 1, 3, and 5 are thus 1, 5, and 2, respectively.

When the literal sequence is b < a, sorting the input strings in lexicographical order yields abbba, abbaa, aa, aaab, aaaaaba. The answers to queries 2 and 4 are thus 2 and 4, respectively.

Sample Input 2

8
abrakatabra
abadaba
abracadabra
atcoder
grand
contest
ababa
a
6
3 abcdefghijklmnopqrstuvwxyz
6 qwertyuiopasdfghjklzxcvbnm
8 poiuytrewqlkjhgfdsamnbvcxz
2 qazwsxedcrfvtgbyhnujmikolp
1 plokmijnuhbygvtfcrdxeszwaq
4 mnbvcxzasdfghjklpoiuytrewq

Sample Output 2

4
8
2
3
4
7

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#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;
}