#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1.0);

void precalc() {
}

const int maxn = (int) 1e5 + 10;

vector<pair<string, int> > ss;

int n, q;

int read() {
  if (scanf("%d", &n) < 1) {
    return 0;
  }
  ss.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> ss[i].first;
    ss[i].second = i;
  }
  scanf("%d", &q);
  return 1;
}

int where[maxn];

vector<pair<string, int> > qs[maxn];

const int maxc = 27;
int cnts[(int) 4e5 + 10][maxc];

int ans[maxn];

vector<int> sums[maxc][maxn];

void solve() {
  sort(ss.begin(), ss.end());
  for (int i = 0; i < n; ++i) {
    where[ss[i].second] = i;
    qs[i].clear();
  }

  for (int i = 0; i < q; ++i) {
    int k;
    string perm;
    scanf("%d", &k);
    --k;
    cin >> perm;

    qs[where[k]].pb(mp(perm, i));
    ans[i] = 1;
  }

  for (int iter = 0; iter < 2; ++iter) {
    memset(cnts[0], 0, sizeof(cnts[0]));

    for (int c = 0; c < maxc; ++c) {
      for (int i = 0; i < n; ++i) {
        string &cur = ss[i].first;
        sums[c][i].resize(sz(cur) + 1);
        for (int j = 0; j <= sz(cur); ++j) {
          sums[c][i][j] = 0;
          if (i) {
            string &prev = ss[i - 1].first;
            if (j <= sz(prev)) {
              int pc = j == sz(prev) ? maxc - 1 : (prev[j] - 'a');
              sums[c][i][j] += sums[c][i - 1][j] + (pc == c);
            }
          }
        }
      }
    }

    eprintf("\nstart %s\n", ss[0].first.c_str());
    int last = 0;
    for (int i = 1; i < n; ++i) {
      string &cur = ss[i].first;
      string &prev = ss[i - 1].first;
      eprintf("i = %d, %s\n", i, cur.c_str());
      int curl = 0;
      for (; curl < sz(cur) && curl < sz(prev) && cur[curl] == prev[curl]; ++curl) ;

      int popped = 0;
      for (; last > curl; --last) {
        popped += accumulate(cnts[last], cnts[last] + maxc, 0);
      }

      if (popped) {
        for (int c = 0; c < maxc; ++c) {
          int toadd = sums[c][i][curl] - sums[c][i - 1 - popped][curl];
          cnts[curl][c] += toadd;
          if (toadd) {
            eprintf("add %dx%d to %d\n", toadd, c, curl);
          }
        }
      } else {
        while (last < curl) {
          ++last;
          memset(cnts[last], 0, sizeof(cnts[last]));
        }

        int c = curl < sz(prev) ? prev[curl] - 'a' : (maxc - 1);
        eprintf("add %d to %d\n", c, curl);
        cnts[curl][c] += 1;
      }

      int cnt[maxc][maxc];
      memset(cnt, 0, sizeof(cnt));
      for (int j = 0; j <= curl; ++j) {
        for (int c = 0; c < maxc; ++c) {
          cnt[j == sz(cur) ? (maxc - 1) : cur[j] - 'a'][c] += cnts[j][c];
        }
      }

      for (auto &q : qs[!iter ? i : (n - 1 - i)]) {
        for (int i = 0; i < maxc - 1; ++i) {
          for (int j = 0; j < i; ++j) {
            ans[q.second] += cnt[q.first[i] - 'a'][q.first[j] - 'a'];
          }
          ans[q.second] += cnt[q.first[i] - 'a'][maxc - 1];
        }
      }
    }

    reverse(ss.begin(), ss.end());
  }

  for (int i = 0; i < q; ++i) {
    printf("%d\n", ans[i]);
  }
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 189440