#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) {cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl;}

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}
bool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef TRI<int, int, int> tri;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 100005;

string s[MAX_N];
// 文字列 i 中の文字 j だけみたときの k の和
int sm[MAX_N][27][27];

const char c = (char)('a' - 1);

class Trie {
public:
	struct Node {
		Node *next[27];
		int sub;
		Node() : sub(0){
            rep(i,27){
                next[i] = nullptr;
            }
        }
	};
	Node* root;
	Trie(){
		root = new Node();
	}
	//Trie木にxを加える
	void add(string& s) {
		Node *curr = root;
        rep(i,(int)s.size()){
			int y = s[i] - 'a' + 1;
			if (!curr->next[y]) {
				curr->next[y] = new Node();
			}
			curr->sub++;
			curr = curr->next[y];
		}
		curr->sub++;
	}
    //何らかのクエリ
	void query(Node *curr, int d, const int id, const string& s){
		int res = (int)(s[d]-'a') + 1;
		rep(i,27){
			if(curr->next[i]) sm[id][res][i] += curr->next[i]->sub;
		}
        if(d == len(s) - 1) return;
        query(curr->next[(int)(s[d]-'a') + 1], d+1, id, s);
	}
};

int id[27];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    Trie t;
    rep(i,n){
        cin >> s[i];
        s[i] += c;
        t.add(s[i]);
    }
    rep(i,n){
        t.query(t.root, 0, i, s[i]);
    }
    int q;
    cin >> q;
    rep(i,q){
        int K;
        string t;
        cin >> K >> t;
        --K;
        t = c + t;
        int ans = 0;
        rep(j,27){
            id[(int)(t[j]-'a') + 1] = j;
        }
        rep(j,27){
            rep(k,27){
                if(id[k] < id[j]) ans += sm[K][j][k];
            }
        }
        cout << ans + 1 << "\n";
    }
    return 0;
}


Measured memory_bytes: 342272