#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;

class PatriciaTrie {
public:
	struct node {
        string s[27];
        node *to[27] = {};
        // sub: 部分木に含まれる要素の数
        // adj: 子の bit 表現
		uint32_t sub, adj;
        node() : sub(1u), adj(0u){}
        node(string&& _s, node *v, unsigned int index, uint32_t _sub) : sub(_sub), adj(1u << index){
            s[index] = move(_s), to[index] = v;
        }
        inline static unsigned int lsb(const uint32_t v){ return v ? __builtin_ctz(v) : 27u; }
        inline unsigned int begin() const { return lsb(adj); }
        inline unsigned int next(const unsigned int cur) const { return lsb(adj & ~((1 << (cur + 1)) - 1)); }
        inline static unsigned int end(){ return 27u; }
        inline bool exist(const unsigned int v) const { return adj >> v & 1u; }
        void direct_push(string&& _s, unsigned int index){
            s[index] = move(_s), to[index] = new node(), ++sub, adj |= (1u << index);
        }
	};
    void make_node(string& ps, const string& ns, unsigned int st, unsigned int pend, unsigned int nend, node*& to){
        to = new node(ps.substr(st, pend - st), to, ps[st] - 'a', to->sub);
        ps.erase(ps.begin() + st, ps.end());
        if(st == nend) return;
        to->direct_push(ns.substr(st, nend - st), ns[st] - 'a');
    }
    template<typename String>
    void push(node *cur, String&& _s){
        string ns = forward<String>(_s);
        while(true){
            const unsigned int num = ns[0] - 'a';
            if(cur->exist(num)){
                ++cur->sub;
                string& os = cur->s[num];
                const unsigned int ls = os.size(), _ls = ns.size();
                for(unsigned int i = 0; i < min(ls, _ls); ++i){
                    if(os[i] == ns[i]) continue;
                    make_node(os, ns, i, ls, _ls, cur->to[num]);
                    return;
                }
                if(ls > _ls) return make_node(os, ns, _ls, ls, _ls, cur->to[num]);
                else if(ls < _ls) cur = cur->to[num], ns.erase(ns.begin(), ns.begin() + ls);
                else return;
            }else{
                return cur->direct_push(move(ns), num);
            }
        }
    }
    // void clear_dfs(node *cur){
    //     if(!cur) return;
    //     for(unsigned int i = 0u; i < 27u; ++i) clear_dfs(cur->to[i]);
    //     delete cur;
    // }
	node* root;
	PatriciaTrie() : root(new node()){}
    // ~PatriciaTrie(){ clear_dfs(root); }
	// Trie 木に 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(!cur || d >= s.size()) break;
            const unsigned int next = s[d] - 'a';
            for(uint32_t 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;
    PatriciaTrie 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: 66688