#include <bits/stdc++.h>
using namespace std;
int MOD = 1e9 + 7;
int INF = 1e9;
int n, q;
string s;
int dp[1505][1505][2];
int prec[26][2505];
vector<int> arr[26];
void precal(char c) {
  int x = c - 'a';
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= n; j++) {
      if (s[i] == c) {
        dp[i][j][1] = (i > 0 ? max(dp[i - 1][j][1], dp[i - 1][j][0]) : 0) + 1;
        dp[i][j][0] = (i > 0 ? max(dp[i - 1][j][1], dp[i - 1][j][0]) : 0) + 1;
      } else {
        if (j > 0)
          dp[i][j][1] =
              (i > 0 ? max(dp[i - 1][j - 1][1], dp[i - 1][j - 1][0]) : 0) + 1;
        else
          dp[i][j][1] = -INF;
        dp[i][j][0] = 0;
      }
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= n; j++)
      prec[x][j] = max(prec[x][j], max(dp[i][j][1], dp[i][j][0]));
  for (int j = 0; j <= n; j++) prec[x][j] = max(prec[x][j], prec[x][j - 1]);
}
int solve() {
  cin >> n >> s >> q;
  for (char c = 'a'; c <= 'z'; c++) precal(c);
  for (int i = 0; i < q; i++) {
    int m;
    char x;
    cin >> m >> x;
    cout << prec[x - 'a'][m] << endl;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  solve();
  return 0;
}
