#include <bits/stdc++.h>
using namespace std;
char garland[1500];
int n, q;
int sum[1600][30];
int DP[1600][30];
int _max(int a, int b) { return a > b ? a : b; }
int _min(int a, int b) { return a > b ? b : a; }
int main() {
  cin >> n;
  memset(sum, 0, sizeof(sum));
  memset(DP, 0, sizeof(DP));
  for (int i = 1; i <= n; i++) {
    cin >> garland[i];
    for (int j = 0; j < 26; j++) sum[i][j] = sum[i - 1][j];
    sum[i][garland[i] - 'a']++;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      for (int k = 0; k < 26; k++) {
        int num = sum[j][k] - sum[i - 1][k];
        DP[j - i + 1 - num][k] = _max(DP[j - i + 1 - num][k], j - i + 1);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k < 26; k++) {
      DP[i][k] = _min(n, _max(DP[i][k], DP[i - 1][k] + 1));
    }
  }
  cin >> q;
  int num1;
  char ch;
  for (int i = 0; i < q; i++) {
    cin >> num1 >> ch;
    cout << DP[num1][ch - 'a'] << endl;
  }
  return 0;
}
