#include<cassert>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include<vector>
#include<utility>

using namespace std;

#define ll long long
#define ld long double
#define X first
#define Y second
#define SZ(x) ((int)(x).size())
#define LN(x) ((int)(x).length())
#define rz(x) resize(x)
#define reset(x,n) (x).clear(), (x).resize(n)
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vint vector<int>
#define el putchar('\n')
#define spc putchar(' ')
#define SS stringstream
#define PQ priority_queue
#define PRF(...) printf(__VA_ARGS__)
#define MS(x,v) memset((x),(v),sizeof(x))
#define MS0(x) memset((x),0,sizeof(x))
#define MSB(x) memset((x),0x3f,sizeof(x))
#define MSBB(x) memset((x),0x7f,sizeof(x))
#define MSM(x) memset((x),0xff,sizeof(x))
#define MSMB(x) memset((x),0x80,sizeof(x))
#define PAR(x,n) for(int ___=0;___<(n);++___)cout<<x[___]<<" ";cout<<'\n';
#define PAR1(x,n) for(int ___=1;___<=(n);++___)cout<<x[___]<<" ";cout<<'\n';
#define CIO ios_base::sync_with_stdio(0);
#define FLH fflush(stdout)

#define tm Ovuvuevuevue
#define y1 Enyetuenwuevue
#define left Ugbemugbem
#define Osas

#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl

#ifdef WEAK
#define PDE1(a) cout<<#a<<" = "<<(a)<<'\n'
#define PDE2(a,b) cout<<#a<<" = "<<(a)<<" , ", PDE1(b)
#define PDE3(a,b,c) cout<<#a<<" = "<<(a)<<" , ", PDE2(b,c)
#define PDE4(a,b,c,d) cout<<#a<<" = "<<(a)<<" , ", PDE3(b,c,d)
#define PDE5(a,b,c,d,e) cout<<#a<<" = "<<(a)<<" , ", PDE4(b,c,d,e)
#define DEB(...) printf(__VA_ARGS__),fflush(stdout)
#define WHR() printf("%s: Line %d",__PRETTY_FUNCTION__,__LINE__),fflush(stdout)
#define LOG(...) printf("%s: Line %d ",__PRETTY_FUNCTION__,__LINE__),printf(__VA_ARGS__),fflush(stdout)
#define FIN freopen("in","r",stdin)
#define FOUT freopen("out","w",stdout)
#define DEBUG "jizz"
#else
#define PDE1(a) ;
#define PDE2(a,b) ;
#define PDE3(a,b,c) ;
#define PDE4(a,b,c,d) ;
#define PDE5(a,b,c,d,e) ;
#define DEB(...) ;
#define WHR() ;
#define LOG(...) ;
#define endl '\n'
#define getchar gtx
#define DEBUG 0
#ifdef WEA
#define FIN freopen("in","r",stdin)
#define FOUT freopen("out","w",stdout)
#else
#define FIN ;
#define FOUT ;
#endif
#endif

