#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x) , end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
ll modpow(ll b, ll e ,ll mod) { ll ans = 1;for (; e; b = b * b % mod, e /= 2){if (e & 1) ans = ans * b % mod;}return ans;}
const int N = 300005;
int a[N] , n , q;
vi check[N] , hist[N] , candidates[N];
struct query{
	int l , r , k , id;
};
int conta[N] , time_stamp[N] , curr_time = 0;
void solve(const vector<query> &Q , int l = 1 , int r = n){
	int mid = l + (r-l) / 2;
	map<int ,int> cnt;
	curr_time ++ ;
	for(int j = mid ; j >= l ; j --){
		candidates[j].clear();
		if(time_stamp[a[j]] != curr_time){
			time_stamp[a[j]] = curr_time;
			conta[a[j]] = 0;
		}
		conta[a[j]] ++ ;
		cnt[a[j]] = conta[a[j]];
		if(sz(cnt) > 6){
			pii z = {INT_MAX , INT_MAX};
			for(auto w : cnt){
				z = min(z , {w.second , -w.first}) ;
			}
			cnt.erase(-z.second);
		}
		for(auto w : cnt){
			candidates[j].push_back(w.first);
		}
	}
	curr_time++ , cnt.clear();
	for(int j = mid + 1 ; j <= r ; j ++){
		candidates[j].clear();
		if(time_stamp[a[j]] != curr_time){
			time_stamp[a[j]] = curr_time;
			conta[a[j]] = 0;
		}
		conta[a[j]] ++ ;
		cnt[a[j]] = conta[a[j]];
		if(sz(cnt) > 6){
			pii z = {INT_MAX , INT_MAX};
			for(auto w : cnt){
				z = min(z , {w.second , -w.first}) ;
			}
			cnt.erase(-z.second);
		}
		for(auto w : cnt){
			candidates[j].push_back(w.first);
		}
	}
	vector<query> Ql , Qr;
	for(auto &w : Q){
		if(w.r < mid){
			Ql.push_back(w);
		}
		else if(w.l > mid){
			Qr.push_back(w);
		}
		else{
			for(auto rw : candidates[w.l]){
				check[w.id].push_back(rw);
			}
			for(auto rw : candidates[w.r]){
				check[w.id].push_back(rw);
			}
		}
	}
	if(l != r){
		if(sz(Ql)) solve(Ql , l , mid);
		if(sz(Qr)) solve(Qr, mid+1 , r);
	}
}
int32_t main(){
	scanf("%d%d" , &n , &q);
	rep(i,1,n+1) scanf("%d" , &a[i]) , hist[a[i]].push_back(i);
	vector<query> Q(q);
	rep(i,0,q){
		scanf("%d%d%d" , &Q[i].l , &Q[i].r, &Q[i].k);
		Q[i].id = i;
	}
	solve(Q);
	rep(i,0,q){
		int ans = INT_MAX;
		int l = Q[i].l , r = Q[i].r , k = Q[i].k;
		int sz = (r-l+1);
		for(auto p : check[i]){
			if((upper_bound(all(hist[p]), r) - lower_bound(all(hist[p]), l))*k > sz){
				ans = min(ans , p);
			}
		}
		if(ans == INT_MAX)
			printf("-1\n");
		else printf("%d\n" , ans);
	}
}
/* clever stuff:
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and STAY ORGANIZED
	* Keep it simple stupid
	* WRITE STUFF DOWN
	* math -> gcd / lcm / divisors?
*/


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x) , end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
ll modpow(ll b, ll e ,ll mod) { ll ans = 1;for (; e; b = b * b % mod, e /= 2){if (e & 1) ans = ans * b % mod;}return ans;}
const int N = 300005;
vi hist[N];
int a[N];

int getint(){
	char c = '-';
	int a = 0;
	while(!(c>='0' && c <='9')) c = getchar();
	while(c>= '0' && c <= '9') a = 10*a + (c-'0') , c = getchar();
	return a;
}

int32_t main(){
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	int n = getint()  , q = getint();
	rep(i,1,n+1){
		a[i] = getint();
		hist[a[i]].push_back(i);
	}
	while(q--){
		int l , r ,k;
		l = getint() , r = getint() , k = getint();
		int ans = INT_MAX , sz = r - l + 1;
		for(int j = 0 ; j < 50; j ++){
			int p = a[rng()%sz + l];
			if((upper_bound(all(hist[p]), r) - lower_bound(all(hist[p]), l))*k > sz){
				ans = min(ans , p);
			}
		}
		if(ans == INT_MAX) puts("-1");
		else printf("%d\n" , ans);
	}
}
/* clever stuff:
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and STAY ORGANIZED
	* Keep it simple stupid
	* WRITE STUFF DOWN
	* math -> gcd / lcm / divisors?
*/


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x) , end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
ll modpow(ll b, ll e ,ll mod) { ll ans = 1;for (; e; b = b * b % mod, e /= 2){if (e & 1) ans = ans * b % mod;}return ans;}
const int N = 300005;
struct query{
	int l , r , k;
	int id;
};
int a[N] , n , q;
vi hist[N];
vi check[N];
vector<query> Q;
vi cobre[4*N];

void add(int x , int y , int id, int l = 1 , int r = n , int curr = 1){
	if(x == l && y == r){
		cobre[curr].push_back(id);
		return ;
	}
	int mid = l + (r-l)/2;
	if(y <= mid) add(x,y,id,l,mid,2*curr);
	else if(x > mid) add(x,y,id,mid+1,r,2*curr+1);
	else{
		add(x,mid,id,l,mid,2*curr);
		add(mid+1,y,id,mid+1,r,2*curr+1);
	}
}

void solve(int l = 1,  int r = n , int curr = 1){
	map<int,int> f;
	for(int i = l ; i <= r ;i ++){
		f[a[i]] ++ ;
	}
	vpi opt;
	for(auto w : f){
		opt.push_back({-w.second , w.first}) ;
	}
	sort(all(opt));
	for(auto wid : cobre[curr]){
		auto w = Q[wid];
		for(int j = 0 ; j < min(sz(opt) , 10) ; j ++){
			check[w.id].push_back(opt[j].second);
		}
	}
	if(l != r){
		solve(l,l + (r-l)/2 , 2*curr);
		solve(l + (r-l)/2 + 1 , r , 2*curr + 1);
	}
}

