#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  long long i, j, n, m;
  cin >> n;
  string s;
  cin >> s;
  long long q;
  cin >> q;
  long long ans[s.length() + 1][26];
  long long len = s.length();
  memset(ans, -1, sizeof(ans));
  for (i = 0; i < 26; i++) {
    char c = 'a' + i;
    for (j = 0; j < len; j++) {
      long long temp = 0;
      for (long long l = j; l < len; l++) {
        if (s[l] != c) {
          temp++;
        }
        ans[temp][i] = max(ans[temp][i], l - j + 1);
      }
    }
  }
  long long lc[26] = {0};
  for (i = 0; i < 26; i++) {
    for (j = 1; j <= len; j++) {
      if (ans[j][i] == -1) {
        lc[i] = j;
        break;
      }
    }
  }
  while (q--) {
    long long num;
    char c;
    cin >> num >> c;
    long long ind = c - 'a';
    if (ans[num][ind] == -1)
      cout << len << endl;
    else
      cout << ans[num][ind] << endl;
  }
  return 0;
}
