#include <bits/stdc++.h>
using namespace std;
void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
}
const int MAXN = 1502;
int dp[MAXN][MAXN][28];
string s;
int ans[MAXN][26];
int main(int argc, char* argv[]) {
  io();
  int n, q;
  cin >> n;
  cin >> s;
  s = " " + s;
  for (int col = 0; col < 26; col++) {
    for (int i = 1; i <= n; i++) {
      if (col == (s[i] - 'a')) {
        if (i == 1) {
          dp[i][0][col] = 1;
        } else {
          dp[i][0][col] = 1 + dp[i - 1][0][col];
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j][col] = 1 + dp[i - 1][j - (col != (s[i] - 'a'))][col];
      }
    }
  }
  for (int col = 0; col < 26; col++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        ans[i][col] = max(ans[i][col], dp[j][i][col]);
      }
    }
  }
  cin >> q;
  while (q--) {
    int m;
    char ch;
    cin >> m >> ch;
    cout << ans[m][ch - 'a'] << '\n';
  }
  return 0;
}
