#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, q, ans[1510][26];
string s;
int go(int m, char le) {
  if (ans[m][le - 'a'] != 0) return ans[m][le - 'a'];
  int l = 0, cnt = 0, res = 0;
  for (int r = 0; r < n; r++) {
    cnt += (s[r] == le);
    while (r - l + 1 - cnt > m) {
      if (s[l] == le) cnt--;
      l++;
    }
    res = max(res, r - l + 1);
  }
  return ans[m][le - 'a'] = res;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  cin >> n >> s;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m;
    char le;
    cin >> m >> le;
    cout << go(m, le) << "\n";
  }
  return 0;
}
