/**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

vector<int> cnt[27][27];
vector<int> precnt;

// from https://ei1333.github.io/luzhiled/snippets/structure/trie.html

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

  int exist;
  vector< int > accept;

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

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);
    ++nodes[node].exist;
  }

  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;
      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, int str_index, int node_index, int idx) {
    if(str_index == str.size()) {
      return;
    } else {
      precnt[idx] += (int)nodes[node_index].accept.size();
      const int c = str[str_index] - margin;
      rep(d, 26) {
          if(c == d) continue;
          if(nodes[node_index].nxt[d] == -1) continue;
          cnt[d][c][idx] += nodes[nodes[node_index].nxt[d]].exist;
      }
      if(nodes[node_index].nxt[c] == -1) return;
      query(str, str_index + 1, nodes[node_index].nxt[c], idx);
    }
  }

  void query(const string &str, int idx) {
    query(str, 0, 0, idx);
  }

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

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

void solve() {
	int n, q;
    cin >> n;
    Trie< 26, 'a' > trie;
    vector<string> s(n);
    rep(i, n) {
        cin >> s[i];
        trie.add(s[i]);
    }
    rep(i, 27) {
        rep(j, 27) {
            cnt[i][j].assign(n, 0);
        }
    }
    precnt.assign(n, 0);
    rep(i, n) {
        trie.query(s[i], i);
    }
    cin >> q;
    rep(i, q) {
        int k;
        cin >> k;
        -- k;
        vector<int> pos(26, -1);
        rep(j, 26) {
            char c; cin >> c;
            pos[(c - 'a')] = j;
        }
        int ans = precnt[k];
        //cerr << ans + 1 << " ";
        rep(a, 26) {
            rep(b, 26) {
                if(pos[a] < pos[b]) {
                    ans += cnt[a][b][k];
                }
            }
        }
        cout << ans + 1 << endl;
    }
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}

Measured memory_bytes: 311532