#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int M = 400400;
const int N = 100100;
const int A = 27;

struct Node {
  Node *to[A];
  int sz;
  int fin;

  Node() {
    fill(to, to + A, nullptr);
    sz = 0;
    fin = 0;
  }
} pool[M];
int last = 0;
Node *root;

int cnt[N][A][A], low[N];
int n;
string s[N];


Node *getNode() {
  return pool + last++;
}

void add(string s) {
  Node* cur = root;
  for (char c : s) {
    int x = c - 'a';
    cur->sz++;
    if (cur->to[x] == nullptr)
      cur->to[x] = getNode();
    cur = cur->to[x];
  }
  cur->sz++;
  cur->fin++;
}

void calc(string s, int id) {
  Node *cur = root;
  for (char c : s) {
    int x = c - 'a';

    for (int k = 0; k < A; ++k)
      if (cur->to[k]) 
        cnt[id][x][k] += cur->to[k]->sz;

    low[id] += cur->fin;
    cur = cur->to[x];
  }
}

void read() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> s[i];
}


void kill() {
  root = getNode();
  for (int i = 0; i < n; ++i)
    add(s[i]);
  for (int i = 0; i < n; ++i)
    calc(s[i], i);

  int q;
  cin >> q;
  while (q--) {
    int k;
    string ord;
    cin >> k >> ord;
    --k;
    int ans = low[k];
    for (int i = 0; i < (int) ord.length(); ++i)
      for (int j = 0; j < i; ++j) {
        int x = ord[i] - 'a';
        int y = ord[j] - 'a';
        ans += cnt[k][x][y];
      }
    cout << ans + 1 << "\n";
  }
}

int main() {
  cout.precision(20);
  cout << fixed;
  ios_base::sync_with_stdio(false);
  read();
  kill();
}
