#include <bits/stdc++.h>
using namespace std;
inline int readInt() {
  int n = 0, ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) n = n * 10 + ch - '0', ch = getchar();
  return n;
}
inline int readChar() {
  int ch = getchar();
  while (!isalpha(ch)) ch = getchar();
  return ch;
}
const int MAX_N = 1500 + 3;
int n, q;
char str[MAX_N];
int ans[26][MAX_N];
void prepare() {
  for (int ch = 'a'; ch <= 'z'; ++ch) {
    static pair<int, int> ranges[MAX_N];
    int cnt = 0, lst = -1, mx = 0;
    for (int i = 0; i <= n; ++i)
      if (str[i] != ch) {
        if (lst != -1)
          ranges[cnt++] = make_pair(lst, i), lst = -1, mx = max(mx, i - lst);
      } else {
        if (lst == -1) lst = i;
      }
    if (cnt == 0) {
      for (int i = 0; i <= n; ++i) ans[ch - 'a'][i] = i;
      continue;
    }
    ans[ch - 'a'][0] = lst;
    for (int m = 1; m <= n; ++m) {
      int s = 0, ans = 0;
      for (int i = 0, j = 0; i < cnt; ++i) {
        while (s > m) {
          s -= ranges[j + 1].first - ranges[j].second;
          ++j;
        }
        ans = max(ans, ranges[i].second - ranges[j].first + (m - s));
        if (i + 1 != cnt) s += ranges[i + 1].first - ranges[i].second;
      }
      ::ans[ch - 'a'][m] = min(n, ans);
    }
  }
}
int main() {
  scanf("%d%s%d", &n, str, &q);
  prepare();
  for (int i = 0; i < q; ++i) {
    int m = readInt(), ch = readChar() - 'a';
    printf("%d\n", ans[ch][m]);
  }
  return 0;
}
