#include <bits/stdc++.h>
using namespace std;
const int maxn = 1505;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
int n, q;
char str[maxn];
int num[30][maxn];
int s[maxn];
void Get_Count() {
  memset((num), (0), sizeof(num));
  for (int i = 1; i <= n; i++) {
    s[i] = str[i] - 'a';
    for (int j = 0; j < 26; j++) {
      num[j][i] = num[j][i - 1];
      if (s[i] != j) num[j][i]++;
    }
  }
}
int main() {
  char c[5];
  int k;
  while (~scanf("%d", &n)) {
    scanf("%s", str + 1);
    Get_Count();
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
      scanf("%d%s", &k, c);
      int id = c[0] - 'a';
      if (num[id][n] == n) {
        printf("%d\n", k);
        continue;
      }
      int *tmp = num[id];
      int end = 1;
      int ans = 0;
      for (int i = 1; i <= n; i++) {
        while (end + 1 <= n && tmp[end + 1] - tmp[i - 1] <= k) end++;
        int len = end - i + 1;
        ans = max(ans, len);
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
