#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
const long long maxn = (long long)2e3 + 10;
const long long maxm = (long long)20;
const int LOGN = 20;
const long long INF = 1e15 + 10;
const long long MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = 1e-12;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  vector<vector<int> > a(26, vector<int>());
  for (int i = 0; i < n; ++i) {
    auto c = s[i] - 'a';
    a[c].push_back(i);
  }
  int ncase;
  cin >> ncase;
  for (int tt = (0); tt < ((ncase)); ++tt) {
    int m;
    char x;
    cin >> m >> x;
    x -= 'a';
    int ans = min(n, m);
    int p = 0;
    int q = 0;
    int s = 0;
    while (q < a[x].size()) {
      s += (p != q ? a[x][q] - a[x][q - 1] - 1 : 0);
      while (s > m) {
        int t = a[x][p + 1] - a[x][p] - 1;
        s -= t;
        ++p;
      }
      assert(m >= s);
      ans = max(ans, min(a[x][q] - a[x][p] + 1 + (m - s), n));
      ++q;
    }
    cout << ans << "\n";
  }
  return 0;
}
