// <3 Appy!
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int fact[1000009], visit[1000009], total[1000009], choice[1000009];
int mod = 1e9 + 7;
ll power(int x, int y)
{
    if(y == 0)
        return 1;
    ll ans = power(x, y/2);
    if(y%2)
        return (((ans*ans) % mod) * x) % mod;
    return (ans * ans) % mod;
}
void facto()
{
    fact[0] = 1;
    for(int i = 1; i <= 1e6; i++)
        fact[i] = (1LL * fact[i-1] * i) % mod;
}
int root(int x)
{
    while(x != visit[x])
    {
        visit[x] = visit[visit[x]];
        x = visit[x];
    }
    return x;
}
ll calInverse(int x)
{
    return power(x, mod-2) % mod;
}
ll nCr(int n, int r)
{
    if(n == 0 || n - r == 0)
        return 1;
    return ((fact[n] * calInverse(fact[r])) % mod) * calInverse(fact[(n-r)]) % mod;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n, sum = 0, count = 0, m, flag = 0, ans = 1, k;
    facto();
    for(int i = 0; i < 3e5; i++)
        visit[i] = i, total[i] = 1;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        ll x,y;
        cin >> x >> y;
        x = root(x);
        y = root(y);
        if(x != y)
        {
            total[x] += total[y];
            choice[x] += choice[y];
            total[y] = 0;
            visit[y] = visit[x];
        }
        choice[root(x)]++;
    }
    for(int i = 1; i<= 2*n; i++)
    {
        //cout<<total[i]<<" "<<choice[i]<<endl;
        if(total[i] == choice[i] + 1 && total[i] > 1)
            ans = (ans * total[i]) % mod;
        else if(total[i] == choice[i] && total[i] > 1)
            ans = (ans * 2) % mod;
    }
    cout<<ans;
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


// <3 Appy!
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int fact[1000009], visit[1000009], total[1000009], choice[1000009];
int mod = 1e9 + 7;
ll power(int x, int y)
{
    if(y == 0)
        return 1;
    ll ans = power(x, y/2);
    if(y%2)
        return (((ans*ans) % mod) * x) % mod;
    return (ans * ans) % mod;
}
void facto()
{
    fact[0] = 1;
    for(int i = 1; i <= 1e6; i++)
        fact[i] = (1LL * fact[i-1] * i) % mod;
}
int root(int x)
{
    while(x != visit[x])
    {
        visit[x] = visit[visit[x]];
        x = visit[x];
    }
    return x;
}
ll calInverse(int x)
{
    return power(x, mod-2) % mod;
}
ll nCr(int n, int r)
{
    if(n == 0 || n - r == 0)
        return 1;
    return ((fact[n] * calInverse(fact[r])) % mod) * calInverse(fact[(n-r)]) % mod;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n, sum = 0, count = 0, m, flag = 0, ans = 1, k;
    facto();
    for(int i = 0; i < 3e5; i++)
        visit[i] = i, total[i] = 1;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        ll x,y;
        cin >> x >> y;
        if(x == y)
            continue;
        x = root(x);
        y = root(y);
        if(x != y)
        {
            total[x] += total[y];
            choice[x] += choice[y];
            total[y] = 0;
            visit[y] = visit[x];
        }
        choice[root(x)]++;
    }
    for(int i = 1; i<= 2*n; i++)
    {
        //cout<<total[i]<<" "<<choice[i]<<endl;
        if(total[i] == choice[i] + 1 && total[i] > 1)
            ans = (ans * total[i]) % mod;
        else if(total[i] == choice[i] && total[i] > 1)
            ans = (ans * 2) % mod;
    }
    cout<<ans;
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


// <3 Appy!
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int fact[1000009], visit[1000009], total[1000009], choice[1000009];
int mod = 1e9 + 7;
ll power(int x, int y)
{
    if(y == 0)
        return 1;
    ll ans = power(x, y/2);
    if(y%2)
        return (((ans*ans) % mod) * x) % mod;
    return (ans * ans) % mod;
}
void facto()
{
    fact[0] = 1;
    for(int i = 1; i <= 1e6; i++)
        fact[i] = (1LL * fact[i-1] * i) % mod;
}
int root(int x)
{
    while(x != visit[x])
    {
        visit[x] = visit[visit[x]];
        x = visit[x];
    }
    return x;
}
ll calInverse(int x)
{
    return power(x, mod-2) % mod;
}
ll nCr(int n, int r)
{
    if(n == 0 || n - r == 0)
        return 1;
    return ((fact[n] * calInverse(fact[r])) % mod) * calInverse(fact[(n-r)]) % mod;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n, sum = 0, count = 0, m, flag = 0, ans = 1, k;
    facto();
    for(int i = 0; i < 3e5; i++)
        visit[i] = i, total[i] = 1;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        ll x,y;
        cin >> x >> y;
        x = root(x);
        y = root(y);
        if(x != y)
        {
            total[x] += total[y];
            choice[x] += choice[y];
            total[y] = 0;
            choice[y] = 0;
            visit[y] = visit[x];
        }
        choice[x]++;
    }
    for(int i = 1; i <= 2*n; i++)
    {
        //cout<<total[i]<<" "<<choice[i]<<endl;
        if(total[i] == choice[i] + 1 && total[i] > 1)
            ans = (ans * total[i]) % mod;
        else if(total[i] == choice[i] && total[i] > 1)
            ans = (ans * 2) % mod;
    }
    cout<<ans;
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


// <3 Appy!
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int fact[1000009], visit[1000009], total[1000009], choice[1000009];
int mod = 1e9 + 7;
ll power(int x, int y)
{
    if(y == 0)
        return 1;
    ll ans = power(x, y/2);
    if(y%2)
        return (((ans*ans) % mod) * x) % mod;
    return (ans * ans) % mod;
}
void facto()
{
    fact[0] = 1;
    for(int i = 1; i <= 1e6; i++)
        fact[i] = (1LL * fact[i-1] * i) % mod;
}
int root(int x)
{
    while(x != visit[x])
    {
        visit[x] = visit[visit[x]];
        x = visit[x];
    }
    return x;
}
ll calInverse(int x)
{
    return power(x, mod-2) % mod;
}
ll nCr(int n, int r)
{
    if(n == 0 || n - r == 0)
        return 1;
    return ((fact[n] * calInverse(fact[r])) % mod) * calInverse(fact[(n-r)]) % mod;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n, sum = 0, count = 0, m, flag = 0, ans = 1, k;
    facto();
    for(int i = 0; i < 3e5; i++)
        visit[i] = i, total[i] = 1;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        ll x,y;
        cin >> x >> y;
        if(x == y)
            count--;
        x = root(x);
        y = root(y);
        if(x != y)
        {
            total[x] += total[y];
            choice[x] += choice[y];
            total[y] = choice[y] = 0;
            visit[y] = visit[x];
        }
        else count++;
        choice[x]++;
    }
    for(int i = 1; i <= 2*n; i++)
    {
        if(total[i] == choice[i] + 1 && total[i] > 1)
            ans = (ans * total[i]) % mod;
        else if(total[i] == choice[i] && total[i] > 1)
            ans = (ans * 2) % mod;
    }
    cout<<ans;
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


