#include <bits/stdc++.h>
using namespace std;
const int NMax = 1502;
const int sigma = 27;
struct statuss {
  int first, last;
};
int n, m, a;
char c;
char x[NMax];
int add[sigma][NMax];
int ans[sigma][NMax];
int main() {
  cin >> n;
  cin.get();
  cin.getline(x, NMax);
  cin >> m;
  for (int i = 'a' - 'a'; i <= 'z' - 'a'; ++i) {
    for (int j = 0; j < n; ++j) {
      if (x[j] == i + 'a') {
        add[i][j] = add[i][j - 1];
      } else
        add[i][j] = add[i][j - 1] + 1;
    }
  }
  for (int i = 'a' - 'a'; i <= 'z' - 'a'; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = j; k < n; ++k) {
        int e = add[i][k] - add[i][j - 1];
        ans[i][e] = max(ans[i][e], k - j + 1);
      }
    }
  }
  for (int i = 'a' - 'a'; i <= 'z' - 'a'; ++i) {
    for (int j = 0; j < n; ++j) {
      ans[i][n] = n;
      ans[i][j] = max(ans[i][j], ans[i][j - 1]);
    }
  }
  for (int i = 1; i <= m; ++i) {
    cin >> a >> c;
    cout << ans[c - 'a'][a] << '\n';
  }
  return 0;
}
