#define DEBUG 0

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

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#define MOD 998244353

namespace FFT {
    const double TAU = 2*acos(-1.0);
    int base;
    complex<double> w[1 << 19];
    int rev[1 << 19];
    int setBase(int nbase) {
        int i,j;
        base = nbase;
        for (i = 0; i < (1 << base); i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (base-1));
        w[0] = complex<double>(0,0),w[1] = complex<double>(1,0);
        for (i = 1; i < nbase; i++) {
            double a = TAU/((1 << (i+1)));
            for (j = (1 << (i-1)); j < (1 << i); j++) {
                w[j << 1] = w[j];
                double a2 = a*(((j << 1)|1)-(1 << i));
                w[(j << 1)|1] = complex<double>(cos(a2),sin(a2));
            }
        }
        return 0;
    }
    int FFT(vector<complex<double> > &a) {
        int i,j,k;
        int l = 0;
        while ((1 << l) < a.size()) l++;
        int s = base - l;
        for (i = 0; i < a.size(); i++) {
            if (i < (rev[i] >> s)) swap(a[i],a[rev[i] >> s]);
        }
        for (k = 1; k < a.size(); k <<= 1) {
            for (i = 0; i < a.size(); i += (k << 1)) {
                for (j = 0; j < k; j++) {
                    complex<double> z = a[i+j+k]*w[j+k];
                    a[i+j+k] = a[i+j]-z,a[i+j] = a[i+j] + z;
                }
            }
        }
        return 0;
    }
    template<typename T>
    vector<T> multiply(vector<T> A,vector<T> B) {
        int i,n = 1;
        while (n < A.size()+B.size()-1) n <<= 1;
        vector<complex<double> > a(n,0);
        for (i = 0; i < n; i++) a[i] = complex<double>((i < A.size()) ? A[i]:0,(i < B.size()) ? B[i]:0);
        FFT(a);
        complex<double> r(0,-0.25/n);
        for (i = 0; i <= (n >> 1); i++) {
            int j = (n-i) & (n-1);
            complex<double> z = (a[j]*a[j] - conj(a[i]*a[i])) * r;
            if (i != j) a[j] = (a[i]*a[i] - conj(a[j]*a[j])) * r;
            a[i] = z;
        }
        FFT(a);
        vector<T> ans(A.size()+B.size()-1);
        for (i = 0; i < A.size()+B.size()-1; i++) ans[i] = a[i].real()+0.5;
        return ans;
    }
}

vi adjList[100000];
int parent[100000],size[100000];
int doDFS(int u,int p) {
    int i;
    parent[u] = p,size[u] = 1;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) size[u] += doDFS(v,u);
    }
    return size[u];
}
int down[100000],up[100000];
vlli mult(vector<vlli> &v,int l,int r) {
    if (l == r) return v[l];
    else if (l > r) return vlli(1,1);
    int i;
    int mid = (l+r) / 2;
    vlli ll = mult(v,l,mid),rr = mult(v,mid+1,r),ans;
    vlli temp = FFT::multiply<LLI>(ll,rr);
    for (i = 0; i < temp.size(); i++) ans.pb(temp[i] % MOD);
    return ans;
}
int ans = 0;
int doDFS2(int u,int p) {
    int i,o = down[u];
    int s = 0;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            doDFS2(v,u);
            down[u] += down[v],down[u] %= MOD;
            ans += ((LLI) down[v]*up[v]) % MOD,ans %= MOD;
            ans += ((LLI) s*down[v]) % MOD,ans %= MOD;
            s += down[v],s %= MOD;
        }
    }
    return 0;
}
int main() {
    int i;
    int n,k,a,b;
    scanf("%d %d",&n,&k);
    for (i = 0; i < n-1; i++) {
        scanf("%d %d",&a,&b);
        a--,b--;
        adjList[a].pb(b);
        adjList[b].pb(a);
    }
    if (k == 1) {
        printf("%d\n",((LLI) n*(n-1)/2) % MOD);
        return 0;
    }

    int j;
    doDFS(0,-1);
    FFT::setBase(17);
    for (i = 0; i < n; i++) {
        vector<vlli> vv;
        for (j = 0; j < adjList[i].size(); j++) {
            int v = adjList[i][j];
            if (v != parent[i]) {
                vlli vvv(2);
                vvv[0] = size[v],vvv[1] = 1;
                vv.pb(vvv);
            }
        }
        vlli poly = mult(vv,0,(int) vv.size()-1);
        LLI c = 1;
        for (j = 0; j < poly.size(); j++) {
            down[i] += (poly[poly.size()-j-1]*c) % MOD,down[i] %= MOD;
            c *= k-j,c %= MOD;
        }
        if (i > 0) {
            vv.clear();
            for (j = 0; j < adjList[parent[i]].size(); j++) {
                int v = adjList[parent[i]][j];
                if (v == i) continue;
                if (v != parent[parent[i]]) {
                    vlli vvv(2);
                    vvv[0] = size[v],vvv[1] = 1;
                    vv.pb(vvv);
                }
                else {
                    vlli vvv(2);
                    vvv[0] = n-size[parent[i]],vvv[1] = 1;
                    vv.pb(vvv);
                }
            }
            vlli poly = mult(vv,0,(int) vv.size()-1);
            LLI c = 1;
            for (j = 0; j < poly.size(); j++) {
                up[i] += (poly[poly.size()-j-1]*c) % MOD,up[i] %= MOD;
                c *= k-j,c %= MOD;
            }
        }
    }
    doDFS2(0,-1);
    printf("%d\n",ans);

    return 0;
}


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

