#include <bits/stdc++.h>
using namespace std;
char c[5];
string s;
int dp[26][1505];
int n, q;
vector<int> ans;
int main() {
  cin >> n;
  cin >> s;
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < n; j++) {
      int c = 0;
      for (int k = j; k < n; k++) {
        if (s[k] != i + 'a') c++;
        dp[i][c] = max(dp[i][c], k - j + 1);
      }
    }
    for (int j = 1; j <= n; j++) dp[i][j] = max(dp[i][j - 1], dp[i][j]);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a;
    char b;
    cin >> a;
    cin >> b;
    ans.push_back(dp[b - 'a'][a]);
  }
  for (int i = 0; i < q; i++) cout << ans[i] << endl;
  return 0;
}
