#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, i, j, k, l, m, b, t, sum, sum1, flag, max1, min1, tmp;
  string s;
  cin >> n >> s;
  vector<long long> v[75];
  map<char, long long> mp;
  for (i = 0; i < n; i++) {
    if (mp[s[i]] == 0) {
      mp[s[i]] = i;
    }
    v[s[i] - '0'].push_back(i);
  }
  cin >> t;
  string s1;
  while (t--) {
    cin >> s1;
    max1 = 0;
    map<char, long long> mp1;
    for (i = 0; i < s1.size(); i++) {
      mp1[s1[i]]++;
      k = mp1[s1[i]] - 1;
      l = v[s1[i] - '0'][k];
      max1 = max(max1, l + 1);
    }
    cout << max1 << endl;
  }
}
