// You are using GCC
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;

int main(){
    int n;
    cin >> n;

    vector<int> arr(n);
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }

    int m;
    cin >> m;

    for(int i = 0; i < m; i++){
        int start, end, k;
        cin >> start >> end >> k;

        // IMPORTANT: If your input is 1-based, convert:
        // start--; end--;

        int result = -1;

        // First find any element divisible by k
        for(int j = start; j <= end; j++){
            if(arr[j] % k == 0){
                result = arr[j];
                break;
            }
        }

        // If none divisible, print -1
        if(result == -1){
            cout << -1 << endl;
            continue;
        }

        // Now compute gcd of all divisible elements
        for(int j = start; j <= end; j++){
            if(arr[j] % k == 0){
                result = gcd(result, arr[j]);
            }
        }

        cout << result << endl;
    }
}