#include <bits/stdc++.h>
using namespace std;
char s[2000];
int main() {
  int n;
  while (~scanf("%d", &n)) {
    scanf("%s", s);
    int q;
    scanf("%d", &q);
    while (q--) {
      int m;
      char ch;
      scanf("%d %c", &m, &ch);
      int l = 0, r = 0;
      int res = -1;
      int need = 0;
      for (int r = 0; r < n; r++) {
        if (s[r] != ch) need++;
        while (need > m) {
          if (s[l] != ch) need--;
          l++;
        }
        res = max(res, r - l + 1);
      }
      printf("%d\n", res);
    }
  }
  return 0;
}
