#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

struct Trie
{
	Trie* nxt[26];
	vector<int>L;
	int end;
	Trie()
	{
		fill(nxt,nxt+26,(Trie*)0);
		L.clear(); end = 0;
	}
};
Trie *root;
int n;
string s[100005];
int q;
string R;
void make(Trie* cur,int index,int ty)
{
	cur->L.pb(ty);
	if(index == s[ty].size()){
		cur->end = 1; return;
	}
	int val = s[ty][index]-'a';
	if(cur->nxt[val]!=(Trie *)0) make(cur->nxt[val],index+1,ty);
	else
	{
		cur->nxt[val]=new Trie;
		make(cur->nxt[val],index+1,ty);
	}
}
string S[100005];
void press(Trie* cur,int dep){
	int x = cur->L.size(),y = 0,z = 0;
	if(cur->end == 1){
		y++;
	}
	for(int i=0;i<26;i++){
		if(cur->nxt[i] != (Trie *)0){
			y++; z = i;
			if(cur->nxt[i]->L.size() == 1){
				int a = (cur->nxt[i]->L)[0];
				S[a].pb('a'+i);
			}
		}
	}
	if(y == 1){
		{
			if(cur->nxt[z] != (Trie *)0){
				if(cur->nxt[z]->L.size() >= 2){
					press(cur->nxt[z],dep+1);
				}
			}
		}
	}
	else{
		for(int i=0;i<26;i++){
			if(cur->nxt[i] != (Trie *)0){
				if(cur->nxt[i]->L.size() >= 2){
					for(int j=0;j<cur->nxt[i]->L.size();j++){
						int a = (cur->nxt[i]->L)[j];
						S[a].pb('a'+i);
					}
					press(cur->nxt[i],dep+1);
				}
			}
		}
	}
}
int LO[100005],nxt = 0;
struct Trie2
{
	Trie2* nxt[26];
	int num,end;
	Trie2()
	{
		fill(nxt,nxt+26,(Trie2*)0);
		num = 0; end = 0;
	}
};
Trie2* root2[5005];
void make2(Trie2* cur,int index,int ty)
{
	if(index == S[ty].size()){
		cur->end=1; return ;
	}
	int val = S[ty][index]-'a';
	if(cur->nxt[val]!=(Trie2 *)0) make2(cur->nxt[val],index+1,ty);
	else
	{
		cur->nxt[val]=new Trie2;
		make2(cur->nxt[val],index+1,ty);
	}
}
int go2(Trie2* cur,int dep){
	cur->num = cur->end;
	for(int i=0;i<26;i++){
		if(cur->nxt[i] != (Trie2*)0){
			cur->num += go2(cur->nxt[i],dep+1);
		}
	}
	return cur->num;
}
void go(Trie* cur,int dep){
	for(int i=0;i<26;i++){
		if(cur->nxt[i] != (Trie *)0){
			go(cur->nxt[i],dep+1);
		}
	}
	if(dep == 1000 && cur->L.size() >= 2){
		vector<int>&vi = cur->L;
		press(cur,dep);
		root2[nxt] = new Trie2;
		for(int i=0;i<vi.size();i++){
			LO[vi[i]] = nxt;
			make2(root2[nxt],0,vi[i]);
		}
		go2(root2[nxt],0);
		nxt++;
	}
}
int ret;
void query(Trie* cur,int idx,int ty){
	if(idx == s[ty].size() || cur->L.size() == 1) return;
	char hoge = s[ty][idx];
	for(int i=0;i<R.size();i++){
		if(R[i] == hoge) break;
		if(cur->nxt[(R[i]-'a')] != (Trie *)0) ret += cur->nxt[(R[i]-'a')]->L.size();
	}
	if(idx == 999) return;
	ret += cur->end;
	query(cur->nxt[(hoge-'a')],idx+1,ty);
}
void query2(Trie2* cur,int idx,int ty){
	if(idx == S[ty].size() || cur->num == 1) return;
	char hoge = S[ty][idx];
	for(int i=0;i<R.size();i++){
		if(R[i] == hoge) break;
		if(cur->nxt[(R[i]-'a')] != (Trie2 *)0) ret += cur->nxt[(R[i]-'a')]->num;
	}
	ret += cur->end;
	query2(cur->nxt[(hoge-'a')],idx+1,ty);
}
int main(){
	memset(LO,-1,sizeof(LO));
	cin >> n; root = new Trie;
	for(int i=0;i<n;i++){
		cin >> s[i];
		make(root,0,i);
	}
	go(root,0);
	cin >> q;
	for(int i=0;i<q;i++){
		int num; cin >> num >> R;
		num--; ret = 1;
		query(root,0,num);
		if(LO[num] == -1){
			cout << ret << endl;
		}
		else{
			int x = LO[num];
			query2(root2[x],0,num);
			cout << ret << endl;
		}
	}
}

Measured memory_bytes: 140292