// <3 Appy!
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int fact[1000009], visit[1000009], total[1000009], choice[1000009];
int mod = 1e9 + 7;
ll power(int x, int y)
{
    if(y == 0)
        return 1;
    ll ans = power(x, y/2);
    if(y%2)
        return (((ans*ans) % mod) * x) % mod;
    return (ans * ans) % mod;
}
void facto()
{
    fact[0] = 1;
    for(int i = 1; i <= 1e6; i++)
        fact[i] = (1LL * fact[i-1] * i) % mod;
}
int root(int x)
{
    while(x != visit[x])
    {
        visit[x] = visit[visit[x]];
        x = visit[x];
    }
    return x;
}
ll calInverse(int x)
{
    return power(x, mod-2) % mod;
}
ll nCr(int n, int r)
{
    if(n == 0 || n - r == 0)
        return 1;
    return ((fact[n] * calInverse(fact[r])) % mod) * calInverse(fact[(n-r)]) % mod;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n, sum = 0, count = 0, m, flag = 0, ans = 1, k;
    facto();
    for(int i = 0; i < 3e5; i++)
        visit[i] = i, total[i] = 1;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        ll x,y;
        cin >> x >> y;
        if(x == y)
            count--;
        x = root(x);
        y = root(y);
        if(x != y)
        {
            total[x] += total[y];
            choice[x] += choice[y];
            total[y] = choice[y] = 0;
            visit[y] = visit[x];
        }
        else count++;
        choice[x]++;
    }
    for(int i = 1; i <= 2*n; i++)
    {
        if(total[i] == choice[i] + 1 && total[i] > 1)
            ans = (ans * total[i]) % mod;
    }
    while(count--)
        ans = (ans * 2) % mod;
    cout<<ans;
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


// <3 Appy!
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<long long int> vi;
int fact[1000009], visit[1000009], total[1000009], choice[1000009];
int mod = 1e9 + 7;
ll power(int x, int y)
{
    if(y == 0)
        return 1;
    ll ans = power(x, y/2);
    if(y%2)
        return (((ans*ans) % mod) * x) % mod;
    return (ans * ans) % mod;
}
void facto()
{
    fact[0] = 1;
    for(int i = 1; i <= 1e6; i++)
        fact[i] = (1LL * fact[i-1] * i) % mod;
}
int root(int x)
{
    while(x != visit[x])
    {
        visit[x] = visit[visit[x]];
        x = visit[x];
    }
    return x;
}
ll calInverse(int x)
{
    return power(x, mod-2) % mod;
}
ll nCr(int n, int r)
{
    if(n == 0 || n - r == 0)
        return 1;
    return ((fact[n] * calInverse(fact[r])) % mod) * calInverse(fact[(n-r)]) % mod;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    ll n, sum = 0, count = 0, m, flag = 0, ans = 1, k;
    facto();
    for(int i = 0; i < 3e5; i++)
        visit[i] = i, total[i] = 1;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        ll x,y;
        cin >> x >> y;
        x = root(x);
        y = root(y);
        if(x != y)
        {
            total[x] += total[y];
            total[y] = 0;
            visit[y] = visit[x];
        }
        choice[x]++;
    }
    for(int i = 1; i<= 2*n; i++)
    {
        //cout<<total[i]<<" "<<choice[i]<<endl;
        if(total[i])
            ans = (ans * total[i]) % mod;
    }
    cout<<ans;
    return 0;
}

/* freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
 */


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

//type helpers
#define ll long long
#define vi vector <long long>
#define vl vector <ll>
#define pii pair<long long, long long>
#define pll pair<ll, ll>
#define vpii vector <pii >
#define vpll vector <pll >
//debugging
#define debug(x) cout<<"debugging "<<x<<endl

//loops
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define FORD(i,a,b) for(ll i=a-1;i>=b;i--)
#define iter(it, s) for(auto it=s.begin(); it!=s.end(); it++)

//io helpers
#define sd(n) scanf("%d", &n)
#define sl(n) scanf("%lld", &n)
#define slf(n) scanf("%lf", &n)
#define pd(n) prlong longf("%d", n)
#define pl(n) prlong longf("%lld", n)
#define plf(n) prlong longf("%0.9lf", n)
#define ps prlong longf(" ")
#define pn prlong longf("\n")

//ds helpers
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define mp make_pair
#define sz(n) (ll)n.size()

//fast input and output when using cin,cout
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//some constants
const long long INF = 1<<29;
#define MOD 1000000007
#define PI 3.14159265358979323846
#define EPS 0.0000000001
#define max_size 100005

//some helper functions
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9;}
inline ll lcm(ll a,ll b){ return (a*b)/(__gcd(a,b));}
#define mod(a) ((a)%MOD)
inline ll ciel(double a) { ll ret = a; if(a-ret>EPS) ret++  ; return ret; }
inline ll powf(ll n, ll p) {if(p==0)return 1; if(n<=1)return n;ll res = 1;while(p){if(p&1) res = mod(res*n);n = mod(n*n);p /= 2;} return res;}
inline bool ispoweroftwo(ll x){return (x && !(x&(x-1)));}

const long long inf = ( 2e9 ) + 2;

template <typename T>
  std::string NumberToString ( T Number )
  {
     std::ostringstream ss;
     ss <<  Number;
     return ss.str();
  }

bool isalp(char c){
  if((c>='a' and c<='z') or (c>='A' and c<='Z')) return true;
  return false;
}

vector<int> ve[200005];
int vis[200005]={0};
ll res=1;
void dfs(int a){
  vis[a]=1;
  for(int i=0;i<ve[a].size();i++){
    if(vis[ve[a][i]]==0){
      res++;
      dfs(ve[a][i]);
    }
  }
  return;
}

int main() {
  // ios_base::sync_with_stdio(false);cin.tie(NULL);
  int n,a,b;
  ll ans=1;
  cin>>n;
  FOR(i,0,n){
    cin>>a>>b;
    if(a!=b){
      ve[a].pb(b);
      ve[b].pb(a);
    }
  }
  FOR(i,1,2*n+1){
    if(vis[i]==0){
      res=1;
      dfs(i);
      ans = (ans*res)%MOD;
    }
  }
  cout<<ans%MOD<<endl;
  return 0;
}


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

//type helpers
#define ll long long
#define vi vector <long long>
#define vl vector <ll>
#define pii pair<long long, long long>
#define pll pair<ll, ll>
#define vpii vector <pii >
#define vpll vector <pll >
//debugging
#define debug(x) cout<<"debugging "<<x<<endl

//loops
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define FORD(i,a,b) for(ll i=a-1;i>=b;i--)
#define iter(it, s) for(auto it=s.begin(); it!=s.end(); it++)

//io helpers
#define sd(n) scanf("%d", &n)
#define sl(n) scanf("%lld", &n)
#define slf(n) scanf("%lf", &n)
#define pd(n) prlong longf("%d", n)
#define pl(n) prlong longf("%lld", n)
#define plf(n) prlong longf("%0.9lf", n)
#define ps prlong longf(" ")
#define pn prlong longf("\n")

//ds helpers
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define mp make_pair
#define sz(n) (ll)n.size()

//fast input and output when using cin,cout
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//some constants
const long long INF = 1<<29;
#define MOD 1000000007
#define PI 3.14159265358979323846
#define EPS 0.0000000001
#define max_size 100005

//some helper functions
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9;}
inline ll lcm(ll a,ll b){ return (a*b)/(__gcd(a,b));}
#define mod(a) ((a)%MOD)
inline ll ciel(double a) { ll ret = a; if(a-ret>EPS) ret++; return ret; }
inline ll powf(ll n, ll p) {if(p==0)return 1; if(n<=1)return n;ll res = 1;while(p){if(p&1) res = mod(res*n);n = mod(n*n);p /= 2;} return res;}
inline bool ispoweroftwo(ll x){return (x && !(x&(x-1)));}

vector<int> ve[200005];
int vis[200005]={0};
ll res=0;
bool loop=false,cycle=false;

void dfs(int a,int p){
  if(vis[a]) {cycle=true;return;}
  vis[a]=1;
  res++;
  for(int i=0;i<ve[a].size();i++){
    if(ve[a][i]==p){
    	continue;
    }else if(ve[a][i]==a){
    	loop = true;
    	return;
    }
    dfs(ve[a][i],a);
  }
  return;
}