int32_t main(){
	scanf("%d%d" , &n , &q);
	rep(i,1,n+1) scanf("%d" , &a[i]) , hist[a[i]].push_back(i);
	rep(i,0,q){
		int l , r, k;
		scanf("%d%d%d" , &l , &r, &k);
		Q.push_back({l,r,k,i});
		add(l,r,sz(Q) -1);
	}
	solve();
	rep(i,0,q){
		int ans = INT_MAX;
		int l = Q[i].l , r = Q[i].r , k = Q[i].k;
		int sz = (r-l+1);
		for(auto p : check[i]){
			if((upper_bound(all(hist[p]), r) - lower_bound(all(hist[p]), l))*k > sz){
				ans = min(ans , p);
			}
		}
		if(ans == INT_MAX)
			printf("-1\n");
		else printf("%d\n" , ans);
	}
}
/* clever stuff:
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and STAY ORGANIZED
	* Keep it simple stupid
	* WRITE STUFF DOWN
	* math -> gcd / lcm / divisors?
*/


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x) , end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
ll modpow(ll b, ll e ,ll mod) { ll ans = 1;for (; e; b = b * b % mod, e /= 2){if (e & 1) ans = ans * b % mod;}return ans;}
const int N = 300005;
vi hist[N];
int a[N];

int getint(){
	char c = '-';
	int a = 0;
	while(!(c>='0' && c <='9')) c = getchar();
	while(c>= '0' && c <= '9') a = 10*a + (c-'0') , c = getchar();
	return a;
}

int32_t main(){
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	int n = getint()  , q = getint();
	rep(i,1,n+1){
		a[i] = getint();
		hist[a[i]].push_back(i);
	}
	while(q--){
		int l , r ,k;
		l = getint() , r = getint() , k = getint();
		int ans = INT_MAX , sz = r - l + 1;
		for(int j = 0 ; j < 100; j ++){
			int p = a[rng()%sz + l];
			if((upper_bound(all(hist[p]), r) - lower_bound(all(hist[p]), l))*k > sz){
				ans = min(ans , p);
			}
		}
		if(ans == INT_MAX) puts("-1");
		else printf("%d\n" , ans);
	}
}
/* clever stuff:
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and STAY ORGANIZED
	* Keep it simple stupid
	* WRITE STUFF DOWN
	* math -> gcd / lcm / divisors?
*/


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x) , end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
ll modpow(ll b, ll e ,ll mod) { ll ans = 1;for (; e; b = b * b % mod, e /= 2){if (e & 1) ans = ans * b % mod;}return ans;}
const int N = 300005;
vi hist[N];
int a[N];

int getint(){
	char c = '-';
	int a = 0;
	while(!(c>='0' && c <='9')) c = getchar();
	while(c>= '0' && c <= '9') a = 10*a + (c-'0') , c = getchar();
	return a;
}

int32_t main(){
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	int n = getint()  , q = getint();
	rep(i,1,n+1){
		a[i] = getint();
		hist[a[i]].push_back(i);
	}
	while(q--){
		int l , r ,k;
		l = getint() , r = getint() , k = getint();
		int ans = INT_MAX , sz = r - l + 1;
		for(int j = 0 ; j < 30; j ++){
			int p = a[rng()%sz + l];
			if((upper_bound(all(hist[p]), r) - lower_bound(all(hist[p]), l))*k > sz){
				ans = min(ans , p);
			}
		}
		if(ans == INT_MAX) puts("-1");
		else printf("%d\n" , ans);
	}
}
/* clever stuff:
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and STAY ORGANIZED
	* Keep it simple stupid
	* WRITE STUFF DOWN
	* math -> gcd / lcm / divisors?
*/


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x) , end(x)
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
ll modpow(ll b, ll e ,ll mod) { ll ans = 1;for (; e; b = b * b % mod, e /= 2){if (e & 1) ans = ans * b % mod;}return ans;}
const int N = 300005;
vi hist[N];
int a[N];
int32_t main(){
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	int n  , q;
	scanf("%d%d" , &n , &q);
	rep(i,1,n+1){
		scanf("%d" , &a[i]);
		hist[a[i]].push_back(i);
	}
	while(q--){
		int l , r ,k;
		scanf("%d%d%d" , &l , &r , &k);
		int ans = INT_MAX , sz = r - l + 1;
		for(int j = 0 ; j < 200; j ++){
			int p = a[rng()%sz + l];
			if((upper_bound(all(hist[p]), r) - lower_bound(all(hist[p]), l))*k > sz){
				ans = min(ans , p);
			}
		}
		if(ans == INT_MAX) puts("-1");
		else printf("%d\n" , ans);
	}
}
/* clever stuff:
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and STAY ORGANIZED
	* Keep it simple stupid
	* WRITE STUFF DOWN
	* math -> gcd / lcm / divisors?
*/


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 150;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  int x = rand() & 0x0000007f;
  x = x << 8;
  x |= rand() & 0x000000ff;
  x = x << 8;
  x |= rand() & 0x000000ff;
  x = x << 8;
  x |= rand() & 0x000000ff;
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 100;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  return rand() * rand();
  int x = rand() & 0x00007fff;
  x = (x << 15) | (rand() & 0x00007fff);
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 80;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  int x = rand() & 0x00007fff;
  x = (x << 15) | (rand() & 0x00007fff);
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 80;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  int x = rand() & 0x00007fff;
  x = (x << 15) | (x);
  // x |= rand() & 0x00007fff;
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 80;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  return rand() * rand() + rand();
  int x = rand() & 0x00007fff;
  x = (x << 15) | (rand() & 0x00007fff);
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(747444747);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 100;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  int x = rand() & 0x00007fff;
  x = x << 15;
  x |= rand() & 0x00007fff;
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 80;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  return (rand() << 15) | rand();
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(747444747);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt * k > (r - l + 1)) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 100;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  int x = rand() & 0x00007fff;
  x = x << 15;
  x |= rand() & 0x00007fff;
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 80;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  return rand() * rand();
  int x = rand() & 0x00007fff;
  x = (x << 15) | (rand() & 0x00007fff);
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 150;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int main()
{
  srand(47477474);
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
  }
  REP(i,q) printf("%d\n",risan[i]);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 150;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  int x = rand() & 0x00007fff;
  x = x << 15;
  x |= rand() & 0x00007fff;
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 80;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  int x = rand() & 0x00007fff;
  x = x << 15;
  x |= rand() & 0x00007fff;
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(47477474);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
int ada[300005];
pair<pii,pii> query[300005];
int risan[300005];
int COBA = 80;
int LIM = 550;

