#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, i, j, k;
  string s;
  cin >> n >> s >> q;
  int a[26][1505] = {};
  for (i = 0; i < 26; i++) {
    for (j = 0; j < s.size(); j++) {
      int used = 0;
      for (k = 0; j + k < s.size(); k++) {
        if (s[j + k] != 'a' + i) used++;
        a[i][used] = max(a[i][used], k + 1);
      }
    }
    for (j = 1; j <= n; j++) {
      a[i][j] = max(a[i][j], a[i][j - 1]);
    }
  }
  for (i = 0; i < q; i++) {
    int t1;
    char t2;
    cin >> t1 >> t2;
    cout << a[(int)(t2 - 'a')][t1] << '\n';
  }
}
