#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979;
const long long int mod = 1e9 + 7;
long long int takemod(long long int a, long long int mod) {
  return ((a % mod) + mod) % mod;
}
long long int bs(long long int a[], long long int l, long long int r,
                 long long int x) {
  if (r >= l) {
    long long int mid = (l + r) / 2;
    if (a[mid] == x)
      return mid;
    else if (a[mid] > x)
      return bs(a, l, mid - 1, x);
    else
      return bs(a, mid + 1, r, x);
  }
  return -1;
}
long long int s_t_i(string second) {
  stringstream geek(second);
  long long int m;
  geek >> m;
  return m;
}
long long int power(long long int b, long long int p) {
  if (p == 0)
    return 1;
  else if (p % 2 == 0)
    return (power(b, p / 2) * (power(b, p / 2)));
  else
    return ((b) * (power(b, p - 1)));
}
bool isprime(long long int n) {
  if (n == 1) return false;
  if (n == 2 or n == 3) return true;
  long long int lim = sqrt(n);
  long long int i = 2;
  while (i <= lim) {
    if (n % i == 0) return false;
    i++;
  }
  return true;
}
vector<long long int> factors(long long int n) {
  vector<long long int> a;
  a.push_back(1);
  long long int lim = sqrt(n), i = 2;
  while (i <= lim) {
    if (n % i == 0) {
      a.push_back(i);
      if (i != n / i) a.push_back(n / i);
    }
    i++;
  }
  if (n != 1) a.push_back(n);
  sort(a.begin(), a.end());
  return a;
}
vector<pair<long long int, long long int> > pf_with_count(long long int b) {
  pair<long long int, long long int> p;
  vector<pair<long long int, long long int> > pf;
  long long int count = 0;
  while (b % 2 == 0) {
    b /= 2;
    count++;
  }
  if (count > 0) {
    p.first = 2;
    p.second = count;
    pf.push_back(p);
  }
  long long int i = 3;
  while (i <= sqrt(b)) {
    count = 0;
    while (b % i == 0) {
      b /= i;
      count++;
    }
    if (count > 0) {
      p.first = i;
      p.second = count;
      pf.push_back(p);
    }
    i += 2;
  }
  if (b > 2) {
    p.first = b;
    p.second = 1;
    pf.push_back(p);
  }
  return pf;
}
long long int bs_t_i(string second) {
  long long int l = strtoull(second.c_str(), NULL, 2);
  return l;
}
string i_to_bs(long long int n) {
  string second = bitset<5>(n).to_string();
  return second;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n;
  cin >> n;
  string second;
  cin >> second;
  vector<long long int> v[26];
  long long int i = 0;
  while (i < n) {
    v[second[i] - 'a'].push_back(i);
    i++;
  }
  long long int m;
  cin >> m;
  while (m--) {
    string h;
    cin >> h;
    sort(h.begin(), h.end());
    long long int j = 0;
    long long int ma = -1;
    while (j < h.size()) {
      long long int count = 1;
      while (j < h.size() - 1 and h[j] == h[j + 1]) {
        j++;
        count++;
      }
      ma = max(ma, v[h[j] - 'a'][count - 1] + 1);
      j++;
    }
    cout << ma << endl;
  }
}
