#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int badCount[26][n + 1];
  for (int i = 0; i < 26; i++) {
    badCount[i][0] = 0;
  }
  char in;
  map<char, int> letterCount;
  auto it = letterCount.begin();
  in = getchar();
  for (int i = 1; i < n + 1; i++) {
    in = getchar();
    for (int j = 0; j < 26; j++) {
      badCount[j][i] = badCount[j][i - 1];
    }
    badCount[(int)(in - 'a')][i]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m;
    char c;
    cin >> m >> c;
    int index = c - 'a';
    int right = 1;
    int maxLength = 0;
    for (int left = 0; left < n; left++) {
      while (right - left - (badCount[index][right] - badCount[index][left]) <=
                 m &&
             right < n + 1) {
        right++;
      }
      maxLength = max(maxLength, right - left - 1);
    }
    cout << maxLength << endl;
  }
}
