#include <stdio.h>  
#include <algorithm>  
#include <assert.h>
#include <bitset>
#include <cmath>  
#include <complex>  
#include <deque>  
#include <functional>  
#include <iostream>  
#include <limits.h>  
#include <map>  
#include <math.h>  
#include <queue>  
#include <deque>
#include <set>  
#include <stdlib.h>  
#include <string.h>  
#include <string>  
#include <time.h>  
#include <unordered_map>  
#include <unordered_set>  
#include <vector>  
#include <chrono>
#include <random>
#include <time.h>
#include <fstream>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define endl "\n"
#define all(c) begin(c),end(c)
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}
void print(vec v){for(auto e:v)cout<<e<<" ";cout<<endl;}
void print(vecll v){for(auto e:v)cout<<e<<" ";cout<<endl;}
void print(map<int,int> mp){for(auto e:mp)cout<<e.first<<" "<<e.second<<endl;cout<<endl;}


#define INF 1e9+7
#define LLINF 1e18+7
const ll MOD=1e9+7;
const int N=1000000;

template< int char_size >
struct TrieNode {
  int nxt[char_size];
  int cnt[char_size];

  int exist;
  vector< int > accept;

  TrieNode() : exist(0) {
    memset(nxt, -1, sizeof(nxt));
    memset(cnt,0,sizeof(cnt));
  }
};

int defo[110000];

template< int char_size, int margin >
struct Trie {
  using Node = TrieNode< char_size >;

  vector< Node > nodes;
  int root;

  Trie() : root(0) {
    nodes.push_back(Node());
  }

  void update_direct(int node, int id) {
    nodes[node].accept.push_back(id);
  }

  void update_child(int node, int child, int id) {
    ++nodes[node].exist;
  }

  void add(const string &str, int str_index, int node_index, int id) {
    if(str_index == str.size()) {
      update_direct(node_index, id);
    } else {
      const int c = str[str_index] - margin;
      nodes[node_index].cnt[c]++;
      if(nodes[node_index].nxt[c] == -1) {
        nodes[node_index].nxt[c] = (int) nodes.size();
        nodes.push_back(Node());
      }
      add(str, str_index + 1, nodes[node_index].nxt[c], id);
      update_child(node_index, nodes[node_index].nxt[c], id);
    }
  }

  void add(const string &str, int id) {
    add(str, 0, 0, id);
  }

  void add(const string &str) {
    add(str, nodes[0].exist);
  }

  void query(const string &str,vector<vec> &dif,int str_index, int node_index) {
    for(auto &idx : nodes[node_index].accept)dif[0][0]++;
    if(str_index == str.size()) {
      return;
    } else {
      const int c = str[str_index] - margin;
      rep(i,26) if(i!=c)dif[c][i]+=nodes[node_index].cnt[i];
      if(nodes[node_index].nxt[c] == -1) return;
      query(str, dif, str_index + 1, nodes[node_index].nxt[c]);
    }
  }

  void query(const string &str,vector<vec> &dif) {
    query(str,dif,0, 0);
  }

  int count() const {
    return (nodes[0].exist);
  }

  int size() const {
    return ((int) nodes.size());
  }
};


main(){
    int n=in();
    vector<string> v;
    Trie<26,'a'> trie;
    rep(i,n){
        string s;cin>>s;
        v.pb(s);
        trie.add(s);
    }
    vector<vector<vec>> dif(n,vector<vec>(26,vec(26)));
    rep(i,n){
        trie.query(v[i],dif[i]);
    }
    int q=in();
    while(q--){
        int t=in();
        string s; cin>>s;
        int inv[26];
        rep(i,26){
            inv[s[i]-'a']=i;
        }
        int ans=0;
        rep(i,26){
            rep(j,26){
                if(inv[i]<=inv[j]){
                    ans+=dif[t-1][j][i];
                }
            }
        }
        cout<<ans<<endl;
    }
}

Measured memory_bytes: 387316