#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#include <utility>
#include <set>
#include <stack>
#include <deque>
#include <ctime>
#include <random>
#include <cassert>
#include <cmath>
#include <climits>
#include <queue>
#include <cstring>
#include <bitset>
#include <iomanip>
#include <chrono>


/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
#pragma GCC target("avx2")
*/


#ifdef LOCAL
#define dbg(x) cout << #x << " : " << x << endl;
#else 
#define dbg(x)
#endif

#define pb push_back
#define ppb pop_back()
#define mp make_pair
#define fi(a,b) for (int i=a;i<b;i++)
#define fj(a,b) for (int j=a;j<b;j++)
#define fk(a,b) for (int k=a;k<b;k++)
#define fi1(a,b) for (int i=a-1;i>=b;i--)
#define fj1(a,b) for (int j=a-1;j>=b;j--)
#define fk1(a,b) for (int k=a-1;k>=b;k--)
#define fx(x,a) for (auto& x : a)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rep1(i, a, b) for (int i = a - 1; i >= b; --i)
#define siz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

template<typename T1, typename T2>inline void mine(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void maxe(T1 &x, T2 y) { if (x < y) x = y; }

ostream& operator << (ostream &a, const vector<int> &b) {
    for (auto k : b) cout << k << ' ';
    return a;
}


typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<ch> vch;
typedef vector<vch> vvch;
typedef vector<str> vs;



const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 401000;
const double EPS = 1e-9;
const int K = 26;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

vi reqs[MX];
str req[MX];
int ans[MX];
int pos[MX];
str ss[MX];

struct nd {
    int to[K];
    int cnt;
    bool term;
};

int sz = 0;
nd trie[MX];
void create() {
    trie[sz].cnt = 0;
    trie[sz].term = false;
    memset(trie[sz].to, -1, sizeof(int) * K);
    ++sz;
}

int get(int x) {
    if (x == -1)
        return 0;
    return trie[x].cnt;
}

void add(int i) {
    int v = 0;
    fx(x, ss[i]) {
        x -= 'a';
        ++trie[v].cnt;
        if (trie[v].to[x] == -1) {
            trie[v].to[x] = sz;
            create();
        }
        v = trie[v].to[x];
    }
    ++trie[v].cnt;
    trie[v].term = 1;
    pos[i] = v;
}

int cnt[K][K];
void dfs(int v, int dp = 0) {
    dp += trie[v].term;
    dbg(v);
    dbg(dp);
    fx(r, reqs[v]) {
        dbg(r);
        assert(trie[v].term);
        str s = req[r];
        ans[r] = dp;
        vi pos(K);
        fi(0, K)
        	pos[s[i]] = i;
        dbg(cnt[0][1]);
        dbg(pos);
        fi(0, K) {
        	fj(0, K) {
        		if (pos[i] > pos[j])
        			ans[r] += cnt[i][j];
        	}
        }
    }
    fi(0, K) {
        if (trie[v].to[i] == -1)
            continue;
        fj(0, K) {
            if (j == i)
                continue;
            cnt[i][j] += get(trie[v].to[j]);
        }
        dfs(trie[v].to[i], dp);
        fj(0, K) {
            if (j == i)
                continue;
            cnt[i][j] -= get(trie[v].to[j]);
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n;
    cin >> n;
    create();
    fi(0, n) {
        cin >> ss[i];
        add(i);
    }
    int q;
    cin >> q;
    fi(0, q) {
        int k;
        cin >> k >> req[i];
        fx(x, req[i])
            x -= 'a';
        reqs[pos[k - 1]].pb(i);
    }
    dfs(0);
    fi(0, q)
        cout << ans[i] << '\n';
}

Measured memory_bytes: 149632