#include <bits/stdc++.h>
using namespace std;
char str[1504], ch;
int N, T, num, ans[30][1504], pre[1504];
int slove(int n, int m) {
  if (ans[m][0]++) return ans[m][n];
  memset(pre, 0, sizeof(pre));
  for (int i = 0; i < N; i++) {
    pre[i + 1] = pre[i] + (str[i] - m == 'a');
  }
  for (int len = 1, j = 1; len <= N; len++) {
    int a = 100000;
    for (int st = 1; st + len - 1 <= N; st++) {
      a = min(a, len - pre[st + len - 1] + pre[st - 1]);
    }
    ans[m][a] = max(ans[m][a], len);
  }
  for (int i = 1; i <= N; i++) ans[m][i] = max(ans[m][i], ans[m][i - 1]);
  return ans[m][n];
}
int main() {
  scanf("%d%s%d", &N, str, &T);
  for (int i = 1; i <= T; i++) {
    char ch;
    scanf("%d %c", &num, &ch);
    printf("%d\n", slove(num, ch - 'a'));
  }
  return 0;
}
