#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <queue>
#include <ctime>
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <bitset>

using namespace std;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define epr(...) fprintf(stderr, __VA_ARGS__)
#define db(x) cerr << #x << " = " << x << endl
#define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; 
#define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")\n"
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)a.size()

#define equal equalll
#define less lesss
const int N = 1e6 + 10;
const long long INF = 1e9 + 19;
const int ALF = 26;
typedef long long ll;

struct Node {
    int next[ALF];
    map<int, vector<pair<int, int>>> onEdge;
    int sum;
    int id;
    int h;
    Node () {
        memset(next, -1, sizeof(next));
        sum = 0;
        id = -1;
        h = -1;
    }
};

Node data[N];
string s[N];
char perm[N];
int cur = 1;
int n;

void add(int id) {
    int len = s[id].size();
    int v = 0;
    data[v].sum++;
    data[v].h = 0;
    for (int i = 0; i < len; i++) {
        int ch = s[id][i] - 'a';
        if (data[v].next[ch] == -1) {
            data[v].next[ch] = cur++;
        }
        v = data[v].next[ch];
        data[v].sum++;
        data[v].h = i + 1;
    }
    data[v].id = id;
}

void zip(int v, int par, int dirCh, vector<pair<int, int>>* onEdge) {
    int cntCh = 0; 
    for (int i = 0; i < ALF; i++)
        cntCh += data[v].next[i] != -1;
    if (par != -1) {
        if (cntCh != 1) {
            data[par].next[dirCh] = v; 
            data[par].onEdge[dirCh] = *onEdge;
            delete onEdge;
            for (int i = 0; i < ALF; i++)
                if (data[v].next[i] != -1)
                    zip(data[v].next[i], v, i, new vector<pair<int, int>>());
        }
        else {
            if (data[v].id != -1)
                onEdge->pb(mp(data[v].h, data[v].id));
            for (int i = 0; i < ALF; i++) {
                if (data[v].next[i] != -1)
                    zip(data[v].next[i], par, dirCh, onEdge);
            }
        }
    }
    else {
        for (int i = 0; i < ALF; i++)
            if (data[v].next[i] != -1)
                zip(data[v].next[i], v, i, new vector<pair<int, int>>());
    }
}

int get(int v, int k, char* perm) {
    //db2(v, k);
    int sum = 0;
    if (data[v].id == k)
        return sum;
    if (data[v].id != -1)
        sum++;

    for (int i = 0; i < ALF; i++) {
        int ch = perm[i] - 'a';
        if (data[v].next[ch] != -1)  {
            
            int have = data[data[v].next[ch]].sum + data[v].onEdge[ch].size();
            //db3(sum, data[v].h, perm[i]);
            if (s[k][data[v].h] == perm[i]) {
                int nextH = data[data[v].next[ch]].h;
                if ((int)s[k].size() < nextH) {
                    int pos = lower_bound(all(data[v].onEdge[ch]), mp((int)s[k].size(), -1)) - data[v].onEdge[ch].begin();
                    sum += pos;
                    return sum;
                }
                sum += data[v].onEdge[ch].size();
                return sum + get(data[v].next[ch], k, perm);
            }
            else {
                sum += have;
            }
        
        }
    }
    assert(false);
}

void read() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        add(i);
    }
    
    zip(0, -1, -1, NULL);

    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        int k;
        scanf("%d", &k);
        k--;
        scanf("%s", perm);
        int answer = get(0, k, perm);
        printf("%d\n", answer + 1);
    }
}

void solve() {

}

void stress() {

}

int main(){
#ifdef MY_DEBUG
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    if (1) {
        int k = 1;
        for (int tt = 0; tt < k; tt++) {
            read();
            solve();
        }
    }
    else {
        stress();
    }

    return 0;
}



Measured memory_bytes: 216580