#include <bits/stdc++.h>
using namespace std;
int arr[1000000];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      if (arr[i] > 0) {
        ans += arr[i];
      }
    }
    stack<pair<int, int> > stk;
    for (int i = n - 1; i >= 0; i--) {
      if (arr[i] < 0) {
        stk.push({i, arr[i]});
      }
    }
    for (int i = 0; i < n; i++) {
      if (arr[i] < 0) {
        continue;
      } else {
        int current = arr[i];
        while (current > 0 && stk.size() > 0) {
          pair<int, int> temp = stk.top();
          stk.pop();
          if (temp.first < i) {
            continue;
          } else {
            int neg = temp.second;
            int x = current + neg;
            if (x >= 0) {
              ans += neg;
            } else {
              ans -= current;
              temp.second = x;
              stk.push(temp);
            }
            current = x;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
