#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  map<char, vector<long long> > m;
  for (long long i = 0; i < n; ++i) {
    m[s[i]].push_back(i + 1);
  }
  long long k;
  cin >> k;
  while (k--) {
    string t;
    cin >> t;
    long long ans = 0;
    map<char, long long> make_pair;
    for (long long i = 0; i < (long long)t.size(); ++i) {
      make_pair[t[i]]++;
      ans = max(ans, m[t[i]][make_pair[t[i]] - 1]);
    }
    cout << ans << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
}
