#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > lista;
int numprod;
vector<long long int> prod;
int esprimo[100];
int n;
vector<int> memoria[17][5];
int numdigitos[17];
int patron[20];
long long int a[100000];
long long int calcula(int b, int imodulo) {
  long long int modulo = prod[imodulo];
  int pat = 0;
  for (int d = 0; d < numdigitos[b]; d++) pat = pat * (b + 1) + patron[d];
  int &mem = memoria[b][imodulo][pat];
  if (mem != -1) return mem;
  mem = 1;
  int interrogante = -1;
  for (int d = 0; d < numdigitos[b] and interrogante == -1; d++)
    if (patron[d] == b) interrogante = d;
  if (interrogante == -1) {
    int valor = 0;
    for (int d = 0; d < numdigitos[b]; d++) valor = valor * b + patron[d];
    if (valor < n) mem = a[valor] % modulo;
  } else {
    for (int dig = 0; dig < b; dig++) {
      patron[interrogante] = dig;
      mem = (mem * calcula(b, imodulo)) % modulo;
    }
    patron[interrogante] = b;
  }
  return mem;
}
int main() {
  for (int i = 2; i < 100; i++) esprimo[i] = 1;
  lista.push_back(vector<int>());
  prod.push_back(1);
  for (int i = 2; i < 100; i++) {
    if (esprimo[i]) {
      if (prod.back() * i > 1000000000) {
        lista.push_back(vector<int>());
        prod.push_back(1);
      }
      lista.back().push_back(i);
      prod.back() *= i;
      for (int j = i + i; j < 100; j += i) esprimo[j] = 0;
    }
  }
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int b = 2; b <= 16; b++) {
    numdigitos[b] = 0;
    int nn = n;
    int tope = 1;
    while (nn) {
      nn /= b;
      numdigitos[b]++;
      tope *= b + 1;
    }
    for (int imodulo = 0; imodulo < 5; imodulo++)
      memoria[b][imodulo] = vector<int>(tope, -1);
  }
  int m;
  cin >> m;
  for (int cas = 0; cas < m; cas++) {
    long long int b, c;
    string s;
    cin >> b >> s >> c;
    bool imposible = false;
    for (int i = 0; i < int(s.size()) - numdigitos[b] and not imposible; i++)
      imposible = s[i] != '?' and s[i] != '0';
    for (int d = 0; d < numdigitos[b]; d++) {
      if (d < numdigitos[b] - int(s.size()))
        patron[d] = 0;
      else {
        char car = s[int(s.size()) - numdigitos[b] + d];
        if (car == '?')
          patron[d] = b;
        else if (car >= '0' and car <= '9')
          patron[d] = car - '0';
        else
          patron[d] = car - 'A' + 10;
      }
    }
    int sol = -1;
    for (int imodulo = 0; imodulo < 5 and sol == -1; imodulo++) {
      long long int modulo = prod[imodulo];
      long long int pr = 1;
      if (not imposible) pr = calcula(b, imodulo);
      long long int final = (pr + c) % modulo;
      for (int i = 0; i < int(lista[imodulo].size()) and sol == -1; i++) {
        int p = lista[imodulo][i];
        if (final % p == 0) sol = p;
      }
    }
    cout << sol << endl;
  }
}
