#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, q;
  string s;
  cin >> n >> s >> q;
  long long dp[1501][150];
  for (char c = 'a'; c <= 'z'; ++c) {
    for (long long i = 0; i < n; ++i) {
      char ch = s[i];
      if (!i) {
        if (ch == c) {
          dp[0][c] = 1;
        } else {
          dp[0][c] = 0;
        }
        continue;
      }
      dp[i][c] = dp[i - 1][c];
      if (ch == c) {
        dp[i][c]++;
      }
    }
  }
  vector<vector<long long> > need(1501, vector<long long>(150, 0));
  for (long long c = 'a'; c <= 'z'; ++c) {
    for (long long j = 0; j < n - 1; ++j) {
      for (long long k = j + 1; k < n; ++k) {
        long long count{};
        count = dp[k][c] - dp[j][c];
        if (s[j] == c) {
          count++;
        }
        count = k - j + 1 - count;
        need[count][c] = max(need[count][c], k - j + 1);
      }
    }
  }
  for (long long i = 0; i < q; ++i) {
    long long m;
    char c;
    cin >> m >> c;
    long long ans{};
    for (long long j = 0; j <= m; ++j) {
      ans = max(need[j][c], ans);
    }
    cout << (!ans ? 1 : ans) << endl;
  }
}
