#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime> 
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>

using namespace std;

const int L = 4e5 + 7;
const int ALP = 26;
const int INF = 1e9 + 239;
const int BASE = 29;
const int MOD = 1e9 + 9;
const int C = 500;

int add(int a, int b) {
	return (a + b) % MOD;
}

int mul(int a, int b) {
	return 1LL * a * b % MOD;
}

int go[L][ALP];
int l[L];
int r[L];
int cnt[L];
int term[L];

int root;

int pnt = 0;

int new_vertex() {
	for (int i = 0; i < ALP; i++) {
		go[pnt][i] = -1;
	}
	l[pnt] = INF;
	r[pnt] = -INF;
	cnt[pnt] = 0;
	term[pnt] = 0;
	pnt++;
	return pnt - 1; 
}

void init() {
	root = new_vertex();
}

void add(string s, int ind) {
	int cur = root;
	for (auto c : s) {
		l[cur] = min(l[cur], ind);
		r[cur] = max(r[cur], ind);
		cnt[cur]++;
		if (go[cur][c - 'a'] == -1) {
			go[cur][c - 'a'] = new_vertex();
		}
		cur = go[cur][c - 'a'];
	}
	cnt[cur]++;
	l[cur] = min(l[cur], ind);
	r[cur] = max(r[cur], ind);
	term[cur] = 1;
}

vector<string> a;

int go_down(vector<int> ord, int k, int lim, int &ans) {
	int cur = root;
	int alr = 0;
	while (r[cur] - l[cur] > lim) {
		if (alr == (int)a[k].size()) {
			return -1;
		}
		ans += term[cur];
		for (auto x : ord) {
			if (go[cur][x] == -1) continue;
			if (a[k][alr] == x + 'a') {
				cur = go[cur][x];
				alr++;
				break;
			} else {
				ans += cnt[go[cur][x]];
			}
		}
	}
	return cur;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);	

	init();

	int n;
	cin >> n;
	a.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	vector<int> ind(n);
	iota(ind.begin(), ind.end(), 0);
	sort(ind.begin(), ind.end(), [&](int i, int j) {
		return a[i] < a[j];
	});
	vector<int> wri(n);
	for (int i = 0; i < n; i++) {
		wri[ind[i]] = i;
	}
	sort(a.begin(), a.end());
	for (int i = 0; i < n; i++) {
		add(a[i], i);
	}

	vector<vector<int>> h(n);
	for (int i = 0; i < n; i++) {
		h[i].resize(a[i].size() + 1);
		for (int j = 0; j < (int)a[i].size(); j++) {
			h[i][j + 1] = add(mul(h[i][j], BASE), a[i][j]);
		}
	}

	auto lcp = [&](int i, int j) {
		int ll = 0;
		int rr = min(a[i].size(), a[j].size()) + 1;
		while (rr - ll > 1) {
			int mm = (rr + ll) / 2;
			if (h[i][mm] == h[j][mm]) {
				ll = mm;
			} else {
				rr = mm;
			}
		}
		return ll;
	};
	auto cmp = [&](int i, int j, vector<int> ord) {
		vector<int> wr(ALP);
		for (int f = 0; f < ALP; f++) {
			wr[ord[f]] = f;
		}
		int l = lcp(i, j);
		if (l == (int)a[i].size()) {
			return true;
		} else if (l == (int)a[j].size()) {
			return false;
		} else {
			return wr[a[i][l] - 'a'] < wr[a[j][l] - 'a'];
		}
	};
	auto process = [&](int l, int r, int x, vector<int> ord, int &ans) {
		int best = l;
		for (int i = l; i <= r; i++) {
			if (i != x) {
				ans += cmp(i, x, ord);
			}
		}
		return best;
	};
	int q;
	cin >> q;
	while (q--) {
		int k;
		string s;
		cin >> k >> s;
		k--;
		k = wri[k];
		vector<int> ord;
		for (auto x : s) {
			ord.push_back(x - 'a');
		}
		int ans = 1;
		int V = go_down(ord, k, C, ans);
		if (V == -1) {
			cout << ans << '\n';
		} else {
			process(l[V], r[V], k, ord, ans);
			cout << ans << '\n';
		}
	}
}

Measured memory_bytes: 52136