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: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;

template<typename T, typename VAL_TYPE>
struct trie{
	typedef function<int(T)> To_Int;
	int SIGMA;
	To_Int to_int;

	struct node{
		vector<VAL_TYPE> val;
		int cnt;
		vector<node *> nexts;
	};

	node *root;

	trie(int SIGMA, To_Int to_int) : SIGMA(SIGMA), to_int(to_int), root(NULL) {}

	int ret_cnt(node *nd){
		if(nd == NULL) return 0;
		else return nd->cnt;
	}

	vector<VAL_TYPE> ret_val(node *nd){
		if(nd == NULL) return vector<VAL_TYPE>(0);
		else return nd->val;
	}

	//ndにSの[l,r)を追加したノードを返す
	node *add(node *nd, string &S, int l, int r, VAL_TYPE v){
		node *ans;
		if(nd == NULL){
			ans = new node;
			ans->val.assign(0, v);
			ans->cnt = 0;
			ans->nexts.assign(SIGMA, NULL);
		}
		else{
			ans = nd;
		}
		ans->cnt++;
		if(l == r){
			ans->val.push_back(v);
		}
		else{
			ans->nexts[to_int(S[l])] = add(ans->nexts[to_int(S[l])], S, l + 1, r, v);
		}
		return ans;
	}

	//(S,v)を追加する
	void add(string &S, VAL_TYPE v){
		root = add(root, S, 0, S.size(), v);
	}

	//ndからSの[l,r)を辿ったノードを返す(無いときはNULL)
	node *search(node *nd, string &S, int l, int r){
		if(nd == NULL){
			return NULL;
		}
		else if(l == r){
			return nd;
		}
		else{
			return search(nd->nexts[to_int(S[l])], S, l + 1, r);
		}
	}

	//Sをprefixに含む文字列の個数
	int prefix_count(string &S){
		return ret_cnt(search(root, S, 0, S.size()));
	}

	//Sの値
	vector<VAL_TYPE> value(string &S){
		return ret_val(search(root, S, 0, S.size()));
	}

	void junp_check(node *nd, string &S, vector<int> &junp, int l, int r){
		if(l < r && nd != NULL && nd->nexts[to_int(S[l])] != NULL){
			if(nd->cnt == nd->nexts[to_int(S[l])]->cnt){
				junp[l] = 1;
			}
			junp_check(nd->nexts[to_int(S[l])], S, junp, l + 1, r);
		}
	}

	void junp_check(string &S, vector<int> &junp){
		junp_check(root, S, junp, 0, S.size());
	}

	node *compression(node *nd){
		if(nd == NULL || ret_cnt(nd) == 0){
			return NULL;
		}
		for(int i = 0; i < SIGMA; i++){
			node *ndi = nd->nexts[i];
			if(nd->cnt == ret_cnt(ndi)){
				return compression(ndi);
			}
			nd->nexts[i] = compression(ndi);
		}
		return nd;
	}

	void compression(){
		root = compression(root);
	}

	int solve(node *nd, string &S, int l, int r, string &p){
		if(l == r || nd == NULL){
			return 0;
		}
		else{
			int ans = ret_val(nd).size();
			for(int i = 0; i < SIGMA; i++){
				if(p[i] == S[l]){
					return ans + solve(nd->nexts[to_int(p[i])], S, l + 1, r, p);
				}
				else{
					ans += ret_cnt(nd->nexts[to_int(p[i])]);
				}
			}
		}
	}

	int solve(string &S, string &p){
		return solve(root, S, 0, S.size(), p);
	}
};

int char_to_int(char c){
	return (int)(c - 'a');
}

int main(){
	int N, Q, i, j;
	scanf("%d", &N);
	vector<string> S(N);
	for(i = 0; i < N; i++){
		cin >> S[i];
	}
	trie<char, int> t(26, char_to_int);
	for(i = 0; i < N; i++){
		t.add(S[i], i);
	}
	vector<vector<int>> junp(N);
	vector<string> Scomp(N, "");
	for(i = 0; i < N; i++){
		junp[i] = vector<int>(S[i].size(), 0);
		t.junp_check(S[i], junp[i]);
		for(j = 0; j < S[i].size(); j++){
			if(junp[i][j] == 0){
				Scomp[i].push_back(S[i][j]);
			}
		}
	}
	t.compression();
/*	printf("compression!\n");
	for(i = 0; i < N; i++){
		cout << Scomp[i] << endl;
	}
	while(1){
		int ins;
		string T;
		scanf("%d", &ins);
		cin >> T;
		if(ins == 0){
			printf("%d\n", t.prefix_count(T));
		}
		else{
			vector<int> ids = t.value(T);
			for(i = 0; i < ids.size(); i++){
				printf("%d ", ids[i]);
			}
			printf("\n");
		}
	}
*/	scanf("%d", &Q);
	for(i = 0; i < Q; i++){
		int k;
		string p;
		scanf("%d", &k);
		cin >> p;
		printf("%d\n", t.solve(Scomp[k - 1], p) + 1);
	}
	return 0;
}