#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long int dp[26][1501];
int main() {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  long long int i, j;
  for (i = 0; i < n; i++) {
    long long int freq[26] = {0};
    for (j = i; j < n; j++) {
      freq[s[j] - 97]++;
      long long int k;
      for (k = 0; k < 26; k++) {
        dp[k][j - i + 1 - freq[k]] = max(dp[k][j - i + 1 - freq[k]], j - i + 1);
      }
    }
  }
  for (i = 0; i < 26; i++) {
    for (j = 1; j <= n; j++) dp[i][j] = max(dp[i][j - 1], dp[i][j]);
  }
  long long int q;
  cin >> q;
  while (q--) {
    long long int oo;
    char c;
    cin >> oo >> c;
    cout << dp[c - 97][oo] << "\n";
  }
}
