#include <bits/stdc++.h>
        #include<iostream>
        #include<cstdio>
        #include<vector>
        #include<queue>
        #include<map>
        #include<cstring>
        #include<string>
        #include <math.h>
        #include<algorithm>
    //    #include <boost/multiprecision/cpp_int.hpp>
        #include<functional>
   //   #define int long long
        #define inf  1000000007
        #define pa pair<int,int>
#define ll long long
        #define pal pair<ll,ll>
        #define ppa pair<int,pa>
        #define  mp make_pair
        #define EPS (1e-10)
        #define equals(a,b) (fabs((a)-(b))<EPS)

        using namespace std;

        class Point{
        	public:
        	double x,y;
        	Point(double x=0,double y=0):x(x),y(y) {}
        	Point operator + (Point p) {return Point(x+p.x,y+p.y);}
        	Point operator - (Point p) {return Point(x-p.x,y-p.y);}
        	Point operator * (double a) {return Point(x*a,y*a);}
        	Point operator / (double a) {return Point(x/a,y/a);}
        	double absv() {return sqrt(norm());}
        	double norm() {return x*x+y*y;}
        	bool operator < (const Point &p) const{
        		return x != p.x ? x<p.x: y<p.y;
        	}
        	bool operator == (const Point &p) const{
        		return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
        	}
        };
        typedef Point Vector;

        struct Segment{
        Point p1,p2;
        };

    double hen(Vector a){
    if(fabs(a.x)<EPS && a.y>0) return acos(0);
    else if(fabs(a.x)<EPS && a.y<0) return 3*acos(0);
    else if(fabs(a.y)<EPS && a.x<0) return 2*acos(0);
    else if(fabs(a.y)<EPS && a.x>0) return 0.0;
    else if(a.y>0) return acos(a.x/a.absv());
    else return 2*acos(0)+acos(-a.x/a.absv());

    }

        double dot(Vector a,Vector b){
        	return a.x*b.x+a.y*b.y;
        }
        double cross(Vector a,Vector b){
        	return a.x*b.y-a.y*b.x;
        }
        int dx[4] = {1, 0, -1, 0};
        int dy[4] = {0, 1, 0, -1};
        //----------------kokomade temple------------

typedef struct trie{
	char moji;
	int las;
	int hukasa;
	int child;
	vector< trie* > ne;
}trie;


void makeend(trie* t){
	(*t).las=1;
	return;
}

trie* maketrie(){
	trie *p;
	p=new trie;
	p->las=0;
	p->child=0;
	return p;
}


string sss;
int d;
void addstring(trie* t,int ichi){
//	cout<<ichi<<endl;
//	int d=sss.length();
	t->child ++;
	if(d==ichi) return;
	
	int l=((*t).ne).size();
	for(int i=0;i<l;i++){
		if((*(((*t).ne)[i])).moji==sss[ichi]){
			if(d==ichi+1) makeend((((*t).ne)[i]));
			addstring((((*t).ne)[i]),ichi+1);
			return;
		}
		
		
	}
 	trie *p;
	p=maketrie();
	p->hukasa=ichi+1;
	p->moji=sss[ichi];
	(*t).ne.push_back(p);
	if(d==ichi+1) makeend((((*t).ne)[l]));
	addstring((((*t).ne)[l]),ichi+1);
			
	
	return;
}



void checktrie(trie *p){
	cout<< p->moji <<" "<<p->hukasa<<" "<<p->child;
	int l=((*p).ne).size();
	for(int i=0;i<l;i++){
		cout<<" "<<((*p).ne)[i]->moji;
	}
	if(p->las==1) cout<<" last";
	cout<<endl;
	for(int i=0;i<l;i++){
		checktrie(((*p).ne)[i]);
	}
}
int owari[100002]={0};
int n,q,k,z;
string st;
string s[100002];
int zz[100002][28][28]={0};
signed main(){
	cin>>n;
	
	
	trie *nekko;
	
	nekko=maketrie();
	nekko->moji='^';
	for(int i=1;i<=n;i++){
		cin>>s[i];
		sss=s[i];
		d=sss.length();
			addstring(nekko,0);
	}
	
	cin>>q;
	
	for(int i=1;i<=n;i++){
		trie *p=nekko,*tugi;
		while(1){
			if(p->hukasa == s[i].length()) break;
			int l=((*p).ne).size();
			int gg;
			for(int j=0;j<l;j++){
				tugi=((*p).ne)[j];
				if(s[i][(tugi->hukasa)-1]== tugi->moji ) gg=j;
				else{
					zz[i][(tugi->moji)-'a'][s[i][(tugi->hukasa)-1]-'a'] += tugi->child;
				}
			}
			if(p->las==1) owari[i]++;
			p=((*p).ne)[gg];
			
		}
	
		
	}
	
	
//	cout<<"s"<<endl;
	for(int zzzz=1;zzzz<=q;zzzz++){
		
		cin>>k;
		
		cin>>st;
		int ans=0;
		for(int i=0;i<26;i++){
			for(int j=i+1;j<26;j++){
				ans += zz[k][st[i]-'a'][st[j]-'a'];
			}
		}
		cout<<ans+owari[k]+1<<endl;
	}

	return 0;
}

//printf("%.10f\n",ans);


Measured memory_bytes: 320000