#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 表現
		int sub;
        node() : sub(1u){}
        node(string&& _s, node *v, int index, int _sub) : sub(_sub){
            s[index] = move(_s), to[index] = v;
        }
        template<typename String>
        void direct_push(String&& _s, uint32_t index){
            s[index] = forward<String>(_s), to[index] = new node();
        }
        template<typename String>
        void push(String&& _s){
            ++sub;
            const int num = _s[0] - 'a';
            if(to[num]){
                const int ls = s[num].size(), _ls = _s.size();
                for(int i = 0; i < min(ls, _ls); ++i){
                    if(s[num][i] == _s[i]) continue;
                    to[num] = new node(s[num].substr(i, ls - i), to[num],
                                        s[num][i] - 'a', to[num]->sub + 1);
                    s[num].erase(s[num].begin() + i, s[num].end());
                    to[num]->direct_push(_s.substr(i, _ls - i), _s[i] - 'a');
                    return;
                }
                if(ls > _ls){
                    to[num] = new node(s[num].substr(_ls, ls - _ls), to[num],
                                        s[num][_ls] - 'a', to[num]->sub);
                    s[num].erase(s[num].begin() + _ls, s[num].end());
                }else if(ls < _ls){
                    to[num]->push(_s.substr(ls, _ls - ls));
                }
            }else{
                direct_push(forward<String>(_s), num);
            }
        }
	};
	node* root;
	PatriciaTrie() : root(new node()){}
	//Trie 木に s を加える
	void add(const string& s){ root->push(s); }
    void add(string&& s){ root->push(move(s)); }
    //何らかのクエリ
	int recur_query(node *cur, unsigned int d, int *index, const string& s){
        if(!cur || d >= s.size()) return 0;
        const int next = s[d] - 'a';
        int res = recur_query(cur->to[next], d + cur->s[next].size(), index, s);
        for(int i = 0; i < 27; ++i){
            if(cur->to[i] && index[i] < index[next]) res += cur->to[i]->sub;
        }
        return res;
	}
    int query(int *index, const string& s){
        return recur_query(root, 0, 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