#define _CRT_SECURE_NO_WARNINGS
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <map>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <time.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define FILL(A,val) memset(A,val,sizeof(A))
#define ITER(it,a) for(__typeof(a.begin()) it=a.begin();it!=a.end();++it)
#define DBG1(a) cerr<<#a<<" = "<<(a)<<"\n"
#define DBG2(a,b) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"

#define ALL(V) V.begin(),V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define y1 frgrwegreg

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;

const double PI = acos(-1.0);
const int MOD = 1000 * 1000 * 1000 + 7;
const int MAX = 100017;

struct node
{
	int A[26];
	int num, cnt;
	PII from;

	node()
	{
		num = -1;
		from = MP(-1,-1);
		cnt = 0;
		FILL(A,-1);
	}
};
int n;
string s;
int QWE[100000];

struct automat
{
	node A[400007];
	int N;

	automat()
	{
		N = 1;
	};

	void add(string s, int pt)
	{
		int pos = 0;
		FOR (i,0,s.size())
		{
			A[pos].cnt++;
			if (A[pos].A[s[i]-'a'] == -1)
			{
				A[pos].A[s[i]-'a'] = N++;
				A[A[pos].A[s[i]-'a']].from = MP(pos,s[i]-'a');
			}
			pos = A[pos].A[s[i]-'a'];
		}
		A[pos].cnt++;
		A[pos].num = pt+1;
		QWE[pt] = pos;
	}

	void go(int pos = 0)
	{
		cout << pos;
		FOR (i,0,26)
			if (A[pos].A[i] != -1)
			{
				cout << char('a'+i) << '(';
				go(A[pos].A[i]);
				cout << ")";
			}
	}

	void comp(int pos, int from, int type)
	{
		if (!pos && from==0 && type==0) throw - 1;
		//cout << pos<<" "<<from<<" "<<type<<endl;
		bool ok = 1;
		if (type == -1) ok = 0;
		if (A[pos].num != -1) ok = 0;
		int cnt = 0, TP = -1;
		FOR (i,0,26)
		 	 if (A[pos].A[i] != -1) cnt++, TP = A[pos].A[i];
		if (ok && cnt < 2)
		{
			//cout << "*";
			A[from].A[type] = TP;
			A[A[from].A[type]].from = MP(from,type);
			comp(TP,from,type);
		}
		else
		{
			FOR (i,0,26)
				if (A[pos].A[i] != -1)
				{
					//cout << i<<" "<<A[pos].A[i]<<"*\n";
					comp(A[pos].A[i],pos,i);
				}
		}
	}

	int get(int pos, int NUM, int from = -1)
	{
		//cout << pos <<" <-> "<<NUM<<" "<<from<<endl;
		//if (!NUM && A[pos].num != -1) return A[pos].num;
		if (A[pos].num != -1) NUM++;
		if (from != -1)
		FOR (q,0,s.size())
		{
			if (s[q]-'a' == from) break;
			//cout << s[q]-'a'<<" "<<from<<endl;
			if (A[pos].A[s[q]-'a'] != -1)
			{
				//cout << pos<<" "<<s[q]-'a'<<" "<<A[A[pos].A[s[q]-'a']].cnt<<endl;
				NUM += A[A[pos].A[s[q]-'a']].cnt;
			}
		}
		if (pos == 0) return NUM;
		return get(A[pos].from.first,NUM,A[pos].from.second);
		//cout << "bad "<<NUM<<endl;
		//return -23;
	}
} AUT;


int q, num;

int main()
{
	//freopen("C.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
	//ios::sync_with_stdio(0); cin.tie(0);

	cin >> n;
	FOR (i,0,n)
	{
		cin >> s;
		AUT.add(s,i);
	}
	//AUT.go();
	//cout << endl;
	AUT.comp(0,-1,-1);
	//AUT.go();
	//cout << endl;
	cin >> q;
	FOR (i,0,q)
	{
		cin >> num >> s;
		num--;
		cout << AUT.get(QWE[num],0) << "\n";
	}
	return 0;

}


Measured memory_bytes: 48128