# 入力 n = int(input()) X = list(map(int,input().split())) q = int(input()) INF = 3*10**18 # 尺取り法の関数 def add(x, cnt): if(x not in cnt): cnt[x] = 0 cnt[x] += 1 def remove(x, cnt): cnt[x] -= 1 if(cnt[x] == 0): del cnt[x] def judge(x, cnt, k): add(x, cnt) flag = len(cnt) <= k remove(x, cnt) return flag def dp(k): if(k == 0): return 0 l = 0 r = 0 # 遷移先 to = [n] * n # 要素数カウント cnt = {} ans = -1 # 尺取り法 while r < n: if(judge(A[r], cnt, k)): add(A[r], cnt) r += 1 else: remove(A[l], cnt) l += 1 to[l] = r dp = [0] * (n+1) dp[0] = 1 dp[1] = -1 for i in range(n): # 累積 if(i != 0): dp[i] += dp[i-1] if(dp[i] > s): return INF # 遷移 dp[i+1] += dp[i] if(to[i]+1 < n+1): dp[to[i]+1] -= dp[i] ans = dp[-1] + dp[-2] return ans # 同じ要素が隣り合っているものの個数累積和 CX = [0] for i in range(n-1): CX.append(CX[-1] + int(X[i] == X[i+1])) CX.append(CX[-1]) # べき乗の前計算 POW = [1] for i in range(62): POW.append(POW[-1]*2) for _ in range(q): l,r,s = map(int,input().split()) l -= 1 r -= 1 if(r-l+1 >= 62): c = CX[r+1]-CX[l+1] if(c >= 62): print(0) continue # f(1)の値 f1 = POW[c] if(f1 <= s): T.append(f1) # print(f1) else: T.append(0) # print(0) else: A = X[l:r+1] # めぐる式二分探索 ok = 0 ng = 70 n = len(A) while abs(ok-ng)>1: mid = (ok+ng)//2 if(dp(mid)<=s): ok = mid else: ng = mid if(ok >= 65): print(-1) else: print(dp(ok))