#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <cassert>
using namespace std;

#define GET_ARG(a,b,c,F,...) F
#define REP3(i,s,e) for (i = s; i <= e; i++)
#define REP2(i,n) REP3 (i,0,(int)(n)-1)
#define REP(...) GET_ARG (__VA_ARGS__,REP3,REP2) (__VA_ARGS__)
#define RREP3(i,s,e) for (i = s; i >= e; i--)
#define RREP2(i,n) RREP3 (i,(int)(n)-1,0)
#define RREP(...) GET_ARG (__VA_ARGS__,RREP3,RREP2) (__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

struct Patricia {
    int num;
    bool end;
    string str;
    vector<Patricia*> children;
    Patricia() {}
    Patricia(string s) : str(s), num(1), end(true) {}
};

Patricia root;

void add(string s) {
    Patricia* node = &root;
    bool down = true;
    while (down && s.size() > 0) {
        down = false;
        for (auto ch: node->children) {
            if (ch->str[0] == s[0]) {
                int i;
                for (i = 0; i < min(ch->str.size(),s.size()) && ch->str[i] == s[i]; i++);
                if (i < ch->str.size()) {
                    Patricia* nnode = new Patricia(ch->str.substr(i));
                    nnode->num = ch->num;
                    nnode->end = ch->end;
                    nnode->children = ch->children;
                    ch->children.clear();
                    ch->children.push_back(nnode);
                    ch->str.erase(i);
                    ch->end = false;
                }
                if (i == s.size()) {
                    ch->end = true;
                }
                s.erase(0,i);
                node = ch;
                down = true;
                ch->num++;
                break;
            }
        }
        if (!down) node->children.push_back(new Patricia(s));
    }
}

int pri[26];

int query(string s) {
    Patricia* node = &root;
    int ret = 0;
    while (s.size()) {
        Patricia* nxt;
        for (auto ch: node->children) {
            if (ch->str[0] == s[0]) {
                ret += ch->end;
                nxt = ch;
            }
            else if (pri[ch->str[0]-'a'] < pri[s[0]-'a']) {
                ret += ch->num;
            }
        }
        s.erase(0,nxt->str.size());
        node = nxt;
    }
    return ret;
}

void print_tree() {
    queue<pair<Patricia*,int>> q;
    q.push({&root,0});
    int now = 0;
    while (!q.empty()) {
        auto p = q.front(); q.pop();
        auto node = p.first;
        int d = p.second;
        if (now < d) {
            cerr << endl;
            now = d;
        }
        cerr << "(" << node->str << "," << node->num << "," << node->end << ") ";
        for (auto ch: node->children) {
            q.push({ch,d+1});
        }
    }
    cerr << endl;
}

string str[100000];

int main(void) {
    int i, n;
    scanf("%d\n",&n);
    REP (i,n) {
        char c;
        while (scanf("%c",&c), c != '\n') str[i] += c;
        add(str[i]);
    }
    //print_tree();
    int q;
    scanf("%d",&q);
    while (q--) {
        int k;
        scanf("%d ",&k);
        k--;
        char c;
        REP (i,26) {
            scanf("%c",&c);
            pri[c-'a'] = i;
        }
        printf("%d\n",query(str[k]));
    }
    return 0;
}


Measured memory_bytes: 19072