#include <bits/stdc++.h>
using namespace std;
const int maxn = 1505, inf = 2000;
int t[26][maxn];
char s[maxn];
int n, q;
void Calt() {
  vector<int> a;
  for (int c = 0; c < 26; c++) {
    a.clear();
    t[c][n] = inf;
    for (int i = 0; i < n; i++) {
      t[c][i] = inf;
      if (s[i] == c + 'a') a.push_back(i);
    }
    if (a.size() == 0) {
      for (int i = 1; i <= n; i++) t[c][i] = i;
      continue;
    }
    t[c][1] = 0;
    for (int step = 1; step < a.size(); step++) {
      for (int i = 0; i < a.size() - step; i++) {
        t[c][a[i + step] - a[i] + 1] =
            min(a[i + step] - a[i] - step, t[c][a[i + step] - a[i] + 1]);
      }
    }
    for (int i = 1; i <= n; i++) {
      t[c][i] = min(t[c][i], t[c][i - 1] + 1);
    }
  }
}
int dich(char c, int m) {
  int l = 1, r = n, mid = (l + r) / 2;
  if (t[c - 'a'][r] <= m) return n;
  if (t[c - 'a'][l] >= m) return 1;
  while (l < r && mid != l) {
    if (t[c - 'a'][mid] > m) {
      r = mid;
    } else {
      l = mid;
    }
    mid = (l + r) / 2;
  }
  return mid;
}
int main() {
  cin >> n >> s >> q;
  Calt();
  char c;
  int m;
  for (int i = 0; i < q; i++) {
    scanf("%d %c", &m, &c);
    printf("%d\n", dich(c, m));
  }
  return 0;
}
