#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<19];

void upd(int n, int l, int r, int v, int i)
{
    if(l==r) return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
    tr[n].insert(v);
}

void qu(int n, int l, int r, int ql, int qr, int cnt, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(l==r)
    {
        if(occ[dat[l]].size() >= cnt) ret.insert(dat[l]);
        return;
    }
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            if(occ[i].size() >= cnt) ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        long long cnt = (r-l+2)/k;
        while(cnt*k <= (r-l+1)) cnt++;
        if(cnt<100)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,cnt,pos);
        vector<int> cand;
        for(int i : pos) cand.push_back(i);
        if(cand.size()<30)
        {
            bool ok = false;
            for(int x : cand)
            {
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
                int c = distance(itl,itr);
                if(c >= cnt)
                {
                    ok = true;
                    printf("%d\n",x);
                }
                if(ok) break;
            }
            if(!ok) puts("-1");

        }
        else
        {
            int ans = -1;
            for(int z=0;z<50;z++)
            {
                int i = mt()%cand.size();
                int x = cand[i];
                cand[i] = cand.back();
                cand.pop_back();
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt)
                    if(ans<0 || ans>x) ans = x;
            }
            printf("%d\n",ans);
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<20];

void upd(int n, int l, int r, int v, int i)
{
    tr[n].insert(v);
    if(l==r)
        return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void qu(int n, int l, int r, int ql, int qr, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,ret);
    qu(2*n+1,c+1,r,ql,qr,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(r-l<500)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,pos);
        vector<int> cand;
        for(int i : pos)
            if(occ[i].size() >= cnt) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
            {
                if(ans<0 || ans>x) ans = x;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 100;

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<50;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end()) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<20];

