#include <bits/stdc++.h>
using namespace std;
int n, q, ans, m, b, e, cnt;
string s;
char letter;
int main() {
  cin >> n >> s >> q;
  for (int i = 0; i < q; i++) {
    scanf("%i %c", &m, &letter);
    b = e = cnt = ans = 0;
    for (int j = 0; j < s.size(); j++) {
      e = j;
      if (s[j] != letter) cnt++;
      while (cnt > m) {
        if (s[b] != letter) cnt--;
        b++;
      }
      ans = max(ans, e - b + 1);
    }
    printf("%i\n", ans);
  }
  return 0;
}
