#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
struct Trie {
	int cnt;
	Trie* ptr[29];
	Trie() { cnt = 0; fill(ptr, ptr + 27, (Trie*)0); }
};
Trie* root = new Trie;
void add(string str) {
	Trie* pos = root; pos->cnt++;
	for (int i = 0; i < str.size(); i++) {
		if (!pos->ptr[str[i] - 96]) pos->ptr[str[i] - 96] = new Trie;
		pos = pos->ptr[str[i] - 96];
		pos->cnt++;
	}
}
int N, Q, x, val[100009][29][29]; string s[100009], t;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> s[i]; s[i] += '`';
		add(s[i]);
	}
	for (int i = 0; i < N; i++) {
		Trie* pos = root;
		for (int j = 0; j < s[i].size(); j++) {
			for (int k = 0; k < 27; k++) {
				if (pos->ptr[k]) val[i][s[i][j] - 96][k] += pos->ptr[k]->cnt;
			}
			pos = pos->ptr[s[i][j] - 96];
		}
	}
	cin >> Q;
	while (Q--) {
		cin >> x >> t; x--;
		t = '`' + t;
		int ret = N;
		for (int i = 0; i < 27; i++) {
			for (int j = i + 1; j < 27; j++) {
				ret -= val[x][t[i] - 96][t[j] - 96];
			}
		}
		cout << ret << "\n";
	}
	return 0;
}