#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int m;
  cin >> m;
  vector<int> pos[26];
  for (int i = 0; i < 26; i++) {
    pos[i].push_back(0);
  }
  for (int i = 0; i < n; i++) {
    int c = s[i] - 'a';
    pos[c].push_back(i + 1);
  }
  for (int i = 0; i < m; i++) {
    vector<int> cnt(26);
    string name;
    cin >> name;
    for (auto c : name) cnt[c - 'a']++;
    int ans = 0;
    for (int j = 0; j < 26; j++) {
      ans = max(ans, pos[j][cnt[j]]);
    }
    cout << ans << '\n';
  }
  return 0;
}
