#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932385;
const long double E = 2.71828182845904523536;
const double eps = 1e-9;
const long long mod = 1e9 + 7;
const long long inf = 1 << 30;
const int N = 2020;
int frq[555][N];
int ans[555][N];
int n;
bool ok(int x, int m, int c) {
  for (int i = x - 1; i < n; i++) {
    int z = frq[c][i] - frq[c][i - x];
    if (x - z <= m) return true;
  }
  return false;
}
int main() {
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) frq[s[i]][i]++;
  for (int c = (int)'a'; c <= (int)'z'; c++)
    for (int i = 1; i < n; i++) frq[c][i] = frq[c][i - 1] + frq[c][i];
  for (int c = (int)'a'; c <= (int)'z'; c++) {
    for (int m = 1; m <= n; m++) {
      int lo = 0, hi = n;
      while (hi > lo) {
        int mid = (lo + hi + 1) >> 1;
        if (ok(mid, m, c))
          lo = mid;
        else
          hi = mid - 1;
      }
      ans[c][m] = lo;
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int m;
    char c;
    cin >> m >> c;
    cout << ans[c][m] << endl;
  }
  return 0;
}
