#include <bits/stdc++.h>
using namespace std;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int dx2[] = {+1, -1, 0, 0, +1, +1, -1, -1};
int dy2[] = {0, 0, +1, -1, +1, -1, -1, +1};
inline long long squ(long long x) { return (x * x); }
inline long long power(long long bs, long long k) {
  long long x = 1LL, y = bs;
  if (k == 0) return 1LL;
  while (k > 0) {
    if (k % 2) x *= y;
    y *= y;
    k /= 2;
  }
  return x;
}
int dp[30][1010 * 2], a[1510], n, sm[26][1010 * 2];
char s[1010 * 2];
bool func(int x, int mx, int id) {
  int i, j, cnt = 0;
  for (i = 0; i <= n - mx; i++) {
    cnt = sm[id][i + mx - 1];
    if (i > 0) cnt -= sm[id][i - 1];
    if (cnt <= x) return true;
  }
  return false;
}
int main() {
  cout.unsetf(ios::floatfield);
  cout.precision(6);
  cout.setf(ios::fixed, ios::floatfield);
  ;
  scanf("%d", &n);
  int i, j;
  scanf("%s", s);
  memset(sm, 0, sizeof(sm));
  for (i = 0; i < n; i++) {
    a[i] = s[i] - 'a';
    for (j = 0; j < 26; j++) {
      if (a[i] != j) sm[j][i] = 1;
    }
  }
  for (j = 0; j < 26; j++) {
    for (i = 1; i <= n; i++) sm[j][i] += sm[j][i - 1];
  }
  int lo, hi, mid;
  for (i = 0; i < 26; i++) {
    for (j = 1; j <= n; j++) {
      lo = 0, hi = n;
      while (lo < hi) {
        mid = (lo + hi + 1) / 2;
        if (func(j, mid, i))
          lo = mid;
        else
          hi = mid - 1;
      }
      dp[i][j] = lo;
    }
  }
  int q, m, ans;
  char c[10];
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d %s", &m, c);
    ans = c[0] - 'a';
    printf("%d\n", dp[ans][m]);
  }
  return 0;
}