typedef long long int LL;
typedef unsigned long long uLL;
typedef pair < int , int > ii;
const int inf = 1e9;
const LL INF = 1e18;
const int N = 3e5 + 5;
const int mod = 1000000007;
const double eps = 1e-8;
const double pi = acos( -1.0 );

int n , q;
int a[ N ];

struct node {
        node *l , *r;
        int val;
        node(){
                l = nullptr , r = nullptr , val = 0;
        }
        node( node *l , node *r , int val ):l(l), r(r), val(val){}

        node* update( int b , int e , int pos , int v ) {
                if( b > e || pos > e || pos < b ) return this;
                if( b == e && b == pos ) {
                        node *ret = new node( l , r , val );
                        ret -> val += v;
                        return ret;
                }
                int mid = ( b + e ) >> 1;
                node *ret = new node( );
                if( pos <= mid ) {
                        ret -> r = r;
                        ret -> l = l -> update( b , mid , pos , v );
                }
                else {
                        ret -> l = l;
                        ret -> r = r -> update( mid+1 , e , pos , v );
                }
                ret -> val = ( ret -> l -> val + ret -> r -> val );
                return ret;
        }

        int query( node *prev , int b , int e , int k ) {
                if( b > e ) return -1;
                if( b == e ) {
                        if( val - prev -> val >= k ) return b;
                        else return -1;
                }
                int mid = ( b + e ) >> 1;
                int lefsum = 0;
                if( l ) lefsum += l -> val;
                if( prev -> l ) lefsum -= prev -> l -> val;
                if( lefsum >= k ) {
                        int res = l -> query( prev -> l , b , mid , k );
                        if( res != -1 ) return res;
                }
                int righsum = 0;
                if( r ) righsum += r -> val;
                if( prev -> r ) lefsum -= prev -> r -> val;
                if( righsum >= k ) {
                        int res = r -> query( prev -> r , mid + 1 , e , k );
                        if( res != -1 ) return res;
                }
                return -1;
        }

} *root[ 10*N + 5 ];

