#include <bits/stdc++.h>
using namespace std;
int n, i;
char a[1505];
int func(char ch, int p) {
  int wL = 0, wR = 0;
  int bestL = 0, bestWindow = 0;
  int zeroCount = 0;
  while (wR < n) {
    if (zeroCount <= p) {
      if (a[wR] != ch) zeroCount++;
      wR++;
    }
    if (zeroCount > p) {
      if (a[wL] != ch) zeroCount--;
      wL++;
    }
    if (wR - wL > bestWindow) {
      bestWindow = wR - wL;
      bestL = wL;
    }
  }
  return bestWindow;
}
int main() {
  scanf("%d", &n);
  scanf(" %s", a);
  int q, p;
  char ch;
  map<pair<int, char>, int> m;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %ch", &p, &ch);
    if (m[{p, ch}]) {
      printf("%d\n", m[{p, ch}]);
      continue;
    }
    int ans = func(ch, p);
    printf("%d\n", ans);
    m[{p, ch}] = ans;
  }
}
