#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

vector<string> s;
vector<vector<vector<int> > > cnt;

void solve(const vector<int>& index, int pos)
{
    if(index.size() == 1)
        return;

    vector<vector<int> > nextChar(27);
    for(int i : index)
        nextChar[s[i][pos]-'a'+1].push_back(i);

    bool isSame = false;
    for(int i=0; i<27; ++i){
        if(nextChar[i].size() == index.size())
            isSame = true;
    }
    if(isSame){
        solve(index, pos+1);
        return;
    }

    for(int i=0; i<27; ++i){
        if(nextChar[i].empty())
            continue;

        for(int k : nextChar[i]){
            for(int j=0; j<27; ++j){
                cnt[k][j][i] += nextChar[j].size();
            }
        }
        solve(nextChar[i], pos+1);
    }
}

int main()
{
    int n;
    cin >> n;
    s.resize(n);
    for(int i=0; i<n; ++i){
        cin >> s[i];
        s[i] += (char)('a' - 1);
    }

    vector<int> index(n);
    for(int i=0; i<n; ++i)
        index[i] = i;
    cnt.assign(n, vector<vector<int> >(27, vector<int>(27, 0)));
    solve(index, 0);

    int q;
    cin >> q;
    while(--q >= 0){
        int k;
        string p;
        cin >> k >> p;
        -- k;
        p = (char)('a' - 1) + p;

        int ans = 1;
        for(int i=0; i<27; ++i){
            for(int j=i+1; j<27; ++j){
                ans += cnt[k][p[i]-'a'+1][p[j]-'a'+1];
            }
        }
        cout << ans << endl;
    }

    return 0;
}