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++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

///#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 {
    vector<pii> vto;
    int to[ABC];
    int link;
    int leaf;
    int cnt;
    int soon;

    Node() {
        memset(to, -1, sizeof to);
        link = -1;
        leaf = -1;
        cnt = 0;
        soon = 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);
    }
}

int h[S];

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

    if (bor[u].soon == 1) {
        for (int to = 0; to < ABC; ++to) {
            int v = bor[u].to[to];
            if (v != -1) {
                if (bor[v].leaf != -1 || bor[v].soon > 1) {
                    bor[u].link = v;
                } else {
                    bor[u].link = bor[v].link;
                }
                break;
            }
        }
    }
}

int pos['z' + 4];

int calc(int k, string& p) {
    --k;
    int res = 0, u = 0;

    for (int i = 0; i < ABC; ++i) {
        pos[p[i]] = i;
    }

    for (int i = 0; i < size(b[k]);) {
        res += (bor[u].leaf != -1);
        if (bor[u].soon == 1) {
            int v = bor[u].link;
            i += h[v] - h[u];
            u = v;
            continue;
        }

        int board = pos[b[k][i]];
        for (pii to : bor[u].vto) {
            if (pos[to.first + 'a'] < board) {
                res += bor[to.second].cnt;
            }
        }
        u = bor[u].to[b[k][i] - 'a'];
        ++i;
    }
    return res + 1;
}

void solve() {
    read();
    dfs(0, 0);

    int q;
    cin >> q;
    while (q--) {
        int k;
        string p;
        cin >> k >> p;
        cout << calc(k, 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;
}