#include <bits/stdc++.h>
const int maxn = 1505;
const int INF = 0x3f3f3f3f;
using namespace std;
char s[maxn];
int main() {
  char ch;
  char sh[2];
  int n, k, q;
  scanf("%d", &n);
  scanf("%s", s);
  scanf("%d", &q);
  while (q--) {
    scanf("%d%s", &k, sh);
    ch = sh[0];
    int l = 0, r, ans = 0;
    for (r = 0; r < n && k > 0; r++) {
      if (s[r] != ch) k--;
    }
    ans = r - l;
    while (r < n) {
      while (r < n && s[r] == ch) {
        r++;
      }
      ans = max(ans, r - l);
      r++;
      while (l < r && s[l] == ch) {
        l++;
      }
      l++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
