#include <bits/stdc++.h>
using namespace std;
const int maxn = 2E5 + 5;
const int maxm = 1E3 + 5;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 0xff0000;
vector<int> pos[30];
void solve() {
  int n, m;
  string s, t;
  cin >> n >> s >> m;
  for (int i = 0; i < n; i++) pos[s[i] - 'a'].push_back(i);
  while (m--) {
    int cnt[30] = {0};
    cin >> t;
    int res = -1;
    for (int i = 0; i < (int)t.size(); i++) cnt[t[i] - 'a']++;
    for (int i = 0; i < 26; i++)
      if (cnt[i]) res = max(res, pos[i][cnt[i] - 1]);
    cout << ++res << endl;
  }
}
int main() {
  int T;
  T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
