#include <bits/stdc++.h>
using namespace std;
int dp[1502][26];
string s;
int n;
int len(int w, char c) {
  if (dp[w][c - 'a'] != -1) {
    return dp[w][c - 'a'];
  }
  int l = 0;
  int m = 1;
  int sum = 0;
  int tail = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == c) {
      l++;
    } else {
      if (sum < w) {
        sum++;
        l++;
      } else {
        while (s[tail] == c && tail < i) {
          tail++;
        }
        tail++;
        l = i - tail + 1;
      }
    }
    if (l > m) {
      m = l;
    }
  }
  return dp[w][c - 'a'] = m;
}
int main() {
  cin >> n;
  int q;
  cin >> s;
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j <= n; j++) {
      dp[j][i] = -1;
    }
  }
  int w;
  char c;
  cin >> q;
  for (int i = 0; i < q; i++) {
    scanf("%d %c", &w, &c);
    printf("%d\n", len(w, c));
  }
  return 0;
}
