#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 6;
int n, q, mx[27][N];
char a[N];
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int k = 0; k < 26; ++k) {
    for (int i = 1; i <= n; ++i) {
      int cnt = 0;
      for (int j = i; j <= n; ++j) {
        if (a[j] - 'a' != k) ++cnt;
        mx[k][cnt] = max(mx[k][cnt], j - i + 1);
      }
    }
  }
  for (int j = 0; j < 26; ++j)
    for (int i = 1; i <= n; ++i) mx[j][i] = max(mx[j][i], mx[j][i - 1]);
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    int x;
    char c;
    cin >> x >> c;
    cout << max(x, mx[c - 'a'][x]) << "\n";
  }
  return 0;
}
