#include <bits/stdc++.h>
using namespace std;
int d[1503][1503][26], e[1503][26], z[1503][26];
int main() {
  ios::sync_with_stdio(false);
  int n, q, m;
  char c;
  string s;
  cin >> n >> s >> q;
  for (int i = 0; i < 26; i++)
    for (int j = 1; j <= n; j++) e[j][i] = e[j - 1][i] + (s[j - 1] == 'a' + i);
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++)
      for (int k = 0; k < 26; k++) d[i][j][k] = e[j][k] - e[i - 1][k];
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 26; j++) {
      int l = 1;
      for (int k = 1; k <= n; k++) {
        while (d[l][k][j] + i < k - l + 1) l++;
        z[i][j] = max(z[i][j], k - l + 1);
      }
    }
  while (q--) cin >> m >> c, cout << z[m][c - 'a'] << '\n';
}
