#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <fstream>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <stack>
#include <map>
#include <set>
#if __cplusplus > 199711L
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
//	tree<key, mapped = null_type, cmp = less<key>, rb_tree_tag, tree_order_statistics_node_update> name;
//	order_of_key
//	find_by_order
#endif

using namespace std;

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fs first
#define sc second
#define abs(a) ((a) < 0 ? -(a) : (a))
#define sqr(a) ((a) * (a))

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

inline pair<pair<bool, bool>, pair<string, bool> > useinout();

void init() {
#ifdef SOL
	if (useinout().fs.fs)
		freopen("input.txt", "r", stdin);
	if (useinout().fs.sc)
		freopen("output.txt", "w", stdout);
#else
	srand(time(0));
	const string file = useinout().sc.fs;
	if (!file.empty()) {
		freopen((file + ".in").c_str(), "r", stdin);
		freopen((file + ".out").c_str(), "w", stdout);
	}
	if(useinout().sc.sc) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	}
#endif
}

int solve();

void finish(int exitval) {
	fflush(stdout);
#ifdef SOL
	fprintf(stderr, "\nexit value: %d\ntime: %.3lf ms\n", exitval, 1000. * clock() / CLOCKS_PER_SEC);
#endif
}

int main() {
	init();
	int exitval = solve();
	finish(exitval);
	return (0);
}

const double eps = 1e-9;
const int mod = (int) 1e+9 + 7;
const double pi = acos(-1.);
const int maxn = 400100;

inline pair<pair<bool, bool>, pair<string, bool> > useinout() {
	return (mp(mp(1, 0), mp("", 0)));
}

int nx[maxn][26];
int val[maxn], sz = 1;
int c[maxn];
int l[maxn];
int fl[maxn];
int fst[maxn];
string s[maxn];

void insert(string &s, int id) {
	int pos = 0;
	for (int i = 0; i < (int) s.length(); i++) {
		c[pos]++;
		if (!nx[pos][s[i] - 'a']) {
			nx[pos][s[i] - 'a'] = sz++;
		}
		fst[pos] = pos;
		l[pos] = i;
		fl[pos] = i;
		pos = nx[pos][s[i] - 'a'];
	}
	fst[pos] = pos;
	l[pos] = s.length();
	fl[pos] = s.length();
	c[pos]++;
	val[pos] = id;
}

void get(string &s, string &t) {
	int k = 0;
	int pos = 0;
	for (int i = 0; i < (int) s.length(); i++) {
		for (int j = 0; j < 26; j++) {
			if (t[j] == s[i])
				break;
			if (nx[pos][t[j] - 'a']) {
				k += c[nx[pos][t[j] - 'a']];
			}
		}
//		cout << i << " " << pos << " " << fst[pos] << endl;
		if (fst[pos] != pos) {
			fst[pos] = fst[fst[pos]];
			fl[pos] = l[fst[pos]];
			i = fl[pos] - 1;
			pos = fst[pos];
		} else {
			if (c[pos] == c[nx[pos][s[i] - 'a']]) {
				fst[pos] = nx[pos][s[i] - 'a'];
				fl[pos] = l[nx[pos][s[i] - 'a']];
			}
			pos = nx[pos][s[i] - 'a'];
		}
		k += (val[pos] > 0);
	}
	printf("%d\n", k);
}

int solve() {

	int n;
	scanf("%d\n", &n);
	for (int i = 0; i < n; i++) {
		getline(cin, s[i]);
		insert(s[i], i + 1);
	}
	int q;
	scanf("%d", &q);
	for (; q--;) {
		int k;
		scanf("%d ", &k);
		string t;
		getline(cin, t);
		get(s[k - 1], t);
	}

	return (0);
}

// by Andrey Kim


Measured memory_bytes: 52608