#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e7 + 9;
int i, j, n, m, k, mx, x, y, l, r, b;
char c, s[2000];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d %c", &b, &c);
    mx = 0;
    l = 1;
    k = 0;
    for (r = 1; r <= n; r++) {
      if (s[r] != c) k++;
      while (k > b) {
        if (s[l] != c) k--;
        l++;
      }
      mx = max(mx, r - l + 1);
    }
    cout << mx << endl;
  }
}
