N = int(input()) X = list(map(int, input().split())) for i in range(N): X[i] -= 1 diff_sum = [0 for _ in range(N)] for i in range(N - 1): if X[i] != X[i + 1]: diff_sum[i + 1] = diff_sum[i] else: diff_sum[i + 1] = diff_sum[i] + 1 kinds = [0 for _ in range(N)] Q = int(input()) for _ in range(Q): l, r, S = map(int, input().split()) l -= 1 r -= 1 if r - l >= 90: diffs = diff_sum[r] - diff_sum[l] if diffs >= 60: print(0) else: if S >= (1 << diffs): print(1) else: print(0) else: vec = [0 for _ in range(r - l + 1)] for j in range(l, r + 1): vec[j - l] = X[j] len_vec = len(vec) ok = 0 ng = len_vec + 2 while ng - ok > 1: mid = (ok + ng) // 2 prev_idx = [-1 for _ in range(len_vec + 1)] now_kind = 0 left = 0 right = 0 while True: if now_kind <= mid: if right == len_vec: prev_idx[right] = left break else: prev_idx[right] = left if kinds[vec[right]] == 0: now_kind += 1 kinds[vec[right]] += 1 right += 1 else: kinds[vec[left]] -= 1 if kinds[vec[left]] == 0: now_kind -= 1 left += 1 dp = [0 for _ in range(len_vec + 1)] dp_sum = [0 for _ in range(len_vec + 1)] dp[0] = 1 dp_sum[0] = 1 infin = False for j in range(1, len_vec + 1): sum_val = dp_sum[j - 1] if prev_idx[j] > 0: sum_val -= dp_sum[prev_idx[j] - 1] if sum_val > S: infin = True break dp[j] = sum_val dp_sum[j] = dp[j] + dp_sum[j - 1] if infin: ng = mid else: ok = mid for j in range(len_vec): kinds[vec[j]] = 0 if ok >= len_vec: print(-1) else: print(ok)