#include <bits/stdc++.h>
using namespace std;
bool ok = false;
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int m;
  cin >> m;
  vector<vector<int> > idx(26);
  for (int i = 0; i < n; i++) {
    idx[s[i] - 'a'].push_back(i + 1);
    ;
  }
  vector<int> cnt(26, 0);
  string t;
  for (int i = 0; i < m; i++) {
    cin >> t;
    cnt = vector<int>(26, 0);
    for (int j = 0; j < (int)(t.size()); j++) {
      cnt[t[j] - 'a']++;
    }
    int ans = 0;
    for (int j = 0; j < 26; j++) {
      cnt[j]--;
      if (cnt[j] < 0) continue;
      ans = max(ans, idx[j][cnt[j]]);
    }
    cout << ans << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
