#include <bits/stdc++.h>
using namespace std;
map<char, vector<long> > mp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long n, k;
  cin >> n;
  string s;
  cin >> s;
  k = 1;
  for (char c : s) {
    mp[c].push_back(k);
    k += 1;
  }
  cin >> k;
  string t;
  long res;
  for (long j = 1; j <= k; j++) {
    res = 0;
    map<char, int> tmp;
    cin >> t;
    for (char c : t) tmp.insert(pair<char, int>(c, 0));
    for (char c : t) {
      tmp[c] += 1;
    }
    for (char c : t) {
      res = max(res, mp[c][tmp[c] - 1]);
    }
    cout << res << "\n";
    tmp.clear();
  }
}
