#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int dp[1600][28];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin.ignore(256, '\n');
  cin >> s;
  int t;
  cin >> t;
  while (t--) {
    int m;
    char c;
    cin >> m >> c;
    int start = 0, convert = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] != c) convert++;
      while (convert > m) {
        if (s[start] != c) convert--;
        start++;
      }
      ans = max(ans, i - start + 1);
    }
    cout << ans << endl;
  }
  return 0;
}
