#include <bits/stdc++.h>
using namespace std;
const int N = 1505;
const int INF = 1 << 31 - 1;
array<int, 26> num;
array<vector<int>, 26> vecs;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < 26; i++) num[i] = -1;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    auto c = s[i];
    int temp = i - num[c - 'a'] - 1;
    vecs[c - 'a'].push_back(temp);
    num[c - 'a'] = i;
  }
  for (int i = 0; i < 26; i++) vecs[i].push_back(s.size() - num[i] - 1);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int num;
    char c;
    cin >> num >> c;
    int sum = 0;
    int res = 0;
    int k = 0;
    for (int j = 0; j < vecs[c - 'a'].size();) {
      sum += vecs[c - 'a'][j++];
      while (k < vecs[c - 'a'].size() && sum > num) sum -= vecs[c - 'a'][k++];
      if (k >= vecs[c - 'a'].size()) break;
      if (k == 0)
        res = max(res, j - k);
      else if (j == vecs[c - 'a'].size())
        res = max(res, j - k);
      else
        res = max(res, j - k + 1);
    }
    int ans = min(res + num, n);
    cout << ans << endl;
  }
  return 0;
}