template<typename TA,typename TB> ostream& operator<<(ostream& ostm, const pair<TA,TB> &p){ostm<<"("<<p.X<<","<<p.Y<<")";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm, const vector<T> &v){ostm<<"[ ";for(auto i:v)ostm<<i<<" ";ostm<<"]";return ostm;}
template<typename TA,typename TB> ostream& operator<<(ostream &ostm, const map<TA,TB> &mp){ostm<<"[ ";for(auto &it:mp)ostm<<it<<" ";ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const set<T> &s){ostm<<"[ ";for(auto &it:s)ostm<<it<<" ";ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const stack<T> &inp){stack<T> st=inp;ostm<<"[ ";while(!st.empty()){ostm<<st.top()<<" ";st.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const queue<T> &inp){queue<T> q=inp;ostm<<"[ ";while(!q.empty()){ostm<<q.front()<<" ";q.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const priority_queue<T> &inp){priority_queue<T> pq=inp;ostm<<"[ ";while(!pq.empty()){ostm<<pq.top()<<" ";pq.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const deque<T> &inp){deque<T> dq=inp;ostm<<"[ ";while(!dq.empty()){ostm<<dq.front()<<" ";dq.pop_front();}ostm<<"]";return ostm;}

#define lowbit(x) ((x)&(-(x)))

inline int gtx(){
    const int N=1048576;
    static char __buffer[N];
    static char *__p=__buffer,*__end=__buffer;
    if(__p==__end){
        if((__end=__buffer+fread(__buffer,1,N,stdin))==__buffer)return EOF;
        __p=__buffer;
    } return *__p++;
}

template<typename T>
inline bool rit(T &x){
    char c=0; bool fg=0;
    while(c=getchar(), (c<'0' && c!='-') || c>'9')if(c==EOF)return false;
    c=='-' ? (fg=1,x=0) : (x=c&15);
    while(c=getchar(), c>='0' && c<='9')x=x*10+(c&15);
    if(fg)x=-x; return true;
}
template<typename T,typename ...Args>
inline bool rit(T& x,Args& ...args){return rit(x)&&rit(args...);}

inline void pit(int x){printf("%d",x);}
inline void pln(ll x){printf("%I64d",x);}
template<typename ...Args>
inline void pit(int x,Args ...args){printf("%d ",x);pit(args...);}
template<typename ...Args>
inline void pln(ll x,Args ...args){printf("%I64d ",x);pit(args...);}
void JIZZ(){cout<<"";exit(0);}

const ld PI=3.14159265358979323846264338327950288;
const ld eps=1e-8;
const ll mod=1e9+7;

#define int long long

int n,sc[123][5],sol[5];

bool can(int np){
    int rp=0;
    for(int p=0;p<5;++p){
        if(sc[0][p]<sc[1][p]){
            LOG("sc[0][p]<sc[1][p]\n");
            if(sol[p]<=n+np && 2ll*sol[p]>n+np)rp-=2ll*(sc[0][p]-sc[1][p]);
            else if(2ll*sol[p]<=n+np && 4ll*sol[p]>n+np)rp-=4ll*(sc[0][p]-sc[1][p]);
            else if(4ll*sol[p]<=n+np && 8ll*sol[p]>n+np)rp-=6ll*(sc[0][p]-sc[1][p]);
            else if(8ll*sol[p]<=n+np && 16ll*sol[p]>n+np)rp-=8ll*(sc[0][p]-sc[1][p]);
            else if(16ll*sol[p]<=n+np && 32ll*sol[p]>n+np)rp-=10ll*(sc[0][p]-sc[1][p]);
            else rp-=12ll*(sc[0][p]-sc[1][p]);
        }
        else if(sc[0][p]>sc[1][p]){
            LOG("sc[0][p]>sc[1][p]\n");
            if(((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 2ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=2ll*(sc[1][p]-sc[0][p]);
            else if(2ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 4ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=4ll*(sc[1][p]-sc[0][p]);
            else if(4ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 8ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=6ll*(sc[1][p]-sc[0][p]);
            else if(8ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 16ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=8ll*(sc[1][p]-sc[0][p]);
            else if(16ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 32ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=10ll*(sc[1][p]-sc[0][p]);
            else rp+=12ll*(sc[1][p]-sc[0][p]);
        }
        PDE2(p,rp);
    }
    PDE2(np,rp);
    return rp>0;
}
main(){
    cin>>n;
    for(int i=0;i<n;++i){
        for(int j=0;j<5;++j){
            cin>>sc[i][j];
            if(sc[i][j]!=-1)++sol[j];
            else sc[i][j]=250;
        }
    }
    for(int i=0;i<32*n;++i){
        if(can(i))return cout<<i<<endl,0;
    }
    cout<<-1<<endl;
}


#include<cassert>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include<vector>
#include<utility>

using namespace std;

#define ll long long
#define ld long double
#define X first
#define Y second
#define SZ(x) ((int)(x).size())
#define LN(x) ((int)(x).length())
#define rz(x) resize(x)
#define reset(x,n) (x).clear(), (x).resize(n)
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vint vector<int>
#define el putchar('\n')
#define spc putchar(' ')
#define SS stringstream
#define PQ priority_queue
#define PRF(...) printf(__VA_ARGS__)
#define MS(x,v) memset((x),(v),sizeof(x))
#define MS0(x) memset((x),0,sizeof(x))
#define MSB(x) memset((x),0x3f,sizeof(x))
#define MSBB(x) memset((x),0x7f,sizeof(x))
#define MSM(x) memset((x),0xff,sizeof(x))
#define MSMB(x) memset((x),0x80,sizeof(x))
#define PAR(x,n) for(int ___=0;___<(n);++___)cout<<x[___]<<" ";cout<<'\n';
#define PAR1(x,n) for(int ___=1;___<=(n);++___)cout<<x[___]<<" ";cout<<'\n';
#define CIO ios_base::sync_with_stdio(0);
#define FLH fflush(stdout)

#define tm Ovuvuevuevue
#define y1 Enyetuenwuevue
#define left Ugbemugbem
#define Osas

#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl

#ifdef WEAK
#define PDE1(a) cout<<#a<<" = "<<(a)<<'\n'
#define PDE2(a,b) cout<<#a<<" = "<<(a)<<" , ", PDE1(b)
#define PDE3(a,b,c) cout<<#a<<" = "<<(a)<<" , ", PDE2(b,c)
#define PDE4(a,b,c,d) cout<<#a<<" = "<<(a)<<" , ", PDE3(b,c,d)
#define PDE5(a,b,c,d,e) cout<<#a<<" = "<<(a)<<" , ", PDE4(b,c,d,e)
#define DEB(...) printf(__VA_ARGS__),fflush(stdout)
#define WHR() printf("%s: Line %d",__PRETTY_FUNCTION__,__LINE__),fflush(stdout)
#define LOG(...) printf("%s: Line %d ",__PRETTY_FUNCTION__,__LINE__),printf(__VA_ARGS__),fflush(stdout)
#define FIN freopen("in","r",stdin)
#define FOUT freopen("out","w",stdout)
#define DEBUG "jizz"
#else
#define PDE1(a) ;
#define PDE2(a,b) ;
#define PDE3(a,b,c) ;
#define PDE4(a,b,c,d) ;
#define PDE5(a,b,c,d,e) ;
#define DEB(...) ;
#define WHR() ;
#define LOG(...) ;
#define endl '\n'
#define getchar gtx
#define DEBUG 0
#ifdef WEA
#define FIN freopen("in","r",stdin)
#define FOUT freopen("out","w",stdout)
#else
#define FIN ;
#define FOUT ;
#endif
#endif

template<typename TA,typename TB> ostream& operator<<(ostream& ostm, const pair<TA,TB> &p){ostm<<"("<<p.X<<","<<p.Y<<")";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm, const vector<T> &v){ostm<<"[ ";for(auto i:v)ostm<<i<<" ";ostm<<"]";return ostm;}
template<typename TA,typename TB> ostream& operator<<(ostream &ostm, const map<TA,TB> &mp){ostm<<"[ ";for(auto &it:mp)ostm<<it<<" ";ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const set<T> &s){ostm<<"[ ";for(auto &it:s)ostm<<it<<" ";ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const stack<T> &inp){stack<T> st=inp;ostm<<"[ ";while(!st.empty()){ostm<<st.top()<<" ";st.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const queue<T> &inp){queue<T> q=inp;ostm<<"[ ";while(!q.empty()){ostm<<q.front()<<" ";q.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const priority_queue<T> &inp){priority_queue<T> pq=inp;ostm<<"[ ";while(!pq.empty()){ostm<<pq.top()<<" ";pq.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const deque<T> &inp){deque<T> dq=inp;ostm<<"[ ";while(!dq.empty()){ostm<<dq.front()<<" ";dq.pop_front();}ostm<<"]";return ostm;}

#define lowbit(x) ((x)&(-(x)))

inline int gtx(){
    const int N=1048576;
    static char __buffer[N];
    static char *__p=__buffer,*__end=__buffer;
    if(__p==__end){
        if((__end=__buffer+fread(__buffer,1,N,stdin))==__buffer)return EOF;
        __p=__buffer;
    } return *__p++;
}

template<typename T>
inline bool rit(T &x){
    char c=0; bool fg=0;
    while(c=getchar(), (c<'0' && c!='-') || c>'9')if(c==EOF)return false;
    c=='-' ? (fg=1,x=0) : (x=c&15);
    while(c=getchar(), c>='0' && c<='9')x=x*10+(c&15);
    if(fg)x=-x; return true;
}
template<typename T,typename ...Args>
inline bool rit(T& x,Args& ...args){return rit(x)&&rit(args...);}

inline void pit(int x){printf("%d",x);}
inline void pln(ll x){printf("%I64d",x);}
template<typename ...Args>
inline void pit(int x,Args ...args){printf("%d ",x);pit(args...);}
template<typename ...Args>
inline void pln(ll x,Args ...args){printf("%I64d ",x);pit(args...);}
void JIZZ(){cout<<"";exit(0);}

const ld PI=3.14159265358979323846264338327950288;
const ld eps=1e-8;
const ll mod=1e9+7;

#define int long long

int n,sc[123][5],sol[5];

bool can(int np){
    int rp=0;
    for(int p=0;p<5;++p){
        if(sc[0][p]<sc[1][p]){
            LOG("sc[0][p]<sc[1][p]\n");
            if(sol[p]<=n+np && 2ll*sol[p]>n+np)rp-=2ll*(sc[0][p]-sc[1][p]);
            else if(2ll*sol[p]<=n+np && 4ll*sol[p]>n+np)rp-=4ll*(sc[0][p]-sc[1][p]);
            else if(4ll*sol[p]<=n+np && 8ll*sol[p]>n+np)rp-=6ll*(sc[0][p]-sc[1][p]);
            else if(8ll*sol[p]<=n+np && 16ll*sol[p]>n+np)rp-=8ll*(sc[0][p]-sc[1][p]);
            else if(16ll*sol[p]<=n+np && 32ll*sol[p]>n+np)rp-=10ll*(sc[0][p]-sc[1][p]);
            else rp-=12ll*(sc[0][p]-sc[1][p]);
        }
        else if(sc[0][p]>sc[1][p]){
            LOG("sc[0][p]>sc[1][p]\n");
            if(((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 2ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=2ll*(sc[1][p]-sc[0][p]);
            else if(2ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 4ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=4ll*(sc[1][p]-sc[0][p]);
            else if(4ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 8ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=6ll*(sc[1][p]-sc[0][p]);
            else if(8ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 16ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=8ll*(sc[1][p]-sc[0][p]);
            else if(16ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 32ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=10ll*(sc[1][p]-sc[0][p]);
            else rp+=12ll*(sc[1][p]-sc[0][p]);
        }
        PDE2(p,rp);
    }
    PDE2(np,rp);
    return rp>0;
}
main(){
    cin>>n;
    for(int i=0;i<n;++i){
        for(int j=0;j<5;++j){
            cin>>sc[i][j];
            if(sc[i][j]!=-1)++sol[j];
            else sc[i][j]=250;
        }
    }
    int L=0,R=n,A=n+1;
    // int L=3,R=4,A=3;
    while(R>=L){
        int M=(L+R)/2;
        PDE4(L,R,M,A);
        if(can(M))R=M-1,A=M;
        else L=M+1;
    }
    if(A==n+1)cout<<-1<<endl;
    else cout<<A<<endl;
}


#include<cassert>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include<vector>
#include<utility>

using namespace std;

#define ll long long
#define ld long double
#define X first
#define Y second
#define SZ(x) ((int)(x).size())
#define LN(x) ((int)(x).length())
#define rz(x) resize(x)
#define reset(x,n) (x).clear(), (x).resize(n)
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vint vector<int>
#define el putchar('\n')
#define spc putchar(' ')
#define SS stringstream
#define PQ priority_queue
#define PRF(...) printf(__VA_ARGS__)
#define MS(x,v) memset((x),(v),sizeof(x))
#define MS0(x) memset((x),0,sizeof(x))
#define MSB(x) memset((x),0x3f,sizeof(x))
#define MSBB(x) memset((x),0x7f,sizeof(x))
#define MSM(x) memset((x),0xff,sizeof(x))
#define MSMB(x) memset((x),0x80,sizeof(x))
#define PAR(x,n) for(int ___=0;___<(n);++___)cout<<x[___]<<" ";cout<<'\n';
#define PAR1(x,n) for(int ___=1;___<=(n);++___)cout<<x[___]<<" ";cout<<'\n';
#define CIO ios_base::sync_with_stdio(0);
#define FLH fflush(stdout)

#define tm Ovuvuevuevue
#define y1 Enyetuenwuevue
#define left Ugbemugbem
#define Osas

#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl

#ifdef WEAK
#define PDE1(a) cout<<#a<<" = "<<(a)<<'\n'
#define PDE2(a,b) cout<<#a<<" = "<<(a)<<" , ", PDE1(b)
#define PDE3(a,b,c) cout<<#a<<" = "<<(a)<<" , ", PDE2(b,c)
#define PDE4(a,b,c,d) cout<<#a<<" = "<<(a)<<" , ", PDE3(b,c,d)
#define PDE5(a,b,c,d,e) cout<<#a<<" = "<<(a)<<" , ", PDE4(b,c,d,e)
#define DEB(...) printf(__VA_ARGS__),fflush(stdout)
#define WHR() printf("%s: Line %d",__PRETTY_FUNCTION__,__LINE__),fflush(stdout)
#define LOG(...) printf("%s: Line %d ",__PRETTY_FUNCTION__,__LINE__),printf(__VA_ARGS__),fflush(stdout)
#define FIN freopen("in","r",stdin)
#define FOUT freopen("out","w",stdout)
#define DEBUG "jizz"
#else
#define PDE1(a) ;
#define PDE2(a,b) ;
#define PDE3(a,b,c) ;
#define PDE4(a,b,c,d) ;
#define PDE5(a,b,c,d,e) ;
#define DEB(...) ;
#define WHR() ;
#define LOG(...) ;
#define endl '\n'
#define getchar gtx
#define DEBUG 0
#ifdef WEA
#define FIN freopen("in","r",stdin)
#define FOUT freopen("out","w",stdout)
#else
#define FIN ;
#define FOUT ;
#endif
#endif

template<typename TA,typename TB> ostream& operator<<(ostream& ostm, const pair<TA,TB> &p){ostm<<"("<<p.X<<","<<p.Y<<")";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm, const vector<T> &v){ostm<<"[ ";for(auto i:v)ostm<<i<<" ";ostm<<"]";return ostm;}
template<typename TA,typename TB> ostream& operator<<(ostream &ostm, const map<TA,TB> &mp){ostm<<"[ ";for(auto &it:mp)ostm<<it<<" ";ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const set<T> &s){ostm<<"[ ";for(auto &it:s)ostm<<it<<" ";ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const stack<T> &inp){stack<T> st=inp;ostm<<"[ ";while(!st.empty()){ostm<<st.top()<<" ";st.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const queue<T> &inp){queue<T> q=inp;ostm<<"[ ";while(!q.empty()){ostm<<q.front()<<" ";q.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const priority_queue<T> &inp){priority_queue<T> pq=inp;ostm<<"[ ";while(!pq.empty()){ostm<<pq.top()<<" ";pq.pop();}ostm<<"]";return ostm;}
template<typename T> ostream& operator<<(ostream &ostm,const deque<T> &inp){deque<T> dq=inp;ostm<<"[ ";while(!dq.empty()){ostm<<dq.front()<<" ";dq.pop_front();}ostm<<"]";return ostm;}

#define lowbit(x) ((x)&(-(x)))

inline int gtx(){
    const int N=1048576;
    static char __buffer[N];
    static char *__p=__buffer,*__end=__buffer;
    if(__p==__end){
        if((__end=__buffer+fread(__buffer,1,N,stdin))==__buffer)return EOF;
        __p=__buffer;
    } return *__p++;
}

template<typename T>
inline bool rit(T &x){
    char c=0; bool fg=0;
    while(c=getchar(), (c<'0' && c!='-') || c>'9')if(c==EOF)return false;
    c=='-' ? (fg=1,x=0) : (x=c&15);
    while(c=getchar(), c>='0' && c<='9')x=x*10+(c&15);
    if(fg)x=-x; return true;
}
template<typename T,typename ...Args>
inline bool rit(T& x,Args& ...args){return rit(x)&&rit(args...);}

inline void pit(int x){printf("%d",x);}
inline void pln(ll x){printf("%I64d",x);}
template<typename ...Args>
inline void pit(int x,Args ...args){printf("%d ",x);pit(args...);}
template<typename ...Args>
inline void pln(ll x,Args ...args){printf("%I64d ",x);pit(args...);}
void JIZZ(){cout<<"";exit(0);}

const ld PI=3.14159265358979323846264338327950288;
const ld eps=1e-8;
const ll mod=1e9+7;

#define int long long

int n,sc[123][5],sol[5];

bool can(int np){
    int rp=0;
    for(int p=0;p<5;++p){
        if(sc[0][p]<sc[1][p]){
            LOG("sc[0][p]<sc[1][p]\n");
            if(sol[p]<=n+np && 2ll*sol[p]>n+np)rp-=2ll*(sc[0][p]-sc[1][p]);
            else if(2ll*sol[p]<=n+np && 4ll*sol[p]>n+np)rp-=4ll*(sc[0][p]-sc[1][p]);
            else if(4ll*sol[p]<=n+np && 8ll*sol[p]>n+np)rp-=6ll*(sc[0][p]-sc[1][p]);
            else if(8ll*sol[p]<=n+np && 16ll*sol[p]>n+np)rp-=8ll*(sc[0][p]-sc[1][p]);
            else if(16ll*sol[p]<=n+np && 32ll*sol[p]>n+np)rp-=10ll*(sc[0][p]-sc[1][p]);
            else rp-=12ll*(sc[0][p]-sc[1][p]);
        }
        else if(sc[0][p]>sc[1][p]){
            LOG("sc[0][p]>sc[1][p]\n");
            if(((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 2ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=2ll*(sc[1][p]-sc[0][p]);
            else if(2ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 4ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=4ll*(sc[1][p]-sc[0][p]);
            else if(4ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 8ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=6ll*(sc[1][p]-sc[0][p]);
            else if(8ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 16ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=8ll*(sc[1][p]-sc[0][p]);
            else if(16ll*((sc[0][p]>=250?sol[p]:sol[p]+np))<=n+np && 32ll*((sc[0][p]>=250?sol[p]:sol[p]+np))>n+np)rp+=10ll*(sc[1][p]-sc[0][p]);
            else rp+=12ll*(sc[1][p]-sc[0][p]);
        }
        PDE2(p,rp);
    }
    PDE2(np,rp);
    return rp>0;
}
main(){
    cin>>n;
    for(int i=0;i<n;++i){
        for(int j=0;j<5;++j){
            cin>>sc[i][j];
            if(sc[i][j]!=-1)++sol[j];
            else sc[i][j]=250;
        }
    }
    int L=0,R=1e9+7,A=1e9+8;
    // int L=3,R=4,A=3;
    while(R>=L){
        int M=(L+R)/2;
        PDE4(L,R,M,A);
        if(can(M))R=M-1,A=M;
        else L=M+1;
    }
    if(A==1000000008)cout<<-1<<endl;
    else cout<<A<<endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
#define i64 __int64
int a[123][6];
int p[6];
int add[6];
int ling[6];
int s[] = {500,1000,1500,2000,2500,3000};
	int n;
int fen(int x,int y) {
	if (2 * y > x)return 500;
	if (4 * y > x)return 1000;
	if (8 * y > x)return 1500;
	if (16 * y > x)return 2000;
	if (32 * y > x)return 2500;
	return 3000;
}
bool ok(int x) {
	//cout << x << " ";
	int s1=0, s2 = 0,ss;
	for (int i = 0; i < 5; i++) {
		if (ling[i] == 1)
			ss = fen(n + x, p[i]);
		else if (ling[i] == -1)
			ss = fen(n + x, p[i]);
		else
			ss = fen(n + x, p[i] + x);
		if (a[0][i] != -1)s1 += ss - a[0][i] * ss / 250;
		if (a[1][i] != -1)s2 += ss - a[1][i] * ss / 250;
		//cout << ss << " ";
	}
	//cout << s1 << " " << s2 << endl;
	return s1 > s2;
}
int main() {
	while (cin >> n)
	{
		memset(p,0,sizeof p);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < 5; j++) {
				cin >> a[i][j];
				if (a[i][j] != -1)
					p[j]++;
			}
		}
		for (int i = 0; i < 5; i++) {
			if ((a[0][i] != -1) && (a[1][i] == -1))
				ling[i] = 1;
			else if ((a[0][i] != -1) && (a[1][i] != -1) && (a[0][i] < a[1][i]))
				ling[i] = 1;
			else if ((a[0][i] != -1) && (a[1][i] != -1) && (a[0][i] >= a[1][i]))
				ling[i] = 0;
			else
				ling[i] = -1;
		}
		int l = 0, r = 1e9+7,mid;
		while (l<=r) {
			mid = (l + r) >> 1;
			if (ok(mid))r = mid - 1;
			else l = mid + 1;
		}
		if (l > (int)(1e9 + 7)) l = -1;
		cout << l << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
#define i64 __int64
int a[123][6];
int p[6];
int ling[6];
int n;
int fen(i64 x,i64 y) {
	if (2 * y > x)return 500;
	if (4 * y > x)return 1000;
	if (8 * y > x)return 1500;
	if (16 * y > x)return 2000;
	if (32 * y > x)return 2500;
	return 3000;
}
bool ok(int x) {
	//cout << x << " ";
	int s1=0, s2 = 0,ss;
	for (int i = 0; i < 5; i++) {
		if (ling[i] == 1)
			ss = fen(n + x, p[i]);
		else if (ling[i] == -1)
			ss = fen(n + x, p[i]);
		else
			ss = fen(n + x, p[i] + x);
		if (a[0][i] != -1)s1 += ss - a[0][i] * ss / 250;
		if (a[1][i] != -1)s2 += ss - a[1][i] * ss / 250;
		//cout << ss << " ";
	}
	//cout << s1 << " " << s2 << endl;
	return s1 > s2;
}
int main() {
	while (cin >> n)
	{
		memset(p,0,sizeof p);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < 5; j++) {
				cin >> a[i][j];
				if (a[i][j] != -1)
					p[j]++;
			}
		}
		if (ok(0)) {
			cout << 0 << endl;
			continue;
		}
		for (int i = 0; i < 5; i++) {
			if ((a[0][i] != -1) && (a[1][i] == -1))
				ling[i] = 1;
			else if ((a[0][i] != -1) && (a[1][i] != -1) && (a[0][i] < a[1][i]))
				ling[i] = 1;
			else if ((a[0][i] != -1) && (a[1][i] != -1) && (a[0][i] >= a[1][i]))
				ling[i] = 0;
			else
				ling[i] = -1;
		}
		int fff = 0;
		for (int i = 1; i < 120 * 330; i++) {
			if (!ok(i - 1) && ok(i)) {
				cout << i << endl;
				fff = 1;
				break;
			}
		}
		if (fff == 0)
		cout << -1 << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
#define i64 __int64
int a[123][6];
int p[6];
int add[6];
int ling[6];
int s[] = {500,1000,1500,2000,2500,3000};
	int n;
int fen(int x,int y) {
	if (2 * y > x)return 500;
	if (4 * y > x)return 1000;
	if (8 * y > x)return 1500;
	if (16 * y > x)return 2000;
	if (32 * y > x)return 2500;
	return 3000;
}
bool ok(int x) {
	int s1=0, s2 = 0,ss;
	for (int i = 0; i < 5; i++) {
		if (ling[i] == 1)
			ss = fen(n + x, p[i]);
		else if (ling[i] == -1)
			ss = fen(n + x, p[i]);
		else
			ss = fen(n + x, p[i] + x);
		if (a[0][i] != -1)s1 += ss - a[0][i] * ss / 250;
		if (a[1][i] != -1)s2 += ss - a[1][i] * ss / 250;
		//cout << ss << " ";
	}
	//cout << s1 << " " << s2 << endl;
	return s1 > s2;
}
int main() {
	while (cin >> n)
	{
		memset(p,0,sizeof p);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < 5; j++) {
				cin >> a[i][j];
				if (a[i][j] != -1)
					p[j]++;
			}
		}
		for (int i = 0; i < 5; i++) {
			if ((a[0][i] != -1) && (a[1][i] == -1))
				ling[i] = 1;
			else if ((a[0][i] != -1) && (a[1][i] != -1) && (a[0][i] <= a[1][i]))
				ling[i] = 1;
			else if ((a[0][i] != -1) && (a[1][i] != -1) && (a[0][i] > a[1][i]))
				ling[i] = 0;
			else
				ling[i] = -1;
		}
		int l = 0, r = 1e9+7,mid;
		while (l<=r) {
			mid = (l + r) >> 1;
			if (ok(mid))r = mid - 1;
			else l = mid + 1;
		}
		if (l > (int)(1e9 + 7)) l = -1;
		cout << l << endl;
	}
	return 0;
}


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

 int Times[3][5] , Sub[6] , N;

 double Calculate_Score( int j ){
  int i;
  double Score = 0 , Num , Temp;

     for( i = 1 ; i <= 5 ; i++ ){
      Num = (double)Sub[i] / N;

         if( Times[j][i] == -1 )
          continue;

         if( Num > 0.5 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 500;
          // cout << 500 << endl;
        }
         else if( Num > 0.25 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 1000;
          // cout << 1000 << endl;
        }
         else if( Num > 0.125 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 1500;
          // cout << 1500 << endl;
        }
         else if( Num > 0.0625 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 2000;
          // cout << 2000 << endl;
        }
         else if( Num > 0.03125 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 2500;
          // cout << 2500 << endl;
        }
         else{
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 3000;
          // cout << 3000 << endl;
        }
    }

  //cout /*<< Score */<< endl;
  return Score;
}

 bool Check( int fake ){
  int i;
  double Score1 , Score2;

     for( i = 1 ; i <= 5 ; i++ )
         if( Times[1][i] != -1 )
             if( Times[2][i] != -1 && ( Times[1][i] > Times[2][i] ) )
              Sub[i] += fake;

 N += fake;
 Score1 = Calculate_Score( 1 );
 Score2 = Calculate_Score( 2 );
 //cout << Score1 << " " << Score2 << endl;
 N -= fake;

     for( i = 1 ; i <= 5 ; i++ )
         if( Times[1][i] != -1 )
             if( Times[2][i] != -1 && ( Times[1][i] > Times[2][i] ) )
              Sub[i] += fake;

     if( Score1 > Score2 )
      return 1;

  return 0;
}

 int main(){
  int i , j , Num;
  double Score1 , Score2;
  scanf( "%d" , &N );

     for( i = 1 ; i <= 2 ; i++ ){
         for( j = 1 ; j <= 5 ; j++ ){
          scanf( "%d" , &Times[i][j] );

             if( Times[i][j] != -1 )
              Sub[j]++;
        }
    }

     for( i = 3 ; i <= N ; i++ ){
         for( j = 1 ; j <= 5 ; j++ ){
          scanf( "%d" , &Num );

             if( Num != -1 )
              Sub[j]++;
        }
    }

  Score1 = Calculate_Score( 1 );
  Score2 = Calculate_Score( 2 );
  // cout << Score1 << " " << Score2 << endl;

     if( Score1 > Score2 ){
      printf( "0\n" );
      return 0;
    }
     else{
         for( i = 1 ; i <= 10000 ; i++ ){
             if( Check( i ) ){
              printf( "%d\n" , i );
              return 0;
            }
        }
    }

  printf( "-1\n" );
  return 0;
}


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

 int Times[3][5] , Sub[6] , N;

 double Calculate_Score( int j ){
  int i;
  double Score = 0 , Num , Temp;

     for( i = 1 ; i <= 5 ; i++ ){
      Num = (double)Sub[i] / N;

         if( Times[j][i] == -1 )
          continue;

         if( Num > 0.5 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 500;
          // cout << 500 << endl;
        }
         else if( Num > 0.25 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 1000;
          // cout << 1000 << endl;
        }
         else if( Num > 0.125 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 1500;
          // cout << 1500 << endl;
        }
         else if( Num > 0.0625 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 2000;
          // cout << 2000 << endl;
        }
         else if( Num > 0.03125 ){
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 2500;
          // cout << 2500 << endl;
        }
         else{
          Temp = (double)( 250 - Times[j][i] ) / 250;
          Score += Temp * 3000;
          // cout << 3000 << endl;
        }
    }

  //cout /*<< Score */<< endl;
  return Score;
}

 bool Check( int fake ){
  int i;
  double Score1 , Score2;

     for( i = 1 ; i <= 5 ; i++ )
         if( Times[1][i] != -1 )
             if( Times[2][i] != -1 && ( Times[1][i] > Times[2][i] ) )
              Sub[i] += fake;

 N += fake;
 Score1 = Calculate_Score( 1 );
 Score2 = Calculate_Score( 2 );
 //cout << Score1 << " " << Score2 << endl;
 N -= fake;

     for( i = 1 ; i <= 5 ; i++ )
         if( Times[1][i] != -1 )
             if( Times[2][i] != -1 && ( Times[1][i] > Times[2][i] ) )
              Sub[i] += fake;

     if( Score1 > Score2 )
      return 1;

  return 0;
}

 int main(){
  int i , j , Num;
  double Score1 , Score2;
  scanf( "%d" , &N );

     for( i = 1 ; i <= 2 ; i++ ){
         for( j = 1 ; j <= 5 ; j++ ){
          scanf( "%d" , &Times[i][j] );

             if( Times[i][j] != -1 )
              Sub[j]++;
        }
    }

     for( i = 3 ; i <= N ; i++ ){
         for( j = 1 ; j <= 5 ; j++ ){
          scanf( "%d" , &Num );

             if( Num != -1 )
              Sub[j]++;
        }
    }

  Score1 = Calculate_Score( 1 );
  Score2 = Calculate_Score( 2 );
  // cout << Score1 << " " << Score2 << endl;

     if( Score1 > Score2 ){
      printf( "0\n" );
      return 0;
    }
     else{
         for( i = 1 ; i <= 1000 ; i++ ){
             if( Check( i ) ){
              printf( "%d\n" , i );
              return 0;
            }
        }
    }

  printf( "-1\n" );
  return 0;
}


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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


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

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

int rand(int l, int r){
  static mt19937
  rng(chrono::steady_clock::now().time_since_epoch().count());

  uniform_int_distribution<int> ludo(l, r);
  return ludo(rng);
}
void solve(){
    ll i,j,k;
    ll l,r;
    cin>>l>>r>>k;
    --l,--r;
    ll ans=inf;
    rep(i,0,20){
        j=a[rand(l,r)];
        if(query(l,r,j)>(r-l+1)/k){
            ans=min(ans,j);
        }
    }
    if(ans!=inf)cout<<ans<<endl;
    else cout<<-1<<endl;
}

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

int rand(int l, int r){
  static mt19937
  rng(chrono::steady_clock::now().time_since_epoch().count());

  uniform_int_distribution<int> ludo(l, r);
  return ludo(rng);
}
void solve(){
    ll i,j,k;
    ll l,r;
    cin>>l>>r>>k;
    --l,--r;
    ll ans=inf;
    rep(i,0,80){
        j=rand(l,r);
        if(query(l,r,j)>(r-l+1)/k){
            ans=min(ans,j);
        }
    }
    if(ans!=inf)cout<<ans<<endl;
    else cout<<-1<<endl;
}

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

int a[N],sb[4*N],se[4*N];
vll v[4*N],ct[4*N];
vll top[4*N];
vll st;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

int rand(int l, int r){
  static mt19937
  rng(chrono::steady_clock::now().time_since_epoch().count());

  uniform_int_distribution<int> ludo(l, r);
  return ludo(rng);
}
void solve(){
    ll i,j,k;
    ll l,r;
    cin>>l>>r>>k;
    --l,--r;
    ll ans=inf;
    rep(i,0,2){
        j=a[rand(l,r)];
        if(query(l,r,j)>(r-l+1)/k){
            ans=min(ans,j);
        }
    }
    if(ans!=inf)cout<<ans<<endl;
    else cout<<-1<<endl;
}

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


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

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

int main()
{
	ll T;
	scanf("%d%d",&n,&T);
	ll i,k,ans;
	ll l,r;
	rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
	memset(top,-1,sizeof(top));
	build(1,0,n-1);
	while(T--){
		scanf("%d%d%d",&l,&r,&k);
		--l,--r;
		v.clear();
		query1(1,l,r,0,n-1);
		sort(v.begin(),v.end());
		UQ(v);
		ans=-1;
		for(auto i : v){
			if(query(l,r,i)>(r-l+1)/k){
				ans=i;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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


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

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

int main()
{
	ll T;
	scanf("%d%d",&n,&T);
	ll i,k,ans;
	ll l,r;
	rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
	memset(top,-1,sizeof(top));
	build(1,0,n-1);
	while(T--){
		scanf("%d%d%d",&l,&r,&k);
		--l,--r;
		v.clear();
		query1(1,l,r,0,n-1);
		sort(v.begin(),v.end());
		UQ(v);
		ans=-1;
		for(auto i : v){
			if(query(l,r,i)>(r-l+1)/k){
				ans=i;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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

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


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

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

void build(int node,int l,int r){
	if(l<r){
		build(2*node,l,(l+r)>>1);
		build(2*node+1,((l+r)>>1)+1,r);
	}
	ll i;
	tmp.clear();
	rep(i,l,r+1){
		if(has[a[i]])continue;
		tmp.pb({query(l,r,a[i]),a[i]});
		has[a[i]]=1;
	}
	sort(tmp.begin(),tmp.end());
	c=0;
	ll x=(r-l+1)/5;
	repi(i,tmp.size()-1,-1){
		if(c>=5&&tmp[i].F<=x)break;top[node][c++]=tmp[i].S;
	}
	rep(i,0,tmp.size())has[tmp[i].S]=0;
}

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

int main()
{
	ll T;
	scanf("%d%d",&n,&T);
	ll i,k,ans;
	ll l,r;
	rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
	memset(top,-1,sizeof(top));
	build(1,0,n-1);
	while(T--){
		scanf("%d%d%d",&l,&r,&k);
		--l,--r;
		v.clear();
		query1(1,l,r,0,n-1);
		sort(v.begin(),v.end());
		UQ(v);
		ans=-1;
		for(auto i : v){
			if(query(l,r,i)>(r-l+1)/k){
				ans=i;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

int rand(int l, int r){
  static mt19937
  rng(chrono::steady_clock::now().time_since_epoch().count());

  uniform_int_distribution<int> ludo(l, r);
  return ludo(rng);
}
void solve(){
    ll i,j,k;
    ll l,r;
    cin>>l>>r>>k;
    --l,--r;
    ll ans=inf;
    rep(i,0,50){
        j=a[rand(l,r)];
        if(query(l,r,j)>(r-l+1)/k){
            ans=min(ans,j);
        }
    }
    if(ans!=inf)cout<<ans<<endl;
    else cout<<-1<<endl;
}

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


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

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

void build(int node,int l,int r){
	if(l<r){
		build(2*node,l,(l+r)>>1);
		build(2*node+1,((l+r)>>1)+1,r);
	}
	ll i;
	tmp.clear();
	rep(i,l,r+1){
		if(has[a[i]])continue;
		tmp.pb({query(l,r,a[i]),a[i]});
		has[a[i]]=1;
	}
	sort(tmp.begin(),tmp.end());
	c=0;
	ll x=(r-l+1)/5;
	repi(i,tmp.size()-1,-1){
		if(c==4||tmp[i].F<=x)break;top[node][c++]=tmp[i].S;
	}
	rep(i,0,tmp.size())has[tmp[i].S]=0;
}

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

int main()
{
	ll T;
	scanf("%d%d",&n,&T);
	ll i,k,ans;
	ll l,r;
	rep(i,0,n)scanf("%d",&a[i]),e[a[i]].pb(i);
	memset(top,-1,sizeof(top));
	build(1,0,n-1);
	while(T--){
		scanf("%d%d%d",&l,&r,&k);
		--l,--r;
		v.clear();
		query1(1,l,r,0,n-1);
		sort(v.begin(),v.end());
		UQ(v);
		ans=-1;
		for(auto i : v){
			if(query(l,r,i)>(r-l+1)/k){
				ans=i;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

int rand(int l, int r){
  static mt19937
  rng(chrono::steady_clock::now().time_since_epoch().count());

  uniform_int_distribution<int> ludo(l, r);
  return ludo(rng);
}
void solve(){
    ll i,j,k;
    ll l,r;
    cin>>l>>r>>k;
    --l,--r;
    ll ans=inf;
    rep(i,0,80){
        j=a[rand(l,r)];
        if(query(l,r,j)>(r-l+1)/k){
            ans=min(ans,j);
        }
    }
    if(ans!=inf)cout<<ans<<endl;
    else cout<<-1<<endl;
}

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

void build(int node,int l,int r){
	sb[node]=l;
	se[node]=r;
	ll i;
	rep(i,l,r+1){
		if(has[a[i]])continue;
		top[node].pb({query(l,r,a[i]),a[i]});
		has[a[i]]=1;
	}
	for(auto i : top[node])has[i.S]=0;
	if(l==r){
		return;
	}
	int mid=(l+r)/2;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
}

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

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

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

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

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

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

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


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

*/

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

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

using namespace std;
using namespace __gnu_pbds;

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

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

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

ll n;

int a[N],sb[4*N],se[4*N];
vll v[4*N],ct[4*N];
vll top[4*N];
vll st;

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

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

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

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

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

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