#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 1e5 + 5;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<int>> ind(26);
  for (int i = 0; i < n; i++) {
    ind[s[i] - 'a'].push_back(i);
  }
  int tc;
  cin >> tc;
  while (tc--) {
    string t;
    cin >> t;
    vector<int> cnt(26);
    for (auto& i : t) {
      cnt[i - 'a']++;
    }
    int ans = -1;
    n = ((int)(t).size());
    for (int i = 0; i < 26; i++) {
      if (cnt[i] == 0) continue;
      ans = max(ans, ind[i][cnt[i] - 1] + 1);
    }
    cout << ans << "\n";
  }
  return 0;
}