bool f(pair<pii,pii> a, pair<pii,pii> b)
{
  if (a.fi.fi / LIM != b.fi.fi / LIM) return a.fi.fi / LIM < b.fi.fi / LIM;
  return a.fi.se > b.fi.se;
}

void update(int x,int y)
{
  ada[a[x]] += y;
}

int _rand()
{
  return rand() * rand();
  int x = rand() & 0x00007fff;
  x = (x << 15) | (rand() & 0x00007fff);
  return x;
}

int main()
{
  // REP(i,100) VALUE(_rand());
  // return 0;
  srand(747444747);
  // int err = 0;
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
  }
  REP(i,q)
  {
    scanf("%d %d %d",&query[i].fi.fi,&query[i].fi.se,&query[i].se.fi);
    --query[i].fi.fi;
    --query[i].fi.se;
    query[i].se.se = i;
  }
  sort(ALLA(query,q),f);
  int L = 0;
  int R = 0;
  REP(i,q)
  {
    while (R <= query[i].fi.se)
    {
      update(R,1);
      ++R;
    }
    while (R > query[i].fi.se + 1)
    {
      --R;
      update(R,-1);
    }
    while (L < query[i].fi.fi)
    {
      update(L,-1);
      ++L;
    }
    while (L > query[i].fi.fi)
    {
      --L;
      update(L,1);
    }
    int ans = INF;
    int l = query[i].fi.fi;
    int r = query[i].fi.se;
    int k = query[i].se.fi;
    REP(i,COBA)
    {
      int x = a[_rand() % (r - l + 1) + l];
      int cnt = ada[x];
      if (cnt > (r - l + 1) / k) MIN(ans,x);
    }
    risan[query[i].se.se] = ans == INF ? -1 : ans;
    // if (ans == INF) ++err;
  }
  REP(i,q) printf("%d\n",risan[i]);
  // VALUE(err);
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
vi pos[300005];
int COBA = 60;

int query(int x,int L,int R)
{
  return upper_bound(ALL(pos[x]),R) - lower_bound(ALL(pos[x]),L);
}

int main()
{
  srand(47477474);
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
    pos[a[i]].pb(i);
  }
  TC(q)
  {
    scanf("%d %d %d",&l,&r,&k);
    --l; --r;
    int risan = INF;
    REP(i,COBA)
    {
      int x = a[rand() % (r - l + 1) + l];
      int ada = query(x,l,r);
      if (ada > (r - l + 1) / k) MIN(risan,x);
      // if (risan == 1) break;
    }
    if (risan == INF) puts("-1");
    else printf("%d\n",risan);
  }
  return 0;
}


//start of jonathanirvings' template v3.0.3 (BETA)

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

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;

double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

#ifdef TESTING
  #define DEBUG fprintf(stderr,"====TESTING====\n")
  #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DEBUG
  #define VALUE(x)
  #define debug(...)
#endif

#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define REP(i,n) FOR(i,0,n)
#define REPN(i,n) FORN(i,1,n)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)

inline string IntToString(LL a){
  char x[100];
  sprintf(x,"%lld",a); string s = x;
  return s;
}

inline LL StringToInt(string a){
  char x[100]; LL res;
  strcpy(x,a.c_str()); sscanf(x,"%lld",&res);
  return res;
}

inline string GetString(void){
  char x[1000005];
  scanf("%s",x); string s = x;
  return s;
}

inline string uppercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
  return s;
}

inline string lowercase(string s){
  int n = SIZE(s);
  REP(i,n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
  return s;
}

inline void OPEN (string s) {
  #ifndef TESTING
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
  #endif
}

//end of jonathanirvings' template v3.0.3 (BETA)

int n,q,l,r,k;
int a[300005];
vi pos[300005];
int COBA = 70;

int query(int x,int L,int R)
{
  return upper_bound(ALL(pos[x]),R) - lower_bound(ALL(pos[x]),L);
}

int main()
{
  srand(47477474);
  scanf("%d %d",&n,&q);
  REP(i,n)
  {
    scanf("%d",&a[i]);
    pos[a[i]].pb(i);
  }
  TC(q)
  {
    scanf("%d %d %d",&l,&r,&k);
    --l; --r;
    int risan = INF;
    REP(i,COBA)
    {
      int x = a[rand() % (r - l + 1) + l];
      int ada = query(x,l,r);
      if (ada > (r - l + 1) / k) MIN(risan,x);
    }
    if (risan == INF) puts("-1");
    else printf("%d\n",risan);
  }
  return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
int ac[280][N];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = sqrt(n)/2 + 1;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			for(int j = 0; j < adj[i].size(); ++j)
				ac[t-1][adj[i][j]] = 1;

			for(int i = 1; i <= n; ++i)
				ac[t-1][i] += ac[t-1][i-1];

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 4*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{

			for(int v : best)
				if(ac[inv[v]][r] - ac[inv[v]][l-1] > need)
					ans = min(ans, v);
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
vector<int> ac[430];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = 700;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			ac[t-1].resize(N);

			for(int j = 0; j < adj[i].size(); ++j)
				ac[t-1][adj[i][j]] = 1;

			for(int i = 1; i <= n; ++i)
				ac[t-1][i] += ac[t-1][i-1];

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 5*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{

			for(int v : best)
				if(ac[inv[v]][r] - ac[inv[v]][l-1] > need)
					ans = min(ans, v);
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
unsigned short acx[550][N];
unsigned char acy[550][N];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = 555;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			for(int j = 0; j < adj[i].size(); ++j)
				acy[t-1][adj[i][j]] = 1;

			for(int i = 1; i <= n; ++i){
				acx[t-1][i] += acx[t-1][i-1];
				int temp = acy[t-1][i] + 0 + acy[t-1][i-1];
				acx[t-1][i] += temp / 255;
				acy[t-1][i] = temp%255;
			}

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 4*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{
			for(int v : best){
				int dx = acx[inv[v]][r] * 255 + acy[inv[v]][r] - acx[inv[v]][l-1] * 255 - acy[inv[v]][l-1];
				if(dx > need)
				    ans = min(ans, v);
			}
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


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

const int N = 3e5 + 5;
int n, q, sq, nb;
int answer[N];

struct Query{
	int l, r, k;
} Q[N];

bool cmp(int x, int y){
	return Q[x].r < Q[y].r;
}

int a[N];
int C[N];
vector<int> small, block[500];
set<pair<int, int>> Set;

void solve(){

	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; ++i)
		scanf("%d", a+i);

	for(int i = 0; i < q; ++i){
		int l, r, k;

		scanf("%d %d %d", &l, &r, &k);
		Q[i] = {l, r, k};
	}

	sq = sqrt(n) + 1;
	nb = n/sq + 1;

	for(int i = 0; i < q; ++i)
		if(Q[i].r - Q[i].l + 1 <= 2200) small.push_back(i);
		else block[Q[i].l/sq + 1].push_back(i);

	int len, fr, ans, pos;
	vector<int> aux(5);
	for(int i = 1; i < nb; ++i){
		if(not block[i].empty()){
			sort(block[i].begin(), block[i].end(), cmp);
			memset(C, 0, sizeof C);
			Set.clear();

			len = 0;
			pos = i * sq;
			for(int v : block[i]){
				while(pos <= Q[v].r){
					int x = a[pos];
					C[x] += 1;
					len += 1;

					if(C[x] == 1) Set.insert({C[x], x});
					else{
						Set.erase({C[x]-1, x});
						Set.insert({C[x], x});
					}

					if(Set.size() > 4) Set.erase(Set.begin());
					pos += 1;
				}

				int nn = 0;
				for(auto s : Set)
					aux[nn++] = s.second;

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] += 1;
					len += 1;
				}

				fr = len / Q[v].k;
				ans = INT_MAX;
				for(int j = 0; j < nn; ++j)
					if(C[aux[j]] > fr)
						ans = min(ans, aux[j]);

				for(int j = Q[v].l; j < i * sq; ++j)
					if(C[a[j]] > fr)
						ans = min(ans, a[j]);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] -= 1;
					len -= 1;
				}

				answer[v] = ans == INT_MAX ? -1 : ans;
			}
		}
	}

	len = 0;
	memset(C, 0, sizeof C);
	for(int v : small){
		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] += 1;
			len += 1;
		}

		fr = len / Q[v].k;
		ans = INT_MAX;

		for(int j = Q[v].l; j <= Q[v].r; ++j)
			if(C[a[j]] > fr)
				ans = min(ans, a[j]);

		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] -= 1;
			len -= 1;
		}

		answer[v] = ans == INT_MAX ? -1 : ans;
	}

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

int main(){

	solve();

	return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
struct{
	short x;
	char y;

} ac[550][N];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = 555;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			for(int j = 0; j < adj[i].size(); ++j)
				ac[t-1][adj[i][j]].y = 1;

			for(int i = 1; i <= n; ++i){
				ac[t-1][i].x += ac[t-1][i-1].x;
				int temp = ac[t-1][i].y + 0 + ac[t-1][i-1].y;
				ac[t-1][i].x += temp / 255;
				ac[t-1][i].y = temp%255;
			}

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 4*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{
			for(int v : best){
				int dx = ac[inv[v]][r].x * 255 + ac[inv[v]][r].y - ac[inv[v]][l-1].x * 255 - ac[inv[v]][l-1].y;
				if(dx > need)
				    ans = min(ans, v);
			}
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
int ac[280][N];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = sqrt(n)/2 + 1;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			for(int j = 0; j < adj[i].size(); ++j)
				ac[t-1][adj[i][j]] = 1;

			for(int i = 1; i <= n; ++i)
				ac[t-1][i] += ac[t][i-1];

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 4*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{

			for(int v : best)
				if(ac[inv[v]][r] - ac[inv[v]][l-1] > need)
					ans = min(ans, v);
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


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

const int N = 3e5 + 5;
int n, q, sq, nb;
int answer[N];

struct Query{
	int l, r, k;
} Q[N];

bool cmp(int x, int y){
	return Q[x].r < Q[y].r;
}

int a[N];
int C[N];
vector<int> small, block[500];
set<pair<int, int>> Set;

void solve(){

	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; ++i)
		scanf("%d", a+i);

	for(int i = 0; i < q; ++i){
		int l, r, k;

		scanf("%d %d %d", &l, &r, &k);
		Q[i] = {l, r, k};
	}

	sq = sqrt(n) + 1;
	nb = n/sq + 1;

	for(int i = 0; i < q; ++i)
		if(Q[i].r - Q[i].l + 1 < sq) small.push_back(i);
		else block[Q[i].l/sq + 1].push_back(i);

	for(int i = 1; i < nb; ++i){
		if(not block[i].empty()){
			sort(block[i].begin(), block[i].end(), cmp);
			memset(C, 0, sizeof C);

			int len = 0;
			int pos = i * sq;
			for(int v : block[i]){
				while(pos <= Q[v].r){
					int x = a[pos];
					C[x] += 1;
					len += 1;

					if(C[x] == 1) Set.insert({C[x], x});
					else{
						Set.erase({C[x]-1, x});
						Set.insert({C[x], x});
					}

					if(Set.size() > 4) Set.erase(Set.begin());
					pos += 1;
				}

				vector<int> aux;

				for(auto s : Set)
					aux.push_back(s.second);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] += 1;
					len += 1;
				}

				int fr = len / Q[v].k;
				int ans = INT_MAX;
				for(int s : aux)
					if(C[s] > fr)
						ans = min(ans, s);

				for(int j = Q[v].l; j < i * sq; ++j)
					if(C[a[j]] > fr)
						ans = min(ans, a[j]);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] -= 1;
					len -= 1;
				}

				answer[v] = ans == INT_MAX ? -1 : ans;
			}
		}
	}

	int len = 0;
	memset(C, 0, sizeof C);
	for(int v : small){
		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] += 1;
			len += 1;
		}

		int fr = len / Q[v].k;
		int ans = INT_MAX;

		for(int j = Q[v].l; j <= Q[v].r; ++j)
			if(C[a[j]] > fr)
				ans = min(ans, a[j]);

		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] -= 1;
			len -= 1;
		}

		answer[v] = ans == INT_MAX ? -1 : ans;
	}

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

