#include <bits/stdc++.h>
using namespace std;
long long memo[1510][28];
long long B[28];
int main() {
  ios::sync_with_stdio(0);
  long long n;
  cin >> n;
  string second;
  cin >> second;
  for (long long i = 0; i < n; i++) {
    B[second[i] - 'a']++;
  }
  for (long long i = 0; i <= 27; i++) {
    char C = 'a' + i;
    for (long long j = 0; j < n; j++) {
      long long Counter = 0;
      for (long long k = j; k < n; k++) {
        if (second[k] != C) Counter++;
        memo[Counter][i] = max(memo[Counter][i], k - j + 1);
      }
    }
  }
  long long q;
  cin >> q;
  long long m;
  char X;
  for (long long i = 0; i < (long long)q; i++) {
    cin >> m >> X;
    if (B[X - 'a'] + m >= n)
      cout << n << endl;
    else
      cout << memo[m][X - 'a'] << endl;
  }
}
