# 【題解】Codeforces 1490G. Old Floppy Drive

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

int cnt, t, n, m, a[200005];
long long pre[200005], mx, ans;
vector <long long> val, idx;
bool flag;

int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--){
cin >> n >> m;
mx = 0;
val.clear();
idx.clear();
val.push_back(0);
idx.push_back(0);
for (int i = 1; i <= n; i++){
cin >> a[i];
pre[i] = pre[i-1]+a[i];
if (pre[i] > mx){
mx = pre[i];
val.push_back(pre[i]);
idx.push_back(i);
}
}
//if (a[1] == 16124 && a[2] == 8057) flag = true;
for (int i = 0, q; i < m; i++){
cin >> q;
cnt++;
//if (flag && cnt == 12701) cout << n << "_" << q << "_" << pre[n] << "_" << val.back() << " ";
if (q > val.back() && pre[n] <= 0){
cout << -1 << " ";
continue;
}
else if (q <= val.back()){
ans = idx[lower_bound(val.begin(), val.end(), q)-val.begin()];
cout << ans-1 << " ";
continue;
}
ans = (long long)n*((q-val.back()-1)/pre[n]+1);
q -= ((q-val.back()-1)/pre[n]+1)*pre[n];
ans += idx[lower_bound(val.begin(), val.end(), q)-val.begin()];
cout << ans-1 << " ";
}
cout << "\n";
}
}
```