#include <bits/stdc++.h>
using namespace std;
int b[20005];
int cnt2 = 0;
int arr[2005];
void findZeroes(int n, int m) {
  int wL = 0, wR = 0;
  int bestL = 0, bestWindow = 0;
  int zeroCount = 0;
  while (wR < n) {
    if (zeroCount <= m) {
      if (arr[wR] == 0) zeroCount++;
      wR++;
    }
    if (zeroCount > m) {
      if (arr[wL] == 0) zeroCount--;
      wL++;
    }
    if (wR - wL > bestWindow) {
      bestWindow = wR - wL;
      bestL = wL;
    }
  }
  for (int i = 0; i < bestWindow; i++) {
    if (arr[bestL + i] == 0) {
      b[cnt2] = bestL + i;
      cnt2++;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, q, m, a[20004], i, j, ans[20005];
  string s;
  char c;
  cin >> n >> s >> q;
  for (i = 0; i < q; i++) {
    int maxi = 0;
    cnt2 = 0;
    cin >> m >> c;
    for (j = 0; j < n; j++) {
      if (s[j] == c) {
        arr[j] = 1;
      } else {
        arr[j] = 0;
      }
    }
    findZeroes(n, m);
    for (j = 0; j < cnt2; j++) {
      arr[b[j]] = 1;
    }
    for (j = 0; j < n; j++) {
      ans[j] = 0;
    }
    if (arr[0] == 1) {
      ans[0] = 1;
    }
    maxi = ans[0];
    for (j = 1; j < n; j++) {
      if (arr[j] == 1 && arr[j - 1] == 1) {
        ans[j] = ans[j - 1] + 1;
      } else if (arr[j] == 1 && arr[j - 1] == 0) {
        ans[j] = 1;
      }
      maxi = max(ans[j], maxi);
    }
    cout << maxi << "\n";
  }
}
