#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) {cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl;}

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>&m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}
template<typename S,typename T,typename U>
struct TRI{S fi;T se;U th;TRI(){}TRI(S f,T s,U t):fi(f),se(s),th(t){}
bool operator<(const TRI&_)const{return(fi==_.fi)?((se==_.se)?(th<_.th):(se<_.se)):(fi<_.fi);}};
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,TRI<S,T,U>&t){return o<<"{"<<t.fi<<","<<t.se<<","<<t.th<<"}";}

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef TRI<int, int, int> tri;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 100005;

// 使う文字は 31 文字以下の文字コード上で連続する文字の列とする.
template<unsigned int CHARACTER_SIZE, char START_CHARACTER>
class CompressedTrie {
private:
    struct node {
        string *s;
        node **to;
        // sub: 部分木に含まれる要素の数, adj: 子の bit 表現
        uint32_t sub, adj;
        node() : s(nullptr), to(nullptr),  sub(1u), adj(0u){}
        node(string&& _s, node *v, unsigned int index, uint32_t _sub)
         : s(new string[CHARACTER_SIZE]()), to(new node*[CHARACTER_SIZE]()),
            sub(_sub), adj(1u << index){
            s[index] = move(_s), to[index] = v;
        }
        #define lsb(v) (__builtin_ctz(v))
        inline unsigned int begin() const { return adj ? lsb(adj) : CHARACTER_SIZE; }
        inline unsigned int next(unsigned int cur) const {
            cur = adj & ~((1u << (cur + 1u)) - 1u);
            return cur ? lsb(cur) : CHARACTER_SIZE;
        }
        inline static unsigned int end(){ return CHARACTER_SIZE; }
        inline bool exist(const unsigned int v) const { return adj >> v & 1u; }
        void direct_push(string&& _s, unsigned int index){
            if(!s) s = new string[CHARACTER_SIZE](), to = new node*[CHARACTER_SIZE]();
            s[index] = move(_s), to[index] = new node(), ++sub, adj |= (1u << index);
        }
    };
    void make_node(string& orgs, unsigned int start, node*& to){
        string tmp = orgs.substr(0, start);
        orgs.erase(orgs.begin(), orgs.begin() + start);
        to = new node(move(orgs), to, orgs[0] - START_CHARACTER, to->sub);
        orgs = move(tmp);
    }
    void push(node *cur, const string& news){
        const unsigned int _ls = news.size();
        unsigned int index = 0u, prefix;
        while(true){
            const unsigned int num = news[index] - START_CHARACTER;
            if(cur->exist(num)){
                ++cur->sub;
                string& orgs = cur->s[num];
                const unsigned int ls = orgs.size();
                for(prefix = 0u; prefix < ls && index < _ls; ++prefix, ++index){
                    if(orgs[prefix] == news[index]) continue;
                    make_node(orgs, prefix, cur->to[num]);
                    string _s(news.substr(index, _ls - index));
                    cur->to[num]->direct_push(move(_s), news[index] - START_CHARACTER);
                    return;
                }
                if(index == _ls){
                    if(prefix == ls) return;
                    make_node(orgs, prefix, cur->to[num]);
                    return;
                }else{
                    cur = cur->to[num];
                }
            }else{
                string _s(news.substr(index, _ls - index));
                cur->direct_push(move(_s), num);
                return;
            }
        }
    }
    // void push(node *cur, string&& _s){
    //     string news = forward<String>(_s);
    //     while(true){
    //         const unsigned int num = news[0] - START_CHARACTER;
    //         if(cur->exist(num)){
    //             ++cur->sub;
    //             string& orgs = cur->s[num];
    //             const unsigned int ls = orgs.size(), _ls = news.size();
    //             for(unsigned int i = 0u; i < min(ls, _ls); ++i){
    //                 if(orgs[i] == news[i]) continue;
    //                 return make_node(orgs, move(news), i, _ls, cur->to[num]);
    //             }
    //             if(ls > _ls) return make_node(orgs, move(news), _ls, _ls, cur->to[num]);
    //             else if(ls < _ls) cur = cur->to[num], news.erase(news.begin(), news.begin() + ls);
    //             else return;
    //         }else{
    //             return cur->direct_push(move(news), num);
    //         }
    //     }
    // }
    // void clear_dfs(node *cur){
    //     if(!cur) return;
    //     for(unsigned int i = 0u; i < 27u; ++i) clear_dfs(cur->to[i]);
    //     delete cur;
    // }
public:
    node* root;
    CompressedTrie() : root(new node()){}
    // ~CompressedTrie(){ clear_dfs(root); }
    // CompressedTrie 木に s を加える
    void add(const string& s){ push(root, s); }
    void add(string&& s){ push(root, move(s)); }
    //何らかのクエリ
    int recur_query(node *cur, unsigned int d, int *index, const string& s){
        int res = 0;
        while(true){
            if(d >= s.size()) break;
            const unsigned int next = s[d] - START_CHARACTER;
            for(unsigned int i = cur->begin(); i != cur->end(); i = cur->next(i)){
                if(index[i] < index[next]) res += cur->to[i]->sub;
            }
            d += cur->s[next].size();
            cur = cur->to[next];
        }
        return res;
    }
    int query(int *index, const string& s){
        return recur_query(root, 0u, index, s);
    }
};

string s[MAX_N];
int id[27];
const char c = (char)('z' + 1);

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    CompressedTrie<27, 'a'> pt;
    rep(i, n){
        cin >> s[i];
        s[i].push_back(c);
        pt.add(s[i]);
    }
    int q;
    cin >> q;
    rep(i, q){
        int k;
        string t;
        cin >> k >> t;
        rep(j, 26) id[(int)(t[j] - 'a')] = j + 1;
        id[26] = 0;
        cout << pt.query(id, s[k-1]) + 1 << "\n";
    }
    return 0;
}


Measured memory_bytes: 27264