#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
int T, n, zh, fu;
int len[N], a[N];
long long val[N];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    zh = fu = 0;
    int ml = 0;
    long long maxx = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      if (a[i] > 0) {
        if (fu) val[i] = val[fu];
        len[i] = len[fu] + 1;
        val[i] += a[i];
        if (len[i] > len[zh] || !zh || (len[i] == len[zh] && a[zh] < a[i]))
          zh = i;
      } else {
        if (zh) val[i] = val[zh];
        len[i] = len[zh] + 1;
        val[i] += a[i];
        if (len[i] > len[fu] || !fu || (len[i] == len[fu] && a[fu] < a[i]))
          fu = i;
      }
      if (ml < len[i])
        maxx = val[i], ml = len[i];
      else if (ml == len[i])
        maxx = max(maxx, val[i]);
    }
    printf("%lld\n", maxx);
  }
}
