#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <numeric>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
using namespace std;

#ifdef DEBUG
    #define debug(...) printf(__VA_ARGS__)
    #define GetTime() fprintf(stderr,"Running time: %.3lf second\n",((double)clock())/CLOCKS_PER_SEC)
#else
    #define debug(...) 
    #define GetTime() 
#endif

//type definitions
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vint;

//abbreviations
#define A first
#define B second
#define MP make_pair
#define PB push_back

//macros
#define REP(i,n) for (int i = 0; i < (n); ++i)
#define REPD(i,n) for (int i = (n)-1; 0 <= i; --i)
#define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for (int i = (a); (b) <= i; --i)
#define FORIT(it,c) for (__typeof ((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define ALL(a) (a).begin(),(a).end()
#define SZ(a) ((int)(a).size())
#define RESET(a,x) memset(a,x,sizeof(a))
#define EXIST(a,s) ((s).find(a) != (s).end())
#define MX(a,b) a = max((a),(b));
#define MN(a,b) a = min((a),(b));

inline void OPEN(const string &s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

/* -------------- end of azaky's template -------------- */

#define MAXN 100100
#define MAXX 500500

int n, nq, k;
char buffer[MAXX];
int child[MAXX][30], cc[MAXX], huba[30][30];
vector<int> idx[MAXX];
vector<pair<string, int> > q[MAXN];
int ans[MAXN];

int dfs(int node) {
    if (node == -1) return 0;
    cc[node] = idx[node].size();
    REP(i, 26) {
        cc[node] += dfs(child[node][i]);
    }
    return cc[node];
}

void dfs2(int node) {
    if (node == -1) return;
    FORIT(it, idx[node]) {
        FORIT(itq, q[*it]) {
            int iq = itq->B;
            ans[iq] = 1;
            for (int i = 0; i < 26; ++i) {
                for (int j = 0; j <= i; ++j) {
                    ans[iq] += huba[itq->A[i] - 'a'][itq->A[j] - 'a'];
                }
            }
        }
    }
    REP(i, 26) {
        REP(j, 26) {
            if (i == j) {
                huba[i][j] += idx[node].size();
            } else {
                if (child[node][j] != -1) {
                    huba[i][j] += cc[child[node][j]];
                }
            }
        }
        dfs2(child[node][i]);
        REP(j, 26) {
            if (i == j) {
                huba[i][j] -= idx[node].size();
            } else {
                if (child[node][j] != -1) {
                    huba[i][j] -= cc[child[node][j]];
                }
            }
        }
    }
}

int main(){
    // build trie
    int root = 0, nnode = 1;
    RESET(child, -1);

    scanf("%d", &n);
    REP(i, n) {
        scanf("%s", buffer);
        string s = buffer;
        int node = root;
        for (int j = 0; j < s.length(); ++j) {
            if (child[node][s[j] - 'a'] == -1) {
                child[node][s[j] - 'a'] = nnode++;
            }
            node = child[node][s[j] - 'a'];
        }
        idx[node].PB(i);
    }


    scanf("%d", &nq);
    REP(i, nq) {
        scanf("%d %s", &k, buffer);
        q[k - 1].PB(MP(buffer, i));
    }
    // sort
    dfs(0);
    dfs2(0);

    REP(i, nq) {
        printf("%d\n", ans[i]);
    }

    return 0;
}


Measured memory_bytes: 120816