#include <bits/stdc++.h>
using namespace std;
int n;
string s;
vector<vector<int> > dp;
vector<int> res;
void recalc(char col) {
  dp = vector<vector<int> >(n + 1, vector<int>(n + 1, 0));
  res = vector<int>(n, 0);
  for (int(i) = 0; (i) < (n + 1); ++(i)) dp[i][0] = 0;
  for (int(ncur) = (1); (ncur) < (n + 1); ++(ncur)) {
    for (int(mcur) = (0); (mcur) < (n + 1); ++(mcur)) {
      if (s[ncur - 1] == col) {
        dp[mcur][ncur] = dp[mcur][ncur - 1] + 1;
        continue;
      }
      if (mcur == 0) {
        dp[mcur][ncur] = 0;
      } else {
        dp[mcur][ncur] = dp[mcur - 1][ncur - 1] + 1;
      }
    }
  }
  for (int(i) = 0; (i) < (n); ++(i)) {
    for (int(j) = (1); (j) < (n + 1); ++(j)) {
      res[i] = max(res[i], dp[i + 1][j]);
    }
  }
}
void solve() {
  cin >> n;
  cin >> s;
  int q;
  cin >> q;
  vector<pair<pair<char, int>, int> > qrs(q);
  for (int(i) = 0; (i) < (q); ++(i)) {
    cin >> qrs[i].first.second >> qrs[i].first.first;
    qrs[i].second = i;
  }
  sort(qrs.begin(), qrs.end());
  vector<pair<int, int> > ans(q);
  char lastcol = -1;
  for (int(i) = 0; (i) < (q); ++(i)) {
    char col = qrs[i].first.first;
    int m = qrs[i].first.second;
    if (col != lastcol) {
      lastcol = col;
      recalc(col);
    }
    ans[i].first = qrs[i].second;
    ans[i].second = res[m - 1];
  }
  sort(ans.begin(), ans.end());
  for (int(i) = 0; (i) < (q); ++(i)) cout << ans[i].second << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
