#include using namespace std; template ostream &operator<<(ostream &s, const pair &v) { s << "(" << v.first << ", " << v.second << ")"; return s; } template >> istream &operator>>(istream &s, T &&v) { for (auto &&x : v) s >> x; return s; } template >> ostream &operator<<(ostream &s, T &&v) { for (auto &&x : v) s << x << ' '; return s; } #ifdef LOCAL template void dbg(T... x) { char e{}; ((cerr << e << x, e = ' '), ...); } #define debug(x...) dbg(#x, '=', x, '\n') #else #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #define debug(...) ((void)0) #endif #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define ff first #define ss second template inline constexpr T inf = numeric_limits::max() / 2; template bool chmin(T &a, T b) { return (b < a and (a = b, true)); } template bool chmax(T &a, T b) { return (a < b and (a = b, true)); } using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128; constexpr i64 mod = 998244353; void solve() { int n; cin >> n; vector A(n); cin >> A; A.push_back(-1); vector dif(n + 1); for (int i = n - 1; i >= 0; i--) { dif[i] = dif[i + 1] + (A[i] != A[i + 1]); } vector cnt(n + 1); auto DP = [&](int l, int r, int k) -> i64 { if (k == 0) return 0; vector dp(r - l + 1); dp[0] = 1; for (int i = l; i < r; i++) cnt[A[i]] = 0; int d = 0; for (int i = l, t = l - 1; i < r; i++) { d += (cnt[A[i]]++ == 0); while (d > k) { t++; d -= (--cnt[A[t]] == 0); } for (int j = t; j < i; j++) { dp[i - l + 1] += dp[j - l + 1]; chmin(dp[i - l + 1], inf); } } return dp.back(); }; int q; cin >> q; while (q--) { int l, r; i64 s; cin >> l >> r >> s; l--; if (r - l > 90) { i64 p = r - l - 1 - (dif[l] - dif[r - 1]); if (p > 60 or (1ll << p) > s) { cout << 0 << '\n'; } else { cout << 1 << '\n'; } } else { int ans = *ranges::partition_point(views::iota(1, r - l + 1), [&](int t) { return DP(l, r, t) <= s; }); if (ans == r - l + 1) { cout << -1 << '\n'; } else { cout << ans - 1 << '\n'; } } } } signed main() { cin.tie(0)->sync_with_stdio(false); cin.exceptions(cin.failbit); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }