#include <bits/stdc++.h>
using namespace std;
template <typename T> inline void read (T &x) { bool b = 0; char c; while (!isdigit (c = getchar()) && c != '-');
if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48; if (b) x = -x; }
template <typename T> inline void wrip(T x) { if (x > 9) wrip(x / 10); putchar(x%10 + 48); }
template <typename T> inline void write(T x) { if (x < 0) putchar('-'), x = -x; wrip(x);}
void reads(string &s) { char c; while (!isalpha(c = getchar())); s = c; while (isalpha(c = getchar())) s += c; }
typedef const int csint;

const int N = 4e5 + 11;
int n, Nnode, child[N][26], f[N], Len[N], Links[N];
int ID[N];
string s[100011];
inline void add(string s, int pos) {
    int node = 0;
    for (int i = 0; i < s.size(); i++) {
        if (!child[node][s[i] - 'a']) {
            child[node][s[i] - 'a'] = ++Nnode;
            Len[Nnode] = Len[node] + 1;
        }
        node = child[node][s[i] - 'a'];
    }
    ID[node] = pos;
    f[node]++;
}
inline int Solve(string str, int id) {
    int node = 0, res = 0;
    while(1) {
        if (Links[node] != node)
            node = Links[node];
        res += (ID[node] > 0);
        if (ID[node] == id)
            break;
        int d = s[id][Len[node]];
        for (int j = 0; j <= 25; j++) {
            if (str[j] == d)
                break;
            if (child[node][str[j] - 'a'])
                res += f[child[node][str[j] - 'a']];
        }
        node = child[node][d - 'a'];
    }
    return res;
}
int main() {
    #ifdef makacha
        freopen("m.inp", "r", stdin);
        freopen("m.out", "w", stdout);
    #endif // makacha
    read(n);
    for (int i = 1; i <= n; i++) {
        reads(s[i]);
        add(s[i], i);
    }
    for (int i = Nnode; i >= 0; i--)
        for (int j = 0; j <= 25; j++)
            if (child[i][j])
                f[i] += f[child[i][j]];
    for (int i = Nnode; i >= 0; i--) {
        Links[i] = i;
        for (int j = 0; j <= 25; j++)
            if (child[i][j] && f[i] == f[child[i][j]])
                Links[i] = Links[child[i][j]];
    }
    int nquery;
    read(nquery);
    while (nquery--) {
        int k;
        string str;
        read(k);
        reads(str);
        write(Solve(str, k));
        putchar('\n');
    }
}
