#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  map<char, vector<int>> ma;
  for (int i = 0; i < n; i++) {
    ma[s[i]].push_back(i + 1);
  }
  int m;
  cin >> m;
  vector<int> ans;
  while (m--) {
    string s1;
    cin >> s1;
    map<char, int> m1;
    for (int i = 0; i < s1.size(); i++) {
      m1[s1[i]]++;
    }
    int a = -1;
    for (auto &i : ma) {
      if (m1.find(i.first) != m1.end()) {
        int b = m1[i.first];
        a = max(a, i.second[b - 1]);
      }
    }
    ans.push_back(a);
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << "\n";
  }
}
int main() {
  ios::sync_with_stdio(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
