#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)5e3 + 100;
const int mod = (int)1e9 + 7;
const int maxlog = (int)20;
const int P = mod;
int n, cnt[28][maxn], q;
char second[maxn];
int get(int l, int r, char c) { return cnt[c - 'a'][r] - cnt[c - 'a'][l - 1]; }
int first(int m, char c) {
  int j = 1;
  int res = 0;
  for (int i = 1; i <= n; i++) {
    j = max(j, i);
    while (j <= n && j - i + 1 - get(i, j, c) <= m) ++j;
    res = max(res, j - i);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> second[i];
    cnt[second[i] - 'a'][i]++;
  }
  for (int i = 0; i < 26; i++)
    for (int j = 1; j <= n; j++) cnt[i][j] += cnt[i][j - 1];
  cin >> q;
  while (q--) {
    int m;
    char c;
    cin >> m >> c;
    cout << first(m, c) << endl;
  }
}
