#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> position(100);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); ++i) position[s[i] - 'a'].push_back(i);
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int ans = 0;
    string t;
    cin >> t;
    vector<int> pos(100);
    for (int j = 0; j < t.size(); ++j) pos[t[j] - 'a']++;
    for (char c = 'a'; c <= 'z'; ++c) {
      if (pos[c - 'a'] != 0) {
        ans = max(ans, position[c - 'a'][pos[c - 'a'] - 1]);
      }
    }
    cout << ans + 1 << endl;
  }
  return 0;
}
