#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)


class Trie {
public:
    struct Node {
        Node* next[26];
        int sz[26];
        int sub;
        int kind;
        int depth;
        bool flag;
        vector<int> s;
        Node() : sub(0),kind(0),depth(0),flag(false){
            s.resize(0);
            for(int i = 0; i < 26; i++){
                next[i] = nullptr;
                sz[i] = 0;
            }
        }
    };
    Node* root;
    Trie(){
        root = new Node();
    }
    //Trie木にxを加える
    void add(string& s) {
        Node *curr = root;
        for(int i = 0; i < (int)s.size(); i++){
            int y = s[i] - 'a';
            if (!curr->next[y]) {
                curr->next[y] = new Node();
                curr->kind++;
                curr->next[y]->depth = curr->depth+1;
                curr->s.push_back(y);
            }
            curr->sub++;
            curr = curr->next[y];
        }
        curr->flag = true;
        curr->sub++;
    }
    Node* compress(Node* curr){
        if(curr->kind==1){
            curr->next[curr->s[0]] = compress(curr->next[curr->s[0]]);
            if(curr->flag){
                return curr;
            }else{
                return curr->next[curr->s[0]];
            }
        }
        if(curr->kind==0){
            return curr;
        }
        for(auto x:curr->s){
            curr->next[x] = compress(curr->next[x]);
        }
        return curr;
    }
    int precalc(Node* curr){
        if(curr->kind==0){
            return 1;
        }
        int sm = 0;
        for(auto x:curr->s){
            curr->sz[x] = precalc(curr->next[x]);
            sm += curr->sz[x];
        }
        if(curr->flag)sm++;
        return sm;
    }
    //何らかのクエリ
    int query(Node *curr,int d) {
        int res = 0;
        for(int i = 0; i < 26; i++){
            if(curr->next[i]){
                res += query(curr->next[i],d+1);
            }
        }
        return res;
    }
    int query() {
        return query(root, 0);
    }
    int query(vector<int> &a,string &s){
        int id = 0;
        int c;
        int sm = 1;
        Node* curr = root;
        while(1){
            //cerr << id << endl;
            if(id==s.size()){
                return sm;
            }else{
                c = s[id]-'a';
                for(auto x:curr->s){
                    if(a[x]<a[c]){
                        sm += curr->sz[x];
                    }
                }
                if(curr->flag)sm++;
                curr = curr->next[c];
                id = curr->depth;
            }
        }
    }
};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<string> s(n);
    rep(i,n){
        cin >> s[i];
    }
    Trie trie;
    rep(i,n){
        trie.add(s[i]);
    }
    trie.compress(trie.root);
    trie.precalc(trie.root);
    int q;
    cin >> q;
    rep(zz,q){
        int k;
        string t;
        cin >> k >> t;
        k--;
        vector<int> a(26);
        rep(i,26){
            a[t[i]-'a'] = i;
        }
        cout << trie.query(a,s[k]) << "\n";
    }
    return 0;
}

Measured memory_bytes: 175864