#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8

typedef pair<string,int> psi;

struct node{
	int to[26];
	int cnt;
	int minidx, maxidx;
	explicit node(int idx){
		fill(ALL(to), -1);
		cnt = 0;
		minidx = maxidx = idx;
	}
};

vector<node> trie;

string input(){
	static char buf[400010];
	scanf("%s", buf);
	return buf;
}


void mainmain(){
	int n;
	scanf("%d", &n);
	vector<psi> ss;
	ss.reserve(n);
	for(int i = 0; i < n; ++i){
		ss.emplace_back(input(), i);
	}
	sort(ALL(ss));
	vector<int> rtbl(n);
	for(int i = 0; i < n; ++i){
		rtbl[ss[i].second] = i;
	}

	trie.emplace_back(0);
	for(int i = 0; i < n; ++i){
		int u = 0;
		for(char c : ss[i].first){
			int d = c - 'a';
			if(trie[u].to[d] == -1){
				trie[u].to[d] = trie.size();
				trie.emplace_back(i);
			}
			u = trie[u].to[d];
			trie[u].maxidx = i;
			++trie[u].cnt;
		}
	}
	trie[0].cnt = n;
	trie[0].maxidx = n - 1;

	vector<int> cnt[26][26];
	for(int i = 0; i < 26; ++i)
	for(int j = 0; j < 26; ++j){
		if(i != j){
			cnt[i][j].assign(n + 1, 0);
		}
	}
	vector<int> prefcnt(n + 1);

	for(cauto nd : trie){
		int val[26] = {};
		for(int i = 0; i < 26; ++i){
			if(nd.to[i] != -1){
				val[i] = trie[nd.to[i]].cnt;
			}
		}
		int edcnt = nd.cnt - accumulate(ALL(val), 0);
		if(edcnt){
			++prefcnt[nd.minidx + 1];
			--prefcnt[nd.maxidx + 1];
		}
		
		int acc = nd.minidx + edcnt;
		for(int i = 0; i < 26; ++i){
			int nacc = acc + val[i];
			for(int j = 0; j < 26; ++j){
				if(i != j){
					cnt[i][j][acc] += val[j];
					cnt[i][j][nacc] -= val[j];
				}
			}
			acc = nacc;
		}
	}
	
	for(int i = 0; i < 26; ++i)
	for(int j = 0; j < 26; ++j)
	if(i != j){
		partial_sum(ALL(cnt[i][j]), cnt[i][j].begin());
	}
	partial_sum(ALL(prefcnt), prefcnt.begin());

	int qcnt;
	for(scanf("%d", &qcnt); qcnt--; ){
		int k;
		scanf("%d", &k);
		int x = rtbl[k - 1];
		string perm = input();
		int ord[26] = {};
		for(int i = 0; i < 26; ++i){
			ord[perm[i] - 'a'] = i;
		}
		int ans = prefcnt[x] + 1;
		for(int i = 0; i < 26; ++i)
		for(int j = 0; j < 26; ++j){
			if(ord[i] > ord[j]){
				ans += cnt[i][j][x];
			}
		}
		printf("%d\n", ans);
	}
}



}
int main() try{
//	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	cerr << fixed << setprecision(4);
	mainmain();
}
catch(...){}


Measured memory_bytes: 275176