#include <bits/stdc++.h>
using namespace std;
string inp;
unordered_map<char, unordered_map<int, int>> alphabet_dict;
vector<char> doesnt_contain_list;
void process(char letter) {
  vector<int> places;
  for (int x = 0; x < inp.size(); ++x) {
    if (inp[x] != letter) {
      places.push_back(x);
    }
  }
  unordered_map<int, int> best;
  for (int y = 0; y < places.size(); ++y) {
    int left;
    if (y != 0) {
      left = places[y - 1] + 1;
    } else {
      left = 0;
    }
    int os = 1;
    for (int z = y; z < places.size(); ++z) {
      int right;
      if (z + 1 >= places.size()) {
        right = inp.size() - 1;
      } else {
        right = places[z + 1] - 1;
      }
      int myrange = right - left + 1;
      if (best.count(os)) {
        best[os] = max(myrange, best[os]);
      } else {
        best[os] = myrange;
      }
      ++os;
    }
  }
  alphabet_dict[letter] = best;
}
int main() {
  std::ios::sync_with_stdio(false);
  int N;
  cin >> N;
  cin >> inp;
  for (char x = 'a'; x <= 'z'; ++x) {
    if (inp.find(x) == string::npos) {
      doesnt_contain_list.push_back(x);
    } else {
      process(x);
    }
  }
  int numqueries;
  cin >> numqueries;
  for (int q = 0; q < numqueries; ++q) {
    int changes;
    char curletter;
    cin >> changes >> curletter;
    if (!alphabet_dict.count(curletter)) {
      cout << changes << endl;
      continue;
    }
    if (!alphabet_dict[curletter].count(changes)) {
      cout << inp.size() << endl;
    } else {
      cout << alphabet_dict[curletter][changes] << endl;
    }
  }
  return 0;
}
