#include <bits/stdc++.h>
using namespace std;
int n, m, q, i, st, dr, cnt, sol;
char s[1501], c;
int main() {
  cin >> n >> ws;
  cin >> (s + 1);
  cin >> q;
  for (i = 1; i <= q; i++) {
    cin >> ws >> m >> ws;
    cin.get(c);
    st = dr = 1;
    cnt = sol = 0;
    while (dr <= n) {
      if (cnt <= m) {
        if (s[dr] != c) cnt++;
        dr++;
      }
      if (cnt > m) {
        if (s[st] != c) cnt--;
        st++;
      }
      sol = max(sol, dr - st);
    }
    cout << sol << "\n";
  }
  return 0;
}
