#include <bits/stdc++.h>
using namespace std;
const int NN = 33;
int n;
int prime[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 41,
               43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
int mod[] = {300690390, 2044926619, 1808691197, 2073080791};
int id[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1,
            1, 1, 2, 2, 3, 2, 3, 3, 3, 3, 2, 2};
int *dp[NN][4], p[NN], L[NN], t[NN];
long long a[10010];
void prepare(int b) {
  int B = b + 1;
  int mx = 1, s = 1;
  while (s < n) s *= b, mx *= B, L[b]++;
  for (int i = 0; i < 4; i++) dp[b][i] = new int[mx + 1];
  for (int i = p[0] = 1; i < L[b]; i++) p[i] = p[i - 1] * B;
  for (int i = 0; i < mx; i++) {
    int mid = i, pp = -1;
    for (int j = 0; j < L[b]; j++) {
      t[j] = mid % B;
      mid /= B;
      if (t[j] == b) pp = j;
    }
    for (int j = 0; j < 4; j++) dp[b][j][i] = 1;
    if (pp == -1) {
      int now = 0;
      for (int j = 0; j < L[b]; j++) {
        now = now * b + t[L[b] - 1 - j];
      }
      if (now < n) {
        for (int j = 0; j < 4; j++) dp[b][j][i] = a[now] % mod[j];
      }
    } else {
      for (int j = 0; j < 4; j++) {
        for (int k = 1; k <= b; k++) {
          dp[b][j][i] =
              (long long)dp[b][j][i] * dp[b][j][i - k * p[pp]] % mod[j];
        }
      }
    }
  }
}
char s[111];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 2; i <= 16; i++) prepare(i);
  int m, d;
  cin >> m;
  long long c;
  while (m--) {
    cin >> d >> s >> c;
    int mx = 0, len = strlen(s);
    int ok = 1;
    for (int i = 0; i < len - L[d]; i++)
      if (s[i] != '?') ok = 0;
    for (int i = max(0, len - L[d]); i < len; i++) {
      mx = mx * (d + 1) + (s[i] == '?'   ? d
                           : s[i] <= '9' ? s[i] - '0'
                                         : s[i] - 'A' + 10);
    }
    int ans = -1;
    for (int i = 0; i < 25; i++) {
      if ((c + dp[d][id[i]][mx]) % prime[i] == 0) {
        ans = prime[i];
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
