#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  long long n, q;
  string s;
  cin >> n >> s >> q;
  long long dp[n + 1][26];
  memset(dp, 0, sizeof(dp));
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < 26; j++) {
      char c = j + 97;
      long long ans = 0, cap = 0, prev = 0;
      long long& cache = dp[i][j];
      for (long long k = 0; k < n; k++) {
        if (s[k] == c) {
          ans++;
        } else {
          if (cap == i) {
            while (prev < k && s[prev] == c) prev++, ans--;
            prev++;
          } else {
            cap++;
            ans++;
          }
        }
        cache = max(cache, ans);
      }
    }
  }
  while (q--) {
    long long m;
    char c;
    cin >> m >> c;
    cout << dp[m][c - 'a'] << endl;
  }
}
