#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<char> S(n);
  for (int i = 0; i < n; ++i) cin >> S[i];
  vector<vector<int>> ST(26, vector<int>(n + 1, 0));
  for (int i = 0; i < 26; ++i) {
    int k = 0;
    for (int j = 0; j < n; ++j) {
      if (S[j] != i + 'a') {
        ++k;
      }
      ST[i][j + 1] = k;
    }
  }
  int q, m;
  char c;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> m >> c;
    int a = 0, s = 1, f = s;
    while (f <= n) {
      if (ST[c - 'a'][f] - ST[c - 'a'][s - 1] <= m)
        ++f;
      else {
        a = max(a, f - s);
        ++s;
        ++f;
      }
    }
    a = max(a, f - s);
    cout << a << endl;
  }
  return 0;
}
