#include <bits/stdc++.h>
using namespace std;
set<long long> gera_primos(long long n) {
  set<long long> primos;
  vector<bool> crivo(n);
  primos.insert(2);
  for (long long i = 3; i <= n; i += 2) {
    if (crivo[i]) continue;
    primos.insert(i);
    for (long long j = i; j <= n; j += i) {
      crivo[j] = true;
    }
  }
  return primos;
}
struct Node {
  long long id, ord, antes;
  bool visitado;
  long long n;
  set<long long> out;
  set<long long> in;
  Node() {
    static long long i = 0;
    id = i++;
  }
};
struct Cluster {
  long long dist, size;
  long long soma_dist, soma_size;
};
int main() {
  long long n, q;
  string s;
  cin >> n >> s >> q;
  s += '0';
  map<char, map<long long, long long>> resp;
  for (long long c = 'a'; c < (long long)'z' + 1; c++) {
    vector<Cluster> clusters(1);
    clusters[0] = {0, 0, 0, 0};
    long long start = -1, end = -1;
    long long j = 0;
    char ant = '\0';
    bool clus = false;
    for (long long i = 0; i < (long long)n + 1; i++) {
      if (s[i] == c) {
        if (clus) continue;
        clus = true;
        j++;
        clusters.push_back({0, 0, 0, 0});
        if (start == -1) {
          end = i - 1;
        }
        start = i;
        clusters[j].dist = start - end - 1;
        clusters[j].soma_dist = clusters[j].dist + clusters[j - 1].soma_dist;
      } else if (clus) {
        clus = false;
        clusters[j].size = i - start;
        clusters[j].soma_size = clusters[j].size + clusters[j - 1].soma_size;
        end = i - 1;
      }
    }
    map<long long, long long> m;
    for (long long i = 0; i < (long long)clusters.size(); i++) {
      m[clusters[i].soma_dist] = i;
    }
    for (long long k = 1; k < (long long)n + 1; k++) {
      long long maximo = -(long long)1e18;
      for (long long i = 1; i < (long long)clusters.size(); i++) {
        auto it = m.upper_bound(k + clusters[i].soma_dist);
        if (it != m.begin()) it--;
        long long j = it->second;
        long long size = clusters[j].soma_size - clusters[i - 1].soma_size + k;
        maximo = max(maximo, size);
      }
      resp[c][k] = max(k, min(n, maximo));
    }
  }
  for (long long i = 0; i < (long long)q; i++) {
    string s;
    long long m;
    cin >> m >> s;
    char c = s[0];
    printf("%lld\n", resp[c][m]);
  }
}