int main(){

	solve();

	return 0;
}


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

const int N = 3e5 + 5;
int n, q, sq, nb;
int answer[N];

struct Query{
	int l, r, k;
} Q[N];

bool cmp(int x, int y){
	return Q[x].r < Q[y].r;
}
//dsdsd
int a[N];
int C[N];
vector<int> small, block[500];
set<pair<int, int>> Set;

void solve(){

	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; ++i)
		scanf("%d", a+i);

	for(int i = 0; i < q; ++i){
		int l, r, k;

		scanf("%d %d %d", &l, &r, &k);
		Q[i] = {l, r, k};
	}

	sq = sqrt(n) + 1;
	nb = n/sq + 1;

	for(int i = 0; i < q; ++i)
		if(Q[i].r - Q[i].l + 1 <= 2500) small.push_back(i);
		else block[Q[i].l/sq + 1].push_back(i);

	int len, fr, ans, pos;
	vector<int> aux(5);
	for(int i = 1; i < nb; ++i){
		if(not block[i].empty()){
			sort(block[i].begin(), block[i].end(), cmp);
			memset(C, 0, sizeof C);
			Set.clear();

			len = 0;
			pos = i * sq;
			for(int v : block[i]){
				while(pos <= Q[v].r){
					int x = a[pos];
					C[x] += 1;
					len += 1;

					if(C[x] == 1) Set.insert({C[x], x});
					else{
						Set.erase({C[x]-1, x});
						Set.insert({C[x], x});
					}

					if(Set.size() > 4) Set.erase(Set.begin());
					pos += 1;
				}

				int nn = 0;
				for(auto s : Set)
					aux[nn++] = s.second;

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] += 1;
					len += 1;
				}

				fr = len / Q[v].k;
				ans = INT_MAX;
				for(int j = 0; j < nn; ++j)
					if(C[aux[j]] > fr)
						ans = min(ans, aux[j]);

				for(int j = Q[v].l; j < i * sq; ++j)
					if(C[a[j]] > fr)
						ans = min(ans, a[j]);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] -= 1;
					len -= 1;
				}

				answer[v] = ans == INT_MAX ? -1 : ans;
			}
		}
	}

	len = 0;
	memset(C, 0, sizeof C);
	for(int v : small){
		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] += 1;
			len += 1;
		}

		fr = len / Q[v].k;
		ans = INT_MAX;

		for(int j = Q[v].l; j <= Q[v].r; ++j)
			if(C[a[j]] > fr)
				ans = min(ans, a[j]);

		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] -= 1;
			len -= 1;
		}

		answer[v] = ans == INT_MAX ? -1 : ans;
	}

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

int main(){

	solve();

	return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
int ac[430][N];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = 700;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			for(int j = 0; j < adj[i].size(); ++j)
				ac[t-1][adj[i][j]] = 1;

			for(int i = 1; i <= n; ++i)
				ac[t-1][i] += ac[t-1][i-1];

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 4*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{

			for(int v : best)
				if(ac[inv[v]][r] - ac[inv[v]][l-1] > need)
					ans = min(ans, v);
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


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

const int N = 3e5 + 5;
int n, q, sq, nb;
int answer[N];

struct Query{
	int l, r, k;
} Q[N];

bool cmp(int x, int y){
	return Q[x].r < Q[y].r;
}

int a[N];
int C[N];
vector<int> small, block[500];
set<pair<int, int>> Set;

void solve(){

	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; ++i)
		scanf("%d", a+i);

	for(int i = 0; i < q; ++i){
		int l, r, k;

		scanf("%d %d %d", &l, &r, &k);
		Q[i] = {l, r, k};
	}

	sq = sqrt(n) + 1;
	nb = n/sq + 1;

	for(int i = 0; i < q; ++i)
		if(Q[i].r - Q[i].l + 1 < sq) small.push_back(i);
		else block[Q[i].l/sq + 1].push_back(i);

	for(int i = 1; i < nb; ++i){
		if(not block[i].empty()){
			sort(block[i].begin(), block[i].end(), cmp);

			int len = 0;
			int pos = i * sq;
			for(int v : block[i]){
				while(pos <= Q[v].r){
					int x = a[pos];
					C[x] += 1;
					len += 1;

					if(C[x] == 1) Set.insert({C[x], x});
					else{
						Set.erase({C[x]-1, x});
						Set.insert({C[x], x});
					}

					if(Set.size() > 4) Set.erase(Set.begin());
					pos += 1;
				}

				vector<int> aux;

				for(auto s : Set)
					aux.push_back(s.second);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] += 1;
					len += 1;
				}

				int fr = len / Q[v].k;
				int ans = INT_MAX;
				for(int s : aux)
					if(C[s] > fr)
						ans = min(ans, s);

				for(int j = Q[v].l; j < i * sq; ++j)
					if(C[a[j]] > fr)
						ans = min(ans, a[j]);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] -= 1;
					len -= 1;
				}

				answer[v] = ans == INT_MAX ? -1 : ans;
			}
		}
	}

	int len = 0;
	for(int v : small){
		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] += 1;
			len += 1;
		}

		int fr = len / Q[v].k;
		int ans = INT_MAX;

		for(int j = Q[v].l; j <= Q[v].r; ++j)
			if(C[a[j]] > fr)
				ans = min(ans, a[j]);

		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] -= 1;
			len -= 1;
		}

		answer[v] = ans == INT_MAX ? -1 : ans;
	}

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

