#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int dp[26][n + 1];
  vector<int> count(26, 0);
  for (int i = 0; i < s.size(); i++) count[s[i] - 'a']++;
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = 0;
      char a = i + 'a';
      int maxs = j;
      if (count[a - 'a'] + maxs >= n) {
        dp[i][j] = n;
        continue;
      }
      int cnt = 0;
      int end = 0;
      int start = 0;
      unordered_map<char, int> m;
      int maxl = 0;
      while (end < n) {
        m[s[end]]++;
        if (s[end] == a) {
          maxl = max(maxl, end - start + 1);
        } else {
          if (cnt == maxs) {
            while (cnt == maxs && start <= end) {
              m[s[start]]--;
              if (s[start] != a) cnt--;
              start++;
            }
            cnt++;
            maxl = max(maxl, end - start + 1);
          } else if (cnt < maxs) {
            maxl = max(maxl, end - start + 1);
            cnt++;
          }
        }
        end++;
      }
      dp[i][j] = maxl;
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int m;
    char x;
    cin >> m >> x;
    cout << dp[x - 'a'][m] << endl;
  }
}
