#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int m;
    char c;
    cin >> m >> c;
    int ans = 0;
    int l = 0, r = 0, oc = 0;
    if (s[r] != c) oc = 1;
    while (l < s.size()) {
      while (true) {
        ++r;
        if (r == s.size()) {
          --r;
          break;
        }
        if (s[r] != c) ++oc;
        if (oc > m) {
          --r;
          --oc;
          break;
        }
      }
      ans = max(ans, r - l + 1);
      if (s[l] != c) --oc;
      ++l;
    }
    cout << ans << endl;
  }
  return 0;
}