int main(){

	solve();

	return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
int ac[280][N];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = sqrt(n)/2 + 1;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			for(int j = 0; j < adj[i].size(); ++j)
				ac[t][adj[i][j]] = 1;

			for(int i = 1; i <= n; ++i)
				ac[t][i] += ac[t][i-1];

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 4*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{

			for(int v : best)
				if(ac[inv[v]][r] - ac[inv[v]][l-1] > need)
					ans = min(ans, v);
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


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

const int N = 3e5 + 5;
int n, q, sq, nb;
int answer[N];

struct Query{
	int l, r, k;
} Q[N];

bool cmp(int x, int y){
	return Q[x].r < Q[y].r;
}

int a[N];
int C[N];
vector<int> small, block[500];
set<pair<int, int>> Set;

void solve(){

	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; ++i)
		scanf("%d", a+i);

	for(int i = 0; i < q; ++i){
		int l, r, k;

		scanf("%d %d %d", &l, &r, &k);
		Q[i] = {l, r, k};
	}

	sq = sqrt(n) + 1;
	nb = n/sq + 1;

	for(int i = 0; i < q; ++i)
		if(Q[i].r - Q[i].l + 1 <= 2500) small.push_back(i);
		else block[Q[i].l/sq + 1].push_back(i);

	int len, fr, ans, pos;
	vector<int> aux(5);
	for(int i = 1; i < nb; ++i){
		if(not block[i].empty()){
			sort(block[i].begin(), block[i].end(), cmp);
			memset(C, 0, sizeof C);
			Set.clear();

			len = 0;
			pos = i * sq;
			for(int v : block[i]){
				while(pos <= Q[v].r){
					int x = a[pos];
					C[x] += 1;
					len += 1;

					if(C[x] == 1) Set.insert({C[x], x});
					else{
						Set.erase({C[x]-1, x});
						Set.insert({C[x], x});
					}

					if(Set.size() > 4) Set.erase(Set.begin());
					pos += 1;
				}

				int nn = 0;
				for(auto s : Set)
					aux[nn++] = s.second;

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] += 1;
					len += 1;
				}

				fr = len / Q[v].k;
				ans = INT_MAX;
				for(int j = 0; j < nn; ++j)
					if(C[aux[j]] > fr)
						ans = min(ans, aux[j]);

				for(int j = Q[v].l; j < i * sq; ++j)
					if(C[a[j]] > fr)
						ans = min(ans, a[j]);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] -= 1;
					len -= 1;
				}

				answer[v] = ans == INT_MAX ? -1 : ans;
			}
		}
	}

	len = 0;
	memset(C, 0, sizeof C);
	for(int v : small){
		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] += 1;
			len += 1;
		}

		fr = len / Q[v].k;
		ans = INT_MAX;

		for(int j = Q[v].l; j <= Q[v].r; ++j)
			if(C[a[j]] > fr)
				ans = min(ans, a[j]);

		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] -= 1;
			len -= 1;
		}

		answer[v] = ans == INT_MAX ? -1 : ans;
	}

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

int main(){

	solve();

	return 0;
}


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

const int N = 3e5 + 5;
int n, q, sq, nb;
int answer[N];

struct Query{
	int l, r, k;
} Q[N];

bool cmp(int x, int y){
	return Q[x].r < Q[y].r;
}

int a[N];
int C[N];
vector<int> small, block[500];
set<pair<int, int>> Set;

void solve(){

	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; ++i)
		scanf("%d", a+i);

	for(int i = 0; i < q; ++i){
		int l, r, k;

		scanf("%d %d %d", &l, &r, &k);
		Q[i] = {l, r, k};
	}

	sq = sqrt(n) + 1;
	nb = n/sq + 1;

	for(int i = 0; i < q; ++i)
		if(Q[i].r - Q[i].l + 1 <= 2200) small.push_back(i);
		else block[Q[i].l/sq + 1].push_back(i);

	int len, fr, ans;
	vector<int> aux(5);
	for(int i = 1; i < nb; ++i){
		if(not block[i].empty()){
			sort(block[i].begin(), block[i].end(), cmp);
			memset(C, 0, sizeof C);
			Set.clear();

			len = 0;
			pos = i * sq;
			for(int v : block[i]){
				while(pos <= Q[v].r){
					int x = a[pos];
					C[x] += 1;
					len += 1;

					if(C[x] == 1) Set.insert({C[x], x});
					else{
						Set.erase({C[x]-1, x});
						Set.insert({C[x], x});
					}

					if(Set.size() > 4) Set.erase(Set.begin());
					pos += 1;
				}

				int nn = 0;
				for(auto s : Set)
					aux[nn++] = s.second;

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] += 1;
					len += 1;
				}

				fr = len / Q[v].k;
				ans = INT_MAX;
				for(int j = 0; j < nn; ++j)
					if(C[aux[j]] > fr)
						ans = min(ans, aux[j]);

				for(int j = Q[v].l; j < i * sq; ++j)
					if(C[a[j]] > fr)
						ans = min(ans, a[j]);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] -= 1;
					len -= 1;
				}

				answer[v] = ans == INT_MAX ? -1 : ans;
			}
		}
	}

	len = 0;
	memset(C, 0, sizeof C);
	for(int v : small){
		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] += 1;
			len += 1;
		}

		fr = len / Q[v].k;
		ans = INT_MAX;

		for(int j = Q[v].l; j <= Q[v].r; ++j)
			if(C[a[j]] > fr)
				ans = min(ans, a[j]);

		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] -= 1;
			len -= 1;
		}

		answer[v] = ans == INT_MAX ? -1 : ans;
	}

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

int main(){

	solve();

	return 0;
}


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

const int N = 3e5 + 5;
int n, q, sq, nb;
int answer[N];

struct Query{
	int l, r, k;
} Q[N];

bool cmp(int x, int y){
	return Q[x].r < Q[y].r;
}

int a[N];
int C[N];
vector<int> small, block[500];
set<pair<int, int>> Set;

