#include<bits/stdc++.h>
using namespace std;
int floor_div(int x, int y) {
assert(y != 0);
if (y < 0) {
y = -y;
x = -x;
}
if (x >= 0) return x / y;
return (x + 1) / y - 1;
}
int ceil_div(int x, int y) {
assert(y != 0);
if (y < 0) {
y = -y;
x = -x;
}
if (x <= 0) return x / y;
return (x - 1) / y + 1;
}
void run_case()
{
int n ;
cin>>n;
vector<vector<int>> pos(100001);
int mx = 0;
for(int i = 1 ; i <= n ; i++)
{
int x;
cin>>x;
pos[x].push_back(i);
mx = max(mx , x);
}
int q;
cin>>q;
vector<vector<tuple<int,int,int,int,int ,int>>> adj(100000 + 1);
vector<int> ans(q);
vector<int> qux;
for(int i = 0 ; i < q; i++)
{
int x , k , l , r , s , e;
cin>>x>>k>>l>>r>>s>>e;
qux.push_back(x);
adj[x].push_back({k , l , r , s , e , i});
}
sort(qux.begin() , qux.end());
qux.erase(unique(qux.begin() , qux.end()) , qux.end());
for(auto x : qux)
{
vector<int> v;
for(int y = x ; y <= mx ; y+=x)
{
if(!pos[y].empty())
v.push_back(y);
}
for(auto [k ,l , r , s , e, i] : adj[x])
{
int lo = s , hi = e + 2;
while(lo + 1 < hi)
{
int md = (lo + hi)/2;
int nb = 0;
for(auto &y : v)
{
if(y > r || y < l)
continue;
nb+= lower_bound(pos[y].begin() , pos[y].end() , md) - lower_bound(pos[y].begin() , pos[y].end() , s);
}
if(nb < k)
{
lo = md;
}
else
hi = md;
}
ans[i] = (lo <= e ? lo : -1);
}
}
for(int i = 0 ; i < q ; i++)
{
cout<<ans[i]<<'\n';
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while(t--)
{
run_case();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGZsb29yX2RpdihpbnQgeCwgaW50IHkpIHsKCWFzc2VydCh5ICE9IDApOwoJaWYgKHkgPCAwKSB7CgkJeSA9IC15OwoJC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LmVuZCgpKTsKICAgIGZvcihhdXRvIHggOiBxdXgpCiAgICB7CiAgICAgICAgdmVjdG9yPGludD4gdjsKICAgICAgICBmb3IoaW50IHkgPSB4IDsgeSA8PSBteCA7IHkrPXgpCiAgICAgICAgewogICAgICAgICAgICBpZighcG9zW3ldLmVtcHR5KCkpCiAgICAgICAgICAgICAgICB2LnB1c2hfYmFjayh5KTsKICAgICAgICB9CiAgICAgICAgZm9yKGF1dG8gW2sgLGwgLCByICwgcyAsIGUsIGldIDogYWRqW3hdKQogICAgICAgIHsKICAgICAgICAgICAgaW50IGxvID0gcyAsIGhpID0gZSArIDI7CiAgICAgICAgICAgIHdoaWxlKGxvICsgMSA8IGhpKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpbnQgbWQgPSAobG8gKyBoaSkvMjsKICAgICAgICAgICAgICAgIGludCBuYiA9IDA7CiAgICAgICAgICAgICAgICBmb3IoYXV0byAmeSA6IHYpCiAgICAgICAgICAgICAgICB7ICAgCiAgICAgICAgICAgICAgICAgICAgaWYoeSA+IHIgfHwgeSA8IGwpCiAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgICAgIG5iKz0gbG93ZXJfYm91bmQocG9zW3ldLmJlZ2luKCkgLCBwb3NbeV0uZW5kKCkgLCBtZCkgLSBsb3dlcl9ib3VuZChwb3NbeV0uYmVnaW4oKSAsIHBvc1t5XS5lbmQoKSAsIHMpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYobmIgPCBrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGxvID0gbWQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgaGkgPSBtZDsKICAgICAgICAgICAgfQogICAgICAgICAgICBhbnNbaV0gPSAobG8gPD0gZSA/IGxvIDogLTEpOwogICAgICAgIH0KICAgIH0KICAgIGZvcihpbnQgaSA9IDAgOyBpIDwgcSA7IGkrKykKICAgIHsKICAgICAgICBjb3V0PDxhbnNbaV08PCdcbic7CiAgICB9Cn0gICAKc2lnbmVkIG1haW4oKQp7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CiAgICBjb3V0LnRpZSgwKTsKICAgIGludCB0ID0gMTsKICAgIC8vIGNpbj4+dDsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBydW5fY2FzZSgpOwogICAgfQp9