#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  string s;
  string* ts;
  cin >> n;
  cin >> s;
  cin >> m;
  ts = new string[m];
  for (int i = 0; i < m; i++) cin >> ts[i];
  map<char, int> s_counts = map<char, int>();
  for (int i = 0; i < s.length(); i++) {
    if (s_counts.count(s[i])) {
      s_counts[s[i]] += 1;
    } else {
      s_counts[s[i]] = 1;
    }
  }
  map<char, int*> s_positions = map<char, int*>();
  for (auto& it : s_counts) {
    s_positions[it.first] = new int[it.second];
    s_counts[it.first] = 0;
  }
  for (int i = 0; i < s.length(); i++) {
    s_positions[s[i]][s_counts[s[i]]] = i;
    s_counts[s[i]] += 1;
  }
  for (int i = 0; i < m; i++) {
    map<char, int> alpha_t = map<char, int>();
    string t = ts[i];
    for (int j = 0; j < t.length(); j++) {
      if (alpha_t.count(t[j])) {
        alpha_t[t[j]] += 1;
      } else {
        alpha_t[t[j]] = 1;
      }
    }
    int maxPosition = -1;
    for (auto& it : alpha_t) {
      maxPosition = max(maxPosition, s_positions[it.first][it.second - 1]);
    }
    cout << maxPosition + 1 << endl;
  }
  return 0;
}
