#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &out, const pair<T, T> &P) {
  out << P.first << ' ' << P.second;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &V) {
  for (int i = 0; i < V.size(); ++i)
    out << V[i] << (i == V.size() - 1 ? "" : " ");
  return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &V) {
  for (T &i : V) in >> i;
  return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &P) {
  in >> P.first >> P.second;
  return in;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  string s;
  cin >> s;
  map<char, vector<int> > mp;
  for (int i = 0; i < s.size(); i++) {
    mp[s[i]].push_back(i + 1);
  }
  int m;
  cin >> m;
  while (m--) {
    string t;
    cin >> t;
    int ans = 0;
    vector<int> pt(26, 0);
    for (char c : t) {
      ans = max(ans, mp[c][pt[c - 'a']]);
      pt[c - 'a']++;
    }
    cout << ans << '\n';
  }
  return 0;
}
