#include <bits/stdc++.h>
using namespace std;
void cass() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void maximize(int &a, int b) { a = max(a, b); }
const int N = 1506;
string st;
int n, q;
int dp[N][30];
int tmp[N][30];
int res[N][30];
void init() {
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      for (int c = 0; c < 26; c++) tmp[j][c] = dp[j][c];
    for (int j = 0; j <= n; j++)
      for (int c = 0; c < 26; c++) {
        if (st[i] - 'a' == c)
          dp[j][c] = tmp[j][c] + 1;
        else {
          if (j)
            dp[j][c] = tmp[j - 1][c] + 1;
          else
            dp[j][c] = 0;
        }
        maximize(res[j][c], dp[j][c]);
      }
  }
}
int main() {
  cass();
  cin >> n;
  cin >> st;
  st = " " + st;
  init();
  cin >> q;
  int x;
  char c;
  while (q--) {
    cin >> x >> c;
    cout << res[x][c - 'a'] << '\n';
  }
}
