#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  string s, t;
  cin >> n >> s >> m;
  vector<int> x[26];
  for (int i = 0; i < int(n); i++) x[s[i] - 'a'].push_back(i + 1);
  while (m--) {
    int maximum = 0;
    cin >> t;
    vector<int> y(26);
    for (char c : t) y[c - 'a']++;
    for (int i = 0; i < int(26); i++)
      if (y[i] > 0) maximum = max(maximum, x[i][y[i] - 1]);
    cout << maximum << endl;
  }
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  solve();
  cout << endl;
  return 0;
}
