#include <bits/stdc++.h>
using namespace std;
int dp[26][1500];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q, x, y, len, z;
  string str;
  cin >> n;
  cin >> str;
  cin >> q;
  for (int i = 0; i < 26; i++) {
    for (int j = 1; j <= n; j++) {
      x = j;
      y = z = 0;
      len = 0;
      while (z < n) {
        if ((str[z] - 'a') != i) x--;
        if (x >= 0) {
          len = max(len, z - y + 1);
          z++;
        } else {
          while ((str[y] - 'a') == i) y++;
          y++;
          z++;
          x = 0;
        }
      }
      dp[i][j] = len;
    }
  }
  while (q--) {
    cin >> x >> str;
    cout << dp[str[0] - 'a'][x] << "\n";
  }
  return 0;
}
