#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int dp[26][1505];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < 26; i++) {
    for (int l = 0; l < n; l++) {
      int cnt = 0;
      for (int r = l; r < n; r++) {
        if (s[r] - 'a' != i) cnt++;
        dp[i][cnt] = max(dp[i][cnt], r - l + 1);
      }
    }
    for (int t = 1; t < n + 1; t++) dp[i][t] = max(dp[i][t], dp[i][t - 1]);
  }
  int q;
  cin >> q;
  while (q--) {
    int m;
    char c;
    cin >> m >> c;
    cout << dp[c - 'a'][m] << "\n";
  }
  return 0;
}
