#include <bits/stdc++.h>
using namespace std;
int pre[27][1505];
int dp[27][1505];
char str[1505];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int k = 0; k < 26; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = n; j > 0; j--) {
        if (str[i] - 'a' == k) {
          dp[k][j] = dp[k][j] + 1;
        } else {
          dp[k][j] = dp[k][j - 1] + 1;
        }
        pre[k][j] = max(pre[k][j], dp[k][j]);
      }
      bool flag = str[i] - 'a' == k ? true : false;
      if (flag)
        dp[k][0] = dp[k][0] + 1;
      else
        dp[k][0] = 0;
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int m;
    char s;
    scanf("%d %c", &m, &s);
    int k = s - 'a';
    printf("%d\n", pre[k][m]);
  }
  return 0;
}
