#include <bits/stdc++.h>
using namespace std;
const int maxn = 1800;
int num[26][maxn];
char str[maxn];
int n, k;
int ans[26][maxn];
void getnum(char *s) {
  for (int i = 1; i <= n; i++) {
    int kk = s[i] - 'a';
    for (int j = 0; j < 26; j++) {
      num[j][i] = num[j][i - 1];
      if (kk != j) num[j][i]++;
    }
  }
}
void solve() {
  for (int i = 0; i < 26; i++) {
    for (int k = 1; k <= n; k++) {
      int *temp = num[i];
      int sum = 0;
      int end = 1;
      for (int j = 1; j <= n; j++) {
        while (end + 1 <= n && temp[end + 1] - temp[j - 1] <= k) end++;
        sum = max(sum, end - j + 1);
      }
      ans[i][k] = sum;
    }
  }
}
int main() {
  cin >> n;
  scanf("%s", str + 1);
  getnum(str);
  solve();
  int q;
  cin >> q;
  while (q--) {
    char l[5];
    scanf("%d%s", &k, l);
    int kk = l[0] - 'a';
    printf("%d\n", ans[kk][k]);
  }
  return 0;
}
