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