#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
const double error = 1e-8;
const double PI = acos(-1);
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
inline long long int MOD(long long int x, long long int m = mod) {
  long long int y = x % m;
  return (y >= 0) ? y : y + m;
}
const int inf = INT_MAX;
const long long int infl = LLONG_MAX;
const int nmax = 2e3 + 10;
char str[nmax];
int memo[26][nmax];
void dp(int n) {
  for (char c = 'a'; c <= 'z'; c++) {
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = i; j >= 0; j--) {
        if (str[j] != c) cnt++;
        memo[c - 'a'][cnt] = max(i - j + 1, memo[c - 'a'][cnt]);
      }
    }
    for (int i = 1; i <= n; i++)
      memo[c - 'a'][i] = max(memo[c - 'a'][i], memo[c - 'a'][i - 1]);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str);
  dp(n);
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int m;
    char c[4];
    scanf("%d %s", &m, c);
    printf("%d\n", memo[c[0] - 'a'][m]);
  }
}
