#include <bits/stdc++.h>
using namespace std;
char s[1505];
int main() {
  int n, q;
  scanf("%d", &n);
  scanf("%s", s);
  scanf("%d", &q);
  for (; q--;) {
    int k;
    char ch;
    scanf("%d %c", &k, &ch);
    int R = 0, ans = 0, cnt = 0;
    for (int i = 0; i < n; i++) {
      while (R < n && s[R] == ch) ++R;
      while (R < n && (cnt < k || s[R] == ch)) {
        if (s[R] != ch) ++cnt;
        ++R;
      }
      ans = max(ans, R - i);
      if (s[i] != ch) --cnt;
    }
    printf("%d\n", ans);
  }
  return 0;
}
