#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q;
  cin >> n;
  string s;
  cin >> s;
  map<char, vector<int>> data, ans;
  for (char t = 'a'; t <= 'z'; t++) {
    vector<int> tmp(n + 1);
    for (int j = 1; j <= n; j++) {
      if (s[j - 1] == t) {
        tmp[j]++;
      }
      tmp[j] += tmp[j - 1];
    }
    data[t] = tmp;
  }
  for (char t = 'a'; t <= 'z'; t++) {
    vector<int> tmp(n + 1);
    ans[t] = tmp;
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        int x = j - i + 1 - (data[t][j] - data[t][i - 1]);
        if (ans[t][x] <= j - i + 1) {
          ans[t][x] = j - i + 1;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      ans[t][i] = max(ans[t][i], ans[t][i - 1]);
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a;
    char c;
    cin >> a >> c;
    cout << ans[c][a] << endl;
  }
}
