#include <bits/stdc++.h>
using namespace std;
int i, j, n, q, k, mx, l, r, b;
char c, s[1509];
int main() {
  cin >> n >> s >> q;
  for (i = 1; i <= q; i++) {
    cin >> b >> c;
    mx = 0;
    l = 0;
    k = 0;
    for (r = 0; r < n; r++) {
      if (s[r] != c) k++;
      if (k > b) {
        l++;
        if (s[l - 1] != c) k--;
      }
      mx = max(mx, r - l + 1);
    }
    cout << mx << endl;
  }
  return 0;
}
