#include <bits/stdc++.h>
using namespace std;
int str[26][1505];
int ans[26][1505];
int main(int argc, char *argv[]) {
  int n;
  scanf("%d", &n);
  getchar();
  char c;
  for (int i = 0; i < n; i++) {
    c = getchar();
    str[c - 'a'][i]++;
  }
  for (int i = 0; i < 26; i++) {
    for (int j = 1; j < n; j++) {
      str[i][j] = str[i][j - 1] + str[i][j];
    }
  }
  int total, m;
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = j; k < n; k++) {
        if (j == 0) {
          total = str[i][k];
        } else {
          total = str[i][k] - str[i][j - 1];
        }
        m = k - j + 1 - total;
        if (ans[i][m] < k - j + 1) {
          ans[i][m] = k - j + 1;
        }
      }
    }
  }
  for (int i = 0; i < 26; i++) {
    for (int j = 1; j <= n; j++) {
      ans[i][j] = ans[i][j] < ans[i][j - 1] ? ans[i][j - 1] : ans[i][j];
    }
  }
  int q;
  scanf("%d", &q);
  int num;
  while ((q--) > 0) {
    scanf("%d %c", &num, &c);
    printf("%d\n", ans[c - 'a'][num]);
  }
  return 0;
}
