#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;
#define forn(a, e) for (i64 a = 0; a < (i64)(e); a++)
#define forr(a, s, e) for (i64 a = s; a < (i64)(e); a++)
#define fore(e, a) for (auto& e : a)

#ifdef LOCAL
#define logv(a) {cerr << #a << " = "; fore(e, a) {cerr << e << " ";} cerr << "\n";}
#define logvp(a) {cerr << #a << " = "; fore(e, a) {cerr << "(" << e.first << ", " << e.second << ") ";} cerr << "\n";}
#define logvv(a) {cerr << #a << " = \n"; fore(r, a) { fore(e, r) {cerr << e << " ";} cerr << "\n";} }
#define logvf(a, field) {cerr << #a"."#field << " = \n"; fore(e, a) { cerr << e.field << " ";} cerr << "\n"; }
#define logvff(a, f1, f2) {cerr << #a".{"#f1 << ", "#f2 << "} = \n"; fore(e, a) { cerr << "(" << e.f1 <<", " << e.f2 << ") ";} cerr << "\n"; }
#define logs(a) cerr << #a << " = " << (a) << "\n";
#define logss(a, b) cerr << #a << " = " << (a) << ", " << #b << " = " << (b) << "\n";
#define logp(a) cerr << #a << " = " << "(" << a.first << ", " << a.second << ")" << "\n";
#define cond(pred, stmt) if (pred) { stmt }
#else
#define logv(a)
#define logvp(a)
#define logvv(a)
#define logvf(a, field)
#define logvff(a, f1, f2)
#define logs(a)
#define logss(a, b)
#define logp(a)
#define cond(pred, stmt)
#endif

using iip = pair<int, int>;
using llp = pair<i64, i64>;
using ivec = vector<int>;
using llvec = vector<i64>;
using svec = vector<string>;
template<typename T> using vec = vector<T>;

template<typename T, typename Dim>
auto make_vec(T value, Dim dim) { return vector<T>(dim, value); }
template<typename T, typename Dim1, typename... Dim>
auto make_vec(T value, Dim1 dim1, Dim... dims) { return make_vec(make_vec(value, dims...), dim1); }

template<typename T>
bool uax(T& v, const T& newv) { if (v < newv) { v = newv; return true; } else return false; }
template<typename T>
bool uin(T& v, const T& newv) { if (v > newv) { v = newv; return true; } else return false; }

template<typename T>
istream& operator>>(istream& is, vector<T>& c) { for (auto& e : c) is >> e; return is; }
template<typename T, size_t N>
istream& operator>>(istream& is, array<T, N>& c) { for (auto& e : c) is >> e; return is; }

template<typename ...T>
istream& read(T&... args) { return (cin >> ... >> args); }

static mt19937 rande(123123);
template<typename T>
T rand_int(T from, T to) { uniform_int_distribution<T> distr(from, to); return distr(rande); }

// const i64 INF = 1e18;
const int INF = 2e9;
const i64 M = 998244353;
const int MXB = 20;
using bin = bitset<MXB + 1>;

struct AhoCorasick {
    static constexpr int ASIZE = 26;
    struct tnode {
        char c;
        bool used = false;
        vector<int> source;
        int p = -1;
        i64 cost = -1e18;
        array<int, ASIZE> anext;
        array<int, ASIZE> tnext;
		int sz = 0;

        tnode() {
            anext.fill(-1);
            tnext.fill(-1);
        }
    };

    vector<int> suf;

    vector<tnode> trie;
    vector<int> used;
    int scount = 0;
    int st_ = 0;

    template<typename Container>
    static AhoCorasick OnStrings(const Container& strs) {
        AhoCorasick ac;
        for (const auto& str : strs) {
            ac.add_string(str);
        }
        ac.suf.resize(ac.trie.size(), -1);
        ac.used.resize(strs.size());
        return ac;
    }

    AhoCorasick(int size = 0) {
        trie.reserve(size);
        trie.emplace_back();
    }

    void reset_state() { st_ = 0; }
    void set_state(int st) { st_ = st; }
    int nstates() const { return trie.size(); }

    void build_all() {
        for (int i = 0; i < trie.size(); i++) {
            set_state(i);
            for (char c = 0; c < ASIZE; c++) {
                move_char('a' + c);
            }
        }
    }

    void add_string(const string& s) {
        int v = 0;
        for (int i = 0; i < s.size(); i++) {
            char c = s[i] - 'a';
            if (trie[v].anext[c] == -1) {
                trie[v].anext[c] = trie.size();
                trie.emplace_back();
                trie.back().p = v;
                trie.back().c = c;
            }

            v = trie[v].anext[c];
        }

        trie[v].source.push_back(scount);
        scount++;
    }

    int slink(int p) {
        if (suf[p] != -1) {
            return suf[p];
        }

        int parent = trie[p].p;
        if (p == 0 || parent == 0) {
            suf[p] = 0;
        } else {
            suf[p] = move(slink(trie[p].p), trie[p].c);
        }

        return suf[p];
    }

    void finish(int st) {
        if (st == 0 || trie[st].used) {
            return;
        } else {
            for (int i = 0; i < trie[st].source.size(); i++) {
                used[trie[st].source[i]] = true;
            }
            trie[st].used = true;
            finish(slink(st));
        }
    }

    int move(int st, char c) {
        if (trie[st].tnext[c] != -1) {
            return trie[st].tnext[c];
        }

        int next_st;
        if (trie[st].anext[c] != -1) {
            next_st = trie[st].anext[c];
        } else if (st == 0) {
            next_st = 0;
        } else {
            next_st = move(slink(st), c);
        }
        trie[st].tnext[c] = next_st;
        return trie[st].tnext[c];
    }

    int move_char(char c) {
        st_ = move(st_, c - 'a');
        finish(st_);
        return st_;
    }
    int move_str(const string& s) {
        for (auto c : s) {
            move_char(c);
        }
        return st_;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    rande.seed(chrono::steady_clock::now().time_since_epoch().count());
	int n;
	while (read(n)) {
		svec ss(n);
		read(ss);

		auto trie = AhoCorasick::OnStrings(ss);

		function<void(int)> dfs = [&](int v) {
			trie.trie[v].sz = trie.trie[v].source.size();
			forn(i, 26) {
				int u = trie.trie[v].anext[i];
				if (u == -1) {
					continue;
				}
				dfs(u);
				trie.trie[v].sz += trie.trie[u].sz;
			}
		};
		dfs(0);

		logv(ss);
		auto cc = make_vec(0, n, 26, 26);
		forn(i, n) {
			auto& s = ss[i];
			int v = 0;
			fore(sym, s) {
				int c = sym - 'a';
				forn(j, 26) if (c != j) {
					int u = trie.trie[v].anext[j];
					if (u != -1) {
						cc[i][c][j] += trie.trie[u].sz;
					}
				}

				// logss(v, c);
				// logs(trie.trie[v].source.size());
				// logv(trie.trie[v].anext);
				cc[i][c][c] += trie.trie[v].source.size();
				v = trie.trie[v].anext[c];
			}
		}

		int q;
		read(q);
		while (q--) {
			int k;
			string p;
			read(k, p);
			int ans = 1;
			forn(i, 26) forn(j, i + 1) {
				ans += cc[k - 1][p[i] - 'a'][p[j] - 'a'];
			}
			cout << ans << endl;
		}
	}
}

Measured memory_bytes: 389844