void solve(){

	scanf("%d %d", &n, &q);
	for(int i = 1; i <= n; ++i)
		scanf("%d", a+i);

	for(int i = 0; i < q; ++i){
		int l, r, k;

		scanf("%d %d %d", &l, &r, &k);
		Q[i] = {l, r, k};
	}

	sq = sqrt(2*n) + 1;
	nb = n/sq + 1;

	for(int i = 0; i < q; ++i)
		if(Q[i].r - Q[i].l + 1 < sq) small.push_back(i);
		else block[Q[i].l/sq + 1].push_back(i);

	for(int i = 1; i < nb; ++i){
		if(not block[i].empty()){
			sort(block[i].begin(), block[i].end(), cmp);
			memset(C, 0, sizeof C);
			Set.clear();

			int len = 0;
			int pos = i * sq;
			for(int v : block[i]){
				while(pos <= Q[v].r){
					int x = a[pos];
					C[x] += 1;
					len += 1;

					if(C[x] == 1) Set.insert({C[x], x});
					else{
						Set.erase({C[x]-1, x});
						Set.insert({C[x], x});
					}

					if(Set.size() > 4) Set.erase(Set.begin());
					pos += 1;
				}

				vector<int> aux;

				for(auto s : Set)
					aux.push_back(s.second);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] += 1;
					len += 1;
				}

				int fr = len / Q[v].k;
				int ans = INT_MAX;
				for(int s : aux)
					if(C[s] > fr)
						ans = min(ans, s);

				for(int j = Q[v].l; j < i * sq; ++j)
					if(C[a[j]] > fr)
						ans = min(ans, a[j]);

				for(int j = Q[v].l; j < i * sq; ++j){
					C[a[j]] -= 1;
					len -= 1;
				}

				answer[v] = ans == INT_MAX ? -1 : ans;
			}
		}
	}

	int len = 0;
	memset(C, 0, sizeof C);
	for(int v : small){
		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] += 1;
			len += 1;
		}

		int fr = len / Q[v].k;
		int ans = INT_MAX;

		for(int j = Q[v].l; j <= Q[v].r; ++j)
			if(C[a[j]] > fr)
				ans = min(ans, a[j]);

		for(int j = Q[v].l; j <= Q[v].r; ++j){
			C[a[j]] -= 1;
			len -= 1;
		}

		answer[v] = ans == INT_MAX ? -1 : ans;
	}

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

int main(){

	solve();

	return 0;
}


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

const int N = 3E5 + 5;
int a[N];
int n, q;
vector<int> adj[N];
int inv[N];
int ac[500][N];
vector<int> best;
int C[N];

int main(){

	scanf("%d %d", &n, &q);

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

	int sq = sqrt(n)/2 + 1;
	int t = 0;
	for(int i = 1; i <= n; ++i)
		if(adj[i].size() >= sq){
			inv[i] = t++;

			for(int j = 0; j < adj[i].size(); ++j)
				ac[t][adj[i][j]] = 1;

			for(int i = 1; i <= n; ++i)
				ac[t][i] += ac[t][i-1];

			best.push_back(i);
		}

	while(q--){

		int l, r, k;
		scanf("%d %d %d", &l, &r, &k);

		int need = (r - l + 1)/k;
		int ans = INT_MAX;
		if(r - l + 1 <= 4*sq){
			for(int i = l; i <= r; ++i){
				C[a[i]] += 1;
				if(C[a[i]] > need) ans = min(ans, a[i]);
			}

			for(int i = l; i <= r; ++i)
				C[a[i]] -= 1;
		}
		else{

			for(int v : best)
				if(ac[inv[v]][r] - ac[inv[v]][l-1] > need)
					ans = min(ans, v);
		}

		printf("%d\n", ans == INT_MAX ? -1 : ans);
	}

	return 0;
}


#include<bits/stdc++.h>
#define N 300000
using namespace std;
struct Seg{
    static Seg mem[30 * N], *pmem;
    Seg *Ls, *Rs;
    int sz;
    Seg(){};
    Seg(int nL, int nR) : sz(0){
        if(nL==nR) return;
        int mid = (nL+nR)>>1;
        Ls = new (pmem++) Seg(nL, mid);
        Rs = new (pmem++) Seg(mid+1, nR);
    }
    Seg* ins(int nL, int nR, int v){
        Seg *t = new (pmem++) Seg (*this);
        t->sz++;
        if(nL!=nR){
            int mid = (nL+nR)>>1;
            if( v <= mid ){
                t->Ls = t->Ls->ins(nL, mid, v);
            } else {
                t->Rs = t->Rs->ins(mid+1, nR, v);
            }
        }
        return t;
    }
}Seg::mem[30 * N], *Seg::pmem = mem;
int n,Q,v,Ql,Qr,k,ans;
Seg *ver[N+10];
void Query(int nL, int nR, Seg *Ql, Seg *Qr){
    if(nL==nR){
        if( (Qr->sz - Ql->sz)>k ){
            ans = min(ans, nL);
        }
    } else {
        int mid = (nL+nR)>>1;
        if( Qr->Ls->sz - Ql->Ls->sz > k ) Query(nL, mid, Ql->Ls, Qr->Ls);
        if( Qr->Rs->sz - Ql->Rs->sz > k ) Query(mid+1, nR, Ql->Rs, Qr->Rs);
    }
    return ;
}
int main(void){
    scanf("%d%d",&n,&Q);
    ver[0] = new (Seg::pmem++) Seg(1,n);
    for(int i=1;i<=n;++i){
        scanf("%d",&v);
        ver[i] = ver[i-1] -> ins(1, n, v) ;
    }
    while(Q--){
        scanf("%d%d%d",&Ql, &Qr, &k);
        --Ql;
        k = (Qr - Ql) / k;
        ans = N+10;
        Query(1, n, ver[Ql], ver[Qr]);
        printf("%d\n",ans==N+10?-1:ans);
    }
}


