#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  string s, t;
  vector<vector<int>> _s(26);
  cin >> n >> s;
  for (int i = 0; i < n; i++) _s[s[i] - 'a'].push_back(i + 1);
  cin >> m;
  while (m--) {
    cin >> t;
    vector<int> _t(26);
    for (int i = 0; i < t.size(); i++) _t[t[i] - 'a']++;
    int ans = -1;
    for (int i = 0; i < 26; i++)
      if (_t[i]) ans = max(ans, _s[i][_t[i] - 1]);
    cout << ans << "\n";
  }
}
