#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int main() {
  string s;
  char a, ch;
  int q, cnt, l, r, ans;
  cin >> n;
  cin >> s;
  cin >> q;
  while (q) {
    q--;
    cin >> k >> ch;
    r = l = ans = cnt = 0;
    while (r < n) {
      if (cnt <= k) {
        if (s[r] != ch) cnt++;
        r++;
      }
      if (cnt > k) {
        if (s[l] != ch) cnt--;
        l++;
      }
      ans = max(r - l, ans);
    }
    cout << ans << endl;
  }
  return 0;
}
