#include <bits/stdc++.h>
using namespace std;
int a[1600];
int d[30][1600];
int t[10000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (char c = 'a'; c <= 'z'; ++c) {
    for (int i = 0; i < n; ++i) {
      a[i] = a[i - 1] + (s[i] == c);
    }
    for (int i = 0; i < n; ++i) {
      for (int j = i; j < n; ++j) {
        int cnt = a[j] - a[i - 1];
        int len = j - i + 1;
        d[c - 'a'][len - cnt] = max(d[c - 'a'][len - cnt], len);
      }
    }
    int mx = d[c - 'a'][0];
    for (int i = 1; i <= n; ++i) {
      d[c - 'a'][i] = max(d[c - 'a'][i], mx);
      mx = max(d[c - 'a'][i], mx);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int m;
    char c;
    cin >> m >> c;
    cout << d[c - 'a'][m] << '\n';
  }
}