#include<bits/stdc++.h>
#define N 300000
#define SZ(x) (int)(x.size())
#define F first
#define S second
using namespace std;
int arr[N+5], ans[N+5], nbidL[1000], cnt[N+5];
struct Query{
    int L, R, id, k, bid;
    bool operator < (const Query &rhs) const {
        return bid < rhs.bid || ( bid==rhs.bid && R < rhs.R ) ;
    }
};
vector<Query> Q;
struct pre_sit{
    int cnt_num, cnt_val, sv_num, sv_val, curadd;
}pre[1000];
int pre_cur;
int sv[10], cur;
int get_Ans(Query &x){
    int mn_id = N, num = (x.R - x.L + 1) / x.k ;
    for(int i=0;i<cur;++i){
        if( cnt[sv[i]] > num ){
            mn_id = min(mn_id, sv[i]);
        }
    }
    return mn_id==N?-1:mn_id ;
}
pre_sit Add(int x){
    pre_sit res;
    res.curadd = 0;
    int fd = -1;
    for(int i=0;i<cur;++i){
        if(sv[i]==x){
            fd = i;
        }
    }
    res.cnt_num = x, res.cnt_val = cnt[x];
    ++cnt[x];
    if(fd>=0){;}
    else {
        int mn_pl = 0;
        for(int i=1;i<cur;++i){
            if( cnt[sv[i]] < cnt[sv[mn_pl]] ){
                mn_pl = i;
            }
        }
        if(cur<4){
            res.sv_num = cur;
            res.sv_val = sv[cur];
            res.curadd = -1;
            sv[cur++] = x;
        }
        else if( cnt[x] > cnt[sv[mn_pl]] ){
            res.sv_num = mn_pl;
            res.sv_val = sv[mn_pl];
            sv[mn_pl] = x;
        }
    }
    return res;
}
void sol(Query &ip){
    for(int i=ip.L; i<=ip.R; ++i){
        Add(arr[i]);
    }
    ans[ip.id] = get_Ans(ip);
    cur=0;
    memset(cnt, 0, sizeof(cnt));
}
void Mo(){
    sort(Q.begin(), Q.end());
    int nL, nR;
    for(int i=0; i<SZ(Q); ++i){
        nL = nbidL[Q[i].bid];
        if(!i||Q[i].bid>Q[i-1].bid){
            nR = nL-1;
            cur=0;
            memset(cnt, 0, sizeof(cnt));
        } else {
            for(int j=pre_cur-1;j>=0;--j){
                cnt[ pre[j].cnt_num ] = pre[j].cnt_val ;
                 sv[ pre[j].sv_num  ] = pre[j].sv_val  ;
                cur+=pre[j].curadd;
            }
        }
        while(nR<Q[i].R) Add( arr[++nR] );
        pre_cur = 0;
        while(nL>Q[i].L) pre [ pre_cur++ ] = Add( arr[--nL] );
        ans[Q[i].id] = get_Ans(Q[i]);
    }
}

int main(void){
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;++i) scanf("%d",arr+i);
    int sqrn = sqrt(n);
    Query ip;
    for(int i=0;i<q;++i){
        scanf("%d%d%d", &ip.L, &ip.R, &ip.k);
        ip.L--, ip.R--;
        ip.id = i;
        if(ip.L / sqrn==ip.R / sqrn){
            sol(ip);
        } else {
            ip.bid = ip.L / sqrn;
            nbidL[ip.bid] = max(nbidL[ip.bid], ip.L);
            Q.push_back(ip);
        }
    }
    Mo();
    for(int i=0;i<q;++i) printf("%d\n",ans[i]);
    return 0;
}


#include<bits/stdc++.h>
#define N 300000
#define SZ(x) (int)(x.size())
#define F first
#define S second
using namespace std;
int arr[N+5], ans[N+5], nbidL[1000], cnt[N+5];
struct Query{
    int L, R, id, k, bid;
    bool operator < (const Query &rhs) const {
        return bid < rhs.bid || ( bid==rhs.bid && R < rhs.R ) ;
    }
};
vector<Query> Q;
struct pre_sit{
    int cnt_num, cnt_val, sv_num, sv_val, curadd;
}pre[N+5];
int pre_cur;
int sv[4], cur;
int get_Ans(Query &x){
    int mn_id = N, num = (x.R - x.L + 1) / x.k ;
    for(int i=0;i<cur;++i){
        if( cnt[sv[i]] > num ){
            mn_id = min(mn_id, sv[i]);
        }
    }
    return mn_id==N?-1:mn_id ;
}
pre_sit Add(int x){
    pre_sit res;
    res.curadd = 0;
    int fd = -1;
    for(int i=0;i<cur;++i){
        if(sv[i]==x){
            fd = i;
        }
    }
    res.cnt_num = x, res.cnt_val = cnt[x];
    ++cnt[x];
    if(fd>=0){
        res.sv_val = sv[ res.sv_num = fd ];
        sv[fd] = x;
    } else {
        int mn_pl = 0;
        for(int i=1;i<cur;++i){
            if( cnt[sv[i]] < cnt[sv[mn_pl]] ){
                mn_pl = i;
            }
        }
        if(cur<4){
            res.curadd = -1;
            res.sv_num = cur;
            res.sv_val = sv[cur];
            sv[cur++] = x;
        }
        else if( cnt[x] > cnt[sv[mn_pl]] ){
            res.sv_num = mn_pl;
            res.sv_val = sv[mn_pl];
            sv[mn_pl] = x;
        }
    }
    return res;
}
void sol(Query &ip){
    for(int i=ip.L; i<=ip.R; ++i){
        Add(arr[i]);
    }
    ans[ip.id] = get_Ans(ip);
    cur=0;
    memset(cnt, 0, sizeof(cnt));
}
void Mo(){
    sort(Q.begin(), Q.end());
    int nL, nR, bnL, bnR;
    for(int i=0; i<SZ(Q); ++i){
        if(!i||Q[i].bid>Q[i-1].bid){
            nL = nbidL[Q[i].bid];
            nR = nL-1;
        } else {
            for(int j=pre_cur-1;j>=0;--j){
                cnt[ pre[j].cnt_num ] = pre[j].cnt_val ;
                 sv[ pre[j].sv_num  ] = pre[j].sv_val  ;
                cur+=pre[j].curadd;
            }
            nL = bnL, nR = bnR;
        }
        while(nR<Q[i].R) Add( arr[++nR] );
        pre_cur = 0;
        while(nL>Q[i].L) pre [ pre_cur++ ] = Add( arr[--nL] );
        ans[Q[i].id] = get_Ans(Q[i]);
        bnL = nL, bnR = nR;
    }
}

int main(void){
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;++i) scanf("%d",arr+i);
    int sqrn = sqrt(n);
    Query ip;
    for(int i=0;i<q;++i){
        scanf("%d%d%d", &ip.L, &ip.R, &ip.k);
        ip.L--, ip.R--;
        ip.id = i;
        if(ip.L / sqrn==ip.R / sqrn){
            sol(ip);
        } else {
            ip.bid = ip.L / sqrn;
            nbidL[ip.bid] = max(nbidL[ip.bid], ip.L);
            Q.push_back(ip);
        }
    }
    Mo();
    for(int i=0;i<q;++i) printf("%d\n",ans[i]);
    return 0;
}