int main( int argc , char const *argv[] ) {
        scanf("%d %d",&n,&q);
        for( int i = 1; i <= n; i++ ) scanf("%d",&a[i]);
        node *null = new node( );
        null -> l = null -> r = null;
        root[0] = null;
        for( int i = 1; i <= n; i++ ) {
                root[i] = root[i-1] -> update( 1 , N , a[i] , +1 );
        }
        while( q-- ) {
                int l , r , k;
                scanf("%d %d %d",&l,&r,&k);
                int ans = root[r] -> query( root[l-1] , 1 , N , ((r-l+1)/k+1) );
                printf("%d\n",ans);
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 1e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

const int block = 450;

struct data {
        int l , r , idx , k;
        data(){}
        data( int l , int r , int idx , int k ):
                l(l), r(r), idx(idx), k(k) {}
        bool operator <( const data &q ) const {
                int block_a = l / block;
                int block_b = q.l / block;
                if( block_a == block_b ) return ( r < q.r );
                return ( block_a < block_b );
        }
} Q[ 3*N ];

int l = 1 , r = 0 , n , q;
int a[ 3*N ];
int cnt[ 3*N ];
int ans[ 3*N ];

void Add( int id ) {
        cnt[ a[id] ]++;
}
void Remove( int id ) {
        cnt[ a[id] ]--;
}
void MO( ) {
        sort( Q + 1 , Q + q + 1 );
        for( int i = 1; i <= q; i++ ) {
                while( l < Q[i].l ) Remove( l++ );
                while( l > Q[i].l ) Add( --l );
                while( r < Q[i].r ) Add( ++r );
                while( r > Q[i].r ) Remove( r-- );
                int an = inf;
                for( int trial = 1; trial <= 100; trial++ ) {
                        int num = a[(rand()<<15|rand())%(Q[i].r-Q[i].l+1)+Q[i].l];
                        if( 1LL * cnt[num] * Q[i].k > ( Q[i].r - Q[i].l + 1 ) ) {
                                an = min( an , num );
                        }
                }
                ans[ Q[i].idx ] = an;
        }
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        srand( time(NULL) );
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) cin >> a[i];
        for( int i = 1; i <= q; i++ ) {
                int l , r , k;
                cin >> l >> r >> k;
                Q[i].l = l , Q[i].r = r , Q[i].k = k;
                Q[i].idx = i;
        }
        MO();
        for( int i = 1; i <= q; i++ ) {
                if( ans[i] == inf ) cout << -1 << endl;
                else cout << ans[i] << endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 1e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

const int block = 450;

inline int64_t HilbertOrder( int x, int y, int pow, int rotate ) {
        if (pow == 0) return 0;
        int hpow = 1 << (pow-1);
        int seg = ( x < hpow ) ? ( ( y < hpow ) ? 0 : 3 ) : ( ( y < hpow ) ? 1 : 2 );
        seg = ( seg + rotate ) & 3;
        const int rotateDelta[4] = { 3, 0, 0, 1 };
        int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
        int nrot = ( rotate + rotateDelta[seg] ) & 3;
        int64_t subSquareSize = int64_t(1) << (2*pow - 2);
        int64_t ans = seg * subSquareSize;
        int64_t add = HilbertOrder(nx, ny, pow-1, nrot);
        ans += ( seg == 1 || seg == 2 ) ? add : ( subSquareSize - add - 1 );
        return ans;
}

struct data {
        int l , r , idx , k;
        int64_t ord;
        data(){}
        data( int l , int r , int idx , int k , int64_t ord  ):
                l(l), r(r), idx(idx), k(k) , ord(ord) {}
        bool operator <( const data &q ) const {
                //int block_a = l / block;
                //int block_b = q.l / block;
                //if( block_a == block_b ) return ( r < q.r );
                //return ( block_a < block_b );
                return ord < q.ord;
        }
} Q[ 3*N ];

int l = 1 , r = 0 , n , q;
int a[ 3*N ];
int cnt[ 3*N ];
int ans[ 3*N ];

void Add( int id ) {
        cnt[ a[id] ]++;
}
void Remove( int id ) {
        cnt[ a[id] ]--;
}
void MO( ) {
        sort( Q + 1 , Q + q + 1 );
        for( int i = 1; i <= q; i++ ) {
                while( l < Q[i].l ) Remove( l++ );
                while( l > Q[i].l ) Add( --l );
                while( r < Q[i].r ) Add( ++r );
                while( r > Q[i].r ) Remove( r-- );
                int an = inf;
                for( int trial = 1; trial <= 50; trial++ ) {
                        int num = a[(rand()<<15|rand())%(r-l+1)+l];
                        if( 1LL * cnt[num] * Q[i].k > ( Q[i].r - Q[i].l + 1 ) ) {
                                an = min( an , num );
                        }
                }
                ans[ Q[i].idx ] = an;
        }
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        srand( time(NULL) );
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) cin >> a[i];
        for( int i = 1; i <= q; i++ ) {
                int l , r , k;
                cin >> l >> r >> k;
                Q[i].l = l , Q[i].r = r , Q[i].k = k;
                Q[i].idx = i;
        }
        MO();
        for( int i = 1; i <= q; i++ ) {
                if( ans[i] == inf ) cout << -1 << endl;
                else cout << ans[i] << endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 3e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

int n , q;
int a[ N ];
vector <int> pos[ N ];

int get_cnt( int num , int l , int r ) {
        return upper_bound( pos[num].begin() , pos[num].end() , r ) -
               lower_bound( pos[num].begin() , pos[num].end() , l );
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) {
                cin >> a[i];
                pos[ a[i] ].push_back( i );
        }
        while( q-- ) {
                int l , r , k;
                cin >> l >> r >> k;
                int need = ( r - l + 1 + k - 1 ) / k;
                int ans = inf;
                for( int trial = 1; trial <= 50; trial++ ) {
                        int num = a[ ran( l , r ) ];
                        int cnt = get_cnt( num , l , r );
                        if( cnt >= need ) ans = min( ans , num );
                }
                if( ans == inf ) ans = -1;
                cout << ans <<endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 3e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

int n , q;
int a[ N ];
vector <int> pos[ N ];

int get_cnt( int num , int l , int r ) {
        return upper_bound( pos[num].begin() , pos[num].end() , r ) -
               lower_bound( pos[num].begin() , pos[num].end() , l );
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) {
                cin >> a[i];
                pos[ a[i] ].push_back( i );
        }
        while( q-- ) {
                int l , r , k;
                cin >> l >> r >> k;
                int ans = inf;
                for( int trial = 1; trial <= 80; trial++ ) {
                        int num = a[ ran( l , r ) ];
                        int cnt = get_cnt( num , l , r );
                        if( k * cnt > r - l + 1 ) ans = min( ans , num );
                }
                if( ans == inf ) ans = -1;
                cout << ans <<endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 3e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

int n , q;
int a[ N ];
vector <int> pos[ N ];

int get_cnt( int num , int l , int r ) {
        return upper_bound( pos[num].begin() , pos[num].end() , r ) -
               lower_bound( pos[num].begin() , pos[num].end() , l );
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) {
                cin >> a[i];
                pos[ a[i] ].push_back( i );
        }
        while( q-- ) {
                int l , r , k;
                cin >> l >> r >> k;
                int ans = inf;
                for( int trial = 1; trial <= 100; trial++ ) {
                        int num = a[ ran( l , r ) ];
                        int cnt = get_cnt( num , l , r );
                        if( k * cnt > r - l + 1 ) ans = min( ans , num );
                }
                if( ans == inf ) ans = -1;
                cout << ans <<endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 3e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

int n , q;
int a[ N ];
vector <int> pos[ N ];

int get_cnt( int num , int l , int r ) {
        return upper_bound( pos[num].begin() , pos[num].end() , r ) -
               lower_bound( pos[num].begin() , pos[num].end() , l );
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) {
                cin >> a[i];
                pos[ a[i] ].push_back( i );
        }
        while( q-- ) {
                int l , r , k;
                cin >> l >> r >> k;
                int ans = inf;
                for( int trial = 1; trial <= 50; trial++ ) {
                        int num = a[ ran( l , r ) ];
                        int cnt = get_cnt( num , l , r );
                        if( k * cnt > r - l + 1 ) ans = min( ans , num );
                }
                if( ans == inf ) ans = -1;
                cout << ans <<endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 1e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

const int block = 450;

inline int64_t HilbertOrder( int x, int y, int pow, int rotate ) {
        if (pow == 0) return 0;
        int hpow = 1 << (pow-1);
        int seg = ( x < hpow ) ? ( ( y < hpow ) ? 0 : 3 ) : ( ( y < hpow ) ? 1 : 2 );
        seg = ( seg + rotate ) & 3;
        const int rotateDelta[4] = { 3, 0, 0, 1 };
        int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
        int nrot = ( rotate + rotateDelta[seg] ) & 3;
        int64_t subSquareSize = int64_t(1) << (2*pow - 2);
        int64_t ans = seg * subSquareSize;
        int64_t add = HilbertOrder(nx, ny, pow-1, nrot);
        ans += ( seg == 1 || seg == 2 ) ? add : ( subSquareSize - add - 1 );
        return ans;
}

struct data {
        int l , r , idx , k;
        int64_t ord;
        data(){}
        data( int l , int r , int idx , int k , int64_t ord  ):
                l(l), r(r), idx(idx), k(k) , ord(ord) {}
        bool operator <( const data &q ) const {
                //int block_a = l / block;
                //int block_b = q.l / block;
                //if( block_a == block_b ) return ( r < q.r );
                //return ( block_a < block_b );
                return ord < q.ord;
        }
} Q[ 3*N ];

int l = 1 , r = 0 , n , q;
int a[ 3*N ];
int cnt[ 3*N ];
int ans[ 3*N ];

void Add( int id ) {
        cnt[ a[id] ]++;
}
void Remove( int id ) {
        cnt[ a[id] ]--;
}
void MO( ) {
        sort( Q + 1 , Q + q + 1 );
        for( int i = 1; i <= q; i++ ) {
                while( l < Q[i].l ) Remove( l++ );
                while( l > Q[i].l ) Add( --l );
                while( r < Q[i].r ) Add( ++r );
                while( r > Q[i].r ) Remove( r-- );
                int an = inf;
                for( int trial = 1; trial <= 50; trial++ ) {
                        int num = a[(rand()<<15|rand())%(Q[i].r-Q[i].l+1)+Q[i].l];
                        if( 1LL * cnt[num] * Q[i].k > ( Q[i].r - Q[i].l + 1 ) ) {
                                an = min( an , num );
                        }
                }
                ans[ Q[i].idx ] = an;
        }
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        srand( time(NULL) );
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) cin >> a[i];
        for( int i = 1; i <= q; i++ ) {
                int l , r , k;
                cin >> l >> r >> k;
                Q[i].l = l , Q[i].r = r , Q[i].k = k;
                Q[i].idx = i;
        }
        for( int i = 1; i <= q; i++ ) Q[i].ord = HilbertOrder( Q[i].l , Q[i].r , 21 , 0 );
        MO();
        for( int i = 1; i <= q; i++ ) {
                if( ans[i] == inf ) cout << -1 << endl;
                else cout << ans[i] << endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 1e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

const int block = 450;

struct data {
        int l , r , idx , k;
        data(){}
        data( int l , int r , int idx , int k ):
                l(l), r(r), idx(idx), k(k) {}
        bool operator <( const data &q ) const {
                int block_a = l / block;
                int block_b = q.l / block;
                if( block_a == block_b ) return ( r < q.r );
                return ( block_a < block_b );
        }
} Q[ 3*N ];

int l = 1 , r = 0 , n , q;
int a[ 3*N ];
int cnt[ 3*N ];
int ans[ 3*N ];

void Add( int id ) {
        cnt[ a[id] ]++;
}
void Remove( int id ) {
        cnt[ a[id] ]--;
}
void MO( ) {
        sort( Q + 1 , Q + q + 1 );
        for( int i = 1; i <= q; i++ ) {
                while( l < Q[i].l ) Remove( l++ );
                while( l > Q[i].l ) Add( --l );
                while( r < Q[i].r ) Add( ++r );
                while( r > Q[i].r ) Remove( r-- );
                int an = inf;
                for( int trial = 1; trial <= 50; trial++ ) {
                        int num = a[(rand()<<15|rand())%(r-l+1)+l];
                        if( 1LL * cnt[num] * Q[i].k > ( Q[i].r - Q[i].l + 1 ) ) {
                                an = min( an , num );
                        }
                }
                ans[ Q[i].idx ] = an;
        }
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        srand( time(NULL) );
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) cin >> a[i];
        for( int i = 1; i <= q; i++ ) {
                int l , r , k;
                cin >> l >> r >> k;
                Q[i].l = l , Q[i].r = r , Q[i].k = k;
                Q[i].idx = i;
        }
        MO();
        for( int i = 1; i <= q; i++ ) {
                if( ans[i] == inf ) cout << -1 << endl;
                else cout << ans[i] << endl;
        }
        return 0;
}


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

typedef long long int LL;
typedef unsigned long long uLL;
typedef pair < int , int > ii;
const int inf = 1e9;
const LL INF = 1e18;
const int N = 3e5 + 5;
const int mod = 1000000007;
const double eps = 1e-8;
const double pi = acos( -1.0 );

int n , q;
int a[ N ];

struct node {
        node *l , *r;
        int val;
        node(){
                l = nullptr , r = nullptr , val = 0;
        }
        node( node *l , node *r , int val ):l(l), r(r), val(val){}

        node* update( int b , int e , int pos , int v ) {
                if( b > e || pos > e || pos < b ) return this;
                if( b == e && b == pos ) {
                        node *ret = new node( l , r , val );
                        ret -> val += v;
                        return ret;
                }
                int mid = ( b + e ) >> 1;
                node *ret = new node( );
                if( pos <= mid ) {
                        ret -> r = r;
                        ret -> l = l -> update( b , mid , pos , v );
                }
                else {
                        ret -> l = l;
                        ret -> r = r -> update( mid+1 , e , pos , v );
                }
                ret -> val = ( ret -> l -> val + ret -> r -> val );
                return ret;
        }

        int query( node *prev , int b , int e , int k ) {
                if( b > e ) return -1;
                if( b == e ) {
                        if( val - prev -> val >= k ) return b;
                        else return -1;
                }
                int mid = ( b + e ) >> 1;
                int lefsum = 0;
                if( l ) lefsum += l -> val;
                if( prev -> l ) lefsum -= prev -> l -> val;
                if( lefsum >= k ) {
                        int res = l -> query( prev -> l , b , mid , k );
                        if( res != -1 ) return res;
                }
                int righsum = 0;
                if( r ) righsum += r -> val;
                if( prev -> r ) lefsum -= prev -> r -> val;
                if( righsum >= k ) {
                        int res = r -> query( prev -> r , mid + 1 , e , k );
                        if( res != -1 ) return res;
                }
                return -1;
        }

} *root[ 10*N + 5 ];

int main( int argc , char const *argv[] ) {
        scanf("%d %d",&n,&q);
        for( int i = 1; i <= n; i++ ) scanf("%d",&a[i]);
        node *null = new node( );
        null -> l = null -> r = null;
        root[0] = null;
        for( int i = 1; i <= n; i++ ) {
                root[i] = root[i-1] -> update( 1 , N , a[i] , +1 );
        }
        while( q-- ) {
                int l , r , k;
                cin >> l >> r >> k;
                cout << root[r] -> query( root[l-1] , 1 , N , ((r-l+1)/k+1) ) << endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 3e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

int n , q;
int a[ N ];
vector <int> pos[ N ];

int get_cnt( int num , int l , int r ) {
        return upper_bound( pos[num].begin() , pos[num].end() , r ) -
               lower_bound( pos[num].begin() , pos[num].end() , l );
}
int get_rand( int l , int r ) {
        return rand()%( r - l + 1 ) + l;
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) {
                cin >> a[i];
                pos[ a[i] ].push_back( i );
        }
        while( q-- ) {
                int l , r , k;
                cin >> l >> r >> k;
                int ans = inf;
                for( int trial = 1; trial <= 40; trial++ ) {
                        int num = a[ get_rand( l , r ) ];
                        int cnt = get_cnt( num , l , r );
                        if( k * cnt > r - l + 1 ) ans = min( ans , num );
                }
                if( ans == inf ) ans = -1;
                cout << ans <<endl;
        }
        return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <typename T> using
Treap = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define endl "\n"
#define all(V) V.begin( ), V.end( )
#define Unique(V) sort( all( V ) ), V.erase( unique( all( V ) ), V.end() )
std::mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() );
#define ran(a, b) rng() % ( (b) - (a) + 1 ) + (a)
#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template < typename Arg1 >
void __f( const char* name , Arg1&& arg1 ) { cerr <<name<<" : "<<arg1<< "\n"; }
template < typename Arg1 , typename... Args >
void __f( const char* names , Arg1&& arg1 , Args&&... args ) {
const char* comma = strchr( names + 1 , ',' );
cerr.write( names , comma - names ) <<" : "<<arg1<<" , "; __f( comma+1 , args...);}

typedef long long int LL;
typedef pair <int,int> pii;
typedef pair <long long,long long> pll;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const int N = 1e5 + 5;
const int inf = 1e9;
const long long INF = 1e18;

inline int64_t HilbertOrder( int x, int y, int pow, int rotate ) {
        if (pow == 0) return 0;
        int hpow = 1 << (pow-1);
        int seg = ( x < hpow ) ? ( ( y < hpow ) ? 0 : 3 ) : ( ( y < hpow ) ? 1 : 2 );
        seg = ( seg + rotate ) & 3;
        const int rotateDelta[4] = { 3, 0, 0, 1 };
        int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
        int nrot = ( rotate + rotateDelta[seg] ) & 3;
        int64_t subSquareSize = int64_t(1) << (2*pow - 2);
        int64_t ans = seg * subSquareSize;
        int64_t add = HilbertOrder(nx, ny, pow-1, nrot);
        ans += ( seg == 1 || seg == 2 ) ? add : ( subSquareSize - add - 1 );
        return ans;
}

struct data {
        int l , r , idx , k;
        int64_t ord;
        data(){}
        data( int l , int r , int idx , int k , int64_t ord  ):
                l(l), r(r), idx(idx), k(k) , ord(ord) {}
        bool operator <( const data &q ) const {
                //int block_a = l / block;
                //int block_b = q.l / block;
                //if( block_a == block_b ) return ( r < q.r );
                //return ( block_a < block_b );
                return ord < q.ord;
        }
} Q[ 3*N ];

int l = 1 , r = 0 , n , q;
int a[ 3*N ];
int cnt[ 3*N ];
int ans[ 3*N ];

void Add( int id ) {
        cnt[ a[id] ]++;
}
void Remove( int id ) {
        cnt[ a[id] ]--;
}
void MO( ) {
        sort( Q + 1 , Q + q + 1 );
        for( int i = 1; i <= q; i++ ) {
                while( l < Q[i].l ) Remove( l++ );
                while( l > Q[i].l ) Add( --l );
                while( r < Q[i].r ) Add( ++r );
                while( r > Q[i].r ) Remove( r-- );
                int an = inf;
                for( int trial = 1; trial <= 50; trial++ ) {
                        int num = a[(rand()<<15|rand())%(r-l+1)+l];
                        if( 1LL * cnt[num] * Q[i].k > ( Q[i].r - Q[i].l + 1 ) ) {
                                an = min( an , num );
                        }
                }
                ans[ Q[i].idx ] = an;
        }
}
int main( int argc , char const *argv[] ) {
        ios_base::sync_with_stdio( false ); cin.tie( nullptr );
        #ifdef forthright48
            freopen( "input.txt" , "r" , stdin );
        #endif // forthright48
        srand( time(NULL) );
        cin >> n >> q;
        for( int i = 1; i <= n; i++ ) cin >> a[i];
        for( int i = 1; i <= q; i++ ) {
                int l , r , k;
                cin >> l >> r >> k;
                Q[i].l = l , Q[i].r = r , Q[i].k = k;
                Q[i].idx = i;
        }
        MO();
        for( int i = 1; i <= q; i++ ) {
                if( ans[i] == inf ) cout << -1 << endl;
                else cout << ans[i] << endl;
        }
        return 0;
}


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

const int fre = 70;

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<65;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 fre = 50;

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])
        if(p.second >= (r-l+1)/5)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;

const int fre = 30;

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(i<=c) 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<fre;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 = 400;

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 fre = 150;

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++) if(occ[dat[i]]>=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;
        }
        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 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)
{
    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<sz;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;

const int fre = 110;

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;

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<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;

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

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;
        }
        int ans = -1;
        for(int z=0;z<30;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            if(occ[x].size()<cnt) continue;
            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];

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;
        }
        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<<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<50;z++)
        {
            if(cand.empty()) break;
            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;

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()) 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 fre = 100;

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++) if(occ[dat[i]]>=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;
        }
        bool ok = false;
        map<int,int> ret;
        qu(1,1,n,l,r,cnt,ret);
        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;

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 = 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 = 60;

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++) if(occ[dat[i]]>=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;
        }
        bool ok = false;
        map<int,int> ret;
        qu(1,1,n,l,r,cnt,ret);
        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;

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<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 sz = 5;

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 = 20;

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(i<=c) 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<fre;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];
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(i<=c) 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 fre = 60;

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(i<=c) 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<fre;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][sz];
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(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<sz;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;

const int fre = 70;

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;
        for(int z=0;z<fre;z++)
        {
            int x = dat[l+mt()%(r-l+1)];
            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;

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;
                if(z<=10) break;
            }
        }
        printf("%d\n",ans);
    }
}


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

const int fre = 40;

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(i<=c) 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<fre;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 = 150;

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++) if(occ[dat[i]]>=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;
        }
        bool ok = false;
        map<int,int> ret;
        qu(1,1,n,l,r,cnt,ret);
        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;

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 fre = 200;

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;

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, 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);
    }
}