#include <bits/stdc++.h>
using namespace std;
int n, o, i, mx, dl, kol, q;
string s;
char c;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  cin >> s;
  cin >> q;
  for (o = 0; o < q; o++) {
    cin >> kol >> c;
    mx = 0;
    dl = 0;
    for (i = 0; i < n; i++) {
      if (s[i] == c)
        dl++;
      else {
        if (kol == 0) {
          if (s[i - dl] != c) kol++;
          dl--;
          i--;
        } else {
          dl++;
          kol--;
        }
      }
      if (dl > mx) mx = dl;
    }
    cout << mx << endl;
  }
  return 0;
}
