#include <bits/stdc++.h>
using namespace std;
const int maxn = 1505;
const int maxq = 200005;
int n, q;
char str[maxn];
struct Query {
  int r, c, i;
  Query() {}
  Query(int rr, int cc, int ii) : r(rr), c(cc), i(ii) {}
};
bool cmp(Query &q1, Query &q2) { return q1.r < q2.r; }
vector<Query> queryVec[26];
vector<int> vec[26];
int dp[26][maxn];
int ans[maxq];
void init() {
  for (int i = 0; i < 26; ++i) {
    queryVec[i].clear();
    vec[i].clear();
  }
  int len = strlen(str);
  for (int i = 0; i < len; ++i) {
    int val = str[i] - 'a';
    vec[val].push_back(i);
  }
  memset(dp, 0, sizeof(dp));
  memset(ans, 0, sizeof(ans));
  for (int i = 0; i < 26; ++i) {
    if (vec[i].size() > 0) {
      dp[i][0] = 1;
    }
    int maxInd = 0;
    for (int j = 0; j < vec[i].size(); ++j) {
      for (int k = j + 1; k < vec[i].size(); ++k) {
        int s = vec[i][j], e = vec[i][k], count = k - j - 1;
        int ind = e - s - 1 - count;
        dp[i][ind] = max(dp[i][ind], e - s + 1);
        maxInd = max(maxInd, ind);
      }
    }
  }
}
void start(int ind) {
  sort(queryVec[ind].begin(), queryVec[ind].end(), cmp);
  int si = 0, preInd = -1, preMax = 0;
  for (int i = 0; i < queryVec[ind].size(); ++i) {
    for (int j = si; j < maxn; ++j) {
      if (dp[ind][j] != 0) {
        preInd = j;
        preMax = max(preMax, dp[ind][preInd] - preInd);
      }
      if (queryVec[ind][i].r == j) {
        int tans = 0;
        tans = min(n, preMax + queryVec[ind][i].r);
        ans[queryVec[ind][i].i] = tans;
        si = j;
        break;
      }
    }
  }
}
int main() {
  while (~scanf("%d", &n)) {
    scanf("%s", str);
    scanf("%d", &q);
    int r;
    char c;
    init();
    for (int i = 0; i < q; ++i) {
      scanf("%d %c", &r, &c);
      queryVec[c - 'a'].push_back(Query(r, c - 'a', i));
    }
    for (int i = 0; i < 26; ++i) {
      start(i);
    }
    for (int i = 0; i < q; ++i) {
      printf("%d\n", ans[i]);
    }
  }
  return 0;
}
