#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = n; i >= 1; i--) s[i] = s[i - 1];
  vector<int> string_ind[26];
  for (int i = 1; i <= n; i++) {
    string_ind[s[i] - 'a'].push_back(i);
  }
  int m;
  cin >> m;
  while (m--) {
    string s1;
    cin >> s1;
    int count[26];
    for (int i = 0; i <= 25; i++) count[i] = 0;
    int ans = -1;
    for (int i = 0; i <= s1.size() - 1; i++) count[s1[i] - 'a']++;
    for (int i = 0; i <= 25; i++) {
      if (count[i] > 0) {
        int x = string_ind[i][count[i] - 1];
        ans = max(ans, x);
      }
    }
    cout << ans << '\n';
  }
}
