#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> void Max(T &m, const T q) { if (m < q) m = q; }
template<typename T> void Min(T &m, const T q) { if (m > q) m = q; }
template<typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

constexpr int Smax = 400001;
constexpr int Nmax = 100001;
int to[Smax][26];
int subtreesz[Smax], here[Smax], n_child[Smax];

int dfs(int now)
{
    subtreesz[now] = here[now];
    for (auto nxt : to[now]) if (nxt) subtreesz[now] += dfs(nxt);
    return subtreesz[now];
}

int N, Q;
vector<string> Ss;

int main()
{
    cin >> N;

    memset(to, 0, sizeof(to));
    memset(subtreesz, 0, sizeof(subtreesz));
    memset(here, 0, sizeof(here));
    memset(n_child, 0, sizeof(n_child));
    int n_st = 1;

    REP(_, N)
    {
        string S;
        cin >> S;
        Ss.push_back(S);

        int now = 0;
        for (auto c : S)
        {
            if (to[now][c - 'a'] == 0)
            {
                to[now][c - 'a'] = n_st++;
                n_child[now]++;
            }
            now = to[now][c - 'a'];
        }
        here[now] = 1;
    }

    dfs(0);

    cin >> Q;
    vector<string> query[Nmax];
    vector<int> q_jun[Nmax];
    REP(i, Q)
    {
        int k;
        string p;
        cin >> k >> p;
        query[k - 1].push_back(p);
        q_jun[k - 1].push_back(i);
    }

    vector<int> res(Q);
    REP(i, N) if (query[i].size())
    {
        vector<int> ans(query[i].size());
        int st_now = 0, here_n = 0;
        REP(d, Ss[i].length())
        {
            if (n_child[st_now] > 1) REP(j, query[i].size())
            {
                for (auto c : query[i][j])
                {
                    if (c == Ss[i][d]) break;
                    int chil = to[st_now][c - 'a'];
                    if (chil > 0) ans[j] += subtreesz[chil];
                }
            }
            here_n += here[st_now];
            st_now = to[st_now][Ss[i][d] - 'a'];
        }
        REP(j, q_jun[i].size()) res[q_jun[i][j]] = ans[j] + here_n + 1;
    }
    for (auto v : res) printf("%d\n", v);
}


Measured memory_bytes: 71568