#include <bits/stdc++.h>
using namespace std;
const int N = 1600;
char ch[N], str;
int x[N][30], y[N][30], n;
bool f(int k, int m, int w) {
  for (int i = 0, j = k; j <= n; i++, j++)
    if (k - (x[j][w] - x[i][w]) <= m) return true;
  return false;
}
int main() {
  int i, j, m, a, b, l, r, mid;
  scanf("%d", &n);
  scanf("%s", ch + 1);
  for (i = 1; i <= n; i++)
    for (j = 0; j < 26; j++)
      x[i][j] = x[i - 1][j] + (ch[i] - 'a' == j), y[i][j] = -1;
  scanf("%d", &m);
  while (m--) {
    scanf("%d %c", &a, &str);
    b = str - 'a';
    if (y[a][b] == -1) {
      l = 1;
      r = n + 1;
      while (l + 1 < r) {
        mid = (l + r) / 2;
        if (f(mid, a, b))
          l = mid;
        else
          r = mid;
      }
      y[a][b] = l;
    }
    printf("%d\n", y[a][b]);
  }
}
