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)

#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

template<class T>
class SparseTable {
	const int MAX_LOG=22;
	vector<vector<T>>t;
	vector<int>l2;
	function<T(T,T)>F;
public:
	int n;
	T INIT;
	void init(int n_,function<T(T,T)>f){
		n=n_;
		F=f;
		t=vector<vector<T>>(MAX_LOG);
		rep(i,MAX_LOG)t[i].assign(n,INT_MAX);
		l2.assign(n+1,0);
		int k=0;
		rep(i,n+1){
			while((1<<(k+1))<=i)k++;
			l2[i]=k;
		}
	}
	void build(int n_,T*a,function<T(T,T)>f){
		init(n_,f);
		rep(i,n)t[0][i]=a[i];
		for (int i=1;i<MAX_LOG;i++) {
			for(int j=0;j+(1<<i)<=n;j++){
				t[i][j]=F(t[i-1][j],t[i-1][j+(1<<(i-1))]);
			}
		}
	}
	T query(int a,int b) {
		int l=l2[b-a];
		return F(t[l][a],t[l][b-(1<<l)]);
	}
};

const int B=300;
char S[500000];
string s[200000];

struct node{
	unordered_map<char,node*>nx;
	int cnt;
	vector<int>v;
}pool[500000];
node*root=&pool[0];
int pointer=1;


node*last[200000];
int node_cnt=0;
map<node*,int>vid;
int vs[1000000];
int pos[26];

SparseTable<int>tab;

void add(int id){
	node*x=root;
	for(char c:s[id]){
		x->v.push_back(id);
		if(x->nx[c]==NULL)x->nx[c]=&pool[pointer++];
		x=x->nx[c];
	}
	x->v.push_back(id);
	last[id]=x;
	x->cnt++;
}

bool Less(int a,int b){
	int A=s[a].size(),B=s[b].size();
	int l=vid[last[a]],r=vid[last[b]];if(l>r)swap(l,r);
	int ok=tab.query(l,r+1);
	if(min(A,B)==A&&ok==A)return true;
	if(min(A,B)==B&&ok==B)return false;
	return pos[s[a][ok]-'a']<pos[s[b][ok]-'a'];
}

void dfs(node*v,int dep){
	vid[v]=node_cnt;
	vs[node_cnt++]=dep;
	for(auto&p:v->nx){
		dfs(p.second,dep+1);
		vs[node_cnt++]=dep;
	}
}

char C[27];

int main(){
	int n;cin>>n;
	rep(i,n){
		scanf("%s",S);
		s[i]=S;
		add(i);
	}
	dfs(root,0);
	tab.build(node_cnt,vs,[](int a,int b){return min(a,b);});
	int q;cin>>q;
	rep(i,q){
		int K;scanf("%d",&K);K--;
		scanf("%s",C);
		rep(j,26){
			pos[C[j]-'a']=j;
		}
		int ans=1;
		node*x=root;
		for(char c:s[K]){
			if(x->v.size()<=B){
				for(int j:x->v){
					if(j==K)continue;
					if(Less(j,K))ans++;
				}
				break;
			}
			for(auto&p:x->nx){
				if(pos[p.first-'a']<pos[c-'a'])ans+=p.second->v.size();
			}
			ans+=x->cnt;
			x=x->nx[c];
		}
		printf("%d\n",ans);
	}
}