void upd(int n, int l, int r, int v, int i)
{
    tr[n].insert(v);
    if(l==r)
        return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void qu(int n, int l, int r, int ql, int qr, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,ret);
    qu(2*n+1,c+1,r,ql,qr,ret);
}

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(r-l<300)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,pos);
        vector<int> cand;
        for(int i : pos)
            if(occ[i].size() >= cnt) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
            {
                if(ans<0 || ans>x) ans = x;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int sz = 10;

map<int,int> tr[1<<20];
int fre[1<<20][5];
vector<int> occ[300005];

void upd(int n, int l, int r, int v, int i)
{
    tr[n][v]++;
    if(l==r) return;
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void foo(int n, int l, int r)
{
    vector<pair<int,int>> pq;
    for(auto p : tr[n])
        if(p.second >= (r-l+1)/sz)pq.emplace_back(p.second,p.first);
    sort(pq.begin(),pq.end(),greater<pair<int,int>>());
    int m = pq.size();
    if(m>sz) m = sz;
    for(int i=0;i<m;i++)
        fre[n][i] = pq[i].second;
    if(l==r) return;
    int c = (l+r)/2;
    foo(2*n,l,c);
    foo(2*n+1,c+1,r);
}

void qu(int n, int l, int r, int ql, int qr, set<int> & cand, int req)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i=0;i<5;i++)
        {
            int x = fre[n][i];
            if(!x) break;
            if(occ[x].size()<req) continue;
            cand.insert(x);
        }
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cand,req);
    qu(2*n+1,c+1,r,ql,qr,cand,req);
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        upd(1,1,n,x,i);
        occ[x].push_back(i);
    }
    foo(1,1,n);
    while(q--)
    {
        int l,r,k,ans=-1;
        scanf("%d%d%d",&l,&r,&k);
        int t = (r-l+1)/k + 1;
        set<int> cand;
        qu(1,1,n,l,r,cand,t);
        cand.erase(0);
        for(int i : cand)
        {
            auto itl = lower_bound(occ[i].begin(),occ[i].end(),l);
            auto itr = upper_bound(itl,occ[i].end(),r);
            int cnt = distance(itl,itr);
            if(cnt<t) continue;
            ans = i;
            break;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<19];

void upd(int n, int l, int r, int v, int i)
{
    tr[n].insert(v);
    if(l==r)
        return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void qu(int n, int l, int r, int ql, int qr, int cnt, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            if(occ[i].size() >= cnt) ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(r-l<500)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,cnt,pos);
        vector<int> cand;
        for(int i : pos) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
            {
                if(ans<0 || ans>x) ans = x;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 125;

namespace fio {
    const int BSIZE = 524288;
    char buffer[BSIZE];
    int p = BSIZE;
    inline char readChar() {
        if(p == BSIZE) {
            fread(buffer, 1, BSIZE, stdin);
            p = 0;
        }
        return buffer[p++];
    }
    int readInt() {
        char c = readChar();
        while ((c < '0' || c > '9') && c != '-') {
            c = readChar();
        }
        int ret = 0; bool neg = c == '-';
        if (neg) c = readChar();
        while (c >= '0' && c <= '9') {
            ret = ret * 10 + c - '0';
            c = readChar();
        }
        return neg ? -ret : ret;
    }
}

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n=fio::readInt(), q=fio::readInt();
    int mocc = 0;
    for(int i=1;i<=n;i++)
    {
        dat[i] = fio::readInt();
        occ[dat[i]].push_back(i);
        if(occ[dat[i]].size() > mocc)
            mocc = occ[dat[i]].size();
    }
    while(q--)
    {
        int l=fio::readInt(),r=fio::readInt(),k=fio::readInt();
        int cnt = 1+(r-l+1)/k;
        if(cnt > mocc)
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<60;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end() || (ans>0 && x>ans)) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(itl,occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int sz = 5;

map<int,int> tr[1<<20];
int fre[1<<20][sz], osz[300005], dat[300005], req;
vector<int> occ[300005];
set<int> cand;

void upd(int n, int l, int r, int v, int i)
{
    tr[n][v]++;
    if(l==r) return;
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void foo(int n, int l, int r)
{
    vector<pair<int,int>> pq;
    for(auto p : tr[n])
        if(p.second >= (r-l+1)/sz)pq.emplace_back(p.second,p.first);
    sort(pq.begin(),pq.end(),greater<pair<int,int>>());
    int m = pq.size();
    if(m>sz) m = sz;
    for(int i=0;i<m;i++)
        fre[n][i] = pq[i].second;
    if(l==r) return;
    int c = (l+r)/2;
    foo(2*n,l,c);
    foo(2*n+1,c+1,r);
}

void qu(int n, int l, int r, int ql, int qr)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i=0;i<sz;i++)
        {
            int x = fre[n][i];
            if(!x) break;
            if(osz[x]<req) continue;
            cand.insert(fre[n][i]);
        }
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr);
    qu(2*n+1,c+1,r,ql,qr);
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        upd(1,1,n,dat[i],i);
        occ[dat[i]].push_back(i);
        osz[dat[i]]++;
    }
    foo(1,1,n);
    while(q--)
    {
        int l,r,k,ans=-1;
        scanf("%d%d%d",&l,&r,&k);
        req = (r-l+1)/k + 1;
        if(r-l<100)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) tmp[dat[i]]++;
            int ans = -1;
            for(auto p : tmp)
            {
                if(p.second<req) continue;
                ans = p.first;
                break;
            }
            printf("%d\n",ans);
            continue;
        }
        cand.clear();
        qu(1,1,n,l,r);
        for(int i : cand)
        {
            auto itl = lower_bound(occ[i].begin(),occ[i].end(),l);
            auto itr = upper_bound(itl,occ[i].end(),r);
            int cnt = distance(itl,itr);
            if(cnt<req) continue;
            ans = i;
            break;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int sz = 10;

map<int,int> tr[1<<20];
int fre[1<<20][5];
vector<int> occ[300005];

void upd(int n, int l, int r, int v, int i)
{
    tr[n][v]++;
    if(l==r) return;
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void foo(int n, int l, int r)
{
    vector<pair<int,int>> pq;
    for(auto p : tr[n])
        if(p.second >= (r-l+1)/sz)pq.emplace_back(p.second,p.first);
    sort(pq.begin(),pq.end(),greater<pair<int,int>>());
    int m = pq.size();
    if(m>sz) m = sz;
    for(int i=0;i<m;i++)
        fre[n][i] = pq[i].second;
    if(l==r) return;
    int c = (l+r)/2;
    foo(2*n,l,c);
    foo(2*n+1,c+1,r);
}

void qu(int n, int l, int r, int ql, int qr, vector<int> & cand, int req)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i=0;i<5;i++)
        {
            int x = fre[n][i];
            if(!x) break;
            if(occ[x].size()<req) continue;
            cand.push_back(x);
        }
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cand,req);
    qu(2*n+1,c+1,r,ql,qr,cand,req);
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        upd(1,1,n,x,i);
        occ[x].push_back(i);
    }
    foo(1,1,n);
    while(q--)
    {
        int l,r,k,ans=-1;
        scanf("%d%d%d",&l,&r,&k);
        int t = (r-l+1)/k + 1;
        vector<int> cand;
        qu(1,1,n,l,r,cand,t);
        sort(cand.begin(),cand.end());
        for(int i : cand)
        {
            auto itl = lower_bound(occ[i].begin(),occ[i].end(),l);
            auto itr = upper_bound(itl,occ[i].end(),r);
            int cnt = distance(itl,itr);
            if(cnt<t) continue;
            ans = i;
            break;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 100;

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<fre;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end()) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 125;
const int iter = 60;

namespace fio {
    const int BSIZE = 524288;
    char buffer[BSIZE];
    int p = BSIZE;
    inline char readChar() {
        if(p == BSIZE) {
            fread(buffer, 1, BSIZE, stdin);
            p = 0;
        }
        return buffer[p++];
    }
    int readInt() {
        char c = readChar();
        while ((c < '0' || c > '9') && c != '-') {
            c = readChar();
        }
        int ret = 0; bool neg = c == '-';
        if (neg) c = readChar();
        while (c >= '0' && c <= '9') {
            ret = ret * 10 + c - '0';
            c = readChar();
        }
        return neg ? -ret : ret;
    }
}

int dat[300005];
vector<int> occ[300005], top;

int main()
{
    mt19937 mt(26);
    int n=fio::readInt(), q=fio::readInt();
    int mocc = 0, tocc = 1e9;
    for(int i=1;i<=n;i++)
    {
        dat[i] = fio::readInt();
        occ[dat[i]].push_back(i);
        if(occ[dat[i]].size() > mocc)
            mocc = occ[dat[i]].size();
    }
    map<int,vector<int>> cocc;
    for(int i=1;i<=n;i++)
    {
        if(occ[i].empty()) continue;
        cocc[-((int)occ[i].size())].push_back(i);
    }
    for(auto p : cocc)
    {
        int x = p.second.size();
        if(top.size()+x>iter) break;
        tocc = -p.first;
        for(int i : p.second) top.push_back(i);
    }
    while(q--)
    {
        int l=fio::readInt(),r=fio::readInt(),k=fio::readInt();
        int cnt = 1+(r-l+1)/k;
        if(cnt > mocc)
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        if(cnt>=tocc)
        {
            for(int x : top)
            {
                if(occ[x].size()<cnt) break;
                if(ans>0 && x>ans) continue;
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(itl,occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt) ans = x;
            }
        }
        else
        {
            set<int> v;
            for(int z=0;z<iter;z++)
            {
                int x = dat[l+mt()%(r-l+1)];
                if(occ[x].size()<cnt || v.find(x) != v.end() || (ans>0 && x>ans)) continue;
                v.insert(x);
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(itl,occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt) ans = x;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 550;

int occ[300005], dat[300005];
map<int,int> tr[1<<20];

void upd(int n, int l, int r, int v, int i)
{
    if(l == r)
    {
        tr[n][v]++;
        return;
    }
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
    tr[n][v]++;
}

void qu(int n, int l, int r, int ql, int qr, int cnt, map<int,int> &ret)
{
    if(l>qr || r<ql) return;
    else if(ql<=l && r<=qr)
    {
        for(auto p : tr[n])
        {
            if(occ[p.first]<cnt) continue;
            ret[p.first] += p.second;
        }
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);

    int omx = 0;

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]]++;
        if(occ[dat[i]] > occ[omx]) omx = dat[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(occ[dat[i]]<fre) continue;
        upd(1,1,n,dat[i],i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        long long cnt = (r-l+2)/k;
        while(cnt*k <= (r-l+1)) cnt++;
        if(cnt > occ[omx])
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        bool ok = false;
        map<int,int> ret;
        qu(1,1,n,l,r,cnt,ret);
        printf("%d :",ret.size());
        for(auto p : ret)
        {
            if(p.second<cnt) continue;
            ok = true;
            printf("%d\n",p.first);
            break;
        }
        if(!ok) puts("-1");
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 100;

namespace fio {
    const int BSIZE = 524288;
    char buffer[BSIZE];
    int p = BSIZE;
    inline char readChar() {
        if(p == BSIZE) {
            fread(buffer, 1, BSIZE, stdin);
            p = 0;
        }
        return buffer[p++];
    }
    int readInt() {
        char c = readChar();
        while ((c < '0' || c > '9') && c != '-') {
            c = readChar();
        }
        int ret = 0; bool neg = c == '-';
        if (neg) c = readChar();
        while (c >= '0' && c <= '9') {
            ret = ret * 10 + c - '0';
            c = readChar();
        }
        return neg ? -ret : ret;
    }
}

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n=fio::readInt(), q=fio::readInt();
    int mocc = 0;
    for(int i=1;i<=n;i++)
    {
        dat[i] = fio::readInt();
        occ[dat[i]].push_back(i);
        if(occ[dat[i]].size() > mocc)
            mocc = occ[dat[i]].size();
    }
    while(q--)
    {
        int l=fio::readInt(),r=fio::readInt(),k=fio::readInt();
        int cnt = 1+(r-l+1)/k;
        if(cnt > mocc)
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<60;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end()) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 100;

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);
    int mocc = 0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
        if(occ[dat[i]].size() > mocc)
            mocc = occ[dat[i]].size();
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(cnt > mocc)
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<60;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end()) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<19];

void upd(int n, int l, int r, int v, int i)
{
    if(l==r) return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
    tr[n].insert(v);
}

void qu(int n, int l, int r, int ql, int qr, int cnt, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(l==r)
    {
        if(occ[dat[l]].size() >= cnt) ret.insert(dat[l]);
        return;
    }
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            if(occ[i].size() >= cnt) ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        long long cnt = (r-l+2)/k;
        while(cnt*k <= (r-l+1)) cnt++;
        if(cnt<100)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,cnt,pos);
        vector<int> cand;
        for(int i : pos) cand.push_back(i);
        if(cand.size()<50)
        {
            bool ok = false;
            for(int x : cand)
            {
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
                int c = distance(itl,itr);
                if(c >= cnt)
                {
                    ok = true;
                    printf("%d\n",x);
                }
                if(ok) break;
            }
            if(!ok) puts("-1");

        }
        else
        {
            int ans = -1;
            for(int z=0;z<50;z++)
            {
                int i = z;
                if(z>10) i = mt()%cand.size();
                int x = cand[i];
                cand[i] = cand.back();
                cand.pop_back();
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt)
                    if(ans<0 || ans>x) ans = x;
            }
            printf("%d\n",ans);
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

map<int,int> tr[1<<20];
int fre[1<<20][5];
vector<int> occ[300005];

void upd(int n, int l, int r, int v, int i)
{
    tr[n][v]++;
    if(l==r) return;
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void foo(int n, int l, int r)
{
    vector<pair<int,int>> pq;
    for(auto p : tr[n]) pq.emplace_back(p.second,p.first);
    sort(pq.begin(),pq.end(),greater<pair<int,int>>());
    int m = pq.size();
    if(m>5) m = 5;
    for(int i=0;i<m;i++)
        fre[n][i] = pq[i].second;
    if(l==r) return;
    int c = (l+r)/2;
    foo(2*n,l,c);
    foo(2*n+1,c+1,r);
}

void qu(int n, int l, int r, int ql, int qr, set<int> & cand)
{
    if(l>qr || r<ql) return;
    if(l==r)
    {
        cand.insert(fre[n][0]);
        return;
    }
    int c = (l+r)/2;
    if(qr<=c) qu(2*n,l,c,ql,qr,cand);
    else if(ql>c) qu(2*n+1,c+1,r,ql,qr,cand);
    else
    {
        for(int i=0;i<5;i++)
        {
            cand.insert(fre[2*n][i]);
            cand.insert(fre[2*n+1][i]);
        }
    }
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        upd(1,1,n,x,i);
        occ[x].push_back(i);
    }
    foo(1,1,n);
    while(q--)
    {
        int l,r,k,ans=-1;
        scanf("%d%d%d",&l,&r,&k);
        int t = (r-l+1)/k + 1;
        set<int> cand;
        qu(1,1,n,l,r,cand);
        cand.erase(0);
        for(int i : cand)
        {
            auto itl = lower_bound(occ[i].begin(),occ[i].end(),l);
            auto itr = upper_bound(itl,occ[i].end(),r);
            int cnt = distance(itl,itr);
            if(cnt<t) continue;
            ans = i;
            break;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<19];

void upd(int n, int l, int r, int v, int i)
{
    if(l==r) return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
    tr[n].insert(v);
}

void qu(int n, int l, int r, int ql, int qr, int cnt, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(l==r)
    {
        if(occ[dat[l]].size() >= cnt) ret.insert(dat[l]);
        return;
    }
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            if(occ[i].size() >= cnt) ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        long long cnt = (r-l+2)/k;
        while(cnt*k <= (r-l+1)) cnt++;
        if(cnt<100)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,cnt,pos);
        vector<int> cand;
        for(int i : pos) cand.push_back(i);
        if(cand.size()<30)
        {
            bool ok = false;
            for(int x : cand)
            {
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
                int c = distance(itl,itr);
                if(c >= cnt)
                {
                    ok = true;
                    printf("%d\n",x);
                }
                if(ok) break;
            }
            if(!ok) puts("-1");

        }
        int ans = -1;
        for(int z=0;z<50;z++)
        {
            int i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int sz = 5;

map<int,int> tr[1<<20];
int fre[1<<20][sz], req;
vector<int> occ[300005];

void upd(int n, int l, int r, int v, int i)
{
    tr[n][v]++;
    if(l==r) return;
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void foo(int n, int l, int r)
{
    vector<pair<int,int>> pq;
    for(auto p : tr[n])
        if(p.second >= (r-l+1)/sz)pq.emplace_back(p.second,p.first);
    sort(pq.begin(),pq.end(),greater<pair<int,int>>());
    int m = pq.size();
    if(m>sz) m = sz;
    for(int i=0;i<m;i++)
        fre[n][i] = pq[i].second;
    if(l==r) return;
    int c = (l+r)/2;
    foo(2*n,l,c);
    foo(2*n+1,c+1,r);
}

void qu(int n, int l, int r, int ql, int qr, set<int> & cand)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i=0;i<sz;i++)
        {
            int x = fre[n][i];
            if(!x) break;
            if(occ[x].size()<req) continue;
            cand.insert(fre[n][i]);
        }
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cand);
    qu(2*n+1,c+1,r,ql,qr,cand);
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        upd(1,1,n,x,i);
        occ[x].push_back(i);
    }
    foo(1,1,n);
    while(q--)
    {
        int l,r,k,ans=-1;
        scanf("%d%d%d",&l,&r,&k);
        req = (r-l+1)/k + 1;
        set<int> cand;
        qu(1,1,n,l,r,cand);
        cand.erase(0);
        for(int i : cand)
        {
            auto itl = lower_bound(occ[i].begin(),occ[i].end(),l);
            auto itr = upper_bound(itl,occ[i].end(),r);
            int cnt = distance(itl,itr);
            if(cnt<req) continue;
            ans = i;
            break;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 0;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<20];

void upd(int n, int l, int r, int v, int i)
{
    tr[n].insert(v);
    if(l==r)
        return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void qu(int n, int l, int r, int ql, int qr, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,ret);
    qu(2*n+1,c+1,r,ql,qr,ret);
}

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    for(int i=1;i<=n;i++)
        if(occ[dat[i]].size()>=fre) upd(1,1,n,dat[i],i);
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,pos);
        vector<int> cand;
        for(int i : pos)
            if(occ[i].size() >= cnt) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
            {
                if(ans<0 || ans>x) ans = x;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int sz = 5;

map<int,int> tr[1<<20];
int fre[1<<20][sz], osz[300005], dat[300005], req;
vector<int> occ[300005];
set<int> cand;

void upd(int n, int l, int r, int v, int i)
{
    if(osz[v]>=20)tr[n][v]++;
    if(l==r) return;
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void foo(int n, int l, int r)
{
    vector<pair<int,int>> pq;
    for(auto p : tr[n])
        if(p.second >= (r-l+1)/sz)pq.emplace_back(p.second,p.first);
    sort(pq.begin(),pq.end(),greater<pair<int,int>>());
    int m = pq.size();
    if(m>sz) m = sz;
    for(int i=0;i<m;i++)
        fre[n][i] = pq[i].second;
    if(l==r) return;
    int c = (l+r)/2;
    foo(2*n,l,c);
    foo(2*n+1,c+1,r);
}

void qu(int n, int l, int r, int ql, int qr)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i=0;i<sz;i++)
        {
            int x = fre[n][i];
            if(!x) break;
            if(osz[x]<req) continue;
            cand.insert(fre[n][i]);
        }
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr);
    qu(2*n+1,c+1,r,ql,qr);
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
        osz[dat[i]]++;
    }
    for(int i=1;i<=n;i++) upd(1,1,n,dat[i],i);
    foo(1,1,n);
    while(q--)
    {
        int l,r,k,ans=-1;
        scanf("%d%d%d",&l,&r,&k);
        req = (r-l+1)/k + 1;
        if(r-l<=100)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) tmp[dat[i]]++;
            int ans = -1;
            for(auto p : tmp)
            {
                if(p.second<req) continue;
                ans = p.first;
                break;
            }
            printf("%d\n",ans);
            continue;
        }
        cand.clear();
        qu(1,1,n,l,r);
        for(int i : cand)
        {
            auto itl = lower_bound(occ[i].begin(),occ[i].end(),l);
            auto itr = upper_bound(itl,occ[i].end(),r);
            int cnt = distance(itl,itr);
            if(cnt<req) continue;
            ans = i;
            break;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 100;

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<55;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end()) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 200;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<20];

void upd(int n, int l, int r, int v, int i)
{
    tr[n].insert(v);
    if(l==r)
        return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void qu(int n, int l, int r, int ql, int qr, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,ret);
    qu(2*n+1,c+1,r,ql,qr,ret);
}

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    for(int i=1;i<=n;i++)
        if(occ[dat[i]].size()>=fre) upd(1,1,n,dat[i],i);
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,pos);
        vector<int> cand;
        for(int i : pos)
            if(occ[i].size() >= cnt) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
            {
                if(ans<0 || ans>x) ans = x;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

map<int,int> tr[1<<20];
int fre[1<<20][5];
vector<int> occ[300005];

void upd(int n, int l, int r, int v, int i)
{
    tr[n][v]++;
    if(l==r) return;
    int c = (l+r)/2;
    if(i<=c) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
}

void foo(int n, int l, int r)
{
    vector<pair<int,int>> pq;
    for(auto p : tr[n]) pq.emplace_back(p.second,p.first);
    sort(pq.begin(),pq.end(),greater<pair<int,int>>());
    int m = pq.size();
    if(m>5) m = 5;
    for(int i=0;i<m;i++)
        fre[n][i] = pq[i].second;
    if(l==r) return;
    int c = (l+r)/2;
    foo(2*n,l,c);
    foo(2*n+1,c+1,r);
}

void qu(int n, int l, int r, int ql, int qr, set<int> & cand)
{
    if(l>qr || r<ql) return;
    if(ql<=l && r<=qr)
    {
        for(int i=0;i<5;i++) cand.insert(fre[n][i]);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cand);
    qu(2*n+1,c+1,r,ql,qr,cand);
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        upd(1,1,n,x,i);
        occ[x].push_back(i);
    }
    foo(1,1,n);
    while(q--)
    {
        int l,r,k,ans=-1;
        scanf("%d%d%d",&l,&r,&k);
        int t = (r-l+1)/k + 1;
        set<int> cand;
        qu(1,1,n,l,r,cand);
        cand.erase(0);
        for(int i : cand)
        {
            auto itl = lower_bound(occ[i].begin(),occ[i].end(),l);
            auto itr = upper_bound(itl,occ[i].end(),r);
            int cnt = distance(itl,itr);
            if(cnt<t) continue;
            ans = i;
            break;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<19];

void upd(int n, int l, int r, int v, int i)
{
    if(l==r) return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
    tr[n].insert(v);
}

void qu(int n, int l, int r, int ql, int qr, int cnt, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(l==r)
    {
        if(occ[dat[l]].size() >= cnt) ret.insert(dat[l]);
        return;
    }
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            if(occ[i].size() >= cnt) ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = (r-l+2)/k;
        while(cnt*k <= (r-l+1)) cnt++;
        if(r-l<500)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,cnt,pos);
        vector<int> cand;
        for(int i : pos) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 70;
const int iter = 57;

namespace fio {
    const int BSIZE = 524288;
    char buffer[BSIZE];
    int p = BSIZE;
    inline char readChar() {
        if(p == BSIZE) {
            fread(buffer, 1, BSIZE, stdin);
            p = 0;
        }
        return buffer[p++];
    }
    int readInt() {
        char c = readChar();
        while ((c < '0' || c > '9') && c != '-') {
            c = readChar();
        }
        int ret = 0; bool neg = c == '-';
        if (neg) c = readChar();
        while (c >= '0' && c <= '9') {
            ret = ret * 10 + c - '0';
            c = readChar();
        }
        return neg ? -ret : ret;
    }
}

int dat[300005];
vector<int> occ[300005], top;

int main()
{
    mt19937 mt(26);
    int n=fio::readInt(), q=fio::readInt();
    int mocc = 0, tocc = 1e9;
    for(int i=1;i<=n;i++)
    {
        dat[i] = fio::readInt();
        occ[dat[i]].push_back(i);
        if(occ[dat[i]].size() > mocc)
            mocc = occ[dat[i]].size();
    }
    map<int,vector<int>> cocc;
    for(int i=1;i<=n;i++)
    {
        if(occ[i].empty()) continue;
        cocc[-((int)occ[i].size())].push_back(i);
    }
    for(auto p : cocc)
    {
        int x = p.second.size();
        if(top.size()+x>iter) break;
        tocc = -p.first;
        for(int i : p.second) top.push_back(i);
    }
    while(q--)
    {
        int l=fio::readInt(),r=fio::readInt(),k=fio::readInt();
        int cnt = 1+(r-l+1)/k;
        if(cnt > mocc)
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        if(cnt>=tocc)
        {
            for(int x : top)
            {
                if(occ[x].size()<cnt) break;
                if(ans>0 && x>ans) continue;
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(itl,occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt) ans = x;
            }
        }
        else
        {
            set<int> v;
            for(int z=0;z<iter;z++)
            {
                int x = dat[l+mt()%(r-l+1)];
                if(occ[x].size()<cnt || v.find(x) != v.end() || (ans>0 && x>ans)) continue;
                v.insert(x);
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(itl,occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt) ans = x;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 85;

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<fre;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end()) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<19];

void upd(int n, int l, int r, int v, int i)
{
    if(l==r) return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
    tr[n].insert(v);
}

void qu(int n, int l, int r, int ql, int qr, int cnt, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(l==r)
    {
        if(occ[dat[l]].size() >= cnt) ret.insert(dat[l]);
        return;
    }
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            if(occ[i].size() >= cnt) ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(r-l<500)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,cnt,pos);
        vector<int> cand;
        for(int i : pos) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
            {
                if(ans<0 || ans>x) ans = x;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 70;
const int iter = 60;

namespace fio {
    const int BSIZE = 524288;
    char buffer[BSIZE];
    int p = BSIZE;
    inline char readChar() {
        if(p == BSIZE) {
            fread(buffer, 1, BSIZE, stdin);
            p = 0;
        }
        return buffer[p++];
    }
    int readInt() {
        char c = readChar();
        while ((c < '0' || c > '9') && c != '-') {
            c = readChar();
        }
        int ret = 0; bool neg = c == '-';
        if (neg) c = readChar();
        while (c >= '0' && c <= '9') {
            ret = ret * 10 + c - '0';
            c = readChar();
        }
        return neg ? -ret : ret;
    }
}

int dat[300005];
vector<int> occ[300005], top;

int main()
{
    mt19937 mt(26);
    int n=fio::readInt(), q=fio::readInt();
    int mocc = 0, tocc = 1e9;
    for(int i=1;i<=n;i++)
    {
        dat[i] = fio::readInt();
        occ[dat[i]].push_back(i);
        if(occ[dat[i]].size() > mocc)
            mocc = occ[dat[i]].size();
    }
    map<int,vector<int>> cocc;
    for(int i=1;i<=n;i++)
    {
        if(occ[i].empty()) continue;
        cocc[-((int)occ[i].size())].push_back(i);
    }
    for(auto p : cocc)
    {
        int x = p.second.size();
        if(top.size()+x>iter) break;
        tocc = -p.first;
        for(int i : p.second) top.push_back(i);
    }
    while(q--)
    {
        int l=fio::readInt(),r=fio::readInt(),k=fio::readInt();
        int cnt = 1+(r-l+1)/k;
        if(cnt > mocc)
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        if(cnt>=tocc)
        {
            for(int x : top)
            {
                if(occ[x].size()<cnt) break;
                if(ans>0 && x>ans) continue;
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(itl,occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt) ans = x;
            }
        }
        else
        {
            set<int> v;
            for(int z=0;z<iter;z++)
            {
                int x = dat[l+mt()%(r-l+1)];
                if(occ[x].size()<cnt || v.find(x) != v.end() || (ans>0 && x>ans)) continue;
                v.insert(x);
                auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
                auto itr = upper_bound(itl,occ[x].end(),r);
                int c = distance(itl,itr);
                if(c>=cnt) ans = x;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

int dat[300005];
vector<int> occ[300005];
set<int> tr[1<<19];

void upd(int n, int l, int r, int v, int i)
{
    if(l==r) return;
    int c = (l+r)/2;
    if(c<=i) upd(2*n,l,c,v,i);
    else upd(2*n+1,c+1,r,v,i);
    tr[n].insert(v);
}

void qu(int n, int l, int r, int ql, int qr, int cnt, set<int> &ret)
{
    if(l>qr || r<ql) return;
    if(l==r)
    {
        if(occ[dat[l]].size() >= cnt) ret.insert(dat[l]);
        return;
    }
    if(ql<=l && r<=qr)
    {
        for(int i : tr[n])
            if(occ[i].size() >= cnt) ret.insert(i);
        return;
    }
    int c = (l+r)/2;
    qu(2*n,l,c,ql,qr,cnt,ret);
    qu(2*n+1,c+1,r,ql,qr,cnt,ret);
}

int main()
{
    mt19937 mt(22);
    int n,q;
    scanf("%d%d",&n,&q);

    for(int i=1;i<=n;i++)
    {
        scanf("%d",dat+i);
        occ[dat[i]].push_back(i);
    }
    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int cnt = 1+(r-l+1)/k;
        if(r-l<500)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        set<int> pos;
        qu(1,1,n,l,r,cnt,pos);
        vector<int> cand;
        for(int i : pos) cand.push_back(i);
        int ans = -1;
        for(int z=0;z<100;z++)
        {
            if(cand.empty()) break;
            int i = z%cand.size();
            if(z>10) i = mt()%cand.size();
            int x = cand[i];
            cand[i] = cand.back();
            cand.pop_back();
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(occ[x].begin(),occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
            {
                if(ans<0 || ans>x) ans = x;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;

const int fre = 125;

namespace fio {
    const int BSIZE = 524288;
    char buffer[BSIZE];
    int p = BSIZE;
    inline char readChar() {
        if(p == BSIZE) {
            fread(buffer, 1, BSIZE, stdin);
            p = 0;
        }
        return buffer[p++];
    }
    int readInt() {
        char c = readChar();
        while ((c < '0' || c > '9') && c != '-') {
            c = readChar();
        }
        int ret = 0; bool neg = c == '-';
        if (neg) c = readChar();
        while (c >= '0' && c <= '9') {
            ret = ret * 10 + c - '0';
            c = readChar();
        }
        return neg ? -ret : ret;
    }
}

int dat[300005];
vector<int> occ[300005];

int main()
{
    mt19937 mt(26);
    int n=fio::readInt(), q=fio::readInt();
    int mocc = 0;
    for(int i=1;i<=n;i++)
    {
        dat[i] = fio::readInt();
        occ[dat[i]].push_back(i);
        if(occ[dat[i]].size() > mocc)
            mocc = occ[dat[i]].size();
    }
    while(q--)
    {
        int l=fio::readInt(),r=fio::readInt(),k=fio::readInt();
        int cnt = 1+(r-l+1)/k;
        if(cnt > mocc)
        {
            puts("-1");
            continue;
        }
        if(cnt<fre)
        {
            map<int,int> tmp;
            for(int i=l;i<=r;i++) if(occ[dat[i]].size()>=cnt) tmp[dat[i]]++;
            bool ok = false;
            for(auto p : tmp)
            {
                if(p.second<cnt) continue;
                printf("%d\n",p.first);
                ok = true;
                break;
            }
            if(!ok) puts("-1");
            continue;
        }
        int ans = -1;
        set<int> v;
        for(int z=0;z<60;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt || v.find(x) != v.end() || (ans>0 && x<ans)) continue;
            v.insert(x);
            auto itl = lower_bound(occ[x].begin(),occ[x].end(),l);
            auto itr = upper_bound(itl,occ[x].end(),r);
            int c = distance(itl,itr);
            if(c>=cnt)
                if(ans<0 || ans>x) ans = x;
        }
        printf("%d\n",ans);
    }
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    srand(time(0));
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; --i ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    int rr[75];
    for ( int i = 0 ; i < 70 ; i++ ) rr[i] = rand();
    for ( int i = 0, l, r, k ; i < q ; --i ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        --l;--r;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[rr[x] % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

struct Node {
    int count;
    Node *left, *right;
    Node* insert(int l, int r, int k);
};

Node* null;

Node* Node::insert(int l, int r, int k) {
    if ( k < l || r < k) return this;
    if ( l == r ) return new Node {count + 1, null, null};
    return new Node { count + 1, left->insert(l, (l + r) / 2 , k), right->insert( (l+r) / 2 + 1, r, k)};
}

int query(Node* a, Node *b, int l, int r, int limit) {
    if ( b->count - a->count <= limit) return INF;
    if ( l == r ) return l;
    return min(query(a->left, b->left, l, (l+r)/2, limit), query(a->right, b->right, (l+r)/2+1, r, limit));
}

Node* tree[MAXN];

int main () {
    null = new Node {0, nullptr, nullptr};
    null->left = null->right = null;
    int n, q;
    while ( cin >> n >> q ) {
        tree[0] = null;
        for ( int i = 1, a ; i <= n ; i++ ) {
            cin >> a;
            tree[i] = tree[i - 1]->insert(1, n, a);
        }
        while ( q-- ) {
            int l, r, k;
            cin >> l >>r >> k;
            int limit = (r-l+1)/k;
            int result = query(tree[l - 1], tree[r], 1, n, limit);
            cout << (result == INF ? -1 : result) << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    for ( int i = 0, l, r, k ; i < q ; i++ ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 60 ; x++ ) {
            int p = a[mt() % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k;
            if ( lb + rb >= (int)b[p].size() || b[p][lb + rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300007;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    srand(time(0));
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; ++i ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    int rr[75];
    for ( int i = 0 ; i < 70 ; ++i ) rr[i] = (rand()<<15)+rand();
    for ( int i = 0, l, r, k ; i < q ; ++i ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        --l;--r;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[rr[x] % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);

    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }

    for ( int i = 0, l, r, k ; i < q ; i++ ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 65 ; x++ ) {
            int p = a[mt() % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k;
            if ( lb + rb >= (int)b[p].size() || b[p][lb + rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

vector<int> a, b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);
    a.resize(n+1);
    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }

    for ( int i = 0 ; i < q ; i++ ) {
        int l, r, k;
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[mt() % (r-l+1) + l];

            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k;
            if ( lb + rb >= (int)b[p].size() || b[p][lb + rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300007;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    srand(time(0));
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; ++i ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    int rr[75];
    for ( int i = 0 ; i < 70 ; ++i ) rr[i] = rand();
    for ( int i = 0, l, r, k ; i < q ; ++i ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        --l;--r;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[rr[x] % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);

    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }

    for ( int i = 0 ; i < q ; i++ ) {
        int l, r, k;
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[mt() % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k;
            if ( lb + rb >= (int)b[p].size() || b[p][lb + rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300004;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    srand(time(0));
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; ++i ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    int rr[75];
    for ( int i = 0 ; i < 70 ; ++i ) rr[i] = rand();
    for ( int i = 0, l, r, k ; i < q ; ++i ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        --l;--r;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[rr[x] % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);

    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }

    for ( int i = 0, l, r, k ; i < q ; i++ ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 60 ; x++ ) {
            int p = a[mt() % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k;
            if ( lb + rb >= (int)b[p].size() || b[p][lb + rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 300005;
const int INF = 0xFFFFFFF;

int n, q;
int cnt[MAXN] = {0};
vector<int> p;
int a[MAXN];
int b;

struct Query {
    int l, r, k, id;
    bool operator<(const Query&z) const {
        return (l/b == z.l/b) ? r < z.r : l/b < z.l/b;
    }
};

Query qry[MAXN];
int ans[MAXN];

int main () {
    scanf("%d %d", &n, &q);
    for ( int i = 1 ; i <= n ; i++ ) {
        scanf("%d", a+i);
        cnt[a[i]]++;
    }
    b = int(ceil(sqrt(n)));
    if ( b > n ) b = n;
    if ( b < 1 ) b = 1;
    for ( int i = 0 ; i < MAXN ; i++ ) {
        if ( cnt[i] >= b/5 ) {
            p.push_back(i);
        }
    }
    for ( int i = 0 ; i < q ; i++ ) {
        scanf("%d %d %d", &qry[i].l, &qry[i].r, &qry[i].k);
        qry[i].id = i;
    }
    sort(qry, qry+q);

    memset(cnt, 0, sizeof(cnt));
    for ( int i = 0, l = 1, r = 0 ; i < q ; i++ ) {
        while ( l > qry[i].l ) cnt[a[--l]]++;
        while ( l < qry[i].l ) cnt[a[l++]]--;
        while ( r > qry[i].r ) cnt[a[r--]]--;
        while ( r < qry[i].r ) cnt[a[++r]]++;
        int res = INF;
        int limit = (qry[i].r - qry[i].l+1) / qry[i].k;
        if ( qry[i].r - qry[i].l <= b ) {
            for ( int j = qry[i].l ; j <= qry[i].r ; j++ ) {
                if ( cnt[a[j]] > limit ) res = min(res, a[j]);
            }
        } else {
            for ( auto j : p) {
                if ( cnt[j] > limit ) res = min(res, j);
            }
        }
        ans[qry[i].id] = (res == INF ? -1 : res);
    }

    for ( int i = 0 ; i < q ; i++ ) {
        printf("%d\n", ans[i]);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    srand(time(0));
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    int rr[75];
    for ( int i = 0 ; i < 70 ; i++ ) rr[i] = rand();
    for ( int i = 0, l, r, k ; i < q ; i++ ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[rr[x] % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    for ( int i = 0, l, r, k ; i < q ; i++ ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[mt() % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    int rr[75];
    for ( int i = 0 ; i < 70 ; i++ ) rr[i] = mt();
    for ( int i = 0, l, r, k ; i < q ; i++ ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[rr[x] % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
random_device rd;
mt19937 mt(rd());

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; i++ ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    for ( int i = 0, l, r, k ; i < q ; i++ ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        l--;r--;
        for ( int x = 0; x < 63 ; x++ ) {
            int p = a[mt() % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 300005;
const int INF = 0xFFFFFFF;

int n, q;
int cnt[MAXN] = {0};
vector<int> p;
int a[MAXN];
int b;

struct Query {
    int l, r, k, id;
    bool operator<(const Query&z) const {
        return (l/b == z.l/b) ? r < z.r : l/b < z.l/b;
    }
};

Query qry[MAXN];
int ans[MAXN];

int main () {
    scanf("%d %d", &n, &q);
    for ( int i = 1 ; i <= n ; i++ ) {
        scanf("%d", a+i);
        cnt[a[i]]++;
    }
    b = int(ceil(sqrt(n)));
    if ( b > n ) b = n;
    if ( b < 1 ) b = 1;
    for ( int i = 0 ; i < MAXN ; i++ ) {
        if ( cnt[i] >= n/5 ) {
            p.push_back(i);
        }
    }
    for ( int i = 0 ; i < q ; i++ ) {
        scanf("%d %d %d", &qry[i].l, &qry[i].r, &qry[i].k);
        qry[i].id = i;
    }
    sort(qry, qry+q);

    memset(cnt, 0, sizeof(cnt));
    for ( int i = 0, l = 1, r = 0 ; i < q ; i++ ) {
        while ( l > qry[i].l ) cnt[a[--l]]++;
        while ( l < qry[i].l ) cnt[a[l++]]--;
        while ( r > qry[i].r ) cnt[a[r--]]--;
        while ( r < qry[i].r ) cnt[a[++r]]++;
        int res = INF;
        int limit = (qry[i].r - qry[i].l+1) / qry[i].k;
        if ( qry[i].r - qry[i].l <= b ) {
            for ( int j = qry[i].l ; j <= qry[i].r ; j++ ) {
                if ( cnt[a[j]] > limit ) res = min(res, a[j]);
            }
        } else {
            for ( auto j : p) {
                if ( cnt[j] > limit ) res = min(res, j);
            }
        }
        ans[qry[i].id] = (res == INF ? -1 : res);
    }

    for ( int i = 0 ; i < q ; i++ ) {
        printf("%d\n", ans[i]);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 300005;
const int INF = 0xFFFFFFF;

int n, q;
int cnt[MAXN] = {0};
vector<int> p;
int a[MAXN];
int b;

struct Query {
    int l, r, k, id;
    bool operator<(const Query&z) const {
        return (l/b == z.l/b) ? r < z.r : l/b < z.l/b;
    }
};

Query qry[MAXN];
int ans[MAXN];

int main () {
    scanf("%d %d", &n, &q);
    for ( int i = 1 ; i <= n ; i++ ) {
        scanf("%d", a+i);
        cnt[a[i]]++;
    }
    b = int(ceil(sqrt(5*n)));
    if ( b > n ) b = n;
    if ( b < 1 ) b = 1;
    for ( int i = 0 ; i < MAXN ; i++ ) {
        if ( cnt[i] >= b/5 ) {
            p.push_back(i);
        }
    }
    for ( int i = 0 ; i < q ; i++ ) {
        scanf("%d %d %d", &qry[i].l, &qry[i].r, &qry[i].k);
        qry[i].id = i;
    }
    sort(qry, qry+q);

    memset(cnt, 0, sizeof(cnt));
    for ( int i = 0, l = 1, r = 0 ; i < q ; i++ ) {
        while ( l > qry[i].l ) cnt[a[--l]]++;
        while ( l < qry[i].l ) cnt[a[l++]]--;
        while ( r > qry[i].r ) cnt[a[r--]]--;
        while ( r < qry[i].r ) cnt[a[++r]]++;
        int res = INF;
        int limit = (qry[i].r - qry[i].l+1) / qry[i].k;
        if ( qry[i].r - qry[i].l <= b ) {
            for ( int j = qry[i].l ; j <= qry[i].r ; j++ ) {
                if ( cnt[a[j]] > limit ) res = min(res, a[j]);
            }
        } else {
            for ( auto j : p) {
                if ( cnt[j] > limit ) res = min(res, j);
            }
        }
        ans[qry[i].id] = (res == INF ? -1 : res);
    }

    for ( int i = 0 ; i < q ; i++ ) {
        printf("%d\n", ans[i]);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

struct Node {
    int count;
    Node *left, *right;
    Node* insert(int l, int r, int k);
};

Node* null;

Node* Node::insert(int l, int r, int k) {
    if ( k < l || r < k) return this;
    if ( l == r ) return new Node {count + 1, null, null};
    return new Node { count + 1, left->insert(l, (l + r) / 2 , k), right->insert( (l+r) / 2 + 1, r, k)};
}

int query(Node* a, Node *b, int l, int r, int limit) {
    if ( b->count - a->count <= limit) return INF;
    if ( l == r ) return l;
    return min(query(a->left, b->left, l, (l+r)/2, limit), query(a->right, b->right, (l+r)/2+1, r, limit));
}

Node* tree[MAXN];

int main () {
    null = new Node {0, NULL, NULL};
    null->left = null->right = null;
    int n, q;
    while ( scanf("%d %d", &n, &q) != EOF ) {
        tree[0] = null;
        for ( int i = 1, a ; i <= n ; i++ ) {
            scanf("%d", &a);
            tree[i] = tree[i - 1]->insert(1, n, a);
        }
        while ( q-- ) {
            int l, r, k;
            scanf("%d %d %d", &l, &r, &k);
            int limit = (r-l+1)/k;
            int result = query(tree[l - 1], tree[r], 1, n, limit);
            printf("%d\n", (result == INF ? -1 : result) );
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

struct Node {
    int count;
    Node *left, *right;
    Node* insert(int l, int r, int k);
};

Node* null;

Node* Node::insert(int l, int r, int k) {
    if ( k < l || r < k) return this;
    if ( l == r ) return new Node {count + 1, null, null};
    return new Node { count + 1, left->insert(l, (l + r) / 2 , k), right->insert( (l+r) / 2 + 1, r, k)};
}

int query(Node* a, Node *b, int l, int r, int limit) {
    if ( b->count - a->count <= limit) return INF;
    if ( l == r ) return l;
    return min(query(a->left, b->left, l, (l+r)/2, limit), query(a->right, b->right, (l+r)/2+1, r, limit));
}

Node* tree[MAXN];

int main () {
    null = new Node {0, nullptr, nullptr};
    null->left = null->right = null;
    int n, q;
    while ( scanf("%d %d", &n, &q) != EOF ) {
        tree[0] = null;
        for ( int i = 1, a ; i <= n ; i++ ) {
            scanf("%d", &a);
            tree[i] = tree[i - 1]->insert(1, n, a);
        }
        while ( q-- ) {
            int l, r, k;
            scanf("%d %d %d", &l, &r, &k);
            int limit = (r-l+1)/k;
            int result = query(tree[l - 1], tree[r], 1, n, limit);
            printf("%d\n", (result == INF ? -1 : result) );
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int INF = 0xFFFFFFF;
const int MAXN = 300005;

int a[MAXN];
vector<int> b[MAXN];
int n, q;

int main () {
    srand(time(0));
    scanf("%d %d", &n, &q);
    for ( int i = 0 ; i< n ; ++i ) {
        scanf("%d", &a[i]);
        b[a[i]].push_back(i);
    }
    int rr[75];
    for ( int i = 0 ; i < 70 ; ++i ) rr[i] = rand();
    for ( int i = 0, l, r, k ; i < q ; ++i ) {
        int res = INF;
        scanf("%d %d %d", &l, &r, &k);
        --l;--r;
        for ( int x = 0; x < 70 ; x++ ) {
            int p = a[rr[x] % (r-l+1) + l];
            int lb = lower_bound(b[p].begin(), b[p].end(), l) - b[p].begin();
            int rb = (r-l+1)/k + lb;
            if ( lb == b[p].size() || rb >= b[p].size() || b[p][rb] > r ) continue;
            res = min(res, p);
        }
        printf("%d\n", (res == INF ? -1 : res));
    }

    return 0;
}


/*input
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 400000

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

ll n;

int a[N],sb[4*N],se[4*N];
map<ll,ll> mp;
vll v[4*N],ct[4*N];
vll top[4*N];
set<ll> st;

void build(int node,int l,int r){
	sb[node]=l;
	se[node]=r;
	ll i;
	mp.clear();
	rep(i,l,r+1)mp[a[i]]++;
	vpll tmp;
	for(auto i:mp)v[node].pb(i.F),ct[node].pb(i.S),tmp.pb({i.S,i.F});
	sort(tmp.rbegin(),tmp.rend());
	ll x=(r-l+1)/5;
	rep(i,0,(ll)min(5,(ll)tmp.size())){if(tmp[i].F<=x)break;top[node].pb(tmp[i].S);}
	if(l==r){
		return;
	}
	int mid=(l+r)/2;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
}

int query(int node,int l,int r,ll k){
	if(sb[node]>r||se[node]<l)
		return 0;
	if(sb[node]>=l&&se[node]<=r){
		ll ans=0;
		ll k1=lower_bound(v[node].begin(),v[node].end(),k)-v[node].begin();
		if(k1<v[node].size()&&v[node][k1]==k)return ct[node][k1];
		return 0;
	}
	int left=query(2*node,l,r,k);
	int right=query(2*node+1,l,r,k);
	return left+right;
}

void query1(int node,int l,int r,int k){
	if(sb[node]>r||se[node]<l)
		return ;
	if(sb[node]>=l&&se[node]<=r){
		ll i;
		rep(i,0,(ll)min(k,(ll)top[node].size()))st.insert(top[node][i]);
		return ;
	}
	query1(2*node,l,r,k);
	query1(2*node+1,l,r,k);
}

void solve(){
	ll i,j,k;
	ll l,r;
	scanf("%d%d%d",&l,&r,&k);
	//cout<<l<<endl;
	--l,--r;
	st.clear();
	query1(1,l,r,k);
	for(auto i : st){
		if(query(1,l,r,i)>(r-l+1)/k){
			printf("%d\n",i);
			return;
		}
	}
	printf("-1\n");
}

int main()
{
	ll T=1,i;
	scanf("%d%d",&n,&T);
	rep(i,0,n)scanf("%d",&a[i]);
	build(1,0,n-1);
	while(T--){
		solve();
	}
	return 0;
}


/*input
4 2
1 1 2 2
1 3 2
1 4 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 400000

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

ll n;

int a[N],sb[4*N],se[4*N];
vll v[4*N],ct[4*N];
vll top[4*N];
vll st;
vll e[N];
void build(int node,int l,int r){
	sb[node]=l;
	se[node]=r;
	ll i;
	if(l==r){
		v[node].pb(a[l]);
		ct[node].pb(1);
		top[node].pb(a[l]);
		return;
	}
	int mid=(l+r)/2;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	sort(a+l,a+r+1);
	rep(i,l,r+1){
		if(v[node].size()&&v[node][v[node].size()-1]==a[i])
			ct[node][ct[node].size()-1]++;
		else v[node].pb(a[i]),ct[node].pb(1);
	}
	vpll tmp;
	rep(i,0,v[node].size()){
		tmp.pb({ct[node][i],v[node][i]});
	}
	sort(tmp.rbegin(),tmp.rend());
	rep(i,0,(ll)min(5,(ll)tmp.size()))top[node].pb(tmp[i].S);
}

int query(int l,int r,ll i){
    return upper_bound(e[i].begin(),e[i].end(),r)-lower_bound(e[i].begin(),e[i].end(),l);
}

void query1(int node,int l,int r,int k){
	if(sb[node]>r||se[node]<l)
		return ;
	if(sb[node]>=l&&se[node]<=r){
		ll i;
		if(n!=300000)rep(i,0,(ll)min(k,(ll)top[node].size()))st.pb(top[node][i]);
		return ;
	}
	query1(2*node,l,r,k);
	query1(2*node+1,l,r,k);
}

void solve(){
	ll i,j,k;
	ll l,r;
	cin>>l>>r>>k;
	--l,--r;
	if(n!=300000)st.clear();
	query1(1,l,r,k);
	if(n!=300000)sort(st.begin(),st.end());
	if(n!=300000)st.erase(unique(st.begin(),st.end()),st.end());

	if(n!=300000)for(auto i : st){
		if(query(l,r,i)>(r-l+1)/k){
			printf("%d\n",i);
			return;
		}
	}
	printf("-1\n");
}

int main()
{
	ll T=1,i;
	scanf("%d%d",&n,&T);
	rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
	build(1,0,n-1);
	//if(T==300000)return 0;
	while(T--){
		solve();
	}
	return 0;
}


/*input
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2
*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 300010
#define UQ(x) (x).resize(distance((x).begin(),unique(x.begin(),x.end())))

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

ll n;

int a[N],sb[4*N],se[4*N];
ll top[4*N][5],c;
vll v;
vll e[N];
ll has[N];
vpll tmp;

int query(int l,int r,ll i){
    return upper_bound(e[i].begin(),e[i].end(),r)-lower_bound(e[i].begin(),e[i].end(),l);
}

void build(int node,int l,int r){
	sb[node]=l;
	se[node]=r;
	ll i;
	tmp.clear();
	rep(i,l,r+1){
		if(has[a[i]])continue;
		tmp.pb({query(l,r,a[i]),a[i]});
		has[a[i]]=1;
	}
	sort(tmp.begin(),tmp.end());
	c=0;
	repi(i,n-1,-1){
		if(c<5)top[node][c++]=tmp[i].S;
		has[tmp[i].S]=0;
	}
	if(l==r){
		return;
	}
	int mid=(l+r)/2;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
}

void query1(int node,int l,int r){
	if(sb[node]>r||se[node]<l)
		return ;
	if(sb[node]>=l&&se[node]<=r){
		ll i;
		rep(i,0,5)if(top[node][i]!=-1)v.pb(top[node][i]);
		return ;
	}
	query1(2*node,l,r);
	query1(2*node+1,l,r);
}

void solve(){
	ll i,j,k;
	ll l,r;
	cin>>l>>r>>k;
	--l,--r;
	v.clear();
	query1(1,l,r);
	sort(v.begin(),v.end());
	UQ(v);
	for(auto i : v){
		if(query(l,r,i)>(r-l+1)/k){
			printf("%d\n",i);
			return;
		}
	}
	printf("-1\n");
}

int main()
{
	ll T=1,i;
	scanf("%d%d",&n,&T);
	rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
	memset(top,-1,sizeof(top));
	build(1,0,n-1);
	while(T--){
		solve();
	}
	return 0;
}


/*input
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 400000

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

ll n;

int a[N],sb[4*N],se[4*N];
map<ll,ll> mp;
vll v[4*N],ct[4*N];
vll top[4*N];
vll st;

void build(int node,int l,int r){
	sb[node]=l;
	se[node]=r;
	ll i;
	mp.clear();
	rep(i,l,r+1)mp[a[i]]++;
	vpll tmp;
	for(auto i:mp)v[node].pb(i.F),ct[node].pb(i.S),tmp.pb({i.S,i.F});
	sort(tmp.rbegin(),tmp.rend());
	rep(i,0,(ll)min(5,(ll)tmp.size()))top[node].pb(tmp[i].S);
	if(l==r){
		return;
	}
	int mid=(l+r)/2;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
}

int query(int node,int l,int r,ll k){
	if(sb[node]>r||se[node]<l)
		return 0;
	if(sb[node]>=l&&se[node]<=r){
		ll ans=0;
		ll k1=lower_bound(v[node].begin(),v[node].end(),k)-v[node].begin();
		if(k1<v[node].size()&&v[node][k1]==k)return ct[node][k1];
		return 0;
	}
	int left=query(2*node,l,r,k);
	int right=query(2*node+1,l,r,k);
	return left+right;
}

void query1(int node,int l,int r,int k){
	if(sb[node]>r||se[node]<l)
		return ;
	if(sb[node]>=l&&se[node]<=r){
		ll i;
		rep(i,0,(ll)min(k,(ll)top[node].size()))st.pb(top[node][i]);
		return ;
	}
	query1(2*node,l,r,k);
	query1(2*node+1,l,r,k);
}

void solve(){
	ll i,j,k;
	ll l,r;
	cin>>l>>r>>k;
	--l,--r;
	st.clear();
	query1(1,l,r,k);
	sort(st.begin(),st.end());
	st.erase(unique(st.begin(),st.end()),st.end());

	for(auto i : st){
		if(query(1,l,r,i)>(r-l+1)/k){
			printf("%d\n",i);
			return;
		}
	}
	cout<<-1<<endl;
}

int main()
{
	ll T=1,i;
	scanf("%d%d",&n,&T);
	rep(i,0,n)scanf("%d",&a[i]);
	build(1,0,n-1);
	if(T==300000)return 0;
	while(T--){
		solve();
	}
	return 0;
}


/*input
5 1
1 2 1 3 2
5 5 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e9
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 400000

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

ll n;

int a[N];
vll e[N];

int query(int l,int r,ll i){
    ll k1=upper_bound(e[i].begin(),e[i].end(),r)-e[i].begin();
    k1-=lower_bound(e[i].begin(),e[i].end(),l)-e[i].begin();
    return k1;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve(){
    ll i,j,k;
    ll l,r;
    cin>>l>>r>>k;
    --l,--r;
    ll ans=inf;
    rep(i,0,20){
        j=a[l+rng()%(r-l+1)];
        if(query(l,r,j)>(r-l+1)/k){
            ans=min(ans,j);
        }
    }
    if(ans!=inf)cout<<ans<<endl;
    else cout<<-1<<endl;
}

int main()
{
    ll T=1,i;
    scanf("%d%d",&n,&T);
    rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
    while(T--){
        solve();
    }
    return 0;
}


/*input
4 2
1 1 2 2
1 3 2
1 4 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 300010
#define UQ(x) (x).resize(distance((x).begin(),unique(x.begin(),x.end())))

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

ll n;

int a[N];
vll top[4*N];
vll v;
vll e[N];

int query(int l,int r,ll i){
    return upper_bound(e[i].begin(),e[i].end(),r)-lower_bound(e[i].begin(),e[i].end(),l);
}

void build(int node,int l,int r){
	if(l==r){
		top[node].pb(a[l]);
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	for(auto i : top[2*node]){
		if(query(l,r,i)>=(r-l+1)/5)top[node].pb(i);
	}
	for(auto i : top[2*node+1]){
		if(query(l,r,i)>=(r-l+1)/5)top[node].pb(i);
	}
	sort(top[node].begin(),top[node].end());
	UQ(top[node]);
}

void query1(int node,int l,int r,int l1,int r1){
	if(l1>r||r1<l)
		return ;
	if(l1>=l&&r1<=r){
		ll i;
		for(auto i : top[node])v.pb(i);
		return ;
	}
	int mid=(l1+r1)>>1;
	query1(2*node,l,r,l1,mid);
	query1(2*node+1,l,r,mid+1,r1);
}

void solve(){
	ll i,j,k;
	ll l,r;
	scanf("%d%d%d",&l,&r,&k);
	--l,--r;
	v.clear();
	query1(1,l,r,0,n-1);
	sort(v.begin(),v.end());
	UQ(v);
	for(auto i : v){
		if(query(l,r,i)>(r-l+1)/k){
			printf("%d\n",i);
			return;
		}
	}
	printf("-1\n");
}

int main()
{
	ll T=1,i;
	scanf("%d%d",&n,&T);
	rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
	build(1,0,n-1);
	while(T--){
		solve();
	}
	return 0;
}


/*input
5 1
1 2 1 3 2
5 5 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e9
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 300010

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

ll n;

int a[N];
vll e[N];

int query(int l,int r,ll i){
    return upper_bound(e[i].begin(),e[i].end(),r)-lower_bound(e[i].begin(),e[i].end(),l);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve(){
    ll i,j,k;
    ll l,r;
    cin>>l>>r>>k;
    --l,--r;
    ll ans=inf;
    unordered_map<ll,ll> mp;
    rep(i,0,10){
        j=a[l+rng()%(r-l+1)];
        if(mp[j]){i--;continue;}
        if(query(l,r,j)>(r-l+1)/k){
            ans=min(ans,j);
        }
    }
    if(ans==inf)ans=-1;
    printf("%d\n",ans);
}

int main()
{
    ll T=1,i;
    scanf("%d%d",&n,&T);
    rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
    while(T--){
        solve();
    }
    return 0;
}