#include <bits/stdc++.h>
using namespace std;
int dp[1501][1501], an[200000], n, q, mnan[1501];
vector<pair<int, int> > qu[26];
char s[1502];
int main() {
  scanf("%d%s%d", &n, s + 1, &q);
  for (int i = 0; i < (int)(q); ++i) {
    int a;
    char c;
    scanf("%d %c", &a, &c);
    qu[c - 'a'].push_back(make_pair(a, i));
  }
  for (int i = 0; i < (int)(26); ++i) {
    for (int k = 0; k < (int)(n + 1); ++k) mnan[k] = 0;
    for (int j = 1; j < (int)(n + 1); ++j)
      for (int k = 0; k < (int)(j + 1); ++k) {
        if (s[j] == 'a' + i)
          dp[j][k] = dp[j - 1][k] + 1;
        else if (k)
          dp[j][k] = dp[j - 1][k - 1] + 1;
        else
          dp[j][k] = 0;
        mnan[k] = max(mnan[k], dp[j][k]);
      }
    for (int j = 1; j < (int)(n + 1); ++j) mnan[j] = max(mnan[j], mnan[j - 1]);
    for (int j = 0; j < (int)(qu[i].size()); ++j)
      an[qu[i][j].second] = mnan[qu[i][j].first];
  }
  for (int i = 0; i < (int)(q); ++i) printf("%d\n", an[i]);
}
