/*
*/

#pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS

#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define have adsgagshdshfhds

#define eps 1e-9
#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 350

using namespace std;

const int INF = 1e9;
const int N = 220031;

int n;
string st[N];
int tests;
int id[N];
string Zr[N];
vector<int> queries[N];

vector<pair<string, int> > O;

int ans[N];
int cnt[N];

vector<long long> P[N];

int lcp(int a, int b)
{
	int l, r;
	l = 0;
	r = min(st[a].size(), st[b].size());
	while (l < r)
	{
		int mid = l + r + 1;
		mid /= 2;
		long long Q1 = P[a][mid];
		long long Q2 = P[b][mid];
		if (Q1 == Q2)
			l = mid;
		else
			r = mid - 1;
	}
	return l;
}

void solve(int l, int r, int start)
{
	//cout << l << "%" << r << " " << start << endl;

	int jump = O[l].first.size();
	for (int i = l; i <= r; i++)
	{
		jump = min(jump, lcp(O[i].second, O[l].second));
	}

	start = jump;

	for (int i = 'a'; i <= 'z'; i++)
	{
		cnt[i] = 0;
	}

	int emp = 0;

	for (int i = l; i <= r; i++)
	{
		if (O[i].first.size() == start)
		{
			emp++;
			continue;
		}
		cnt[O[i].first[start]]++;
	}

	for (int i = l; i <= r; i++)
	{
		int id = O[i].second;
		if (O[i].first.size() == start)
		{
			for (int j = 0; j < queries[id].size(); j++)
			{
				int query_id = queries[id][j];
				ans[query_id] += i - l;
			}
			continue;
		}

		for (int j = 0; j < queries[id].size(); j++)
		{
			int query_id = queries[id][j];
			//	cout << i << "%" << j << " " << query_id << " "<<ans[query_id]<<" "<<l<<" "<<r<<" "<<start<<endl;

			for (int p = 0; p < 26; p++)
			{
				if (Zr[query_id][p] == O[i].first[start])
					break;
				ans[query_id] += cnt[Zr[query_id][p]];
			}
			ans[query_id] += emp;
			//	cout << i << "%" << j << " " << query_id << " " << ans[query_id] << " " << l << " " << r << " " << start << endl;
		}
	}
	int ptr = l;
	do
	{
		if (O[ptr].first.size() == start)
		{
			++ptr;
			continue;
		}
		int ptr2 = ptr;
		while (ptr2 + 1 <= r&&O[ptr2 + 1].first[start] == O[ptr].first[start])
			++ptr2;
		solve(ptr, ptr2, start + 1);
		ptr = ptr2 + 1;
	} while (ptr <= r);
}

vector<int> entries[N];
vector<int> id_order;
/*
bool cmp(int p1, int p2)
{
	int Q = lcp(p1, p2);
	if (Q == st[p1].size() && Q == st[p2].size())
		return p1 < p2;
	if (Q == st[p1].size())
		return true;
	if (Q == st[p2].size())
		return false;
	return st[p1][Q] < st[p2][Q];
}
*/
int main(){
	//freopen("fabro.in","r",stdin);
	//freopen("fabro.out","w",stdout);
	//freopen("F:/in.txt", "r", stdin);
	//freopen("F:/output.txt", "w", stdout);
	ios_base::sync_with_stdio(0);
	//cin.tie(0);

	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> st[i];
		P[i].resize(st[i].size() + 1);
		long long here = 0;
		for (int j = 0; j < st[i].size(); j++)
		{
			here = here * 173 + st[i][j];
			//here %= bs;
			P[i][j + 1] = here;
		}

		O.push_back(make_pair(st[i], i));
	}

	sort(O.begin(), O.end());

	//	sort(O.begin(), O.end(),cmp);

	cin >> tests;
	for (int test = 1; test <= tests; test++)
	{
		cin >> id[test];
		cin >> Zr[test];
		queries[id[test]].push_back(test);
	}

	solve(0, n - 1, 0);

	for (int i = 1; i <= tests; i++)
	{
		cout << ans[i] + 1 << endl;
	}

	cin.get(); cin.get();
	return 0;
}

Measured memory_bytes: 43632