#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1512, MAXL = 28;
char s[MAXN];
int memo[MAXL][MAXN], n;
int size_seg[MAXN];
int dist_seg[MAXN];
int solve(int c, int m) {
  if (memo[c][m] != -1) {
    return memo[c][m];
  }
  int qtd_seg = 0;
  int last_seg_end = -1;
  for (int i = 0; i < n; i++) {
    int cur_seg = 0, first_i = i;
    while (i < n && s[i] - 'a' == c) {
      cur_seg++;
      i++;
    }
    if (cur_seg > 0) {
      size_seg[qtd_seg] = cur_seg;
      dist_seg[qtd_seg] = first_i - 1 - last_seg_end;
      last_seg_end = i - 1;
      qtd_seg++;
    }
  }
  dist_seg[qtd_seg] = n - 1 - last_seg_end;
  size_seg[qtd_seg] = 0;
  int answer = 0;
  int left = 0;
  int budget = m;
  int cur_size = 0;
  for (int right = 0; right <= qtd_seg; right++) {
    cur_size += dist_seg[right] + size_seg[right];
    budget -= dist_seg[right];
    while (budget < 0) {
      budget += dist_seg[left];
      cur_size -= dist_seg[left];
      if (left > 0) {
        cur_size -= size_seg[left - 1];
      }
      left++;
    }
    answer = max(answer, min(cur_size + budget, n));
  }
  return memo[c][m] = answer;
}
int main() {
  scanf("%d", &n);
  scanf(" %s", s);
  for (int i = 0; i < MAXL; i++) {
    for (int k = 0; k < MAXN; k++) {
      memo[i][k] = -1;
    }
  }
  int q, m;
  char c;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %c", &m, &c);
    printf("%d\n", solve(c - 'a', m));
  }
  return 0;
}
