#include <bits/stdc++.h>
using namespace std;
#define int int64_t
using vi = vector<int>;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t; while (t--) {
int n, m, v; cin >> n >> m >> v;
vi a(n); for (auto& i : a) cin >> i;
auto cal = [&] (vi f) {
vi r(n);
int csum = 0;
FOR(i, 0, n) {
csum += a[i];
if (csum >= v) csum = 0, r[i] = 1;
if (i) r[i] += r[i - 1];
}
return r;
};
vi p = cal(a);
reverse(a.begin(), a.end());
vi s = cal(a); reverse(s.begin(), s.end());
reverse(a.begin(), a.end());
vi psum = a; FOR(i, 1, n) psum[i] += psum[i - 1];
if (p.back() < m) {
cout << -1 << endl;
continue;
}
int ans = 0;
for (int i = 0; i < n; i++) {
int pp = i ? p[i - 1] : 0;
int lo = i, hi = n - 1;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
int ss = mid == n - 1 ? 0 : s[mid + 1];
if (pp + ss >= m) lo = mid;
else hi = mid - 1;
}
int ss = lo == n - 1 ? 0 : s[lo + 1];
if (pp + ss >= m) ans = max(ans, psum[lo] - (i ? psum[i - 1] : 0));
}
cout << ans << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW50IGludDY0X3QKdXNpbmcgdmkgPSB2ZWN0b3I8aW50PjsKI2RlZmluZSBGT1IoaSwgYSwgYikgZm9yIChpbnQgaSA9IChhKTsgaSA8IChiKTsgaSsrKQoKc2lnbmVkIG1haW4oKSB7Cglpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsKCWludCB0OyBjaW4gPj4gdDsgd2hpbGUgKHQtLSkgewoJCWludCBuLCBtLCB2OyBjaW4gPj4gbiA+PiBtID4+IHY7CgkJdmkgYShuKTsgZm9yIChhdXRvJiBpIDogYSkgY2luID4+IGk7CgkJYXV0byBjYWwgPSBbJl0gKHZpIGY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