#include <bits/stdc++.h>
using namespace std;
int n;
char s[1505];
int k;
int main() {
  int l, r, num, ans;
  scanf("%d", &n);
  scanf("%s", s);
  scanf("%d", &k);
  int m;
  char p;
  while (k--) {
    scanf("%d %c", &m, &p);
    l = 0, r = 0, num = m;
    ans = 0;
    int res = 0;
    while (r < n) {
      if (s[r] != p) {
        num--;
      }
      r++;
      ans++;
      while (num < 0) {
        if (s[l] != p) num++;
        ans--;
        l++;
      }
      res = max(res, ans);
    }
    printf("%d\n", res);
  }
  return 0;
}
