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)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int sigma = 26;

struct Node {
  int go[sigma];
  int p, cnt, t, id, l, r;
};

const int maxn = (int) 4e5 + 5;
int n;
string s[maxn];
char tmp[maxn];
char alpha[sigma + 5];
Node trie[maxn];
int freenode;
int ver[maxn];

int newNode(int id, int l, int r, int p) {
  for (int i = 0; i < sigma; i++) {
    trie[freenode].go[i] = -1;
  }
  trie[freenode].cnt = 0;
  trie[freenode].t = -1;
  trie[freenode].id = id;
  trie[freenode].l = l;
  trie[freenode].r = r;
  trie[freenode].p = p;
  return freenode++;
}

int splitEdge(int v, int pos) {
  int u = newNode(trie[v].id, trie[v].l, pos, trie[v].p);
  trie[u].cnt = trie[v].cnt;
  trie[u].go[s[trie[v].id][pos] - 'a'] = v;
  trie[trie[v].p].go[s[trie[v].id][trie[v].l] - 'a'] = u;
  trie[v].l = pos;
  trie[v].p = u;
  return u;
}

void add(int id) {
  int v = 0;
  for (int i = 0; i < sz(s[id]);) {
    trie[v].cnt++;
    if (trie[v].go[s[id][i] - 'a'] == -1) {
      trie[v].go[s[id][i] - 'a'] = newNode(id, i, sz(s[id]), v);
      v = trie[v].go[s[id][i] - 'a'];
      trie[v].cnt++;
      trie[v].t = id;
      ver[id] = v;
      return;
    }
    v = trie[v].go[s[id][i] - 'a'];
    int vid = trie[v].id, l = trie[v].l, r = trie[v].r;
    for (int j = l; j < r; j++, i++) {
      if (i == sz(s[id]) || s[vid][j] != s[id][i]) {
        v = splitEdge(v, j);
        break;
      }
    }
  }
  trie[v].cnt++;
  trie[v].t = id;
  ver[id] = v;
}

int get(int v) {
  int ans = 0;
  int pr = v;
  v = trie[v].p;
  while (v != -1) {
    if (trie[v].t != -1) {
      ans++;
    }
    for (int i = 0; i < sigma; i++) {
      int c = alpha[i] - 'a';
      int u = trie[v].go[c];
      if (u == -1) {
        continue;
      }
      if (u == pr) {
        break;
      }
      ans += trie[u].cnt;
    }
    pr = v;
    v = trie[v].p;
  }
  return ans;
}

int read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  freenode = 0;
  newNode(-1, 0, 0, -1);
  for (int i = 0; i < n; i++) {
    scanf("%s", tmp);
    s[i] = tmp;
    add(i);
  }
  return true;
}

void solve() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int k;
    scanf("%d%s", &k, alpha);
    k--;
    printf("%d\n", get(ver[k]) + 1);
  }
}

int main() {
  precalc();
#ifdef LOCAL
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (true) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}