#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long int arr[n + 1];
    for (int i = 0; i < n; i++) cin >> arr[i];
    long int neg = 0, pos = 0, minNeg = 1000000010, maxPos = 0, ans = 0;
    if (arr[n - 1] < 0)
      arr[n] = 1;
    else
      arr[n] = -1;
    for (int i = 0; i <= n; i++) {
      if (arr[i] < 0) {
        neg++;
        minNeg = min(minNeg, -arr[i]);
        if (pos > 0) {
          ans += maxPos;
          pos = 0;
          maxPos = 0;
        }
      } else {
        pos++;
        maxPos = max(maxPos, arr[i]);
        if (neg > 0) {
          ans -= minNeg;
          neg = 0;
          minNeg = 1000000010;
        }
      }
    }
    cout << ans << '\n';
  }
}
