#include <bits/stdc++.h>
using namespace std;
int cnt[27][1505];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  int n, q, m;
  char c;
  cin >> n;
  cin >> s;
  for (int i = (0); i <= (25); i++) {
    char ch = i + 'a';
    for (int j = (0); j <= (n - 1); j++) {
      int tot = 0;
      int need = 0;
      for (int k = (j); k <= (n - 1); k++) {
        tot++;
        if (s[k] != ch) need++;
        cnt[i][need] = max(cnt[i][need], tot);
      }
    }
    for (int j = (1); j <= (n); j++) cnt[i][j] = max(cnt[i][j], cnt[i][j - 1]);
  }
  cin >> q;
  while (q--) {
    cin >> m >> c;
    cout << cnt[c - 'a'][m] << "\n";
  }
  return 0;
}
