#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long bigmod(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
inline void sc(int &n) {
  n = 0;
  int c = getchar();
  int f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    n = (n << 3) + (n << 1) + c - '0', c = getchar();
  }
  n = n * f;
}
string s;
int n;
void calc(int x, int c) {
  int l = 1, co = 0, res = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] != c) co++;
    while (co > x) {
      if (s[l] != c) co--;
      l++;
    }
    res = max(res, i - l + 1);
  }
  cout << res << endl;
}
using namespace std;
int main() {
  cin >> n >> s;
  s.insert(0, "#");
  int q;
  cin >> q;
  while (q--) {
    char c;
    int x;
    cin >> x >> c;
    calc(x, c);
  }
}