int main() {
  // ios_base::sync_with_stdio(false);cin.tie(NULL);
  int n,a,b;
  ll ans=1LL;
  cin>>n;
  FOR(i,0,n){
    cin>>a>>b;
    ve[a].pb(b);
    ve[b].pb(a);

  }
  FOR(i,1,2*n+1){
  	loop =false,cycle=false;
  	res=0;
    if(vis[i]==0){
      dfs(i,0);
      if(loop)	continue;
      if(cycle) ans = (ans*2LL)%MOD;
      else ans = ((ans%MOD)*(res%MOD))%MOD;
    }
  }
  cout<<ans%MOD<<endl;
  return 0;
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        for(int j = 0; j < 70; j++) {
            int t = 1LL * rand() * rand() % (r + 1) + l;
            if(f(A[t], l, r) > (r - l + 1) / k && ans > A[t]) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        for(int j = 0; j < 40; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(f(A[t], l, r) > (r - l + 1) / k && ans > A[t]) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];
int chk[MN];

bool f(int x, int l, int r, int bound) {
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    if(b <= bound) return false;
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    return b - a > bound;
}

int main() {
    srand(1111);

    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int x = (r - l + 1) / k;
        vector<int> tmp;
        int ans = 1e9;
        for(int j = 0; j < 70; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            //if(V[ A[t] ].size() <= (r - l + 1) / k) continue;
            if(chk[t]) continue;
            chk[t] = 1;
            tmp.push_back(t);
            if(ans > A[t] && f(A[t], l, r, x)) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);

        for(int i = 0; i < tmp.size(); i++) chk[ tmp[i] ] = 0;
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int Count(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

struct node {
    int l, r;
    vector<int> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    int pos1 = 0, pos2 = 0;
    while(pos1 < a.V.size() && pos2 < b.V.size() && ret.V.size() <= 4) {
        if(a.V[pos1] == b.V[pos2]) {
            if(Count(a.V[pos1], ret.l, ret.r)) ret.V.push_back(a.V[pos1]);
            pos1++;
            pos2++;
        }
        else if(a.V[pos1] < b.V[pos2]) {
            if(Count(a.V[pos1], ret.l, ret.r)) ret.V.push_back(a.V[pos1]);
            pos1++;
        }
        else {
            if(Count(b.V[pos2], ret.l, ret.r)) ret.V.push_back(b.V[pos2]);
            pos2++;
        }
    }
    while(pos1 < a.V.size() && ret.V.size() <= 4) {
        if(Count(a.V[pos1], ret.l, ret.r)) ret.V.push_back(a.V[pos1]);
        pos1++;
    }
    while(pos2 < b.V.size() && ret.V.size() <= 4) {
        if(Count(b.V[pos2], ret.l, ret.r)) ret.V.push_back(b.V[pos2]);
        pos2++;
    }
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back(A[l]);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<int>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            int u = tmp.V[j];
            if(Count(u, l, r) > (r - l + 1) / k && ans > u) ans = u;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

typedef pair<int, int> pii;

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

struct node {
    int l, r;
    vector<pii> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    vector<pii> tmp;
    for(int i = 0; i < a.V.size(); i++) {
        if(a.V[i].second > (ret.r - ret.l + 1) / 5) tmp.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        if(b.V[i].second > (ret.r - ret.l + 1) / 5) tmp.push_back(b.V[i]);
    }
    sort(tmp.begin(), tmp.end());
    for(int i = 0; i < tmp.size(); i++) {
        if(ret.V.size() && ret.V.back().first == tmp[i].first) ret.V.back().second += tmp[i].second;
        else ret.V.push_back(tmp[i]);
    }

    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back({A[l], 1});
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<pii>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            if(tmp.V[j].second > (r - l + 1) / k && ans > tmp.V[j].first) ans = tmp.V[j].first;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

int main() {
    srand(1111);

    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        for(int j = 0; j < 30; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(f(A[t], l, r) > (r - l + 1) / k && ans > A[t]) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, vector<int> &a) {
    int s = 0, e = (int)a.size() - 1, p = -1;
    while(s <= e) {
        int m = (s + e)>>1;
        if(a[m] <= x) {
            p = m;
            s = m + 1;
        }
        else e = m - 1;
    }
    return p;
}

int Count(int x, int l, int r, int bound) {
    int p = f(r, V[x]);
    if(p < bound) return false;
    else return V[x][p - bound] >= l;
}

struct node {
    int l, r;
    vector<int> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    int x = (ret.r - ret.l + 1) / 5;
    for(int i = 0; i < a.V.size(); i++) {
        if(Count(a.V[i], ret.l, ret.r, x)) ret.V.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        if(Count(b.V[i], ret.l, ret.r, x)) ret.V.push_back(b.V[i]);
    }
    sort(ret.V.begin(), ret.V.end());
    ret.V.resize(unique(ret.V.begin(), ret.V.end()) - ret.V.begin());
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back(A[l]);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<int>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        int x = (r - l + 1) / k;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            int u = tmp.V[j];
            if(ans > u && Count(u, l, r, x)) ans = u;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];
int chk[MN];

bool f(int x, int l, int r, int bound) {
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    if(b <= bound) return false;
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    return b - a > bound;
}

int main() {
    srand(1111);

    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int x = (r - l + 1) / k;
        vector<int> tmp;
        int ans = 1e9;
        for(int j = 0; j < 60; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(V[ A[t] ].size() <= (r - l + 1) / k) continue;
            if(chk[t]) continue;
            chk[t] = 1;
            tmp.push_back(t);
            if(ans > A[t] && f(A[t], l, r, x)) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);

        for(int i = 0; i < tmp.size(); i++) chk[ tmp[i] ] = 0;
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int Count(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

struct node {
    int l, r;
    vector<int> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    for(int i = 0; i < a.V.size(); i++) {
        if(Count(a.V[i], ret.l, ret.r) > (ret.r - ret.l + 1) / 5) ret.V.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        if(Count(b.V[i], ret.l, ret.r) > (ret.r - ret.l + 1) / 5) ret.V.push_back(b.V[i]);
    }
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back(A[l]);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<int>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            int u = tmp.V[j];
            if(Count(u, l, r) > (r - l + 1) / k && ans > u) ans = u;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

typedef pair<int, int> pii;

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

struct node {
    int len;
    vector<pii> V;
};

node mrg(node &a, node &b) {
    if(a.len == -1) return b;
    if(b.len == -1) return a;
    node ret;
    ret.len = a.len + b.len;

    int pos1 = 0, pos2 = 0;
    while(pos1 < a.V.size() && pos2 < b.V.size()) {
        if(a.V[pos1].first == b.V[pos2].first) {
            if(a.V[pos1].second + b.V[pos2].second > ret.len / 5) ret.V.push_back({ a.V[pos1].first, a.V[pos1].second + b.V[pos2].second });
            pos1++;
            pos2++;
        }
        else if(a.V[pos1].first < b.V[pos2].first) {
            if(a.V[pos1].second > ret.len / 5) ret.V.push_back(a.V[pos1]);
            pos1++;
        }
        else {
            if(b.V[pos2].second > ret.len / 5) ret.V.push_back(b.V[pos2]);
            pos2++;
        }
    }
    while(pos1 < a.V.size()) {
        if(a.V[pos1].second > ret.len / 5) ret.V.push_back(a.V[pos1]);
        pos1++;
    }
    while(pos2 < b.V.size()) {
        if(b.V[pos2].second > ret.len / 5) ret.V.push_back(b.V[pos2]);
        pos2++;
    }
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].len = 1;
            tree[n].V.push_back({A[l], 1});
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, vector<pii>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);
    //N = 100000;
    //Q = 100000;

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        //A[i] = rand() % N + 1;
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            if(tmp.V[j].second > (r - l + 1) / k && ans > tmp.V[j].first) ans = tmp.V[j].first;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        for(int j = 0; j < 70; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(f(A[t], l, r) > (r - l + 1) / k && ans > A[t]) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        for(int j = 0; j < 50; j++) {
            int t = rand() % (r + 1) + l;
            if(f(A[t], l, r) > (r - l + 1) / k && ans > A[t]) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];
struct Query {
    int l, k, id;
};
vector<Query> query[MN];
int ans[MN], pos[MN];

int f(int x, vector<int> &a) {
    int s = 0, e = (int)a.size() - 1, p = -1;
    while(s <= e) {
        int m = (s + e)>>1;
        if(a[m] <= x) {
            p = m;
            s = m + 1;
        }
        else e = m - 1;
    }
    return p;
}

int Count(int x, int l, int r, int bound) {
    if(V[x].size() <= bound || V[x][bound] > r) return false;
    int p = f(r, V[x]);
    if(p < bound) return false;
    else return V[x][p - bound] >= l;
}

int test(int x, int l, int bound) {
    int p = pos[x] - 1;
    if(p < bound) return false;
    else return V[x][p - bound] >= l;
}

struct node {
    int l, r;
    vector<int> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    int x = (ret.r - ret.l + 1) / 5;
    for(int i = 0; i < a.V.size(); i++) {
        if(Count(a.V[i], ret.l, ret.r, x)) ret.V.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        if(Count(b.V[i], ret.l, ret.r, x)) ret.V.push_back(b.V[i]);
    }
    sort(ret.V.begin(), ret.V.end());
    ret.V.resize(unique(ret.V.begin(), ret.V.end()) - ret.V.begin());
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back(A[l]);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    int quer(int a, int b, int ll, int k, int l, int r, int n) {
        if(b < l || r < a) return 1e9;
        if(a <= l && r <= b) {
            for(int i = 0; i < tree[n].V.size(); i++) {
                if(test(tree[n].V[i], ll, k)) return tree[n].V[i];
            }
            return 1e9;
        }
        int m = (l + r)>>1;
        int L = quer(a, b, ll, k, l, m, 2*n);
        int R = quer(a, b, ll, k, m + 1, r, 2*n + 1);
        return min(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;
        query[r].push_back({l, k, i});
    }

    for(int q = 0; q < N; q++) {
        pos[ A[q] ]++;
        for(int i = 0; i < query[q].size(); i++) {
            int l = query[q][i].l;
            int r = q;
            int k = query[q][i].k;
            int id = query[q][i].id;

            int t = bit.quer(l, r, l, (r - l + 1) / k, 0, N - 1, 1);
            if(t == 1e9) ans[id] = -1;
            else ans[id] = t;
        }
    }
    for(int i = 0; i < Q; i++) {
        printf("%d\n", ans[i]);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];
int chk[MN];

bool f(int x, int l, int r, int bound) {
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    if(b <= bound) return false;
    else return V[x][b - bound - 1] >= l;
}

int main() {
    srand(1111);

    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int x = (r - l + 1) / k;
        vector<int> tmp;
        int ans = 1e9;
        for(int j = 0; j < 100; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(ans <= A[t]) continue;
            if(V[ A[t] ].size() <= (r - l + 1) / k) continue;
            if(chk[t]) continue;
            chk[t] = 1;
            tmp.push_back(t);
            if(f(A[t], l, r, x)) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);

        for(int i = 0; i < tmp.size(); i++) chk[ tmp[i] ] = 0;
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];
int chk[MN];

bool f(int x, int l, int r, int bound) {
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    if(b <= bound) return false;
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    return b - a > bound;
}

int main() {
    srand(1111);

    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int x = (r - l + 1) / k;
        vector<int> tmp;
        int ans = 1e9;
        for(int j = 0; j < 70; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(ans > A[t] && f(A[t], l, r, x)) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];
int chk[MN];

bool f(int x, int l, int r, int bound) {
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    if(b <= bound) return false;
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    return b - a > bound;
}

int main() {
    srand(1111);

    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int x = (r - l + 1) / k;
        vector<int> tmp;
        int ans = 1e9;
        for(int j = 0; j < 70; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(ans <= A[t]) continue;
            if(V[ A[t] ].size() <= (r - l + 1) / k) continue;
            if(chk[t]) continue;
            chk[t] = 1;
            tmp.push_back(t);
            if(f(A[t], l, r, x)) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);

        for(int i = 0; i < tmp.size(); i++) chk[ tmp[i] ] = 0;
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, vector<int> &a) {
    int s = 0, e = (int)a.size() - 1, p = -1;
    while(s <= e) {
        int m = (s + e)>>1;
        if(a[m] <= x) {
            p = m;
            s = m + 1;
        }
        else e = m - 1;
    }
    return p;
}

int Count(int x, int l, int r) {
    return f(r, V[x]) - f(l - 1, V[x]);
}

struct node {
    int l, r;
    vector<int> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    int pos1 = 0, pos2 = 0;
    while(pos1 < a.V.size() && pos2 < b.V.size() && ret.V.size() < 4) {
        if(a.V[pos1] == b.V[pos2]) {
            if(Count(a.V[pos1], ret.l, ret.r)) ret.V.push_back(a.V[pos1]);
            pos1++;
            pos2++;
        }
        else if(a.V[pos1] < b.V[pos2]) {
            if(Count(a.V[pos1], ret.l, ret.r)) ret.V.push_back(a.V[pos1]);
            pos1++;
        }
        else {
            if(Count(b.V[pos2], ret.l, ret.r)) ret.V.push_back(b.V[pos2]);
            pos2++;
        }
    }
    while(pos1 < a.V.size() && ret.V.size() < 4) {
        if(Count(a.V[pos1], ret.l, ret.r)) ret.V.push_back(a.V[pos1]);
        pos1++;
    }
    while(pos2 < b.V.size() && ret.V.size() < 4) {
        if(Count(b.V[pos2], ret.l, ret.r)) ret.V.push_back(b.V[pos2]);
        pos2++;
    }
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back(A[l]);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<int>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            int u = tmp.V[j];
            if(Count(u, l, r) > (r - l + 1) / k && ans > u) ans = u;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, vector<int> &a) {
    int s = 0, e = (int)a.size() - 1, p = -1;
    while(s <= e) {
        int m = (s + e)>>1;
        if(a[m] <= x) {
            p = m;
            s = m + 1;
        }
        else e = m - 1;
    }
    return p;
}

int Count(int x, int l, int r, int bound) {
    if(V[x].size() <= bound || V[x][bound] > r) return false;
    int p = f(r, V[x]);
    if(p < bound) return false;
    else return V[x][p - bound] >= l;
}

struct node {
    int l, r;
    vector<int> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    int x = (ret.r - ret.l + 1) / 5;
    for(int i = 0; i < a.V.size(); i++) {
        if(Count(a.V[i], ret.l, ret.r, x)) ret.V.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        if(Count(b.V[i], ret.l, ret.r, x)) ret.V.push_back(b.V[i]);
    }
    sort(ret.V.begin(), ret.V.end());
    ret.V.resize(unique(ret.V.begin(), ret.V.end()) - ret.V.begin());
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back(A[l]);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    int quer(int a, int b, int ll, int rr, int k, int l, int r, int n) {
        if(b < l || r < a) return 1e9;
        if(a <= l && r <= b) {
            for(int i = 0; i < tree[n].V.size(); i++) {
                if(Count(tree[n].V[i], ll, rr, k)) return tree[n].V[i];
            }
            return 1e9;
        }
        int m = (l + r)>>1;
        int L = quer(a, b, ll, rr, k, l, m, 2*n);
        int R = quer(a, b, ll, rr, k, m + 1, r, 2*n + 1);
        return min(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = bit.quer(l, r, l, r, (r - l + 1) / k, 0, N - 1, 1);
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

typedef pair<int, int> pii;

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];
struct Query {
    int l, k, id;
};
vector<Query> query[MN];
int ans[MN], pos[MN], chk[MN];

int f(int x, int l, int bound) {
    int p = pos[x] - 1;
    if(p < bound) return false;
    else return V[x][p - bound] >= l;
}

int main() {
    srand(1111);

    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        query[r].push_back({l, k, i});
    }

    for(int q = 0; q < N; q++) {
        pos[ A[q] ]++;
        for(int i = 0; i < query[q].size(); i++) {
            int l = query[q][i].l;
            int r = q;
            int k = query[q][i].k;
            int id = query[q][i].id;

            int x = (r - l + 1) / k;
            int mn = 1e9;
            for(int j = 0; j < 100; j++) {
                int t = 1LL * rand() * rand() % (r - l + 1) + l;
                if(mn > A[t] && f(A[t], l, x)) mn = A[t];
            }
            if(mn == 1e9) ans[id] = -1;
            else ans[id] = mn;
        }
    }
    for(int i = 0; i < Q; i++) {
        printf("%d\n", ans[i]);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int Count(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

struct node {
    int l, r;
    vector<int> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    for(int i = 0; i < a.V.size(); i++) {
        if(Count(a.V[i], ret.l, ret.r) > (ret.r - ret.l + 1) / 5) ret.V.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        if(Count(b.V[i], ret.l, ret.r) > (ret.r - ret.l + 1) / 5) ret.V.push_back(b.V[i]);
    }
    sort(ret.V.begin(), ret.V.end());
    ret.V.resize(unique(ret.V.begin(), ret.V.end()) - ret.V.begin());
    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back(A[l]);
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<int>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            int u = tmp.V[j];
            if(Count(u, l, r) > (r - l + 1) / k && ans > u) ans = u;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

typedef pair<int, int> pii;

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

struct node {
    int l, r;
    vector<pii> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    vector<pii> tmp;
    for(int i = 0; i < a.V.size(); i++) {
        tmp.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        tmp.push_back(b.V[i]);
    }
    sort(tmp.begin(), tmp.end());
    for(int i = 0; i < tmp.size(); i++) {
        if(ret.V.size() && ret.V.back().first == tmp[i].first) ret.V.back().second += tmp[i].second;
        else {
            if(ret.V.size() && ret.V.back().second <= (ret.r - ret.l + 1) / 5) ret.V.pop_back();
            ret.V.push_back(tmp[i]);
        }
    }

    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back({A[l], 1});
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<pii>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            if(tmp.V[j].second > (r - l + 1) / k && ans > tmp.V[j].first) ans = tmp.V[j].first;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

typedef pair<int, int> pii;

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

struct node {
    int l, r;
    vector<pii> V;
};

node mrg(node &a, node &b) {
    if(a.l == -1) return b;
    if(b.l == -1) return a;
    node ret;
    ret.l = a.l;
    ret.r = b.r;

    vector<pii> tmp;
    for(int i = 0; i < a.V.size(); i++) {
        tmp.push_back(a.V[i]);
    }
    for(int i = 0; i < b.V.size(); i++) {
        tmp.push_back(b.V[i]);
    }
    sort(tmp.begin(), tmp.end());
    for(int i = 0; i < tmp.size(); i++) {
        if(ret.V.size() && ret.V.back().first == tmp[i].first) ret.V.back().second += tmp[i].second;
        else {
            if(ret.V.size() && ret.V.back().second <= (ret.r - ret.l + 1) / 5) ret.V.pop_back();
            ret.V.push_back(tmp[i]);
        }
    }
    if(ret.V.size() && ret.V.back().second <= (ret.r - ret.l + 1) / 5) ret.V.pop_back();

    return ret;
}

struct BIT {
    vector<node> tree;
    void init() {
        tree = vector<node>(4 * N);
        build(0, N - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n].l = l;
            tree[n].r = r;
            tree[n].V.push_back({A[l], 1});
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = mrg(tree[2*n], tree[2*n + 1]);
    }
    node quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return {-1, -1, vector<pii>()};
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        node L = quer(a, b, l, m, 2*n);
        node R = quer(a, b, m + 1, r, 2*n + 1);
        return mrg(L, R);
    }
} bit;

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    bit.init();
    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        node tmp = bit.quer(l, r, 0, N - 1, 1);
        for(int j = 0; j < tmp.V.size(); j++) {
            if(tmp.V[j].second > (r - l + 1) / k && ans > tmp.V[j].first) ans = tmp.V[j].first;
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


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

const int MN = 300010;

int N, Q;
int A[MN];
vector<int> V[MN];

int f(int x, int l, int r) {
    int a = lower_bound(V[x].begin(), V[x].end(), l) - V[x].begin();
    int b = lower_bound(V[x].begin(), V[x].end(), r + 1) - V[x].begin();
    return b - a;
}

int main() {
    scanf("%d %d", &N, &Q);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        V[ A[i] ].push_back(i);
    }

    for(int i = 0; i < Q; i++) {
        int l, r, k; scanf("%d %d %d", &l, &r, &k);
        l--; r--;

        int ans = 1e9;
        for(int j = 0; j < 30; j++) {
            int t = 1LL * rand() * rand() % (r - l + 1) + l;
            if(f(A[t], l, r) > (r - l + 1) / k && ans > A[t]) ans = A[t];
        }
        if(ans == 1e9) printf("-1\n");
        else printf("%d\n", ans);
    }
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define at 5
#else
#define at 100
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

int tab[N + 1];
pair<int, int> quer[N + 1];
int quer_k[N + 1];
vector<int> start[N + 1], fin[N + 1];
int n, q;

int ans[N + 1], cnd[N + 1], cnt[N + 1], cnd_cnt[N + 1];

ll randll(){ return ll(rand()) * RAND_MAX + rand();}

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 1, n + 1){
        cin >> tab[i];
    }
    rep(i, 0, q){
        cin >> quer[i].ft >> quer[i].sd >> quer_k[i];
        start[quer[i].ft].pb(i);
        fin[quer[i].sd].pb(i);
        ans[i] = IINF;
    }
    rep(_, 0, at){
        ll rd = randll();
        rep(i, 0, q){
            cnd[i] = tab[quer[i].ft + rd % (1 + quer[i].sd - quer[i].ft)];
            cnd_cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            for(auto& s : start[i]){
                cnd_cnt[s] -= cnt[cnd[s]];
            }
            cnt[tab[i]]++;
            for(auto& f : fin[i]){
                cnd_cnt[f] += cnt[cnd[f]];
            }
        }
        rep(i, 0, q){
            if(cnd_cnt[i] > (1 + quer[i].sd - quer[i].ft) / quer_k[i]){
                DBG(i, cnd[i], cnd_cnt[i]);
                ans[i] = min(ans[i], cnd[i]);
            }
        }
    }
    rep(i, 0, q){
        if(ans[i] == IINF){
            cout << "-1\n";
        } else {
            cout << ans[i] << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 50;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb(0);
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb(i);
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), l); it1--;
            auto it2 = upper_bound(all(pos[cand]), r); it2--;
            if(it2 - it1 > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 100;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb({0, 0});
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb({i, SZ(pos[a[i]])});
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), make_pair(l, 0)); it1--;
            auto it2 = upper_bound(all(pos[cand]), make_pair(r, IINF)); it2--;
            DBG((*it1).sd, (*it2).sd);
            if((*it2).sd - (*it1).sd > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 66;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb(0);
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb(i);
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), l); it1--;
            auto it2 = upper_bound(all(pos[cand]), r); it2--;
            if(it2 - it1 > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 80;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb(0);
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb(i);
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), l); it1--;
            auto it2 = upper_bound(all(pos[cand]), r); it2--;
            if(it2 - it1 > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 33;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb(0);
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb(i);
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), l); it1--;
            auto it2 = upper_bound(all(pos[cand]), r); it2--;
            if(it2 - it1 > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 55;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb(0);
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb(i);
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), l); it1--;
            auto it2 = upper_bound(all(pos[cand]), r); it2--;
            if(it2 - it1 > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define at 5
#else
#define at 100
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

int tab[N + 1];
pair<int, int> quer[N + 1];
int quer_k[N + 1], thr[N + 1];
vector<int> start[N + 1], fin[N + 1];
int n, q;

int ans[N + 1], cnd[N + 1], cnt[N + 1], cnd_cnt[N + 1];

ll randll(){ return ll(rand()) * RAND_MAX + rand();}

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 1, n + 1){
        cin >> tab[i];
    }
    rep(i, 0, q){
        cin >> quer[i].ft >> quer[i].sd >> quer_k[i];
        start[quer[i].ft].pb(i);
        fin[quer[i].sd].pb(i);
        ans[i] = IINF;
        thr[i] = (1 + quer[i].sd - quer[i].ft) / quer_k[i];
    }
    rep(_, 0, at){
        ll rd = randll();
        rep(i, 0, q){
            cnd[i] = tab[quer[i].ft + rd % (1 + quer[i].sd - quer[i].ft)];
            cnd_cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            for(auto& s : start[i]){
                cnd_cnt[s] -= cnt[cnd[s]];
            }
            cnt[tab[i]]++;
            for(auto& f : fin[i]){
                cnd_cnt[f] += cnt[cnd[f]];
            }
        }
        rep(i, 0, q){
            if(cnd_cnt[i] > thr[i]){
                DBG(i, cnd[i], cnd_cnt[i]);
                ans[i] = min(ans[i], cnd[i]);
            }
        }
    }
    rep(i, 0, q){
        if(ans[i] == IINF){
            cout << "-1\n";
        } else {
            cout << ans[i] << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define at 5
#else
#define at 50
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

int tab[N + 1];
pair<int, int> quer[N + 1];
int quer_k[N + 1], thr[N + 1];
vector<int> start[N + 1], fin[N + 1];
int n, q;

int ans[N + 1], cnd[N + 1], cnt[N + 1], cnd_cnt[N + 1];

ll randll(){ return ll(rand()) * RAND_MAX + rand();}

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 1, n + 1){
        cin >> tab[i];
    }
    rep(i, 0, q){
        cin >> quer[i].ft >> quer[i].sd >> quer_k[i];
        start[quer[i].ft].pb(i);
        fin[quer[i].sd].pb(i);
        ans[i] = IINF;
        thr[i] = (1 + quer[i].sd - quer[i].ft) / quer_k[i];
    }
    rep(_, 0, at){
        ll rd = randll();
        rep(i, 0, q){
            cnd[i] = tab[quer[i].ft + rd % (1 + quer[i].sd - quer[i].ft)];
            cnd_cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            for(auto& s : start[i]){
                cnd_cnt[s] -= cnt[cnd[s]];
            }
            cnt[tab[i]]++;
            for(auto& f : fin[i]){
                cnd_cnt[f] += cnt[cnd[f]];
            }
        }
        rep(i, 0, q){
            if(cnd_cnt[i] > thr[i]){
                DBG(i, cnd[i], cnd_cnt[i]);
                ans[i] = min(ans[i], cnd[i]);
            }
        }
    }
    rep(i, 0, q){
        if(ans[i] == IINF){
            cout << "-1\n";
        } else {
            cout << ans[i] << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

const int at = 80;

int tab[N + 1];
pair<int, int> quer[N + 1];
int quer_k[N + 1], thr[N + 1];
vector<int> start[N + 1], fin[N + 1];
int n, q;

int rd[N + 1], ans[N + 1], cnd[N + 1][at], cnt[N + 1], cnd_cnt[N + 1][at];

ll randll(){ return ll(rand()) * RAND_MAX + rand();}

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 1, n + 1){
        cin >> tab[i];
    }
    rep(i, 0, at){
        rd[i] = randll();
    }
    rep(i, 0, q){
        cin >> quer[i].ft >> quer[i].sd >> quer_k[i];
        start[quer[i].ft].pb(i);
        fin[quer[i].sd].pb(i);
        ans[i] = IINF;
        thr[i] = (1 + quer[i].sd - quer[i].ft) / quer_k[i];
        rep(j, 0, at){
            cnd[i][j] = tab[quer[i].ft + rd[j] % (1 + quer[i].sd - quer[i].ft)];
        }
    }
    rep(i, 1, n + 1){
        for(auto& s : start[i]){
            rep(j, 0, at){
                cnd_cnt[s][j] -= cnt[cnd[s][j]];
            }
        }
        cnt[tab[i]]++;
        for(auto& f : fin[i]){
            rep(j, 0, at){
                cnd_cnt[f][j] += cnt[cnd[f][j]];
            }
        }
    }

    rep(i, 0, q){
        rep(j, 0, at){
            if(cnd_cnt[i][j] > thr[i]){
                ans[i] = min(ans[i], cnd[i][j]);
            }
        }
        if(ans[i] == IINF){
            cout << "-1\n";
        } else {
            cout << ans[i] << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define at 5
#else
#define at 50
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

int tab[N + 1];
pair<int, int> quer[N + 1];
int quer_k[N + 1], thr[N + 1];
vector<int> start[N + 1], fin[N + 1];
int n, q;

int ans[N + 1], cnd[N + 1], cnt[N + 1], cnd_cnt[N + 1];

ll randll(){ return ll(rand()) * RAND_MAX + rand();}

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 1, n + 1){
        cin >> tab[i];
    }
    rep(i, 0, q){
        cin >> quer[i].ft >> quer[i].sd >> quer_k[i];
        start[quer[i].ft].pb(i);
        fin[quer[i].sd].pb(i);
        ans[i] = IINF;
        thr[i] = (1 + quer[i].sd - quer[i].ft) / quer_k[i];
    }
    rep(_, 0, at){
        ll rd = randll();
        rep(i, 0, q){
            cnd[i] = tab[quer[i].ft + rd % (1 + quer[i].sd - quer[i].ft)];
            /* cnd_cnt[i] = 0; */
        }
        memset(cnd_cnt, 0, (n + 1) * sizeof(int));
        memset(cnt, 0, (n + 1) * sizeof(int));
        /* rep(i, 1, n + 1){ */
        /*     cnt[i] = 0; */
        /* } */
        rep(i, 1, n + 1){
            for(auto& s : start[i]){
                cnd_cnt[s] -= cnt[cnd[s]];
            }
            cnt[tab[i]]++;
            for(auto& f : fin[i]){
                cnd_cnt[f] += cnt[cnd[f]];
            }
        }
        rep(i, 0, q){
            if(cnd_cnt[i] > thr[i]){
                DBG(i, cnd[i], cnd_cnt[i]);
                ans[i] = min(ans[i], cnd[i]);
            }
        }
    }
    rep(i, 0, q){
        if(ans[i] == IINF){
            cout << "-1\n";
        } else {
            cout << ans[i] << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 70;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb({0, 0});
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb({i, SZ(pos[a[i]])});
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), make_pair(l, 0)); it1--;
            auto it2 = upper_bound(all(pos[cand]), make_pair(r, IINF)); it2--;
            DBG((*it1).sd, (*it2).sd);
            if((*it2).sd - (*it1).sd > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define at 5
#else
#define at 100
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

int tab[N + 1];
pair<int, int> quer[N + 1];
int quer_k[N + 1];
vector<int> start[N + 1], fin[N + 1];
int n, q;

int ans[N + 1], cnd[N + 1], cnt[N + 1], cnd_cnt[N + 1];

ll randll(){ return ll(rand()) * RAND_MAX + rand();}

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 1, n + 1){
        cin >> tab[i];
    }
    rep(i, 0, q){
        cin >> quer[i].ft >> quer[i].sd >> quer_k[i];
        start[quer[i].ft].pb(i);
        fin[quer[i].sd].pb(i);
        ans[i] = IINF;
    }
    rep(_, 0, at){
        rep(i, 0, q){
            cnd[i] = tab[quer[i].ft + randll() % (1 + quer[i].sd - quer[i].ft)];
            cnd_cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            cnt[i] = 0;
        }
        rep(i, 1, n + 1){
            for(auto& s : start[i]){
                cnd_cnt[s] -= cnt[cnd[s]];
            }
            cnt[tab[i]]++;
            for(auto& f : fin[i]){
                cnd_cnt[f] += cnt[cnd[f]];
            }
        }
        rep(i, 0, q){
            if(cnd_cnt[i] > (1 + quer[i].sd - quer[i].ft) / quer_k[i]){
                DBG(i, cnd[i], cnd_cnt[i]);
                ans[i] = min(ans[i], cnd[i]);
            }
        }
    }
    rep(i, 0, q){
        if(ans[i] == IINF){
            cout << "-1\n";
        } else {
            cout << ans[i] << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 42;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb(0);
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb(i);
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), l); it1--;
            auto it2 = upper_bound(all(pos[cand]), r); it2--;
            if(it2 - it1 > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) ;
#define cerr if(0)cout
#endif

const int N = 3e5;

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

const int att = 60;

int main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    cin >> n >> q;
    rep(i, 0, N + 1){
        pos[i].pb(0);
    }
    rep(i, 1, n + 1){
        cin >> a[i];
        pos[a[i]].pb(i);
    }
    rep(_, 0, q){
        int r, l, k;
        cin >> l >> r >> k;
        int ans = IINF;
        rep(__, 0, att){
            int cand  = a[l + rand() % (r - l + 1)];
            DBG(cand);
            auto it1 = lower_bound(all(pos[cand]), l); it1--;
            auto it2 = upper_bound(all(pos[cand]), r); it2--;
            if(it2 - it1 > (r - l + 1) / k){
                ans = min(ans, cand);
            }
        }
        if(ans == IINF){
            cout << "-1\n";
        } else {
            cout << ans << "\n";
        }
    }
    return 0;
}


#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];
set<int> check[N];
vector<query> Q;
vector<query> cobre[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(Q[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 w : cobre[curr]){
		for(int j = 0 ; j < min(sz(opt) , 10) ; j ++){
			check[w.id].insert(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;
struct query{
	int l , r , k;
	int id;
};
int a[N] , n , q;
vi hist[N];
set<int> check[N];
vector<query> Q;
vector<query> 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(Q[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 w : cobre[curr]){
		for(int j = 0 ; j < min(sz(opt) , 10) ; j ++){
			check[w.id].insert(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>

static struct FASTIO {

  char READ_CHARACTER; bool REMAINING_CHARACTER = false;

  inline void ignore(); inline void flush();

  template <typename T> inline bool READ_INT(T &x); template <typename T> inline bool READ_STRING(T &x);
  /*                                                          Fast I/O Code Optimizer                                                          */
  template<size_t N> inline bool READ_CHAR_ARRAY(char (&x)[N]); template<size_t N> inline bool READ_VAR(char (&x)[N]);
  /*                    A tool to optimize execution time of C++ codes by replacing methods of reading and writing variables                   */
  template <typename T> inline bool READ_CHAR(T &x); inline bool READ_CHAR_ARRAY(char*& x); inline bool READ_GETLINE(std::string &x);
  /*                                                    Use it on fastio.pythonanywhere.com                                                    */
  template <typename T> inline bool READ_FLOAT(T &x); template <typename T> inline bool READ_DOUBLE(T &x);
  /*                                          Github Project: github.com/bfs07/Fast-IO-Code-Optimizer                                          */
  template<std::size_t N> inline bool READ_BITSET(std::bitset<N> &bit); template<std::size_t N> inline bool READ_VAR(std::bitset<N> &bit);
  inline bool READ_VAR(bool &x); inline bool READ_VAR(short int &x); inline bool READ_VAR(int &x);
  inline bool READ_VAR(long int &x); inline bool READ_VAR(long long int &x); inline bool READ_VAR(unsigned short int &x);
  inline bool READ_VAR(unsigned int &x); inline bool READ_VAR(unsigned long &x); inline bool READ_VAR(unsigned long long &x);
  inline bool READ_VAR(std::string &x); inline bool READ_VAR(char &x); inline bool READ_VAR(char*& x); inline bool READ_VAR(float &x);
  inline bool READ_VAR(double &x); inline bool READ_VAR(long double &x); template <typename T> inline void WRITE_INT(T x);
  inline void WRITE_STRING(std::string &x); inline void WRITE_CHAR(char x); inline void WRITE_CHAR_ARRAY(const char *x);
  inline void WRITE_FLOAT(float x); template <typename T> inline void WRITE_DOUBLE(T x); inline void WRITE_VAR(bool x);
  inline void WRITE_VAR(short int x); inline void WRITE_VAR(int x); inline void WRITE_VAR(long int x); inline void WRITE_VAR(long long int x);
  inline void WRITE_VAR(unsigned short int x); inline void WRITE_VAR(unsigned int x); inline void WRITE_VAR(unsigned long x);
  inline void WRITE_VAR(unsigned long long x); inline void WRITE_VAR(char x); inline void WRITE_VAR(const char *x);
  inline void WRITE_VAR(std::string &x); inline void WRITE_VAR(float x); inline void WRITE_VAR(double x); inline void WRITE_VAR(long double x);
  template<std::size_t N> inline void WRITE_VAR(std::bitset<N> &bit); template<std::size_t N> inline void WRITE_BITSET(std::bitset<N> &bit);

} __FIO__;

#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 jf[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);
	}
	int cz = 0 ;
	while(q--){
		++ cz;
		int l , r ,k;
		scanf("%d%d%d" , &l , &r , &k);
		int ans = INT_MAX , sz = r - l + 1;
		for(int j = 0 ; j < 75; j ++){
			int p = a[rng()%sz + l];
			if(jf[p] == cz) continue;
			jf[p] = cz;
			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);
	}
}

#undef rep
#undef per
#undef trav
#undef allin
#undef all
#undef sz
#undef F
#undef S
#undef rep
#undef per
#undef trav
#undef allin
#undef all
#undef sz
#undef pb

inline void FASTIO::ignore() {
  if(REMAINING_CHARACTER == true) REMAINING_CHARACTER = false; else READ_CHARACTER = getchar();
}

inline void FASTIO::flush() {
  fflush(stdout);
}

// cin modifications

template <typename T>
inline bool FASTIO::READ_INT(T &x) {
  x = 0; T sig = 1;
  if(!REMAINING_CHARACTER) READ_CHARACTER = getchar(), REMAINING_CHARACTER = true; else REMAINING_CHARACTER = false;
  while (!isdigit(READ_CHARACTER) && READ_CHARACTER != EOF) sig = (READ_CHARACTER == '-' ? -sig : sig), READ_CHARACTER = getchar();
  if(READ_CHARACTER == EOF) return REMAINING_CHARACTER = false, false;
  while (isdigit(READ_CHARACTER)) x = x * 10 + READ_CHARACTER - '0', READ_CHARACTER = getchar();
  x *= sig; REMAINING_CHARACTER = true;
  return true;
}

template <typename T>
inline bool FASTIO::READ_STRING(T &x) {
  x = "";
  if(!REMAINING_CHARACTER) READ_CHARACTER = getchar(), REMAINING_CHARACTER = true; else REMAINING_CHARACTER = false;
  while ((READ_CHARACTER == '\n' || READ_CHARACTER == '\t' || READ_CHARACTER == ' ')) READ_CHARACTER = getchar();
  if(READ_CHARACTER == EOF) return REMAINING_CHARACTER = false, false;
  while ((READ_CHARACTER != '\n' && READ_CHARACTER != '\t' && READ_CHARACTER != ' ' && READ_CHARACTER != EOF)) x += READ_CHARACTER, READ_CHARACTER = getchar();
  REMAINING_CHARACTER = true;
  return true;
}

inline bool FASTIO::READ_GETLINE(std::string &x) {
  x = "";
  if(!REMAINING_CHARACTER) READ_CHARACTER = getchar(), REMAINING_CHARACTER = true; else REMAINING_CHARACTER = false;
  if(READ_CHARACTER == EOF) return REMAINING_CHARACTER = false, false;
  while ((READ_CHARACTER != '\n' && READ_CHARACTER != EOF)) x += READ_CHARACTER, READ_CHARACTER = getchar();
  REMAINING_CHARACTER = false;
  return true;
}

template <typename T>
inline bool FASTIO::READ_CHAR(T &x) {
  if(!REMAINING_CHARACTER) READ_CHARACTER = getchar(), REMAINING_CHARACTER = true; else REMAINING_CHARACTER = false;
  if(READ_CHARACTER == EOF) return REMAINING_CHARACTER = false, false;
  while ((READ_CHARACTER == '\n' || READ_CHARACTER == '\t' || READ_CHARACTER == ' ')) READ_CHARACTER = getchar();
  x = READ_CHARACTER; REMAINING_CHARACTER = false;
  return true;
}

template<size_t N>
inline bool FASTIO::READ_CHAR_ARRAY(char (&x)[N]) {
  if(!REMAINING_CHARACTER) READ_CHARACTER = getchar(), REMAINING_CHARACTER = true; else REMAINING_CHARACTER = false;
  while ((READ_CHARACTER == '\n' || READ_CHARACTER == '\t' || READ_CHARACTER == ' ')) READ_CHARACTER = getchar();
  if(READ_CHARACTER == EOF) return REMAINING_CHARACTER = false, false;
  char *ptr = &x[0];
  while ((READ_CHARACTER != '\n' && READ_CHARACTER != '\t' && READ_CHARACTER != ' ' && READ_CHARACTER != EOF)) *ptr++ = READ_CHARACTER, READ_CHARACTER = getchar();
  *ptr = '\0', REMAINING_CHARACTER = true;
  return true;
}

inline bool FASTIO::READ_CHAR_ARRAY(char*& x) {
  std::string y;
  if(READ_STRING(y) == false)
    return false;
  x = new char[(int)y.size() + 1];
  strcpy(x, y.c_str());
  return true;
}

template <typename T>
inline bool FASTIO::READ_FLOAT(T &x) {
  return (scanf("%f", &x) != EOF);
}

template <typename T>
inline bool FASTIO::READ_DOUBLE(T &x) {
  double y;
  if(scanf("%lf", &y) == EOF) return false;
  x = y;
  return true;
}

template<std::size_t N>
inline bool FASTIO::READ_BITSET(std::bitset<N> &x) {
  if(!REMAINING_CHARACTER) READ_CHARACTER = getchar(), REMAINING_CHARACTER = true; else REMAINING_CHARACTER = false;
  while ((READ_CHARACTER == '\n' || READ_CHARACTER == '\t' || READ_CHARACTER == ' ')) READ_CHARACTER = getchar();
  if(READ_CHARACTER == EOF) return REMAINING_CHARACTER = false, false;
  int i = 0; REMAINING_CHARACTER = true;
  while (READ_CHARACTER == '0' || READ_CHARACTER == '1') x[i++] = READ_CHARACTER - '0', READ_CHARACTER = getchar();
  return true;
}

inline bool FASTIO::READ_VAR(short int &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(int &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(long int &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(long long int &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(unsigned short int &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(unsigned int &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(unsigned long &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(unsigned long long &x) {
  return READ_INT(x);
}

inline bool FASTIO::READ_VAR(std::string &x) {
  return READ_STRING(x);
}

inline bool FASTIO::READ_VAR(char &x) {
  return READ_CHAR(x);
}

template<size_t N>
inline bool FASTIO::READ_VAR(char (&x)[N]) {
  return READ_CHAR_ARRAY(x);
}

inline bool FASTIO::READ_VAR(char*& x) {
  return READ_CHAR_ARRAY(x);
}

inline bool FASTIO::READ_VAR(float &x) {
  return READ_FLOAT(x);
}

inline bool FASTIO::READ_VAR(double &x) {
  return READ_DOUBLE(x);
}

inline bool FASTIO::READ_VAR(long double &x) {
  return READ_DOUBLE(x);
}

template<std::size_t N>
inline bool FASTIO::READ_VAR(std::bitset<N> &x) {
  return READ_BITSET(x);
}

// cout modifications

template <typename T>
inline void FASTIO::WRITE_INT(T x) {
  if (x < 0) {putchar('-'); x = -x; }
  char writeBuffer[20], *writePtr = writeBuffer;
  do {
    *writePtr++ = '0' + x % 10;
    x /= 10;
  }
  while (x);
  do  { putchar(*--writePtr); }
  while (writePtr > writeBuffer);
}

inline void FASTIO::WRITE_CHAR(char x) {
  putchar(x);
}

inline void FASTIO::WRITE_CHAR_ARRAY(const char *x) {
  while(*x != '\0')
    putchar(*x++);
}

inline void FASTIO::WRITE_STRING(std::string &x) {
  for(char c: x)
    putchar(c);
}

inline void FASTIO::WRITE_FLOAT(float x) {
  printf("%f", x);
}

template <typename T>
inline void FASTIO::WRITE_DOUBLE(T x) {
  printf("%lf", (double)x);
}

template<std::size_t N>
inline void FASTIO::WRITE_BITSET(std::bitset<N> &x) {
  for(int i = (int)x.size() - 1; i >= 0; i--)
    putchar(x[i] + 48);
}

inline void FASTIO::WRITE_VAR(bool x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(short int x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(int x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(long int x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(long long int x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(unsigned short int x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(unsigned int x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(unsigned long x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(unsigned long long x) {
  WRITE_INT(x);
}

inline void FASTIO::WRITE_VAR(std::string &x) {
  WRITE_STRING(x);
}

inline void FASTIO::WRITE_VAR(char x) {
  WRITE_CHAR(x);
}

inline void FASTIO::WRITE_VAR(const char *x) {
  WRITE_CHAR_ARRAY(x);
}

inline void FASTIO::WRITE_VAR(float x) {
  WRITE_FLOAT(x);
}

inline void FASTIO::WRITE_VAR(double x) {
  WRITE_DOUBLE(x);
}

inline void FASTIO::WRITE_VAR(long double x) {
  WRITE_DOUBLE(x);
}

template<std::size_t N>
inline void FASTIO::WRITE_VAR(std::bitset<N> &x) {
  WRITE_BITSET(x);
}