#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1501, MOD = 1000000007;
long long ans[MAXN][26];
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  long long n, q;
  string s;
  cin >> n >> s >> q;
  for (long long i = 0; i < 26; i++) {
    vector<long long> c(n);
    c[0] = 0;
    if (s[0] - 'a' != i) {
      c[0]++;
    }
    for (long long j = 1; j < n; j++) {
      c[j] = c[j - 1];
      if (s[j] - 'a' != i) {
        c[j]++;
      }
    }
    for (long long j = 1; j <= n; j++) {
      long long r = 0;
      while (r < n - 1) {
        if (c[r + 1] <= j) {
          r++;
        } else {
          break;
        }
      }
      long long kek = r + 1;
      for (long long k = 1; k < n; k++) {
        while (r < n - 1) {
          if (c[r + 1] - c[k - 1] <= j) {
            r++;
          } else {
            break;
          }
        }
        kek = max(kek, r - k + 1);
      }
      ans[j][i] = kek;
    }
  }
  while (q--) {
    long long m;
    char c;
    cin >> m >> c;
    cout << ans[m][c - 'a'] << endl;
  }
  return 0;
}
