#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[2010];
int dp[2010];
map<char, vector<int> > ans;
bool check(int x, char c, int q) {
  for (int i = 0; i < n - x + 1; ++i) {
    int l = i;
    int r = i + x - 1;
    int cnt = (l == 0 ? dp[r] : dp[r] - dp[l - 1]);
    if (cnt <= q) return true;
  }
  return false;
}
int getAns(int x, char c) {
  int l = 1, r = n + 1;
  memset(dp, 0, sizeof(dp));
  dp[0] = (s[0] != c);
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1] + (s[i] != c);
  }
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (check(m, c, x))
      l = m;
    else
      r = m;
  }
  return l;
}
int query(int x, int c) { return ans[c][x]; }
void preProcess() {
  for (int i = 0; i < 26; ++i) {
    char c = 'a' + i;
    vector<int> mp;
    for (int j = 0; j <= n; ++j) {
      int maxLen = getAns(j, c);
      mp.push_back(maxLen);
    }
    ans[c] = mp;
  }
}
int main() {
  ios::sync_with_stdio(false);
  while (cin >> n) {
    for (int i = 0; i < n; ++i) cin >> s[i];
    cin >> m;
    ans.clear();
    preProcess();
    for (int i = 0; i < m; ++i) {
      int x;
      char c;
      cin >> x >> c;
      cout << query(x, c) << endl;
    }
  }
}
