#include <bits/stdc++.h>

using namespace std;

const int maxn = 100007;

struct data
{
    int next[26], cnt, eow;
};

int prefix[maxn], sum[maxn][26][26];
string D, str[maxn];
vector <data> V;

data newNode()
{
    data emp;
    for (int i = 0; i < 26; ++i) emp.next[i] = -1;
    emp.eow = emp.cnt = 0;
    return emp;
}

void add(string x)
{
    int r = 0;
    for (char s: x)
    {
        int f = s - 'a';
        if (V[r].next[f] < 0)
        {
            V[r].next[f] = (int)V.size();
            V.push_back(newNode());
        }
        r = V[r].next[f];
        ++V[r].cnt;
    }
    ++V[r].eow;
}

void query(int k)
{
    int ans = prefix[k];
    for (char i: D)
        for (char j: D)
        {
            if (j == i) break;
            ans += sum[k][i - 'a'][j - 'a'];
        }

    cout << ans << '\n';
}

int main()
{
//    freopen("TEST.INP", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    V.push_back(newNode());
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> str[i], add(str[i]);

    for (int i = 1; i <= n; ++i)
    {
        int r = 0;
        for (char j: str[i])
        {
            for (int k = 0; k < 26; ++k)
                if (j - 'a' != k && V[r].next[k]!= -1)
                    sum[i][j - 'a'][k] += V[V[r].next[k]].cnt;

            r = V[r].next[j - 'a'];
            prefix[i] += V[r].eow;
        }
    }

    int q;
    cin >> q;
    while (q--)
    {
        int k;
        cin >> k >> D;
        query(k);
    }

    return 0;
}
