#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char s[1505];
int f[1505][26];
signed main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n >> (s + 1);
  for (int j = 0; j < 26; ++j) {
    char ch = j + 'a';
    int lst = 0;
    vector<pair<int, int>> vec;
    for (int i = 1; i <= n; ++i)
      if (s[i] == ch) {
        int pos = i;
        if (lst == 0) lst = i - 1;
        while (pos + 1 <= n && s[pos + 1] == s[pos]) ++pos;
        vec.push_back({pos - i + 1, i - lst - 1});
        lst = i = pos;
      }
    for (int k = 1; k <= n; ++k) {
      f[k][j] = k;
      for (int i = 0; i < vec.size(); ++i) {
        int pos = i, x = k, sum = vec[i].first;
        while (pos + 1 < vec.size() && x - vec[pos + 1].second >= 0)
          x -= vec[pos + 1].second, sum += vec[pos + 1].first, ++pos;
        f[k][j] = max(f[k][j], sum + k);
      }
      f[k][j] = min(f[k][j], n);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int x;
    char y;
    cin >> x >> y;
    cout << f[x][y - 'a'] << "\n";
  }
  return 0;
}
