#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  scanf("%d", &N);
  char ch[N + 1];
  scanf("%s", ch + 1);
  int t[26][N + 1];
  for (int j = 0; j < 26; ++j) t[j][1] = (ch[1] == 'a' + j), t[j][0] = 0;
  for (int i = 1; i <= N; ++i)
    for (int c = 0; c < 26; ++c) t[c][i] = t[c][i - 1] + (ch[i] == 'a' + c);
  int d[26][N + 1];
  for (int c = 0; c < 26; ++c)
    for (int i = 1; i <= N; ++i) {
      int l = 1, r = 1;
      int ans = 0;
      for (; l <= N; ++l) {
        while (r <= N && r - l + 1 - t[c][r] + t[c][l - 1] <= i) ++r;
        --r;
        ans = max(ans, r - l + 1);
      }
      d[c][i] = ans;
    }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int x;
    char c;
    scanf("%d %c", &x, &c);
    printf("%d\n", d[c - 'a'][x]);
  }
  return 0;
}
