#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int m, f1[30];
  cin >> m;
  for (int i = 0; i < 26; i++) f1[i] = 0;
  map<pair<char, int>, int> ma;
  for (int i = 0; i < n; i++) {
    f1[s[i] - 'a']++;
    ma[{s[i], f1[s[i] - 'a']}] = i + 1;
  }
  while (m--) {
    string p;
    cin >> p;
    int f[30], ans = 0;
    for (int i = 0; i < 26; i++) f[i] = 0;
    for (int i = 0; i < p.length(); i++) f[p[i] - 'a']++;
    string g = "abcdefghijklmnopqrstuvwxyz";
    for (int i = 0; i < 26; i++) {
      if (f[i] > 0) ans = max(ans, ma[{g[i], f[i]}]);
    }
    cout << ans << endl;
  }
}
int main() { solve(); }
