#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int main() {
  int n, a, q, ss[28][1505];
  char str[1505], b;
  while (~scanf("%d", &n)) {
    scanf("%s", str);
    scanf("%d", &q);
    for (int i = 0; i <= 25; i++) {
      char cc = i + 'a';
      ss[i][0] = 0;
      for (int j = 0; j < n; j++) {
        if (str[j] == cc)
          ss[i][j + 1] = ss[i][j];
        else
          ss[i][j + 1] = ss[i][j] + 1;
      }
    }
    while (q--) {
      scanf("%d %c", &a, &b);
      int maxx = a;
      for (int i = 1; i <= n - maxx; i++) {
        for (int j = i + maxx; j <= n; j++) {
          int sum = ss[b - 'a'][j] - ss[b - 'a'][i - 1];
          if (sum > a)
            break;
          else
            maxx = max(maxx, j - i + 1);
        }
      }
      printf("%d\n", maxx);
    }
  }
  return 0;
}
