#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1500 + 10;
const int INF = 1e9;
int ans[MAX_N][26];
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int it = 0; it < 26; ++it) {
    char c = char(it + 'a');
    for (int i = 0; i < n; ++i) {
      int cnt = 0;
      for (int j = i; j < n; ++j) {
        if (s[j] != c) cnt++;
        ans[cnt][it] = max(ans[cnt][it], j - i + 1);
      }
    }
    for (int i = 1; i <= n; ++i) {
      if (ans[i][it] == 0) {
        ans[i][it] = ans[i - 1][it];
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int k;
    char c;
    cin >> k >> c;
    cout << ans[k][(int)(c - 'a')] << endl;
  }
  return 0;
}
