#include <bits/stdc++.h>
using namespace std;
int n, q, dp[26][1505];
string dat;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> dat;
  for (int c = 0; c < 26; ++c) {
    for (int i = 0; i < n; ++i) {
      int gap = 0;
      for (int j = i; j < n; ++j) {
        if (dat[j] - 'a' != c) {
          ++gap;
        }
        dp[c][gap] = max(dp[c][gap], j - i + 1);
      }
    }
    for (int i = 1; i <= n; ++i) {
      dp[c][i] = max(dp[c][i], dp[c][i - 1]);
    }
  }
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    char c;
    int cnt;
    cin >> cnt >> c;
    cout << dp[c - 'a'][cnt] << endl;
  }
}
