#include <bits/stdc++.h>
using namespace std;
int n;
string s;
void tp(string s, int n, int u, char v) {
  int i = 0, dem = 0, maxx = 0, j = 0;
  while (j < n) {
    for (; j < n; j++) {
      if (s[j] != v) {
        if (u == 0) {
          break;
        }
        u--;
        dem++;
        maxx = max(maxx, dem);
      } else {
        dem++;
        maxx = max(maxx, dem);
      }
    }
    for (; i < n; i++) {
      if (s[i] != v) {
        u++;
        dem--;
        if (u != 0) {
          i++;
          break;
        }
      } else {
        dem--;
      }
    }
  }
  cout << maxx << "\n";
}
int main() {
  cin >> n;
  cin >> s;
  int q, m;
  cin >> q;
  char c;
  for (int i = 0; i < q; i++) {
    cin >> m >> c;
    tp(s, n, m, c);
  }
}
