You're given an array $a$ of $n$ integers, such that $a_1 + a_2 + \cdots + a_n = 0$.

In one operation, you can choose two different indices $i$ and $j$ ($1 \le i, j \le n$), decrement $a_i$ by one and increment $a_j$ by one. If $i < j$ this operation is free, otherwise it costs one coin.

How many coins do you have to spend in order to make all elements equal to $0$?


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). Description of the test cases follows.

The first line of each test case contains an integer $n$ ($1 \le n \le 10^5$)  — the number of elements.

The next line contains $n$ integers $a_1, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$). It is given that $\sum_{i=1}^n a_i = 0$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.


-----Output-----

For each test case, print the minimum number of coins we have to spend in order to make all elements equal to $0$.


-----Example-----
Input
7
4
-3 5 -3 1
2
1 -1
4
-3 2 -3 4
4
-1 1 1 -1
7
-5 7 -6 -4 17 -13 4
6
-1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000
1
0

Output
3
0
4
1
8
3000000000
0



-----Note-----

Possible strategy for the first test case:   Do $(i=2, j=3)$ three times (free), $a = [-3, 2, 0, 1]$.  Do $(i=2, j=1)$ two times (pay two coins), $a = [-1, 0, 0, 1]$.  Do $(i=4, j=1)$ one time (pay one coin), $a = [0, 0, 0, 0]$.

Difficulty: EASY
Tags: Implementation

Constructive algorithms

Starter code:

Solutions:
def cost(a):
	s = 0
	for x in a:
		if x < 0:
			m = min(s, abs(x))
			s -= m
		else:
			s += x
	return s
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	print(cost(a))


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	n = ri()
	aa = rl()
	ans = 0
	i = 0
	j = 1
	while i < n:
		if aa[i] <= 0:
			ans -= aa[i]
			i += 1
		else:
			if j <= i:
				j = i + 1
			while j < n and aa[i] > 0:
				if aa[i] + aa[j] > 0:
					aa[i] += aa[j]
					aa[j] = 0
					j += 1
				else:
					aa[j] += aa[i]
					aa[i] = 0
					break
			i += 1
	print(ans)


import sys
import math
input = sys.stdin.readline
from collections import deque
from queue import LifoQueue
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	to = 0
	for i in range(n):
		if a[i] > 0:
			to += a[i]
		if a[i] < 0 and to >= -a[i]:
			to += a[i]
			a[i] = 0
		elif a[i] < 0 and to > 0:
			a[i] += to
			to = 0
	ans = 0
	for i in range(n):
		if a[i] < 0:
			ans += a[i]
	print(-ans)


for i in range(int(input())):
	n = int(input())
	l = [int(c) for c in input().split()]
	l = list(reversed(l))
	negative = 0
	o = 0
	for i in range(n):
		if l[i] > 0 and negative <= 0:
			o += l[i]
		elif l[i] > 0 and negative > 0:
			if l[i] > negative:
				o += l[i] - negative
				negative = 0
			else:
				negative = negative - l[i]
		elif l[i] <= 0:
			negative += abs(l[i])
	print(o)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	curr = 0
	for i in range(len(a)):
		curr = max(0, curr + a[i])
	print(curr)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(sum1, sum2) = (0, 0)
	ans = 0
	for i in a:
		if i <= 0:
			sum1 += i
		elif sum1 < 0:
			ans += sum1
			sum1 = i
		else:
			sum1 += i
	print(-ans)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	sm = 0
	lft = 0
	for i in range(0, n):
		if a[i] >= 0:
			sm += a[i]
		else:
			lft += max(0, -a[i] - sm)
			sm = max(0, sm + a[i])
	print(lft)
t = int(input())
for i in range(t):
	main()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		alst = list(map(int, input().split()))
		ans = 0
		total = 0
		for a in alst:
			total -= a
			ans = max(ans, total)
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(ans, lt) = (0, 0)
	for i in range(n):
		lt += l[i]
		rt = 0 - lt
		if lt < 0 and rt > 0:
			ans = max(ans, abs(lt), abs(rt))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ans = 0
	for x in a:
		s += x
		ans = min(ans, s)
	print(-ans)


import sys
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(readline())

def read_int_n():
	return list(map(int, readline().split()))

def read_float():
	return float(readline())

def read_float_n():
	return list(map(float, readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.perf_counter()
		ret = f(*args, **kwargs)
		e = time.perf_counter()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, A):
	j = 1
	for i in range(N):
		j = max(j, i + 1)
		while A[i] > 0:
			if j >= N:
				break
			if A[j] < 0:
				t = min(A[i], -A[j])
				A[i] -= t
				A[j] += t
			else:
				j += 1
	return sum(filter(lambda a: a > 0, A))

def main():
	for _ in range(read_int()):
		N = read_int()
		A = read_int_n()
		print(slv(N, A))
main()


t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	a = list(map(int, input().split()))
	for j in range(n):
		c = max(0, c + a[j])
	print(c)


for ctr in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cur = 0
	s = sum(a)
	for i in range(n - 1):
		s -= a[i]
		cur = max(cur, s)
	print(cur)


for i in range(int(input())):
	n = input()
	n = [int(x) for x in input().split()]
	s = 0
	smin = 0
	for i in range(len(n)):
		s += n[i]
		if s < smin:
			smin = s
	print(abs(smin))


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	cost = 0
	s = 0
	for i in range(N):
		s = s + A[i]
		if s < 0:
			cost = cost - s
			s = 0
	print(cost)


from itertools import permutations
from collections import defaultdict
from math import *
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
from bisect import *
import copy
import itertools
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	(plus, minus) = (0, 0)
	p = 0
	for i in range(n):
		if arr[i] >= 0:
			plus += arr[i]
			p += arr[i]
		elif p == 0:
			minus += -arr[i]
		elif p + arr[i] >= 0:
			p += arr[i]
			plus -= -arr[i]
		else:
			minus += -p - arr[i]
			plus -= p
			p = 0
	print(max(plus, minus))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	last_pos = n - 1
	first_neg = 0
	cost = 0
	for i in range(n):
		if a[i] == 0:
			continue
		if a[i] < 0:
			cost += -a[i]
			while a[i] != 0:
				while a[last_pos] <= 0:
					last_pos -= 1
				t = a[i] + a[last_pos]
				if t >= 0:
					a[i] = 0
					a[last_pos] = t
					break
				else:
					a[i] = t
					a[last_pos] = 0
		else:
			while a[i] != 0:
				while a[first_neg] >= 0:
					first_neg += 1
				t = a[i] + a[first_neg]
				if t >= 0:
					a[i] = t
					a[first_neg] = 0
				else:
					a[i] = 0
					a[first_neg] = t
					break
	print(cost)


def gt():
	return list(map(int, input().split()))
(t,) = gt()
while t:
	t -= 1
	(n,) = gt()
	ar = gt()
	ans = 0
	tot = 0
	for i in range(n - 1):
		tot += ar[i]
		if tot < 0:
			ans -= tot
			tot = 0
	print(ans)


t = int(input())
for c in range(t):
	n = int(input())
	arr = [int(x) for x in input().split(' ')]
	cur = 0
	cost = 0
	for each in arr:
		cur += each
		if cur < 0:
			cost -= cur
			cur = 0
	print(cost)


import math as mt
import collections as cc
import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	l = I()
	i = 0
	ans = 0
	pos = 0
	neg = 0
	while i < n:
		while i < n and l[i] >= 0:
			pos += l[i]
			i += 1
		while i < n and l[i] <= 0:
			neg += l[i]
			i += 1
		ans += abs(min(0, neg + pos))
		if neg + pos > 0:
			pos = pos + neg
			neg = 0
		else:
			neg = 0
			pos = 0
	print(ans)


import sys, bisect
from sys import stdin, stdout
from bisect import bisect_left, bisect_right, bisect, insort, insort_left, insort_right
from math import gcd, ceil, floor, sqrt

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def solve():
	n = inp()
	l = li()
	x = [-i for i in l]
	y = [0]
	for i in x:
		y.append(y[-1] + i)
	z = [i for i in y if i >= 0]
	print(max(z))
for _ in range(inp()):
	solve()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pre = [a[0]]
	for i in range(1, n):
		pre.append(pre[i - 1] + a[i])
	print(abs(min(pre)))


t = int(input())
for k in range(t):
	n = int(input())
	vec = list(map(int, input().split()))
	i = 0
	while vec[i] < 0:
		i += 1
	rem = vec[i]
	for j in range(i + 1, n):
		if vec[j] >= 0:
			rem += vec[j]
		else:
			rem -= min(-vec[j], rem)
	print(rem)


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	i = 0
	j = 0
	while j < N and i < N:
		if A[i] <= 0:
			i += 1
		elif A[j] >= 0 or j <= i:
			j += 1
		elif abs(A[j]) > A[i]:
			A[j] += A[i]
			A[i] = 0
		else:
			A[i] += A[j]
			A[j] = 0
	sm = 0
	for a in A:
		sm += max(a, 0)
	print(sm)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	m = 0
	for i in range(n):
		s = s + l[i]
		m = min(s, m)
	print(abs(m))


def main():
	trials = input()
	for i in range(int(trials)):
		size = int(input())
		data = [int(s) for s in input().split()]
		bal = 0
		tot = 0
		for i in range(size):
			if bal + data[i] < 0:
				tot -= bal + data[i]
				bal = 0
			else:
				bal += data[i]
		print(tot)
main()


from sys import stdin, stdout

def input():
	return stdin.readline().strip()

def ans(A):
	A = A[::-1]
	ret = float('-inf')
	s = 0
	for a in A:
		s += a
		ret = max(ret, s)
	return ret
T = int(input())
for t in range(T):
	input()
	A = list(map(int, input().split()))
	print(ans(A))


t = int(input())
for i in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	s = 0
	for j in range(n - 1, -1, -1):
		if A[j] < 0:
			s += A[j]
		if A[j] > 0:
			s += min(A[j], abs(s))
	print(abs(s))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def out(ret):
	print(ret)

def outlt(ret):
	print(' '.join(ret))

class Solution:

	def main(self, n, a):
		tot = 0
		for i in range(n):
			if a[i] > 0:
				tot += a[i]
			else:
				k = min(tot, -a[i])
				a[i] += k
				tot -= k
		tot = 0
		for j in range(n):
			if a[j] < 0:
				tot -= a[j]
		return tot
t = inp()
solution = Solution()
for i in range(t):
	n = inp()
	a = inlt()
	out(solution.main(n, a))


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	minus = 0
	plus = 0
	for i in lst:
		if i == 0:
			pass
		if i < 0:
			if not plus:
				minus += i
			if plus:
				plus += i
				if plus < 0:
					minus += plus
					plus = 0
		if i > 0:
			plus += i
	print(-minus)


T = int(input().rstrip())

def solve(arr):
	i = 0
	j = 0
	arr2 = arr.copy()
	arr2_len = len(arr2)
	res = 0
	while i < arr2_len - 1:
		neg_contrib = 0
		if arr2[i] > 0:
			rsum = arr2[i]
			for j in range(i + 1, arr2_len):
				if arr2[j] < 0:
					if abs(arr2[j]) > rsum:
						neg_contrib = rsum + arr2[j]
						break
					else:
						rsum += arr2[j]
				else:
					rsum += arr2[j]
			i = j
		else:
			neg_contrib = arr2[i]
		i += 1
		res += neg_contrib
	return -res
for test in range(T):
	n = int(input().rstrip())
	perm = [int(x) for x in input().rstrip().split()]
	print(solve(perm))


import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '7\n4\n-3 5 -3 1\n2\n1 -1\n4\n-3 2 -3 4\n4\n-1 1 1 -1\n7\n-5 7 -6 -4 17 -13 4\n6\n-1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000\n1\n0\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield input()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]

def solve(data):
	negative = 0
	for i in reversed(range(len(data))):
		if data[i] < 0:
			negative += -data[i]
			data[i] = 0
		elif data[i] > 0:
			if data[i] >= negative:
				data[i] -= negative
				negative = 0
			else:
				negative -= data[i]
				data[i] = 0
	return negative
t = getType(int)
for _ in range(t):
	n = getType(int)
	data = getArray(int)
	print(solve(data))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ans = 0
	pos = 0
	neg = 0
	for i in range(n):
		if ar[i] < 0:
			pos += ar[i]
			if pos < 0:
				ans += abs(pos)
				pos = 0
		elif ar[i] > 0:
			pos += ar[i]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	tmp = 0
	a.reverse()
	for j in a:
		tmp = tmp + j
		res = max(res, tmp)
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	sum = 0
	j = 0
	while j < n - 1:
		if arr[j] > 0:
			sum = sum + arr[j]
			k = j + 1
			while sum >= 0 and k <= n - 1:
				sum += arr[k]
				if sum >= 0:
					arr[k] = 0
					arr[j] = sum
					k += 1
				else:
					arr[j] = 0
					arr[k] = sum
					j = k
					sum = 0
					break
		j += 1
	key1 = 0
	key2 = 0
	for elem in range(n):
		if arr[elem] > 0:
			key1 += arr[elem]
		else:
			key2 += arr[elem]
	print(max(key1, abs(key2)))


t = int(input())
for _ in range(t):
	n = int(input())
	v = [int(x) for x in input().split()]
	pos = 0
	for i in v:
		if i > 0:
			pos += i
		elif pos > 0:
			pos -= min(pos, abs(i))
	print(pos)


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	quota = 0
	for x in a:
		k = abs(x)
		if x >= 0:
			quota += k
		else:
			r = max(0, k - quota)
			quota -= k - r
			ans += r
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	g = 0
	h = 0
	for i in a:
		h = h + i
		g = min(g, h)
	print(-1 * g)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	s = 0
	for i in range(len(li)):
		if li[i] > 0:
			s += li[i]
		else:
			s -= min(s, abs(li[i]))
	print(s)


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	pos = 0
	neg = 0
	for i in l:
		if i > 0:
			pos += i
		elif i < 0:
			pos = max(0, pos + i)
	print(pos)
t = int(input())
while t:
	t -= 1
	solve()


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	st = []
	for num in a:
		if num < 0 and len(st) != 0:
			if st[0] + num <= 0:
				st.pop()
			else:
				st[0] += num
		if num > 0:
			if len(st) == 0:
				st.append(num)
			else:
				st[0] += num
	if len(st) == 0:
		print(0)
	else:
		print(st[0])


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(lambda x: int(x), input().split(' ')))
	remain = 0
	saved = 0
	for i in range(n):
		if arr[i] < 0:
			if -arr[i] - saved > 0:
				saved = 0
				remain += -arr[i] - saved
			else:
				saved -= -arr[i]
		else:
			saved += arr[i]
	print(saved)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	c = 0
	for i in range(n):
		c += w[i]
		if c < 0:
			c = 0
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	z = [int(x) for x in input().split(' ')]
	(c, ans, a) = (0, 0, 0)
	for i in range(n):
		if z[i] >= 0:
			c += z[i]
		if z[i] < 0:
			a = z[i]
			z[i] += c
			if z[i] < 0:
				ans += abs(z[i])
			c = max(0, c + a)
	print(ans)


from sys import stdin, stdout
import heapq as hq
from collections import defaultdict
t = int(input())
for tc in range(t):
	n = int(stdin.readline())
	ar = list(map(int, stdin.readline().split()))
	res = 0
	i = 0
	j = n - 1
	while j > i:
		if ar[i] == 0:
			i += 1
		elif ar[j] == 0:
			j -= 1
		elif ar[i] > 0:
			ar[i + 1] += ar[i]
			ar[i] = 0
			i += 1
		else:
			d = abs(ar[i])
			if d < ar[j]:
				res += d
				ar[j] -= d
				ar[i] = 0
				i += 1
			else:
				res += ar[j]
				ar[i] += ar[j]
				ar[j] = 0
				j -= 1
	stdout.write(str(res) + '\n')


import sys
input = sys.stdin.readline
t = int(input())
for case in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	p = [0 for i in range(len(a))]
	s = 0
	ans = 0
	for i in range(n):
		s += a[i]
		p[i] = s
	for l in range(n):
		ans = max(ans, p[n - 1] - p[l])
	print(ans)


from typing import List
t = int(input())
for q in range(t):
	l = int(input())
	ls = list(map(int, input().split()))
	free_coin = 0
	for i in range(l):
		if ls[i] > 0:
			free_coin = free_coin + ls[i]
			ls[i] = 0
		if ls[i] < 0:
			if free_coin >= abs(ls[i]):
				if free_coin == 0:
					pass
				else:
					free_coin = free_coin + ls[i]
					ls[i] = 0
			else:
				free_coin = 0
				ls[i] = free_coin + ls[i]
	print(free_coin)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for val in arr:
		if val < 0:
			ans += abs(val)
	curr_neg = ans
	curr_pos = 0
	coins = 0
	for val in arr:
		if val < 0:
			if curr_pos > abs(val):
				curr_pos -= abs(val)
				curr_neg += abs(val)
			else:
				coins += abs(val) - curr_pos
				curr_pos = 0
				curr_neg += abs(val) - curr_pos
		elif curr_neg > abs(val):
			curr_neg -= abs(val)
			curr_pos += abs(val)
		else:
			coins += abs(val) - curr_neg
			curr_neg = 0
			curr_pos += abs(val) - curr_neg
	print(coins)


import bisect
import sys
import io, os
t = int(input())
for ii in range(t):
	n = int(input())
	a = [int(i) for i in input().split() if i != '\n']
	suff = [a[-1]]
	for i in range(n - 2, -1, -1):
		suff.append(suff[-1] + a[i])
	sys.stdout.write(str(max(suff)) + '\n')


n_sets = int(input())
for _ in range(n_sets):
	_ = int(input())
	a = map(int, input().split())
	r_min = 0
	r = 0
	for x in a:
		r += x
		r_min = min(r, r_min)
	print(-r_min)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	neg = 0
	pos = 0
	ans = 0
	for i in range(n):
		if l[i] >= 0:
			pos += l[i]
		elif pos >= -l[i]:
			pos -= abs(l[i])
		else:
			pos = 0
			l[i] += pos
			neg += l[i]
	ans += pos
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	mx = 0
	cnt = 0
	for i in range(n - 1, -1, -1):
		cnt += a[i]
		mx = max(cnt, mx)
	print(mx)


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	neg = 0
	pos = 0
	ans = 0
	for i in range(n):
		if l[i] >= 0:
			pos += l[i]
		else:
			neg += l[i]
		if abs(neg) > pos:
			ans += abs(neg) - pos
			neg = 0
			pos = 0
		else:
			pos -= abs(neg)
			neg = 0
	print(ans)
	t -= 1


import math
t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	c = 0
	c2 = 0
	for i in range(n):
		if l[i] > 0:
			c += l[i]
		elif c > 0:
			c -= min(c, abs(l[i]))
	print(c)


myList = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sum1 = 0
	sum2 = 0
	k = 0
	p = 0
	for i in range(n):
		if a[i] >= 0:
			sum1 += a[i]
		elif sum1 != 0:
			sum1 = sum1 + a[i]
			if sum1 < 0:
				sum2 += sum1
				sum1 = 0
		else:
			sum2 += a[i]
	myList.append(-sum2)
for elem in myList:
	print(elem)


T = int(input())
while T:
	n = int(input())
	arr = list(map(int, input().split()))
	cmf = 0
	for i in range(len(arr)):
		if arr[i] < 0 and cmf != 0:
			cmf = max(0, cmf + arr[i])
		if arr[i] > 0:
			cmf += arr[i]
	print(cmf)
	T -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	coins = 0
	for num in a:
		coins = max(0, coins + num)
	print(coins)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	indp = 0
	for indm in range(n):
		while A[indm] > 0:
			while indp < n and (indp <= indm or A[indp] >= 0):
				indp += 1
			if indp == n:
				break
			if A[indm] > 0 and A[indp] < 0:
				D = min(A[indm], -A[indp])
				A[indm] -= D
				A[indp] += D
		if indp == n:
			break
	ANS = 0
	for a in A:
		if a > 0:
			ANS += a
	print(ANS)


def solve(n, a):
	q = [0] * n
	for i in range(n):
		if a[i] > 0:
			q[i] = 1
		if a[i] < 0:
			q[i] = -1
	(x, y) = (0, 1)
	while x < n and y < n:
		if q[x] == 1 and q[y] == -1:
			d = a[x]
			a[x] -= d
			a[y] += d
			if a[y] < 0:
				q[y] = -1
			elif a[y] == 0:
				q[y] = 0
			else:
				q[y] = 1
			if a[x] < 0:
				q[x] = -1
			elif a[x] == 0:
				q[x] = 0
			else:
				q[x] = 1
			if q[y] == 0:
				y += 1
			else:
				x += 1
		elif q[x] == 1:
			y += 1
		elif q[y] == -1:
			if x < y:
				x += 1
			else:
				y += 1
		else:
			y += 1
	ans = 0
	for i in range(n):
		if q[i] == 1:
			ans += a[i]
	return ans

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(i) for i in input().split()]
		print(solve(n, a))
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	en = 0
	ep = 0
	for i in range(n):
		if l[i] >= 0:
			ep += l[i]
		elif ep == 0:
			en += l[i]
		elif ep > abs(l[i]):
			ep += l[i]
		elif ep < abs(l[i]):
			en += ep + l[i]
			ep = 0
		else:
			ep = 0
	ans = 0
	ans += max(abs(en), ep)
	print(ans)


from __future__ import division, print_function
import os, sys
import math
import collections
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

class my_dictionary(dict):

	def __init__(self):
		self = dict()

	def add(self, key, value):
		self[key] = value

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def sli():
	return list(msi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey, reverse=True)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r))

def binary_search(arr, low, high, x):
	if high >= low:
		mid = (high + low) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binary_search(arr, low, mid - 1, x)
		else:
			return binary_search(arr, mid + 1, high, x)
	else:
		return -1

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def padded_bin_with_complement(x):
	if x < 0:
		return bin(2 ** 16 - abs(x))[2:].zfill(16)
	else:
		return bin(x)[2:].zfill(16)

def binaryToDecimal(binary):
	binary1 = binary
	(decimal, i, n) = (0, 0, 0)
	while binary != 0:
		dec = binary % 10
		decimal = decimal + dec * pow(2, i)
		binary = binary // 10
		i += 1
	print(decimal)

def CountFrequency(my_list):
	freq = {}
	for item in my_list:
		if item in freq:
			freq[item] += 1
		else:
			freq[item] = 1
	return freq

def pos(a):
	b = [0] * len(a)
	c = sorted(a)
	for i in range(len(a)):
		for j in range(len(a)):
			if c[j] == a[i]:
				b[i] = j
				break
	return b

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n

def commonn(a, b, n):
	c = []
	for i in range(n):
		if a[i] == b[i]:
			c.append('-1')
		else:
			c.append(b[i])
	return c

def find_lcm(num1, num2):
	if num1 > num2:
		num = num1
		den = num2
	else:
		num = num2
		den = num1
	rem = num % den
	while rem != 0:
		num = den
		den = rem
		rem = num % den
	gcd = den
	lcm = int(int(num1 * num2) / int(gcd))
	return lcm

def sumdigit(n):
	n = str(n)
	k = 0
	for i in range(len(n)):
		k += int(n[i])
	return k

def main():
	for _ in range(ii()):
		n = ii()
		a = li()
		res = [0] * n
		res[0] = a[0]
		for i in range(1, n):
			res[i] = a[i] + res[i - 1]
		print(abs(min(res)))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = s = 0
	for i in range(n - 1, -1, -1):
		if a[i] < 0:
			s += abs(a[i])
		else:
			c += min(s, a[i])
			s = max(s - a[i], 0)
	print(s)


t = int(input())
i = 0
while i < t:
	n = int(input())
	p = list(map(int, input().split()))
	p.append(0)
	j = 0
	v = 0
	while j < n:
		if p[j] <= 0:
			v += p[j]
		elif p[j] > 0:
			k = p[j]
			k += p[j + 1]
			p[j + 1] = k
		j += 1
	print(abs(v))
	i += 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in a:
		s += i
		if s < 0:
			ans += s
			s = 0
	print(abs(s))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	m = 0
	for i in l:
		if i < 0:
			if abs(i) >= s:
				x = abs(i) - s
				s = 0
				m += x
			else:
				s -= abs(i)
		else:
			s += i
	print(m)


from sys import stdin, stdout

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(input())):
			n = int(input())
			a = [int(x) for x in R().split()]
			pos = []
			neg = []
			for j in range(n):
				if a[j] < 0:
					neg.append(j)
				else:
					pos.append(j)
			p = 0
			q = 0
			while p < len(pos) and q < len(neg):
				if pos[p] < neg[q]:
					mn = min(a[pos[p]], abs(a[neg[q]]))
					a[pos[p]] -= mn
					a[neg[q]] += mn
					if a[pos[p]] == 0:
						p += 1
					else:
						q += 1
				else:
					q += 1
			tmp = 0
			for j in range(len(pos)):
				tmp += a[pos[j]]
			ans += [str(tmp)]
		W('\n'.join(ans))

def main():
	s = SOLVE()
	s.solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	s = 0
	prev = 0
	for i in range(n):
		if a[i] < 0:
			ad = min(prev + a[i], 0)
			s += abs(ad)
			prev = max(prev + a[i], 0)
		else:
			prev += a[i]
	print(s)


t = int(input())
for case in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pos_count = 0
	neg_count = 0
	for i in range(n):
		if arr[i] > 0:
			pos_count += arr[i]
		elif arr[i] < 0:
			if pos_count > 0:
				if pos_count >= abs(arr[i]):
					pos_count -= abs(arr[i])
					arr[i] = 0
				else:
					arr[i] += pos_count
					pos_count = 0
					neg_count += arr[i]
			else:
				neg_count += arr[i]
		else:
			pass
	print(abs(neg_count))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
from collections import Counter
from collections import defaultdict as dc
for _ in range(N()):
	n = N()
	a = RLL()
	now = 0
	res = 0
	for x in a:
		if x < 0:
			delta = min(now, -x)
			now -= delta
			res += -(delta + x)
		else:
			now += x
	print(res)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	neg = 0
	for x in s[::-1]:
		if x < 0:
			neg += x
		else:
			neg += x
		if neg > 0:
			neg = 0
	print(-neg)


for t in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	ans = 0
	for i in l:
		ans = max(0, ans + i)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	for i in a:
		sum = max(0, sum + i)
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	curr_free = 0
	for item in a:
		if item < 0:
			if curr_free >= -item:
				curr_free += item
			else:
				ans += -item - curr_free
				curr_free = 0
		else:
			curr_free += item
	print(ans)


import sys
for i in range(int(input())):
	x = int(input())
	lista = input().split()
	if len(lista) == 1:
		print(0)
		continue
	for k in range(len(lista)):
		lista[k] = int(lista[k])
	j = 0
	g = len(lista) // 2
	while j < g:
		j = j + 1
	d = 0
	lookingFree = 0
	while d < len(lista) - 1 and lookingFree < len(lista) - 1:
		while lista[lookingFree] < 0 and lista[d] > 0 and (d < lookingFree):
			if lista[d] >= lista[lookingFree]:
				aux = lista[d]
				lista[lookingFree] = lista[lookingFree] + lista[d]
				lista[d] = 0
				break
			lista[d] = lista[d] - 1
			lista[lookingFree] = lista[lookingFree] + 1
		if lista[lookingFree] >= 0 or lookingFree <= d:
			lookingFree = lookingFree + 1
		if lista[d] <= 0:
			d = d + 1
	coins = 0
	d = 0
	lookingFree = len(lista) - 1
	while d < len(lista) - 1 and lookingFree >= 0:
		while lista[lookingFree] > 0 and lista[d] < 0:
			if lista[d] <= lista[lookingFree]:
				aux = lista[d]
				lista[lookingFree] = lista[lookingFree] - lista[d]
				lista[d] = 0
				if d <= lookingFree:
					if aux < 0:
						aux = aux * -1
					coins = coins + aux
				break
			lista[d] = lista[d] + 1
			lista[lookingFree] = lista[lookingFree] - 1
			if d <= lookingFree:
				coins = coins + 1
		if lista[lookingFree] <= 0:
			lookingFree = lookingFree - 1
		if lista[d] >= 0:
			d = d + 1
	print(coins)


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	cost = 0
	for i in l:
		if i < 0:
			cost += i
			if cost < 0:
				cost = 0
		else:
			cost += i
	return cost
for _ in range(int(input())):
	print(solve())


t = int(input())
nlist = []
permutations = []
for _ in range(t):
	nlist.append(int(input()))
	permutations.append(list(map(int, input().split())))
for per in permutations:
	result = 0
	for i in range(len(per) - 1):
		if per[-1 - i] < 0:
			per[-2 - i] += per[-1 - i]
			per[-1 - i] = 0
	for num in per:
		if num < 0:
			result += num
	print(-result)


def main(args):
	t = int(input())
	for i in range(t):
		n = int(input())
		m = list(map(int, input().split()))
		j = n - 2
		k = n - 1
		count = 0
		while j >= 0 and k >= 0:
			if m[j] > 0 and m[k] < 0:
				n = min(m[j], -m[k])
				m[j] -= n
				m[k] += n
			if m[j] <= 0:
				j -= 1
			if m[k] >= 0:
				k -= 1
			if j == k:
				j = k - 1
		for j in m:
			if j > 0:
				count += j
		print(count)
import sys
sys.exit(main(sys.argv))


def ans_f(a):
	pow = 0
	for i in a:
		if i < 0:
			if pow > 0:
				pow = pow - min(pow, abs(i))
		else:
			pow = pow + i
	return pow
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans.append(ans_f(a))
for i in ans:
	print(i)


import sys
inputlines = sys.stdin.readlines()
number_of_testcases = int(inputlines[0])
for i in range(number_of_testcases):
	number_of_elements = int(inputlines[2 * i + 1])
	elements = list(map(int, inputlines[2 * i + 2].split(' ')))
	positives_now = 0
	for i in range(len(elements)):
		if elements[i] > 0:
			positives_now += elements[i]
		elif positives_now >= abs(elements[i]):
			positives_now += elements[i]
			elements[i] = 0
		else:
			elements[i] += positives_now
			positives_now = 0
	final_result = 0
	for i in elements:
		if i < 0:
			final_result += i
	print(abs(final_result))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in a:
		ans = max(0, ans + i)
	print(ans)


for _ in range(0, int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	t = 0
	for i in b:
		t = max(0, t + i)
	print(t)


T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	d = False
	cu = 0
	cu_m = 0
	for i in range(n):
		cu += a[i]
		cu_m = min(cu_m, cu)
	print(-cu_m)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	j = 0
	summ = 0
	while j < n:
		if a[j] > 0:
			summ += a[j]
			a[j] = 0
			j += 1
		elif a[j] < 0:
			if summ and summ >= abs(a[j]):
				summ += a[j]
				a[j] = 0
			elif summ and summ < abs(a[j]):
				a[j] += summ
				summ = 0
			j += 1
		else:
			j += 1
	count = 0
	for i in a:
		if i < 0:
			count += i
	print(abs(count))


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	n = int(ii())
	data = idata()
	for i in range(1, n):
		data[i] += data[i - 1]
	print(-min(data))
	return
for t in range(int(ii())):
	solve()


for _ in range(int(input())):
	length = int(input())
	num = list(map(int, input().split()))
	sum = 0
	p_sum = 0
	for n in range(length):
		if num[n] > 0:
			p_sum += num[n]
		elif num[n] < 0:
			if p_sum + num[n] < 0:
				sum -= p_sum + num[n]
				p_sum = 0
			else:
				p_sum += num[n]
	print(sum)


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		curr = 0
		cost = 0
		for i in range(n):
			if curr <= 0 and a[i] < 0:
				cost += -a[i]
			elif curr + a[i] < 0 and curr >= 0:
				cost += -(curr + a[i])
			curr = max(0, curr + a[i])
		print(cost)
prog()


for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	for i in range(len(a) - 1):
		if a[i] > 0:
			a[i + 1] += a[i]
			a[i] -= a[i]
	print(max(a))


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		values = list(map(int, input().split()))
		total = 0
		next_total = 0
		for i in range(n):
			if values[i] >= 0:
				total += values[i]
			elif total > -values[i]:
				next_total += -values[i]
				total += values[i]
				values[i] = 0
			else:
				next_total += total
				values[i] += total
				total = 0
		for i in range(n):
			if values[i] > 0:
				v = min(values[i], next_total)
				values[i] -= v
				next_total -= v
		ans = 0
		for i in values:
			ans += abs(i)
		print(ans // 2)
main()


for _ in range(int(input())):
	n = int(input())
	array = list(map(int, input().split()))
	if len(array) == 1:
		print(abs(array[0]))
	else:
		inc = 0
		dec = 0
		if array[0] >= 0:
			inc += array[0]
		else:
			dec += array[0]
		for i in range(1, n):
			if array[i] < 0:
				inc_1 = max(0, inc + array[i])
				diff = inc_1 - inc
				inc = inc_1
				dec += array[i] - diff
			else:
				inc += array[i]
		print(abs(inc))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [a[0]]
	cnt = 0
	for x in a[1:]:
		b.append(b[-1] + x)
	print(-min(b))


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(reversed(list(map(int, input().split()))))
	for j in range(1, n):
		arr[j] = arr[j] + arr[j - 1]
	print(max(arr))


t = int(input())
while t > 0:
	n = int(input())
	li = list(map(int, input().split(' ')))
	k = []
	var = 0
	for i in li:
		if i > 0:
			var += i
		elif var >= 0 - i:
			var += i
		else:
			x = var + i
			var = 0
			k.append(x)
	print(0 - sum(k))
	t -= 1


for _ in range(int(input())):
	n = int(input())
	data = [int(x) for x in input().split()]
	temp = 0
	ans = 0
	for q in range(n):
		if data[q] > 0:
			temp += data[q]
		elif data[q] < 0:
			t = min(abs(data[q]), temp)
			temp -= t
			data[q] += t
			ans += abs(data[q])
	print(ans)


for i in [0] * int(input()):
	plus = 0
	ans = 0
	input()
	l = list(map(int, input().split()))
	for i in l[:-1]:
		if i > 0:
			plus += i
		elif i < 0:
			if plus > 0:
				plus += i
				if plus < 0:
					ans += plus
					plus = 0
			else:
				ans += i
	print(-ans)


t = int(input())
for _ in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	totp = 0
	totn = 0
	for i in range(n):
		if totp == 0 and b[i] <= 0:
			totn += b[i]
		elif b[i] > 0:
			totp += b[i]
		elif b[i] < 0:
			c = totp + b[i]
			if c < 0:
				totn += c
				totp = 0
			elif c >= 0:
				totp += b[i]
	print(totp)


for i in range(int(input())):
	n = int(input())
	m = list(map(int, input().split()))
	left = 0
	ans = 0
	for i in m:
		if i < 0 and left == 0:
			ans += abs(i)
		elif i > 0:
			left += i
		elif i < 0 and left != 0 and (left >= abs(i)):
			left -= abs(i)
		elif i < 0 and left != 0 and (left < abs(i)):
			ans += abs(i) - left
			left = 0
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = 0
	l = list(map(int, input().split()))
	for i in range(0, n):
		s = max(0, s + l[i])
	print(s)


import sys, math, heapq, copy
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right
from functools import cmp_to_key
from itertools import permutations, combinations, combinations_with_replacement
mod = 1000000007

def get_int():
	return int(sys.stdin.readline().strip())

def get_str():
	return sys.stdin.readline().strip()

def get_float():
	return float(sys.stdin.readline().strip())

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_strs():
	return map(str, sys.stdin.readline().strip().split())

def get_floats():
	return map(float, sys.stdin.readline().strip().split())

def list_ints():
	return list(map(int, sys.stdin.readline().strip().split()))

def list_strs():
	return list(map(str, sys.stdin.readline().strip().split()))

def list_floats():
	return list(map(float, sys.stdin.readline().strip().split()))
t = get_int()
for _ in range(t):
	n = get_int()
	l = list_ints()
	ans = 0
	for i in l:
		ans = max(0, ans + i)
	print(ans)


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = linput()
	for i in range(n):
		if a[i] > 0:
			break
	pos = 0
	for j in range(i, n):
		if a[j] < 0:
			pos -= min(pos, abs(a[j]))
		else:
			pos += a[j]
	print(pos)


def array_cancellation(size, arr):
	num = 0
	minimum = 0
	for i in range(size):
		num += arr[i]
		if num < minimum:
			minimum = num
	return -1 * minimum
n = int(input())
for i in range(n):
	size = int(input())
	arr = list(map(int, input().split()))
	result = array_cancellation(size, arr)
	print(result)


import math
from decimal import *
import random
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	tot = 0
	ans = 0
	for i in range(n):
		if arr[i] > 0:
			tot += arr[i]
		else:
			mn = min(tot, abs(arr[i]))
			tot -= mn
			arr[i] += mn
			ans += abs(arr[i])
	print(ans)


def solve(A, n):
	sum = 0
	res = 0
	for a in A:
		if sum + a < 0:
			res += -(sum + a)
			sum = 0
		else:
			sum += a
	return res
t = int(input())
while t:
	t -= 1
	n = int(input())
	A = [i for i in map(int, input().strip().split())]
	print(solve(A, n))


for _ in range(int(input())):
	n = int(input())
	al = list(map(int, input().split()))
	ans = 0
	l = 1
	for i in range(n - 1):
		if al[i] > 0:
			al[i + 1] += al[i]
			al[i] = 0
	print(al[-1])


from collections import deque

def find_right_positive(num_deque, prev_ind):
	ind = prev_ind
	while num_deque[ind] <= 0 and ind >= 0:
		ind += -1
	return ind

def find_left_negative(num_deque, prev_ind):
	ind = prev_ind
	while num_deque[ind] >= 0 and ind < len(num_deque):
		ind += 1
	return ind

def count_coin(num_deque, coins):
	head_ind = 0
	right_neg_ind = 0
	right_pos_ind = len(num_deque) - 1
	while head_ind < len(num_deque):
		while head_ind < len(num_deque) and num_deque[head_ind] == 0:
			head_ind += 1
		if head_ind < len(num_deque) and num_deque[head_ind] < 0:
			while head_ind < len(num_deque) and num_deque[head_ind] < 0:
				right_pos_ind = find_right_positive(num_deque, right_pos_ind)
				if num_deque[right_pos_ind] < abs(num_deque[head_ind]):
					num_deque[head_ind] = num_deque[head_ind] + num_deque[right_pos_ind]
					coins += num_deque[right_pos_ind]
					num_deque[right_pos_ind] = 0
				else:
					num_deque[right_pos_ind] += num_deque[head_ind]
					coins += abs(num_deque[head_ind])
					num_deque[head_ind] = 0
					head_ind += 1
		elif head_ind < len(num_deque) and num_deque[head_ind] > 0:
			while head_ind < len(num_deque) and num_deque[head_ind] > 0:
				right_neg_ind = find_left_negative(num_deque, right_neg_ind)
				if abs(num_deque[right_neg_ind]) < num_deque[head_ind]:
					num_deque[head_ind] += num_deque[right_neg_ind]
					num_deque[right_neg_ind] = 0
				else:
					num_deque[right_neg_ind] += num_deque[head_ind]
					num_deque[head_ind] = 0
					head_ind += 1
	return coins
coins = 0
t = int(input())
for i in range(t):
	n = int(input())
	num_deque = list(map(int, input().split()))
	total_coins = count_coin(num_deque, coins)
	print(total_coins)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	ans = 0
	for x in a:
		ans = max(0, ans + x)
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	ar = list(map(int, input().split()))
	s = 0
	m = 0
	for i in range(n):
		s += ar[i]
		if m > s:
			m = s
	print(abs(m))
	t -= 1


import math, queue, heapq
import sys
sys.setrecursionlimit(10 ** 6)
fastinput = sys.stdin.readline
fastout = sys.stdout.write
t = int(fastinput())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, fastinput().split()))
	used = 0
	possum = 0
	for i in a:
		if i > 0:
			possum += i
	ans = 0
	for i in a[::-1]:
		if i >= 0:
			possum -= i
			used -= i
			if used < 0:
				used = 0
		elif abs(i) > possum - used:
			ans += abs(i) - (possum - used)
			used += possum - used
		else:
			used += abs(i)
	print(ans)


from sys import stdin
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	left = 0
	a = a[::-1]
	for i in a:
		if i < 0:
			left += i
		elif left != 0:
			left += min(i, abs(left))
	print(abs(left))


def solve(a, l):
	(p, n) = (0, 0)
	for i in range(l - 1, -1, -1):
		if a[i] >= 0:
			if n == 0:
				p += a[i]
			elif a[i] >= n:
				p += a[i] - n
				n = 0
			else:
				n -= a[i]
		else:
			n += -1 * a[i]
	if n == 0 and p == 0:
		return 0
	else:
		return p
for _ in range(int(input())):
	l = int(input())
	a = list(map(int, input().split()))
	print(solve(a, l))


import math
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	free = 0
	carry = 0
	for num in a:
		if num > 0:
			carry += num
		else:
			free += 2 * min(carry, abs(num))
			carry -= min(carry, abs(num))
	tot_sum = sum((abs(num) for num in a))
	cost = int((tot_sum - free) / 2)
	print(cost)
	t -= 1


n = int(input())
for i in range(n):
	t = int(input())
	arr = [int(i) for i in input().split()]
	total = 0
	for i in range(len(arr)):
		if arr[i] > 0:
			total += arr[i]
		else:
			b = min(total, -arr[i])
			total -= b
			arr[i] += b
	total = 0
	for i in range(len(arr)):
		if arr[i] < 0:
			total += abs(arr[i])
	print(total)


from sys import stdin, stdout, setrecursionlimit
import heapq
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	arr = iia()
	res = 0
	hold = 0
	for i in range(len(arr)):
		if arr[i] > 0:
			hold += arr[i]
		else:
			cur = abs(arr[i])
			if cur >= hold:
				cur -= hold
				hold = 0
				res += cur
			else:
				hold -= cur
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	positive = 0
	ind = 0
	while ind < len(a):
		if a[ind] >= 0:
			positive += a[ind]
			a[ind] = 0
		elif positive >= abs(a[ind]):
			positive += a[ind]
			a[ind] = 0
		else:
			a[ind] += positive
			positive = 0
		ind += 1
	negative = 0
	for el in a:
		if el <= 0:
			negative += el
		else:
			break
	print(abs(negative))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lis = []
	ind = n - 1
	for j in range(n):
		if arr[j] > 0:
			lis.append(arr[j])
			ind = j
			break
	for j in range(ind + 1, n):
		if arr[j] < 0:
			temp = min(lis[-1], abs(arr[j]))
			lis[-1] -= temp
			arr[j] += temp
		else:
			lis[-1] += arr[j]
	s = 0
	for j in range(n):
		if arr[j] < 0:
			s += abs(arr[j])
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ps = 0
	for i in range(n):
		if a[i] > 0:
			ps += a[i]
		elif a[i] < 0:
			ps = max(0, ps + a[i])
		else:
			continue
	print(ps)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
from bisect import bisect_left
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	j = n - 1
	count = 0
	x = 0
	y = 0
	for i in range(n - 1, -1, -1):
		if l[i] != 0:
			if l[i] > 0:
				if l[i] <= abs(y):
					y += l[i]
					l[i] = 0
				else:
					l[i] -= abs(y)
					y = 0
			else:
				y += l[i]
	for i in l:
		if i > 0:
			x += i
	count = x
	print(count)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	for i in range(n):
		if res + a[i] >= 0:
			res += a[i]
		else:
			res = 0
	print(res)
ct = int(input())
while ct > 0:
	solve()
	ct -= 1


n = int(input())
for i in range(n):
	m = int(input())
	a = list(map(int, input().split(' ')))
	sum = 0
	for j in range(len(a)):
		if a[j] > 0:
			sum += a[j]
		elif sum > 0:
			if sum > -a[j]:
				sum = sum + a[j]
				a[j] = 0
			else:
				a[j] = sum + a[j]
				sum = 0
	print(sum)


from sys import stdin
import math
from collections import deque
from bisect import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	(n,) = gil()
	a = gil()
	(e, c) = (0, 0)
	for v in reversed(a):
		c += v
		if c >= 0:
			e += c
			c = 0
	print(e)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	coins = 0
	for j in range(n):
		coins = max(0, coins + arr[j])
	print(coins)


import sys
import math
from math import *
from collections import Counter, defaultdict, deque
lip = lambda : list(map(int, input().split()))
ip = lambda : int(input())
sip = lambda : input().split()

def main():
	n = ip()
	arr = lip()
	neg = 0
	for i in range(n - 1, -1, -1):
		if arr[i] < 0:
			neg += arr[i]
		else:
			neg = min(neg + arr[i], 0)
	print(-neg)
for i in range(ip()):
	main()


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input().split()
	for i in range(0, n):
		a[i] = int(a[i])
	count = 0
	for i in a:
		if i >= 0:
			count = count + i
		elif abs(i) >= count:
			count = 0
		else:
			count = count - abs(i)
	print(count)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cs = 0
	pay = 0
	for i in a:
		cs += i
		if cs < 0:
			pay += abs(cs)
			cs = 0
	print(pay)


t = int(input())

def ret(l):
	for i in range(len(l) - 1):
		if l[i] > 0:
			l[i + 1] += l[i]
			l[i] = 0
	return l[-1]
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(ret(arr))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	ans = 0
	for i in range(n):
		if l[i] >= 0:
			c += l[i]
		elif c + l[i] >= 0:
			c += l[i]
		else:
			ans += abs(c + l[i])
			c = 0
	print(ans)


import itertools
for _ in range(int(input())):
	n = int(input())
	print(-min(itertools.accumulate(map(int, input().split()))))


t = input()
t = int(t)

def solve(n, arr):
	min_sum = 0
	cur_sum = 0
	for x in arr:
		cur_sum += x
		min_sum = min(min_sum, cur_sum)
	print(-min_sum)
for i in range(t):
	n = input()
	n = int(n)
	arr = list(map(int, input().split()))
	solve(n, arr)


import math
tst = int(input())
while tst > 0:
	tst = tst - 1
	n = int(input())
	arr = list(map(int, input().strip().split()))
	s = 0
	for i in range(n - 1, -1, -1):
		if arr[i] < 0:
			s += abs(arr[i])
		elif s > arr[i]:
			s -= arr[i]
		else:
			arr[i] -= s
			s = 0
	print(s)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	(s1, s2) = (0, 0)
	for i in arr:
		if i < 0:
			temp = max(-s2, i)
			s2 += temp
			s1 += i - temp
		else:
			s2 += i
	out(s2)


t = int(input())
for i in range(t):
	n = int(input())
	p = [int(i) for i in input().split()]
	cnt = 0
	count = 0
	k = True
	for i in p:
		if i > 0:
			count += i
	pos = 0
	for i in p:
		if i > 0:
			pos += i
			k = False
		elif k == False:
			ct = abs(i - 0)
			count -= min(pos, ct)
			pos -= min(pos, ct)
			if pos <= 0:
				k = True
	print(count)


def pro(arr):
	n = len(arr)
	suff = [0] * n
	suff[-1] = arr[-1]
	for i in range(n - 2, -1, -1):
		suff[i] = suff[i + 1] + arr[i]
	print(max(suff))
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pro(arr)


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
ans = 0

def pr_list(a):
	print(*a, sep=' ')

def swap(a, b):
	temp = a
	a = b
	b = temp
	return [a, b]

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		n = inp()
		a = inlt()
		i = 0
		j = 1
		while j < n:
			while j < n and a[j] >= 0:
				j = j + 1
			while i < j and a[i] <= 0:
				i = i + 1
			if i != j and j != n:
				if abs(a[j]) > abs(a[i]):
					a[j] = a[j] + a[i]
					a[i] = 0
					i = i + 1
					if i == j:
						j = j + 1
				else:
					a[i] = a[i] + a[j]
					a[j] = 0
					j = j + 1
			else:
				j = j + 1
		a.sort()
		i = 0
		j = n - 1
		ans = 0
		while i < j:
			if abs(a[i]) > abs(a[j]):
				ans = ans + abs(a[j])
				a[i] = a[i] + a[j]
				a[j] = 0
				j = j - 1
			else:
				ans = ans + abs(a[i])
				a[j] = a[j] + a[i]
				a[i] = 0
				i = i + 1
		print(ans)
main()


import sys

def ints():
	return list(map(int, sys.stdin.readline().strip().split()))
tc = int(input())
while tc:
	tc -= 1
	n = int(input())
	ls = ints()
	maxneg = 0
	temp = 0
	for item in ls:
		temp += item
		if temp < maxneg:
			maxneg = temp
	print(-1 * maxneg)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	a = inputlist()
	neg = True
	cur = 0
	for i in range(n):
		if neg and a[i] > 0:
			cur = a[i]
			neg = False
		elif not neg and a[i] > 0:
			cur += a[i]
		elif not neg and a[i] < 0:
			if a[i] + cur > 0:
				cur += a[i]
			else:
				neg = True
				cur = 0
	print(cur)


for i in range(int(input())):
	n = int(input())
	x = [int(num) for num in input().split()]
	tot = 0
	for i in range(n):
		if x[i] >= 0:
			tot = tot + x[i]
		else:
			tot = tot + x[i]
			tot = max(tot, 0)
	print(tot)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	need = 0
	for i in reversed(a):
		if i > 0 and need:
			need -= min(need, i)
		elif i < 0:
			need += -i
	print(need)
t = int(input())
for _ in range(t):
	main()


import sys
input = sys.stdin.buffer.readline

def solution():
	n = int(input())
	l = list(map(int, input().split()))
	ps = 0
	for i in range(n):
		if l[i] >= 0:
			ps += l[i]
		else:
			x = ps
			ps -= min(ps, abs(l[i]))
			l[i] += min(x, abs(l[i]))
	print(ps)
t = int(input())
for _ in range(t):
	solution()


n = int(input())
for i in range(n):
	k = int(input())
	s = list(input().split())
	bal = 0
	ans = 0
	for j in s:
		bal += int(j)
		if bal < 0:
			ans += -bal
			bal = 0
	print(ans)


from collections import deque, defaultdict, Counter
from sys import stdin, stdout

def inp():
	return int(stdin.readline())

def inlt():
	return list(map(int, stdin.readline().split()))

def insr():
	return list(stdin.readline().strip())

def invr():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def lds(arr, n):
	lds = [0] * n
	max = 0
	for i in range(n):
		lds[i] = 1
	for i in range(1, n):
		for j in range(i):
			if arr[i] < arr[j] and lds[i] < lds[j] + 1:
				lds[i] = lds[j] + 1
	for i in range(n):
		if max < lds[i]:
			max = lds[i]
	return max
import math

def fillPrefixSum(arr, n, prefixSum):
	prefixSum[0] = arr[0]
	for i in range(1, n):
		prefixSum[i] = prefixSum[i - 1] + arr[i]

def solve():
	for _ in range(inp()):
		n = inp()
		a = inlt()
		for i in range(n):
			a[i] = a[i] * -1
		prefixSum = [0 for i in range(n + 1)]
		fillPrefixSum(a, n, prefixSum)
		pr(max(prefixSum))
solve()


import sys
from functools import reduce
from collections import Counter
import time
import datetime
from math import sqrt, gcd

def ip():
	return int(sys.stdin.readline())

def sip():
	return sys.stdin.readline()

def mip():
	return map(int, sys.stdin.readline().split())

def mips():
	return map(str, sys.stdin.readline().split())

def lip():
	return list(map(int, sys.stdin.readline().split()))

def matip(n, m):
	lst = []
	for i in range(n):
		arr = lip()
		lst.append(arr)
	return lst

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def minJumps(arr, n):
	jumps = [0 for i in range(n)]
	if n == 0 or arr[0] == 0:
		return float('inf')
	jumps[0] = 0
	for i in range(1, n):
		jumps[i] = float('inf')
		for j in range(i):
			if i <= j + arr[j] and jumps[j] != float('inf'):
				jumps[i] = min(jumps[i], jumps[j] + 1)
				break
	return jumps[n - 1]

def dic(arr):
	return Counter(arr)

def check_prime(n):
	if n < 2:
		return False
	for i in range(2, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True
t = ip()
while t:
	t -= 1
	n = ip()
	arr = lip()
	val = 0
	for i in range(n):
		if arr[i] > 0:
			val += arr[i]
		else:
			val -= min(val, abs(arr[i]))
	print(val)


from sys import stdin
from math import ceil
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	cumm = 0
	ans = 0
	for i in range(n):
		cumm += a[i]
		if cumm < 0:
			ans = min(ans, cumm)
	print(-1 * ans)


for z in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pos = 0
	neg = 0
	ans = 0
	for i in a:
		if i > 0:
			pos += i
		elif i < 0:
			if pos >= abs(i):
				pos -= abs(i)
			else:
				ans += abs(i) - pos
				pos = 0
	print(ans)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return list(map(int, sys.stdin.readline().split()))

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	a = LI()
	d = [0] * n
	s = 0
	for i in range(n):
		s += a[i]
		d[i] = s
	ans = min(d)
	if ans > 0:
		ans = 0
	print(-ans)


t = int(input())
for _ in range(t):
	n = int(input())
	aa = list(map(int, input().split()))
	cost = 0
	pi = 0
	ni = 0
	while pi < n and ni < n:
		if aa[pi] <= 0:
			cost -= aa[pi]
			pi += 1
			continue
		if ni <= pi or aa[ni] >= 0:
			ni += 1
			continue
		d = min(aa[pi], 0 - aa[ni])
		aa[pi] -= d
		aa[ni] += d
	while ni < n:
		if aa[ni] < 0:
			cost -= aa[ni]
		ni += 1
	print(cost)


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	partionSum = []
	res = 0
	for i in range(n):
		if l[i] <= 0:
			res = res + l[i]
		if l[i] > 0:
			if res <= 0:
				partionSum.append(res)
				res = l[i]
			else:
				res += l[i]
	print(abs(sum(partionSum)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	if len(a) == 1:
		print(0)
	else:
		answer = 0
		pos_sum = 0
		neg_sum = 0
		flag = 0
		for i in range(len(a)):
			if a[i] < 0 and flag == 0:
				continue
			if a[i] > 0 and flag == 0:
				pos_sum += a[i]
				flag = 1
				continue
			if a[i] > 0 and flag == 1:
				pos_sum += a[i]
				continue
			if a[i] < 0 and flag == 1:
				neg_sum += abs(a[i])
				flag = -1
				continue
			if a[i] < 0 and flag == -1:
				neg_sum += abs(a[i])
				continue
			if a[i] > 0 and flag == -1:
				if pos_sum < neg_sum:
					pos_sum = a[i]
				else:
					pos_sum = a[i] + pos_sum - neg_sum
				flag = 1
				neg_sum = 0
		print(pos_sum - neg_sum)


def ans(arr):
	total = 0
	neg = 0
	for i in arr:
		if i > 0:
			total += i
		elif i < 0:
			mini = min(total, abs(i))
			total -= mini
			i += mini
			neg += i
	return abs(neg)
T = int(input())
for i in range(T):
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	print(ans(arr))


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		ans = 0
		(l, r) = (0, 0)
		s = 0
		for x in a:
			if x > 0:
				if s <= 0:
					ans += abs(s)
					s = 0
			s += x
		print(ans)
main()


from bisect import *
from collections import *
from decimal import *

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	n = inp()
	a = lis()
	pos = 0
	neg = 0
	cos = 0
	for i in range(n):
		if a[i] > 0:
			pos += a[i]
		elif a[i] < 0:
			neg += -a[i]
			if pos >= neg:
				pos -= neg
				neg = 0
			else:
				cos += abs(pos - neg)
				pos = 0
				neg = 0
	print(cos)


TC = int(input())
for tc in range(TC):
	N = int(input())
	A = list(map(int, input().split()))
	result = 0
	s_p = 0
	s_m = 0
	for (i, a) in enumerate(A):
		if a < 0:
			if s_p > -a:
				s_p += a
			else:
				result += -a - s_p
				s_m -= -a - s_p
				s_p = 0
		else:
			s_p += a
	print(result)


import sys
input = sys.stdin.readline
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(l, r) = (0, 0)
	while l < n and r < n:
		if a[l] > 0:
			r = max(l + 1, r)
			while r < n and a[r] >= 0:
				r += 1
			if r < n:
				d = min(a[l], abs(a[r]))
				a[l] -= d
				a[r] += d
		else:
			l += 1
	ans = 0
	for i in a:
		if i > 0:
			ans += i
	print(ans)


t = int(input())
for _ in range(t):
	N = int(input())
	a = list(map(int, input().split()))
	p = 0
	n = 0
	for i in range(N):
		if i == 0:
			if a[i] < 0:
				n = n + abs(a[i])
			else:
				p = p + a[i]
		elif a[i] >= 0:
			p = p + a[i]
		elif p >= abs(a[i]):
			p = p - abs(a[i])
		else:
			n = n + abs(abs(a[i]) - p)
			p = 0
	print(abs(n))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	p = 0
	for x in a[::-1]:
		if x > 0:
			z = min(x, p)
			x -= z
			p -= z
			if x > 0:
				s += x
		else:
			p -= x
	print(s)


import math
from collections import deque
from collections import defaultdict
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().strip().split(' ')))
	sarr = deque()
	sm = 0
	for i in range(n):
		if arr[i] > 0:
			sarr.append(i)
		elif arr[i] < 0:
			if len(sarr) > 0:
				while sarr and arr[i] < 0:
					g = sarr.popleft()
					if arr[g] > abs(arr[i]):
						arr[g] += arr[i]
						arr[i] = 0
						sarr.append(g)
					else:
						arr[i] += arr[g]
						arr[g] = 0
	for i in arr:
		sm += abs(i)
	print(sm // 2)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = len(a) - 1
	j = i
	while j > 0 and i >= 0:
		if a[j] >= 0:
			j -= 1
			if i >= j:
				i = j - 1
		else:
			while i >= 0 and a[i] <= 0:
				i -= 1
			if i >= 0:
				m = min(a[i], -a[j])
				a[i] -= m
				a[j] += m
	print(sum((i for i in a if i > 0)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = [0] * n
	p[0] = a[0]
	for i in range(1, n):
		p[i] = a[i] + p[i - 1]
	ans = 0
	for i in range(n):
		if a[i] > 0 and p[i] < 0 or (a[i] < 0 and p[i] < 0):
			ans = max(ans, abs(p[i]))
	print(ans)


my_mass = list()
size_1 = 0
size_2 = 0
a = list()
b = list()
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	min = 10000000
	sum = 0
	for i in a:
		sum += i
		if sum < min:
			min = sum
	print(abs(min))


import os
import sys
from io import BytesIO, IOBase
from collections import deque

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		nums = list(map(int, input().split()))
		(pos, neg) = ([], deque([]))
		ans = 0
		for (ind, elem) in enumerate(nums):
			if elem > 0:
				pos.append((ind, elem))
			if elem < 0:
				neg.append((ind, elem))
		totlen = len(pos) + len(neg)
		while totlen:
			if pos[-1][0] < neg[-1][0]:
				num = neg.pop()[1]
			else:
				num = pos.pop()[1]
			if num > 0:
				while num:
					(ind, ln) = neg.popleft()
					if abs(ln) < num:
						num += ln
						ans += abs(ln)
					else:
						ans += num
						rem = ln + num
						if rem != 0:
							neg.appendleft((ind, rem))
						break
			else:
				while num:
					(ind, lp) = pos.pop()
					if abs(num) <= lp:
						rem = lp + num
						if rem != 0:
							pos.append((ind, rem))
						break
					else:
						num += lp
			totlen = len(pos) + len(neg)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(n, a) = (int(input()), list(map(int, input().split())))
	r = 0
	for v in a:
		r += v
		r = max(r, 0)
	print(r)


import math

def dtb(n):
	return bin(n).replace('0b', '')

def btd(n):
	return int(n, 2)
t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	i = 0
	j = 0
	ans = 0
	while i < n:
		if a[i] <= 0:
			i += 1
		else:
			if a[i] > 0:
				j = i + 1
				ans = a[i]
				x = a[i]
				while j < n:
					if ans > 0 and a[j] > 0:
						ans += a[j]
					elif ans > 0 and a[j] < 0:
						ans += a[j]
					elif ans <= 0:
						break
					j += 1
			i = j
	print(ans)


t = int(input())
for it in range(t):
	n = int(input())
	x = input()
	alist = [int(i) for i in x.split()]
	cost = 0
	sum = 0
	for i in range(n):
		if alist[i] >= 0:
			sum += alist[i]
		elif sum + alist[i] > 0:
			sum += alist[i]
			alist[i] = 0
		else:
			alist[i] += sum
			sum = 0
	for i in range(n):
		if alist[i] < 0:
			cost += -1 * alist[i]
	print(cost)


tests = int(input())
for t in range(tests):
	n = int(input())
	ls = list(map(int, input().split()))
	curr = 0
	res = 0
	for item in ls:
		curr += item
		if curr < res:
			res = curr
	print(-res)


t = int(input())
for t in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	neg = 0
	pos = 0
	temp = 0
	for i in range(n):
		if a[i] < 0 and temp == 0:
			neg += abs(a[i])
		elif a[i] > 0:
			temp += a[i]
		elif a[i] < 0 and temp > 0:
			if abs(a[i]) > temp:
				neg += abs(a[i]) - temp
				temp = 0
			else:
				temp -= abs(a[i])
	print(abs(neg))


def solve(n, arr):
	maxNeg = 0
	currSum = 0
	for i in arr:
		currSum += i
		if currSum < maxNeg:
			maxNeg = currSum
	return abs(maxNeg)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = [int(x) for x in input().split()]
		assert n == len(arr)
		answer = solve(n, arr)
		print(answer)
main()


t = lambda : range(int(input()))
A = lambda : list(map(int, input().split()))
for _ in t():
	n = int(input())
	a = A()
	if n == 1:
		print(0)
		continue
	tmp = 0
	pt = 0
	ng = 0
	st = False
	for i in range(n):
		if a[i] > 0:
			pt += a[i]
			st = True
		elif st:
			x = pt + a[i]
			if x > 0:
				pt = x
			else:
				ng += x
				pt = 0
				st = False
		else:
			ng += a[i]
	print(pt)


t = int(input())
for i in range(t):
	n = int(input())
	mystr = input()
	mystr = mystr.split()
	pos = 0
	ans = 0
	for j in range(n):
		v = int(mystr[j])
		if v > 0:
			ans = ans + v
			pos = pos + v
		elif v < 0:
			vabs = abs(v)
			if pos - vabs >= 0:
				pos = pos - vabs
				ans = ans - vabs
			else:
				ans = ans + vabs - pos - pos
				pos = 0
	print(int(ans / 2))


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	coin = 0
	i = 0
	while a[i] < 0:
		coin += abs(a[i])
		i += 1
	su = a[i]
	i += 1
	while i < n:
		su += a[i]
		if su < 0:
			coin += abs(su)
			su = 0
		i += 1
	print(coin)


for _ in range(int(input())):
	n = int(input())
	c = list(map(int, input().split()))
	s = 0
	for i in range(n):
		if c[i] > 0:
			s += c[i]
		else:
			mn = min(s, -c[i])
			c[i] += mn
			s -= mn
	print(sum((-min(i, 0) for i in c)))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cum = [0 for _ in range(n + 1)]
	for j in range(n):
		cum[j + 1] = cum[j] + a[j]
	print(min(cum) * -1)


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = [int(i) for i in input().split()]
	po = []
	ne = []
	for i in range(n):
		if l1[i] < 0:
			ne.append(i)
		else:
			po.append(i)
	i = 0
	j = 0
	while j < len(ne) and i < len(po):
		if po[i] > ne[j]:
			j += 1
		else:
			x = l1[po[i]]
			y = -1 * l1[ne[j]]
			m = min(x, y)
			l1[po[i]] -= m
			l1[ne[j]] += m
			if l1[po[i]] == 0:
				i += 1
			else:
				j += 1
	c = 0
	for i in l1:
		if i > 0:
			c += i
	print(c)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	sp = 0
	for each in x:
		if each > 0:
			sp += each
		elif each < 0 and sp > 0:
			if sp > abs(each):
				sp += each
				each = 0
			else:
				each += sp
				sp = 0
	print(sp)


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import *
getcontext().prec = 30
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	s = 0
	b = 0
	for i in range(n - 1, -1, -1):
		s += l[i]
		b = max(s, b)
	print(b)


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list(map(int, input().strip().split()))
	sum = 0
	balance = 0
	for i in range(n):
		if list1[i] >= 0:
			balance += list1[i]
		elif abs(list1[i]) <= balance:
			balance -= abs(list1[i])
		else:
			sum += abs(list1[i]) - balance
			balance = 0
	print(sum)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	res = 0
	for i in li:
		res = max(res + i, 0)
	print(res)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		ans = max(0, ans + x[i])
	print(ans)


for p in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(1, n):
		a[i] += a[i - 1]
	print(abs(min(a)))


import sys, io, os, time
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	pos = 0
	neg = 0
	for i in range(n):
		if arr[i] < 0:
			check = abs(arr[i])
			if check > pos:
				check -= pos
				pos = 0
				ans += check
			else:
				pos -= check
		else:
			pos += arr[i]
	print((ans + pos) // 2)


t = int(input())
for i in range(t):
	n = int(input())
	c = []
	a = list(map(int, input().split()))
	x = sum(a)
	c.append(x)
	for i in range(1, n):
		x -= a[i - 1]
		c.append(x)
	print(max(c))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	r = 0
	avl = 0
	for i in a:
		if i > 0:
			avl += i
		else:
			i = abs(i)
			d = min(avl, i)
			avl -= d
			r += i - d
	print(r)


for _ in ' ' * int(input()):
	(n, a, s, res) = (int(input()), list(map(int, input().split())), 0, 0)
	for i in a:
		s += i
		res = min(res, s)
	print(-res)


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(i) for i in input().split()]
	ans = 0
	for i in range(n):
		if arr[i] > 0:
			ans += arr[i]
			arr[i] = 0
		else:
			arr[i] = ans + arr[i]
			if arr[i] > 0:
				ans = arr[i]
				arr[i] = 0
			else:
				ans = 0
	for i in range(n):
		ans += abs(arr[i])
	print(ans // 2 + ans % 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = 0
	for i in range(n):
		if a[i] > 0:
			cnt += a[i]
		else:
			cnt -= min(cnt, -a[i])
	print(cnt)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	pos = neg = 0
	for x in range(n):
		if a[x] > 0:
			pos += a[x]
		if a[x] < 0:
			neg = neg - a[x]
			if pos > 0:
				m = min(pos, -a[x])
				pos -= m
				neg -= m
	print((pos + neg) // 2)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	count = 0
	for i in range(n):
		if a[i] > 0:
			count += a[i]
		elif abs(count) >= abs(a[i]):
			count -= abs(a[i])
		else:
			ans += abs(a[i]) - count
			count = 0
	print(ans)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
import io
from sys import stdin, stdout
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = lambda : sys.stdin.readline().rstrip('\r\n')

def gdc(a, b):
	if a % b == 0:
		return b
	else:
		return gdc(b, a % b)
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	(j, i) = (n - 1, n - 1)
	while True:
		while ar[j] >= 0 and j > 0:
			j -= 1
			if i >= j:
				i -= 1
		while ar[i] <= 0 and i >= 0:
			i -= 1
		if i < 0 or j == 0:
			break
		if abs(ar[j]) <= ar[i]:
			ar[i] -= abs(ar[j])
			ar[j] = 0
			j -= 1
		else:
			ar[j] += ar[i]
			ar[i] = 0
			i -= 1
	answer = 0
	for i in range(n):
		if ar[i] > 0:
			answer += ar[i]
	print(answer)
sys.exit(0)
n = int(input())
lis = list(map(int, input().split()))
answer = 0
for i in range(n):
	answer += lis[i] - 1
if n == 1:
	print(n - 1)
elif n > 32:
	print(answer)
	sys.exit(0)
lis.sort()
for i in range(2, 50000):
	localMin = 0
	for j in range(n):
		needVal = i ** j
		localMin += abs(lis[j] - needVal)
		if localMin > answer:
			break
	else:
		answer = min(localMin, answer)
print(answer)
sys.exit(0)

class Person:

	def __init__(self, name, age):
		self.name = name
		self.age = age
p1 = Person('heelo', 27)
print(help(Person))
age = 26
name = 'Swaroop'
print('Возрас {} -- {} лет'.format(name, age))
print(help(object))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	p = 0
	for j in range(n - 1, -1, -1):
		if l[j] > 0:
			if l[j] > p:
				c += l[j] - p
				p = 0
			else:
				p -= l[j]
		else:
			p += abs(l[j])
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n - 1):
		if arr[i] > 0:
			arr[i + 1] = arr[i] + arr[i + 1]
			arr[i] = 0
	print(arr[-1])


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	cost = 0
	buff = 0
	for i in l:
		if i < 0:
			i = -i
			temp = min(buff, i)
			buff -= temp
			i -= temp
			cost += i
		else:
			buff += i
	print(cost)


t = int(input())
while t != 0:
	n = int(input())
	a = list(map(int, input().split()))[:n]
	cost = 0
	for i in a:
		cost += i
		if cost < 0:
			cost = 0
	print(cost)
	t -= 1


t = int(input())
for test in range(t):
	n = int(input())
	arr1 = [int(i) for i in input().split(' ')]
	arr2 = [0 for i in range(n)]
	count = 0
	for j in range(n):
		count = count + arr1[j]
		arr2[j] = count
	res_str = abs(min(arr2))
	print(res_str)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for i in arr:
		ans += i
		if ans < 0:
			ans = 0
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().strip().split()))
	a = 0
	for i in l:
		if i < 0 and a != 0:
			q = abs(i)
			if a >= q:
				a -= q
			else:
				a = 0
		elif i < 0:
			continue
		else:
			a += i
	print(a)


T = int(input())
ans = [0] * T
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	cost = 0
	yoyuu = 0
	for i in range(N):
		A[i] += yoyuu
		if A[i] < 0:
			cost -= A[i]
			yoyuu = 0
		else:
			yoyuu = A[i]
	ans[t] = cost
print(*ans, sep='\n')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	down = 0
	up = 0
	for i in arr:
		up = up + i
		down = min(down, down + up)
		up = max(0, up)
	print(0 - down)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	neg = []
	pos = []
	for i in range(n):
		if arr[i] < 0:
			while len(pos) != 0 and arr[i] < 0:
				k = pos.pop()
				arr[i] += k
			if arr[i] < 0:
				neg.append(arr[i])
			elif arr[i] != 0:
				pos.append(arr[i])
		else:
			pos.append(arr[i])
	print(-1 * sum(neg))


import sys
from itertools import accumulate
input = lambda : sys.stdin.readline().rstrip('\r\n')

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = a[:]
	a = list(accumulate(a))
	b = list(accumulate(b[::-1]))[::-1]
	print(max(b))
for _ in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	stack = []
	new = []
	ans = 0
	s = 0
	for j in l:
		if j < 0:
			stack.append(s)
			new.append(j)
			s = 0
		else:
			s += j
	if len(new) == 0:
		print(0)
		continue
	for i in range(len(new)):
		if stack[i] >= abs(new[i]):
			if i + 1 < len(new):
				stack[i + 1] += new[i] + stack[i]
		else:
			ans += -(new[i] + stack[i])
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().strip().split(' ')))
	j = 0
	k = j + 1
	while j < n and k < n:
		if lst[j] > 0:
			if lst[k] < 0:
				if abs(lst[k]) < lst[j]:
					lst[j] += lst[k]
					lst[k] = 0
					k += 1
				else:
					lst[k] += lst[j]
					lst[j] = 0
					j = k + 1
					k = j + 1
			else:
				lst[j] += lst[k]
				lst[k] = 0
				k += 1
		else:
			j += 1
			k += 1
	lst1 = list(filter(lambda x: x > 0, lst))
	s = sum(lst1)
	print(s)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	ans = 0
	val = 0
	for i in range(n):
		val += arr[i]
		ans = min(ans, val)
	print(-ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s1 = 0
	s2 = 0
	for i in a:
		if i > 0:
			s1 += i
		elif i < 0:
			if s1 >= -1 * i:
				s1 -= -1 * i
			else:
				s2 += s1 - -1 * i
				s1 = 0
	ans = -1 * s2
	print(ans)


import math
from decimal import Decimal
import heapq
import copy
import heapq
from collections import deque
from collections import defaultdict
MOD = 1000000007

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def da():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	return (n, m, a)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(n):
	tc = []
	ans = {}
	d = 2
	while d * d <= n:
		if n % d == 0:
			tc.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		tc.append(n)
	for i in tc:
		ans[i] = ans.get(i, 0) + 1
	return ans

def to_zero(n, s):
	lst = s[0]
	ans = []
	for i in range(n):
		if lst == s[i]:
			continue
		elif lst == '0':
			lst = '1'
			ans.append(i)
		else:
			lst = '0'
			ans.append(i)
	if lst == '1':
		ans.append(n)
	return ans

def divs(n):
	ans = 0
	while n:
		ans += n % 10
		n = n // 10
	return ans
for _ in range(int(input())):
	(n, a) = na()
	tec = 0
	for i in a:
		tec = max(0, tec + i)
	print(tec)


from itertools import accumulate
I = input
for _ in range(int(I())):
	n = int(I())
	lst = list(map(int, I().split()))
	presum = list(accumulate(lst))
	print(-min(presum))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sumv = 0
	for i in l:
		sumv = max(0, sumv + i)
	print(sumv)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(m, p, ans) = (0, 0, 0)
	for i in range(n - 1, -1, -1):
		if l[i] > 0:
			if m > 0:
				x = min(l[i], m)
				l[i] -= x
				m -= x
			ans += l[i]
		elif l[i] < 0:
			m += abs(l[i])
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	min_sum = 0
	act_sum = 0
	for i in range(n):
		act_sum += a[i]
		min_sum = min([min_sum, act_sum])
	print(-min_sum)


from sys import stdin
input = lambda : stdin.readline().strip()
import math
from collections import deque as d
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	ans = 0
	for i in a:
		c += i
		ans = min(ans, c)
	print(-ans)


for t in range(int(input())):
	n = int(input())
	a = [int(s) for s in input().split()]
	count = 0
	coins = 0
	for i in range(n - 1, -1, -1):
		if a[i] > 0:
			if a[i] <= count:
				count -= a[i]
			else:
				coins += a[i] - count
				count = 0
		else:
			count -= a[i]
	print(coins)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	i = 0
	sm = 0
	mn = 0
	while i < n:
		sm += a[i]
		if sm < 0:
			mn = min(sm, mn)
		i += 1
	print(-1 * mn)


MOD = 1000000007
from collections import defaultdict as dd, Counter, deque

def si():
	return input()

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def mi():
	return map(int, input().split())

def out(v):
	print(v)

def spout():
	print(v, end=' ')

def d2b(n):
	return bin(n).replace('0b', '')

def twod(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def vow():
	return ['a', 'e', 'i', 'o', 'u']

def let():
	return [chr(i) for i in range(97, 123)]

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ispow2(x):
	return x and (not x & x - 1)

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)
t = ii()
while t:
	t -= 1
	n = ii()
	a = li()
	s = m = 0
	for i in range(n):
		s += a[i]
		m = min(m, s)
	print(-m)


import math
test = int(input())
for t in range(test):
	n = int(input())
	A = list(map(int, input().split()))
	if n == 2:
		if A[0] >= A[1]:
			print(0)
			continue
	sump = 0
	sumn = 0
	B = []
	for i in range(n):
		if A[i] > 0:
			sump += A[i]
			if sumn != 0:
				B.append(sumn)
			sumn = 0
		else:
			sumn += A[i]
			if sump != 0:
				B.append(sump)
			sump = 0
	if sumn != 0:
		B.append(sumn)
	if sump != 0:
		B.append(sump)
	for i in range(0, len(B) - 1):
		if B[i] < 0:
			continue
		else:
			a = min(B[i], abs(B[i + 1]))
			B[i + 1] += a
			B[i] -= a
			if i + 2 != len(B):
				B[i + 2] += B[i]
				B[i] = 0
	ans = 0
	for i in B:
		if i > 0:
			ans += i
	print(abs(ans))


import math
import sys
from sys import stdin, stdout
sys.setrecursionlimit(100000)
MOD = 10 ** 9 + 7
int_input = lambda : int(stdin.readline())
list_input = lambda : list(map(int, stdin.readline().split()))
sum_of_digit = lambda n: sum(map(int, list(str(n))))

def factors(n):
	fact = [1, n]
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			fact.append(i)
			if n // i != i:
				fact.append(n // i)
	return fact

def sieve(n):
	primes = [True] * (n + 1)
	(primes[0], primes[1]) = (False, False)
	for i in range(2, int(math.sqrt(n)) + 1):
		if primes[i]:
			for j in range(i * i, n + 1, i):
				primes[j] = False
	return primes
for _ in range(int_input()):
	n = int_input()
	l = list_input()
	count = 0
	ans = 0
	for i in range(n):
		if l[i] == 0:
			continue
		if l[i] > 0:
			count += l[i]
		else:
			if abs(l[i]) > count:
				ans += abs(count + l[i])
			count = max(count + l[i], 0)
	print(ans)


import math
import os
import random
import re
import string
from sys import *
from collections import *

def bs(arr, x, l, r):
	if l <= r:
		mid = (r + l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return bs(arr, x, l, mid - 1)
		else:
			return bs(arr, x, mid + 1, r)
	else:
		return -1

def solve():
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, stdin.readline().strip().split()))
		c = 0
		pos = 0
		for i in range(n):
			if l[i] < 0:
				a = l[i]
				l[i] = min(0, l[i] + pos)
				c += abs(l[i])
				pos -= l[i] - a
			else:
				pos += l[i]
		print(c)
solve()


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = get_int()
	ls = get_list()
	(ans, neg, pos) = (0, 0, 0)
	for val in ls:
		if val > 0:
			pos += val
		else:
			neg -= val
		if pos > neg:
			pos -= neg
			neg = 0
		else:
			neg -= pos
			ans += neg
			(neg, pos) = (0, 0)
	ans = max(ans, pos, neg)
	print(ans)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = [0] * n
	a[n - 1] = l[n - 1]
	for i in range(n - 2, -1, -1):
		l[i] += l[i + 1]
	print(max(l))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	pos = 0
	neg = 0
	for i in range(n):
		if a[i] < 0:
			if -1 * a[i] > pos:
				ans += -1 * a[i] - pos
				pos = 0
			else:
				pos += a[i]
		else:
			pos += a[i]
	print(ans)


def ii():
	return int(input())

def li():
	return [int(i) for i in input().split()]
for t in range(ii()):
	n = ii()
	a = li()
	a.reverse()
	store = 0
	for i in range(n):
		if a[i] < 0:
			store += -a[i]
		elif store >= a[i]:
			store -= a[i]
			a[i] = 0
		else:
			a[i] -= store
			store = 0
	ans = 0
	for i in a:
		if i > 0:
			ans += i
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	c = 0
	for i in a:
		if i >= 0:
			s += i
		elif s + i < 0:
			c += -(s + i)
			s = 0
		else:
			s += i
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = 0
	z = 0
	for i in range(n):
		z += a[i]
		ans = min(ans, z)
	print(-ans)


def solve(arr):
	i = 0
	j = 0
	while i < len(arr) and j < len(arr):
		while i < len(arr) and arr[i] <= 0:
			i += 1
		while j < len(arr) and arr[j] >= 0:
			j += 1
		if i >= len(arr) or j >= len(arr):
			break
		if i < j and arr[i] > 0 and (arr[j] < 0):
			diff = min(arr[i], -arr[j])
			arr[i] -= diff
			arr[j] += diff
		if i > j:
			j += 1
	return sum((a for a in arr if a > 0))
for _ in range(int(input())):
	n = int(input())
	(*arr,) = map(int, input().split())
	print(solve(arr))


for I in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	negsum = 0
	for num in a:
		if num < 0:
			negsum += num
	operation = []
	carry = 0
	for i in range(n):
		if a[i] > 0:
			operation.append((a[i], abs(negsum)))
			negsum = min(0, negsum + a[i])
			carry += a[i]
		elif abs(a[i]) >= carry:
			negsum += abs(abs(a[i]) - carry)
			carry = 0
		else:
			carry += a[i]
	result = 0
	for i in range(len(operation)):
		if operation[i][0] > operation[i][1]:
			result += operation[i][0] - operation[i][1]
	print(result)


for T in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	bal = 0
	ans = 0
	for i in l:
		if i >= 0:
			bal += i
		elif abs(i) > bal:
			ans += abs(i) - bal
			bal = 0
		else:
			bal += i
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	co = 0
	for i in l:
		if i < 0 and co != 0:
			k = abs(i)
			if co >= k:
				co -= k
			else:
				co = 0
		elif i < 0:
			continue
		else:
			co += i
	print(co)


t = int(input())
while t > 0:
	n = int(input())
	arr = [int(i) for i in input().split()]
	sum1 = 0
	ans = 0
	for i in arr:
		sum1 += i
		if sum1 < 0:
			ans += abs(sum1)
			sum1 = 0
	print(ans)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 2
	j = n - 1
	l = False
	r = False
	while i >= 0:
		if a[i] > 0 and i < j:
			l = True
		else:
			l = False
			i -= 1
		if a[j] < 0:
			r = True
		else:
			j -= 1
			r = False
		if l and r:
			v = min(a[i], -a[j])
			a[i] -= v
			a[j] += v
			l = r = False
	answer = sum((v for v in a if v > 0))
	print(answer)


t = int(input())
for i in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	c = 0
	s = 0
	for val in ar:
		if val < 0:
			s += val
			if s < 0:
				if abs(s) > c:
					c = abs(s)
		else:
			s += val
	print(c)


import sys

def input():
	return sys.stdin.readline()[:-1]

def getInt():
	return int(input())

def getIntIter():
	return list(map(int, input().split()))

def getIntList():
	return list(getIntIter())
for _ in range(getInt()):
	n = getInt()
	nums = getIntList()
	minSum = 0
	currSum = 0
	for num in nums:
		currSum += num
		minSum = min(currSum, minSum)
	print(abs(minSum))


n = int(input())
for i in range(n):
	m = int(input())
	list1 = list(map(int, input().split()))
	sum = 0
	min = list1[0]
	for i in range(m):
		sum = sum + list1[i]
		if sum < min:
			min = sum
	print(-min)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	cost = 0
	p = 0
	for i in range(n):
		p += l[i]
		if p < 0:
			cost += p
			p = 0
	print(abs(cost))


def read_int():
	n = int(input())
	return n

def read_ints():
	ints = [int(x) for x in input().split(' ')]
	return ints

def solve(n, a):
	rtotal = 0
	non_free = 0
	for ai in a:
		if rtotal > 0 and ai < 0:
			if rtotal >= abs(ai):
				rtotal += ai
			else:
				non_free = abs(ai + rtotal)
				rtotal += ai
			continue
		if rtotal >= 0 and ai > 0:
			rtotal += ai
			continue
		if rtotal < 0 and ai > 0:
			if abs(rtotal) >= ai:
				rtotal += ai
			else:
				non_free = abs(ai + rtotal)
				rtotal += ai
			continue
		if rtotal <= 0 and ai < 0:
			rtotal += ai
			non_free -= ai
			continue
	print('rtotal: {}, non_free: {}'.format(rtotal, non_free))
	return non_free

def solve2(n, a):
	dp = [[0 for x in range(n)] for y in range(2)]
	for i in range(n):
		if i == 0:
			if a[i] > 0:
				dp[0][0] = a[i]
			dp[1][0] = abs(a[i])
		if a[i] >= 0:
			dp[0][i] = dp[0][i - 1] + a[i]
			dp[1][i] = dp[1][i - 1] + a[i]
		else:
			dp[0][i] = dp[0][i - 1]
		if a[i] < 0:
			if dp[0][i] > 0:
				if dp[0][i] >= abs(a[i]):
					dp[0][i] += a[i]
					dp[1][i] = dp[1][i - 1] + a[i]
				else:
					dp[1][i] = dp[1][i - 1] - abs(a[i] + dp[0][i])
					dp[0][i] = 0
			else:
				dp[1][i] = dp[1][i - 1] + abs(a[i])
	return dp[0][-1]
t = read_int()
for case in range(t):
	n = read_int()
	a = read_ints()
	sol = solve2(n, a)
	print(sol)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	res = 0
	for i in range(n):
		if a[i] > 0:
			res += a[i]
			a[i] = 0
		else:
			k = a[i]
			a[i] += res
			a[i] = min(0, a[i])
			res += k
			res = max(0, res)
	ans = 0
	a.sort()
	for i in range(n):
		if a[i] < 0:
			ans += a[i]
	print(abs(ans))


t = int(input())
while t > 0:
	t -= 1
	curr = 0
	n = int(input())
	l = list(map(int, input().split()))
	for i in l:
		curr = max(0, curr + i)
	print(curr)


def ans(arr):
	temp = []
	summ = 0
	for i in range(n):
		summ -= arr[i]
		temp.append(summ)
	return max(temp)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	print(ans(a))


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	a = []
	sum = 0
	total = 0
	for i in lst:
		sum = sum + i
		if sum < 0:
			a.append(sum)
			sum = 0
		elif sum > 0:
			a.append(sum)
	for i in a:
		if i < 0:
			total = total + abs(i)
	print(total)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = s = 0
	for i in range(n):
		s += a[-1 - i]
		m = max(m, s)
	print(m)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	(cur, ans) = (0, 0)
	for i in a:
		if i < 0:
			if cur > abs(i):
				cur += i
			else:
				ans -= cur + i
				cur = 0
		else:
			cur += i
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = []
	neg = []
	for i in range(n):
		if arr[i] > 0:
			pos.append(i)
		elif arr[i] < 0:
			neg.append(i)
	coin = 0
	headn = 0
	headp = 0
	while headn < len(neg) and pos[0] > neg[headn]:
		coin -= arr[neg[headn]]
		headn += 1
	while headn < len(neg) and headp < len(pos):
		if pos[headp] > neg[headn]:
			while headn < len(neg) and pos[headp] > neg[headn]:
				coin -= arr[neg[headn]]
				headn += 1
		if headn >= len(neg):
			break
		if abs(arr[pos[headp]]) > abs(arr[neg[headn]]):
			arr[pos[headp]] -= abs(arr[neg[headn]])
			headn += 1
		elif abs(arr[pos[headp]]) < abs(arr[neg[headn]]):
			arr[neg[headn]] += abs(arr[pos[headp]])
			headp += 1
		else:
			arr[neg[headn]] = 0
			arr[pos[headp]] = 0
			headp += 1
			headn += 1
	print(coin)


inp = input()
cases = []
for i in range(int(inp)):
	curr = int(input())
	cases.append([int(x) for x in input().split()])
for case in cases:
	p = 0
	ans = 0
	for i in case:
		if i > 0:
			p += i
		else:
			ans += -min(p + i, 0)
			p = max(p + i, 0)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(ans, summ) = (0, 0)
	for i in range(n):
		summ += arr[i]
		ans = min(ans, summ)
	print(-ans)


for _ in range(int(input())):
	(n,) = map(int, input().split())
	l = list(map(int, input().split()))
	cur = 0
	for i in range(n):
		cur = max(0, l[i] + cur)
	print(cur)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	var = 0
	for i in range(n - 1, -1, -1):
		var += arr[i]
		if var > 0:
			ans = max(ans, var)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	neg = []
	for i in range(n - 1):
		if a[i] < 0:
			neg.append(a[i])
			continue
		else:
			a[i + 1] += a[i]
	print(abs(sum(neg)))


def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	for p in range(n + 1):
		if prime[p]:
			print(p)
nV = 4
INF = 999

def floyd_warshall(G):
	distance = list(map(lambda i: list(map(lambda j: j, i)), G))
	for k in range(nV):
		for i in range(nV):
			for j in range(nV):
				distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j])
	print_solution(distance)
from collections import defaultdict

def print_solution(distance):
	for i in range(nV):
		for j in range(nV):
			if distance[i][j] == INF:
				print('INF', end=' ')
			else:
				print(distance[i][j], end='  ')
		print(' ')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	(s, m) = (0, 10 ** 12)
	for i in range(n):
		s += l[i]
		if m > s:
			m = s
	print(abs(m))


def saurabh(n, arr):
	x = 0
	temp = 0
	for i in arr:
		if x >= 0:
			x += i
		if x < 0:
			temp += abs(x)
			x = 0
	return temp
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(saurabh(n, arr))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	y = 0
	while x < n and y < n:
		if a[x] <= 0:
			x += 1
		elif a[y] >= 0:
			y += 1
		elif x > y:
			y = x + 1
		elif a[x] >= -a[y]:
			(a[x], a[y]) = (a[x] + a[y], 0)
		else:
			(a[x], a[y]) = (0, a[x] + a[y])
	s = sum((el for el in a if el > 0))
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	su = 0
	ans = 0
	fl = 0
	k = 0
	su = 0
	for i in range(len(a)):
		if a[i] > 0:
			su += a[i]
			a[i] = 0
		else:
			(a[i], su) = (min(0, su + a[i]), max(0, su + a[i]))
	print(sum(a) * -1)


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		pos = 0
		neg = 0
		l = list(In())
		for x in range(n):
			if l[x] > 0:
				pos += l[x]
			elif pos >= abs(l[x]):
				pos += l[x]
			else:
				l[x] += pos
				pos = 0
				neg += l[x]
		print(abs(neg))
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cost = 0
	balance = 0
	for i in range(n - 1, -1, -1):
		if a[i] > 0:
			balance = a[i] + balance
			if balance > 0:
				cost += balance
				balance = 0
		if a[i] < 0:
			balance = balance + a[i]
	print(cost)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = LIST()
	cur = 0
	ans = 0
	for a in A:
		cur += a
		ans = max(ans, -cur)
	print(ans)


from collections import Counter, defaultdict as dft

def mp():
	return map(int, input().split())

def ml():
	return list(map(int, input().split()))

def solve():
	n = int(input())
	arr = ml()
	s = []
	mx = 0
	for i in range(n - 1, -1, -1):
		if not s:
			s.append(arr[i])
		else:
			if arr[i] < 0:
				if s[-1] > 0:
					s.append(arr[i])
				else:
					s[-1] += arr[i]
			if arr[i] > 0:
				if s[-1] > 0:
					s[-1] += arr[i]
				else:
					take = min(arr[i], abs(s[-1]))
					arr[i] -= take
					s[-1] += take
					if arr[i]:
						s.append(arr[i])
	print(abs(s[-1]))
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	first_pos = 0
	for i in range(n):
		if a[i] > 0:
			first_pos = i
			break
	i = first_pos + 1
	ans = abs(sum(a[0:first_pos]))
	dec = a[i - 1]
	while i < n - 1:
		a[i] += dec
		dec = a[i]
		if a[i] < 0:
			ans += abs(dec)
			dec = 0
		i += 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	ans = 0
	for i in range(n):
		if a[i] > 0:
			sum = sum + a[i]
		else:
			mn = min(sum, -a[i])
			sum -= mn
			a[i] += mn
			ans += -a[i]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	b = n - 1
	a = 0
	ans = 0
	for i in range(n):
		if l[i] >= 0:
			x = l[i]
			l[i] = 0
			while a < n and x > 0:
				if l[a] < 0:
					d = -1 * l[a]
					if d <= x:
						l[a] = 0
						x -= d
					else:
						l[a] = -1 * (d - x)
						x = 0
						a -= 1
				a += 1
		else:
			ans += -1 * l[i]
			x = -1 * l[i]
			l[i] = 0
			while b >= 0 and x > 0:
				if l[b] >= 0:
					d = l[b]
					if d <= x:
						l[b] = 0
						x -= d
					else:
						l[b] -= x
						x = 0
						b += 1
				b -= 1
	print(ans)


for _ in range(int(input())):
	p = 0
	n = int(input())
	a = [int(x) for x in input().split()]
	cnt = 0
	for i in a:
		if i > 0:
			p += i
		elif i < 0:
			t = min(p, abs(i))
			p -= t
			i += t
			cnt += abs(i)
	print(cnt)


for _ in range(int(input())):
	(n, s) = (int(input()), 0)
	for i in map(int, input().split()):
		s = max(0, s + i)
	print(s)


for i in range(int(input())):
	pocet = int(input())
	vstup = list(map(float, input().split(' ')))
	suma1 = 0
	mini = 0
	for i in range(pocet):
		suma1 = suma1 + vstup[i]
		mini = min(mini, suma1)
	suma2 = 0
	maxi = 0
	for i in range(pocet):
		suma2 = suma2 + vstup[-i - 1]
		maxi = max(maxi, suma2)
	print(int(max(-mini, maxi)))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	result = 0
	psum = 0
	for num in arr:
		psum += num
		result = min(result, psum)
	print(abs(result))


T = int(input())
for _ in range(T):
	n = int(input())
	lis = list(map(int, input().split()))
	count = 0
	temp = 0
	for i in lis:
		temp += i
		count = min(count, count + temp)
		temp = max(0, temp)
	print(0 - count)


try:
	t = int(input())
	while t != 0:
		n = int(input())
		arr = list(map(int, input().split()))
		result = 0
		temp = 0
		for i in range(n):
			if arr[i] < 0:
				temp += arr[i]
			if arr[i] > 0:
				if temp < 0:
					result += temp
					temp = arr[i]
				else:
					temp += arr[i]
		if temp < 0:
			result += temp
		print(abs(result))
		t = t - 1
except EOFError:
	print(' ')


import sys, os, time
from math import log, sqrt, ceil, floor
from os import path
import math
oj = True
if path.exists('inp.txt'):
	print('YES')
	sys.stdin = open('inp.txt', 'r')
	sys.stdout = open('out.txt', 'w')
	oj = False
mod = 1000000007
INF = float('inf')

def readint():
	return int(sys.stdin.readline())

def readlst():
	return list(map(int, sys.stdin.readline().strip().split()))

def readmul():
	return map(int, sys.stdin.readline().strip().split())

def is_power_two(x):
	return x ^ x - 1 & x == x

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	if b == 0:
		return a
	if a == b:
		return a
	if a > b:
		return gcd(a - b, b)
	return gcd(a, b - a)

def seive():
	(primes[0], primes[1]) = (False, False)
	for i in range(2, maxN + 1):
		for j in range(i * i, maxN + 1, i):
			if primes[j]:
				primes[j] = False

def power_2(n):
	return 1 << n

def sol():
	n = readint()
	a = readlst()
	ans = 0
	for i in range(n):
		ans = max(0, ans + a[i])
	print(ans)
	return

def main():
	tc = readint()
	start = time.time()
	while tc:
		sol()
		tc -= 1
	if not oj:
		total = time.time() - start
		print(str(total * 1000) + 'ms')
main()


import math
import os
import random
import re
import sys
from collections import defaultdict, Counter, deque
import heapq
from functools import lru_cache
from itertools import accumulate, chain, combinations, permutations, groupby, zip_longest
from math import floor, ceil, sqrt

def solve(arr):
	arr = arr[::-1]
	(pos, neg) = (deque(), deque())
	res = 0
	for (i, v) in enumerate(arr):
		if v > 0:
			pos.append((i, v))
		elif v < 0:
			neg.append((i, v))
	for i in range(len(arr)):
		while arr[i] < 0:
			if not pos:
				res += abs(arr[i])
				break
			else:
				(j, v2) = pos.popleft()
				if j > i:
					add = min(-arr[i], v2)
					v2 -= add
					arr[i] += add
					if v2 > 0:
						pos.appendleft((j, v2))
	return res
t = int(input())
res = []
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	res += (solve(arr),)
for v in res:
	print(v)


ansal = []

def solve(n, a):
	global ansal
	ans = 0
	rem = 0
	for _ in range(n):
		ai = a.pop()
		if ai > 0:
			if rem > ai:
				rem -= ai
			else:
				ans += ai - rem
				rem = 0
		else:
			rem -= ai
	ansal.append(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	solve(n, a)
for ans in ansal:
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	val = 0
	cost = 0
	i = 0
	while i < n:
		if val >= 0:
			val += arr[i]
		if val < 0:
			cost += abs(val)
			val = 0
		i += 1
	print(cost)


t = int(input())
for i in range(t):
	n = int(input())
	b = input().split(' ')
	a = []
	m = 0
	for j in range(n):
		h = int(b[j])
		if h > 0:
			a.append(0)
			m = m + h
		elif m <= -h:
			a.append(m + h)
			m = 0
		else:
			a.append(0)
			m = m + h
	print(m)


for t in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	sum1 = 0
	cost = 0
	for i in range(n):
		if l1[i] >= 0:
			sum1 += l1[i]
		elif l1[i] < 0:
			if sum1 > 0:
				sum1 += l1[i]
				if sum1 < 0:
					cost += sum1
					sum1 = 0
			else:
				cost += l1[i]
	print(abs(cost))


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from itertools import permutations, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	n = fi()
	a = li()
	if n == 1:
		print(0)
		continue
	p = []
	m = []
	ans = 0
	for i in range(n):
		if a[i] < 0:
			if len(p):
				while len(p):
					z = min(-a[i], p[-1])
					a[i] += min(-a[i], p[-1])
					p[-1] -= z
					if p[-1] == 0:
						p.pop()
					if a[i] == 0:
						break
			if a[i] < 0:
				m.append(-a[i])
		else:
			p.append(a[i])
	print((sum(p) + sum(m)) // 2)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
ii = lambda : int(input())
si = lambda : input()
jn = lambda x, l: x.join(map(str, l))
sl = lambda : list(map(str, input().strip()))
mi = lambda : map(int, input().split())
mif = lambda : map(float, input().split())
lii = lambda : list(map(int, input().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 1000000007

def solve(arr, n):
	temp = 0
	ans = 0
	for i in range(n):
		if arr[i] < 0:
			temp += arr[i]
			if temp < 0:
				if abs(temp) > ans:
					ans = abs(temp)
		else:
			temp += arr[i]
	return ans
for _ in range(ii()):
	n = ii()
	arr = lii()
	print(solve(arr, n))


import os
import sys
from io import BytesIO, IOBase
import threading
from collections import defaultdict

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	s = sum([abs(i) for i in arr])
	pp = 0
	cnt = 0
	for i in range(n):
		if arr[i] < 0:
			dec = min(abs(arr[i]), pp)
			pp -= dec
			cnt = cnt + 2 * dec
		else:
			pp += arr[i]
	print((s - cnt) // 2)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	main()


T = int(input().strip())
for t in range(T):
	N = int(input().strip())
	content = list(map(int, input().strip().split()))

	def find_pos(prev=0):
		for pos in range(prev, N):
			if content[pos] > 0:
				return pos
		return -1

	def find_neg(start):
		for neg in range(start, N):
			if content[neg] < 0:
				return neg
		return -1
	pos = find_pos()
	neg = 0
	while True:
		neg = find_neg(max(pos, neg))
		if neg == -1:
			break
		pos_val = content[pos]
		neg_val = -content[neg]
		reduction = min(pos_val, neg_val)
		if reduction == 0:
			break
		content[pos] -= reduction
		content[neg] += reduction
		pos = find_pos(pos)
		if pos == -1:
			break
	print(sum((a for a in content if a > 0)))


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 5
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	p = [0] * n
	p[0] = a[0]
	for i in range(1, n):
		p[i] = p[i - 1] + a[i]
	print(abs(min(p)))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c = arr[-1]
	ans = 0
	for i in range(1, n):
		arr[i] += arr[i - 1]
	m = float('inf')
	for i in range(n):
		m = min(arr[i], m)
	print(-1 * m)


t = int(input())
while t > 0:
	t -= 1
	input()
	arr = input().strip().split(' ')
	balance = 0
	ans = 0
	for ele in arr:
		ele = int(ele)
		balance += ele
		if balance < 0:
			ans -= balance
			balance = 0
	print(ans)


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in range(n):
		if a[i] < 0:
			ans += max(0, -(s + a[i]))
			s += a[i]
			s = max(s, 0)
		else:
			s += a[i]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	put = 0
	for el in a:
		if el > 0:
			put += el
		else:
			put = max(put + el, 0)
	print(put)


for i in range(int(input())):
	n = int(input())
	s = 0
	an = 0
	a = [int(x) for x in input().split()]
	for j in range(n - 1, -1, -1):
		if a[j] > 0:
			if abs(s) > a[j]:
				s += a[j]
			else:
				an += a[j] - abs(s)
				s = 0
		elif a[j] < 0:
			s += a[j]
	print(abs(s))


import sys
read = lambda : sys.stdin.readline().strip()
read_ints = lambda : map(int, read().split())
for _ in range(int(read())):
	n = read_ints()
	nums = list(read_ints())
	total = sum(nums)
	max_total = total
	for i in nums:
		if total - i > max_total:
			max_total = total - i
		total -= i
	print(max_total)


def calc(a):
	res = 0
	a_plus = []
	for item in a:
		if item > 0:
			a_plus.append(item)
		else:
			while item < 0 and len(a_plus) > 0:
				item += a_plus[0]
				if item > 0:
					a_plus[0] = item
					item = 0
				else:
					del a_plus[0]
			res -= item
	return res

def get_ints():
	return [int(n) for n in input().split()]

def get_floats():
	return [float(n) for n in input().split()]

def seq2str(seq):
	return ' '.join((str(item) for item in seq))
zzz = get_ints()
assert len(zzz) == 1
t = zzz[0]
for i in range(t):
	zzz = get_ints()
	assert len(zzz) == 1
	n = zzz[0]
	zzz = get_ints()
	assert len(zzz) == n
	a = zzz
	res = calc(a)
	print(res)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		continue
	temp = 0
	ans = 0
	for ele in arr:
		if ele < 0:
			if temp >= abs(ele):
				temp += ele
			else:
				ans += abs(temp + ele)
				temp = 0
		else:
			temp += ele
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = input().split()
	a = [int(a[x]) for x in range(n)]
	i = 0
	j = 1
	while j != n:
		if a[i] <= 0:
			i += 1
		elif a[j] >= 0:
			j += 1
		else:
			move_num = min(a[i], -a[j])
			a[i] -= move_num
			a[j] += move_num
		if i == j:
			j += 1
	print(sum([abs(x) for x in a]) // 2)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	countneg = 0
	countpov = 0
	for j in range(n):
		if l[j] > 0:
			countpov += l[j]
		elif l[j] < 0:
			x = min(countpov, abs(l[j]))
			countpov -= x
			countneg += abs(l[j]) - x
	print(countneg)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()[:n]))
	p = 0
	for x in l:
		if x > 0:
			p += x
		else:
			p = max(0, p + x)
	print(p)


from math import *
from bisect import bisect_left as bl
from bisect import bisect_right as br
from collections import Counter as ctr
from collections import deque as dq
from array import array
from re import search
li = lambda : list(map(int, input().split()))
arr = lambda a: array('i', a)
bi = lambda n: bin(n).replace('0b', '')
yn = lambda f: print('NYOE S'[f::2])
sbstr = lambda a, s: search('.*'.join(a), s)

def solve():
	for _ in range(int(input())):
		n = int(input())
		a = li()[::-1]
		s = 0
		c = 0
		for i in range(n):
			if a[i] > s:
				c += a[i] - s
				s = 0
			elif a[i] > 0:
				s -= a[i]
			else:
				s += -a[i]
		print(c)
solve()


for _ in range(int(input())):
	lens = int(input())
	arrs = [int(x) for x in input().split()]
	psum = 0
	for i in arrs:
		psum += i
		psum = max(psum, 0)
	print(psum)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	sn = 0
	for i in range(n - 1, -1, -1):
		if a[i] > 0:
			k = a[i] + sn
			if a[i] + sn >= 0:
				ans += k
				sn = 0
			else:
				sn = a[i] + sn
		else:
			sn += a[i]
	print(ans)


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x, s='\n': print(s.join(map(str, x)))
t = ini()
for _ in range(t):
	n = ini()
	a = inl()
	ans = 0
	count = 0
	for i in a:
		if i > 0:
			count += i
		else:
			ans += min(0, i + count)
			count = 0 if i + count < 0 else i + count
	print(-ans)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	i = 0
	ans = 0
	main = []
	while i < n:
		temp = lst[i]
		j = i + 1
		while j < n:
			if temp * lst[j] < 0:
				break
			temp += lst[j]
			j += 1
		main.append(temp)
		i = j
	n = len(main)
	i = 0
	ans = 0
	if main[i] < 0:
		ans += abs(main[i])
		i += 1
	temp = 0
	while i < n - 1:
		t = temp + main[i] + main[i + 1]
		if t > 0:
			temp = t
			i += 2
		else:
			ans += abs(t)
			temp = 0
			i += 2
	print(ans)


T = int(input())
for _ in range(T):
	N = int(input())
	arr = list(map(int, input().split()))
	minus = 0
	ans = 0
	for i in arr[::-1]:
		if i > 0:
			if abs(minus) > i:
				minus += i
			elif abs(minus) <= i:
				ans += i + minus
				minus = 0
		elif i < 0:
			minus += i
	print(ans)


t = int(input())
for p in range(t):
	n = int(input())
	ara = list(map(int, input().strip().split()))
	total = 0
	minn = 0
	ans = list()
	for i in range(n):
		total += ara[i]
		if total < minn:
			minn = total
	print(abs(minn))


for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(s, s1) = (0, 0)
	for i in range(n):
		if a[i] < 0:
			if s >= -1 * a[i]:
				s -= -1 * a[i]
			else:
				s1 += s - -1 * a[i]
				s = 0
		else:
			s += a[i]
	print(-1 * s1)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = n - 1
	neg = 0
	sum = 0
	ans = 0
	for i in range(n):
		sum += a[i]
		if sum < 0:
			ans += abs(sum)
			sum = 0
	print(ans)


t = int(input())
for qtqt in range(t):
	n = int(input())
	sm = 0
	ans = 0
	a = list(map(int, input().split()))
	for i in range(n):
		sm += a[i]
		ans += -min(0, sm)
		sm = max(0, sm)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	o2 = 1000000001
	su = 0
	for i in arr[:-1]:
		su += i
		o2 = min(su, o2)
	if o2 >= 0:
		print(0)
	else:
		print(-1 * o2)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * (2 * 10 ** 5 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj
mod = 10 ** 9 + 7

def solve():
	n = II()
	li = L()
	sm = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		sm += li[i]
		if li[i] > 0:
			ans = max(ans, sm)
	P(ans)
	return
t = II()
for i in range(t):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	(pos, neg) = ([], [])
	for i in range(n):
		if l[i] > 0:
			pos.append([i, l[i]])
		else:
			neg.append([i, l[i]])
	(posptr, negptr) = (0, 0)
	while posptr < len(pos) and negptr < len(neg):
		if neg[negptr][0] < pos[posptr][0]:
			negptr += 1
		else:
			negv = abs(neg[negptr][1])
			posv = pos[posptr][1]
			if posv == negv:
				neg[negptr][1] = 0
				pos[posptr][1] = 0
				posptr += 1
				negptr += 1
			elif posv > negv:
				neg[negptr][1] = 0
				pos[posptr][1] = posv - negv
				negptr += 1
			else:
				pos[posptr][1] = 0
				neg[negptr][1] = -negv + posv
				posptr += 1
	c = 0
	for i in range(len(pos)):
		if pos[i][1] > 0:
			c += pos[i][1]
	print(c)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	postive = 0
	for i in range(n):
		if arr[i] > 0:
			postive += arr[i]
		if arr[i] < 0:
			if abs(arr[i]) <= postive:
				postive = postive + arr[i]
			else:
				postive = 0
				arr[i] = arr[i] + postive
	print(postive)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = [int(x) for x in input().split()]
	n = 0
	cost = 0
	for a in reversed(arr):
		if a < 0:
			n -= a
		elif a > n:
			cost += abs(n - a)
			n = 0
		else:
			n -= a
	cost += n
	print(cost // 2)


tc = int(input())
for _ in range(tc):
	n = int(input())
	a = list(map(int, input().split(' ')))
	(cost, suffix_sum) = (0, 0)
	for e in reversed(a):
		suffix_sum += e
		cost = max(cost, suffix_sum)
	print(cost)


t = int(input())
for l in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	minNo = 0
	for i in a:
		sum += i
		minNo = min(minNo, sum)
	print(abs(minNo))


t = int(input())
answer = []
for _ in range(t):
	n = int(input())
	liste = list(map(int, input().split(' ')))
	sum = 0
	mini = 0
	for i in range(n):
		sum += liste[i]
		mini = min(sum, mini)
	answer.append(str(-mini))
print('\n'.join(answer))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	pos = neg = price = 0
	for i in range(n):
		if a[i] > 0:
			pos += a[i]
		else:
			neg = a[i]
			minimum = min(pos, abs(neg))
			pos -= minimum
			neg += minimum
		price += abs(neg)
		neg = 0
	print(price)


for _ in range(int(input())):
	n = int(input())
	ans = 0
	for i in input().split():
		if int(i) > 0:
			ans += int(i)
		else:
			ans = max(ans + int(i), 0)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = [0]
	for i in range(n - 1, 0, -1):
		ans.append(l[i] + ans[-1])
	print(max(ans))


for tc in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = 0
	neg = 0
	for x in arr:
		if x > 0:
			pos = x + pos
		else:
			t = abs(x)
			mn = min(t, pos)
			pos -= mn
			neg += t - mn
	print(int((pos + neg) / 2))


def I():
	return int(input())

def MI():
	return map(int, input().split())

def ML():
	return list(MI())

def findarr1(arr):
	array = []
	ii = 0
	while ii < len(arr):
		sum1 = 0
		j = ii
		if arr[j] <= 0:
			while j < len(arr):
				if arr[j] <= 0:
					sum1 += arr[j]
				else:
					break
				j += 1
			array.append(sum1)
		elif arr[j] >= 0:
			while j < len(arr) and arr[j] >= 0:
				sum1 += arr[j]
				j += 1
			array.append(sum1)
		ii = j
	return array
for t in range(I()):
	n = I()
	arr = ML()
	arr = findarr1(arr)
	while True:
		if arr[0] <= 0:
			ii = 1
			while ii < len(arr):
				if ii + 1 < len(arr):
					minm = min(abs(arr[ii]), abs(arr[ii + 1]))
					arr[ii] -= minm
					arr[ii + 1] += minm
				ii += 2
		else:
			ii = 0
			while ii < len(arr):
				if ii + 1 < len(arr):
					minm = min(abs(arr[ii]), abs(arr[ii + 1]))
					arr[ii] -= minm
					arr[ii + 1] += minm
				ii += 2
		arr = findarr1(arr)
		if len(arr) == 2 and arr[0] <= 0 and (arr[1] >= 0):
			print((abs(arr[0]) + arr[1]) // 2)
			break
		elif len(arr) == 1:
			print(0)
			break


from math import *
t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	x = []
	y = []
	for i in range(n):
		if a[i] < 0:
			x.append([abs(a[i]), i])
		elif a[i] > 0:
			y.append([a[i], i])
	j = 0
	k = len(x) - 1
	for i in range(len(y) - 1, -1, -1):
		val = y[i][0]
		ind = y[i][1]
		while val != 0:
			if j == k and x[j][1] < ind:
				hmm = min(abs(x[j][0]), val)
				val = val - hmm
				x[j][0] -= hmm
				cnt += hmm
				if x[j][0] == 0:
					j += 1
				continue
			if j == k and x[j][1] > ind:
				hmm = min(abs(x[j][0]), val)
				val = val - hmm
				x[j][0] -= hmm
				if x[j][0] == 0:
					j += 1
				continue
			if x[k][1] > ind:
				hmm = min(abs(x[k][0]), val)
				val = val - hmm
				x[k][0] -= hmm
				if x[k][0] == 0:
					k -= 1
			else:
				hmm = min(abs(x[j][0]), val)
				val = val - hmm
				x[j][0] -= hmm
				cnt += hmm
				if x[j][0] == 0:
					j += 1
	print(cnt)


import sys, math
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	mas = list(map(int, input().split()))
	mas = mas[::-1]
	part = []
	for i in range(n):
		if i == 0:
			part.append(mas[i])
		else:
			part.append(mas[i] + part[-1])
	print(max(part))


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	l = li()
	ans = curr = 0
	for i in l:
		if i >= 0:
			curr += i
			continue
		else:
			temp = min(curr, abs(i))
			curr -= temp
		if abs(i) > temp:
			ans += abs(i) - temp
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	sum_n = 0
	for i in range(n):
		if li[n - i - 1] < 0:
			sum_n += abs(li[n - i - 1])
		elif li[n - i - 1] > 0:
			if li[n - i - 1] >= sum_n:
				sum_n = 0
			else:
				sum_n -= li[n - i - 1]
	print(sum_n)


for _ in range(int(input())):
	input()
	a = [int(x) for x in input().split()]
	positives = [[i, x] for (i, x) in enumerate(a) if x > 0]
	negatives = [[i, -x] for (i, x) in enumerate(a) if x < 0]
	ptr_1 = 0
	ptr_2 = 0
	result = 0
	while ptr_1 < len(negatives) and ptr_2 < len(positives):
		if positives[ptr_2][0] > negatives[ptr_1][0]:
			result += negatives[ptr_1][1]
			ptr_1 += 1
		elif positives[ptr_2][1] > negatives[ptr_1][1]:
			positives[ptr_2][1] -= negatives[ptr_1][1]
			ptr_1 += 1
		else:
			negatives[ptr_1][1] -= positives[ptr_2][1]
			ptr_2 += 1
	result += sum([x for (_, x) in negatives[ptr_1:]])
	print(result)


for i in range(int(input())):
	n = int(input())
	numbers = [int(i) for i in input().split()]
	amount = 0
	for j in range(n - 1, -1, -1):
		if numbers[j] < 0:
			amount += -numbers[j]
		elif numbers[j] > 0:
			amount = max(0, amount - numbers[j])
	print(amount)


T = int(input())
for _ in range(0, T):
	n = int(input())
	s = [int(x) for x in input().split()]
	nrem = 0
	ans = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] > 0:
			tt = min(nrem, s[i])
			s[i] -= tt
			ans += s[i]
			nrem -= tt
		else:
			nrem += abs(s[i])
	print(ans)


from sys import stdin

def get_int():
	return int(stdin.readline())

def get_str():
	return stdin.readline().strip()

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_array():
	return list(map(int, stdin.readline().strip().split()))

def get_array2d(row):
	return [list(map(int, stdin.readline().strip().split())) for _ in range(row)]
T = get_int()
for _ in range(T):
	N = get_int()
	A = get_array()
	minCoin = 0
	for a in A:
		minCoin = max(0, minCoin + a)
	print(minCoin)


for s in [*open(0)][2::2]:
	t = m = 0
	for x in s.split():
		t -= int(x)
		m = max(m, t)
	print(m)


T = int(input())
for t in range(T):
	_ = input()
	a = [int(x) for x in input().split()]
	bank = 0
	for i in a:
		if i > 0:
			bank += i
		elif i < 0:
			bank = max([0, bank + i])
	print(bank)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(i, j) = (0, 0)
	while i < n:
		if l[i] > 0:
			j = i + 1
			while j < n:
				if l[j] < 0:
					if -l[j] > l[i]:
						l[j] += l[i]
						l[i] = 0
						i += 1
					else:
						l[i] += l[j]
						l[j] = 0
						j += 1
				else:
					j += 1
			if j >= n - 1:
				break
		else:
			i += 1
	ans = 0
	for i in l:
		if i > 0:
			ans += i
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	ps = list(map(int, input().split()))
	res = 0
	spare = 0
	end = 0
	for p in ps:
		if p > 0:
			spare += p
		if p < 0:
			if spare + p >= 0:
				spare += p
			else:
				p += spare
				spare = 0
				res += -p
	print(res)


T = int(input())
while T > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	pos = 0
	for i in range(n):
		if arr[i] >= 0:
			pos += arr[i]
		else:
			pos = max(0, pos + arr[i])
	res = pos
	print(res)
	T -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ns = 0
	for i in range(n - 1, -1, -1):
		if a[i] <= 0:
			ns += -a[i]
		elif a[i] >= ns:
			a[i] -= ns
			ns = 0
		else:
			ns -= a[i]
			a[i] = 0
	print(sum([i for i in a if i > 0]))


import math
num_cases = int(input())
for _ in range(num_cases):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	cost = 0
	spare = 0
	for i in range(n):
		if arr[i] > 0:
			spare += arr[i]
		else:
			num = -1 * arr[i]
			if spare > num:
				spare -= num
			else:
				cost += num - spare
				spare = 0
	print(cost)


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		b.append(a[n - i - 1])
	curr = 0
	ans = 0
	for i in range(n):
		curr += b[i]
		if curr > 0:
			ans += curr
			curr = 0
	print(ans)


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	sol = 0
	for i in range(n - 1, -1, -1):
		sol += p[i]
		sol = min(sol, 0)
	print(-sol)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	x = 0
	y = 0
	for i in a:
		if i > 0:
			x += i
		else:
			i = abs(i)
			k = min(x, i)
			x -= k
			y += i - k
	print(x)


import bisect
import copy
import fractions
import functools
import heapq
import math
import random
import sys
T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	total = 0
	min_ = 0
	for a in A:
		total += a
		min_ = min(min_, total)
	print(str(abs(min_)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	coins = 0
	reserve = 0
	for i in range(n):
		if a[i] < 0:
			coins += max(abs(a[i]) - reserve, 0)
			reserve = max(reserve - abs(a[i]), 0)
		else:
			reserve += a[i]
	print(coins)


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	n = int(input())
	arr = [int(i) for i in input().split()]
	ans = 0
	curr = 0
	for (idx, i) in enumerate(arr):
		ans += abs(i)
		if i > 0:
			curr += i
		else:
			i = abs(i)
			if i < curr:
				ans -= 2 * i
				curr -= i
			else:
				ans -= 2 * curr
				curr = 0
	print(ans // 2)

def main():
	tc = int(input().strip())
	for case in range(tc):
		solve()
main()


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	sum_ = 0
	count = 0
	for a in arr:
		sum_ += a
		if sum_ < 0:
			count += abs(sum_)
			sum_ = 0
	print(count)


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	k = 0
	for i in b:
		if i > 0:
			k += i
		elif k + i < 0:
			k = 0
		else:
			k += i
	print(k)


def solve():
	n = int(input())
	a = list(map(int, input().split()))

	def find_positive(start_idx):
		while start_idx < n and a[start_idx] <= 0:
			start_idx += 1
		return start_idx

	def find_negative(start_idx):
		while start_idx < n and a[start_idx] >= 0:
			start_idx += 1
		return start_idx
	positive = find_positive(0)
	negative = find_negative(0)
	while positive < n and negative < n:
		if positive < negative:
			exchange = min(-a[negative], a[positive])
			a[negative] += exchange
			a[positive] -= exchange
			positive = find_positive(positive)
			negative = find_negative(negative)
		else:
			negative = find_negative(negative + 1)
	print(sum((abs(i) for i in a)) // 2)
runs = int(input())
while runs > 0:
	solve()
	runs -= 1


from sys import stdin
N = int(stdin.readline())
for case in range(N):
	bank = 0
	sol = 0
	length = int(stdin.readline())
	if length == 0:
		print(0)
		continue
	array = [int(i) for i in stdin.readline().split()]
	for i in range(length):
		if array[i] >= 0:
			bank += array[i]
			array[i] = 0
		elif bank > 0:
			cost = -1 * array[i]
			sol += cost
			x = min(bank, cost)
			sol -= x
			bank -= x
		else:
			sol += array[i] * -1
	print(sol)


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	line = input()
	a = [int(n) for n in line.split(' ')]
	positives = 0
	negatives = 0
	for j in range(n - 1, -1, -1):
		if a[j] > 0:
			to_match = min(a[j], negatives)
			negatives -= to_match
			positives += a[j] - to_match
		else:
			negatives += abs(a[j])
	print(max(positives, negatives))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mx = 0
	for i in range(n - 1):
		if a[i] < 0:
			mx = max(mx, -a[i])
		a[i + 1] += a[i]
	print(mx)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math
from collections import defaultdict
for _ in range(ri()):
	n = ri()
	l = ria()
	ans = 0
	for i in range(n):
		ans = max(0, ans + l[i])
	wi(ans)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	(pos, total_neg) = (0, 0)
	cost = 0
	for i in a:
		if i > 0:
			pos += i
			continue
		if pos > 0 and i < 0:
			temp = min(-i, pos)
			left = i + temp
			pos -= temp
			total_neg += left
		else:
			total_neg += i
	print(-total_neg)
	t -= 1


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	neg = []
	for i in range(n):
		if a[i] and a[i] // abs(a[i]) != 1:
			neg.append([a[i], i])
	for i in range(n - 1, -1, -1):
		if a[i] and a[i] // abs(a[i]) == 1:
			while neg and neg[-1][1] > i and (a[i] != 0):
				if abs(neg[-1][0]) > a[i]:
					neg[-1][0] += a[i]
					a[i] = 0
				else:
					a[i] += neg[-1][0]
					neg.pop()
	s = 0
	for i in neg:
		s += i[0]
	print(-s)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	s = 0
	for i in range(n):
		s += l[i]
		ans = max(ans, -s)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	for j in range(n):
		if res != 0 or arr[j] > 0:
			res += arr[j]
		if res < 0:
			res = 0
	print(res)


import sys, math, bisect
from collections import Counter, defaultdict as dd

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def l():
	return list(sp())

def strl():
	return list(strsp())

def sp():
	return map(int, data().split())

def strsp():
	return map(str, data().split())
for _ in range(int(data())):
	n = int(input())
	a = l()
	b = 0
	for i in range(n):
		b += a[i]
		b = max(b, 0)
	print(b)


test_cases = int(input())
for t in range(test_cases):
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	agg = 0
	agg_list = [0 for i in range(n)]
	for j in range(n):
		agg = agg + arr[j]
		agg_list[j] = agg
	s = -min(agg_list)
	print(s)


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	temparr = input()
	temparr = temparr.split()
	arr = []
	for i in temparr:
		arr.append(int(i))
	pos = 0
	neg = 0
	for i in arr:
		if i == 0:
			continue
		if i > 0:
			pos += i
		else:
			absneg = abs(i)
			if absneg > pos:
				diff = absneg - pos
				neg += diff
				pos = 0
			else:
				pos -= absneg
	print(neg)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(ans, pos, neg) = (0, 0, 0)
	ans = 0
	stack = 0
	for i in range(n):
		if arr[i] < 0 and stack == 0:
			ans += abs(arr[i])
		elif arr[i] > 0 and stack == 0:
			stack += arr[i]
		elif arr[i] < 0 and stack > 0:
			stack += arr[i]
			if stack < 0:
				ans += abs(stack)
				stack = 0
		elif arr[i] > 0 and stack > 0:
			stack += arr[i]
	print(ans)


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = 10 ** 9 + 7

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = defaultdict(int)
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors[i] += 1
			n //= i
		i += 1
	if n > 1:
		factors[n] += 1
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	n = int(input())
	arr = array()
	pos = 0
	for i in range(n):
		if arr[i] >= 0:
			pos += arr[i]
		else:
			diff = min(-arr[i], pos)
			arr[i] += diff
			pos -= diff
	ans = 0
	for i in range(n):
		if arr[i] < 0:
			ans += abs(arr[i])
	print(ans)
t = 1
t = int(input())
for _ in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	x = y = 0
	a = list(map(int, input().split()))
	for i in range(n):
		y += abs(min(x + a[i], 0))
		x = max(0, x + a[i])
	print(y)


t = int(input())
while t > 0:
	sum = 0
	k = 0
	a = int(input())
	l = list(map(int, input().split()))
	for i in range(0, len(l), 1):
		sum += l[i]
		k = min(k, sum)
	print(abs(k))
	t -= 1


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
for __ in range(itg()):
	n = itg()
	add = ans = 0
	for a in mpint():
		if a < 0:
			a += add
			add = 0
			if a >= 0:
				(a, add) = (0, a)
			else:
				ans += -a
		else:
			add += a
	print(ans)


a = int(input())
for x in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	j = 0
	for y in c:
		j += y
		j = max(j, 0)
	print(j)


t = int(input())
for _ in range(t):
	n = int(input())
	num = list(map(int, input().split()))
	(left, right) = (0, 0)
	length = len(num)
	plus = 0
	for i in range(len(num)):
		if num[i] > 0:
			plus += num[i]
		elif num[i] + plus > 0:
			plus += num[i]
			num[i] = 0
		else:
			num[i] += plus
			plus = 0
	cost = 0
	for i in range(len(num)):
		if num[i] < 0:
			cost += abs(num[i])
	print(cost)


from sys import stdin, stdout

def input():
	a = stdin.readline()
	if a[-1] == '\n':
		a = a[:-1]
	return a

def print(*argv, end='\n', sep=' '):
	n = len(argv)
	for i in range(n):
		if i == n - 1:
			stdout.write(str(argv[i]))
		else:
			stdout.write(str(argv[i]) + sep)
	stdout.write(end)
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def comb(lst, x):
	return list(c(lst, x))

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def smaller(lst, x):
	return bisect_left(lst, x) - 1

def smallerEq(lst, x):
	return bisect_right(lst, x) - 1

def solve(n, lst):
	summ = 0
	coin = 0
	for i in range(n):
		summ += lst[i]
		if summ < 0:
			coin -= summ
			summ = 0
	return coin
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(n, lst))


def process_arr(arr):
	coins = 0
	buf = 0
	for i in arr:
		if i > 0:
			buf += i
		elif i < 0:
			if -i > buf:
				coins -= i + buf
				buf = 0
			else:
				buf += i
	return coins

def main():
	a = int(input())
	i = 0
	ans = []
	while i < a:
		n = input()
		arr = map(int, input().split(' '))
		i += 1
		ans.append(process_arr(arr))
	for i in ans:
		print(i)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list((int(n) for n in input().split()))
	prefix_sum = [0]
	for ele in list1:
		prefix_sum.append(prefix_sum[-1] + ele)
	m = min(prefix_sum)
	print(m * -1)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	h = 0
	ans = 0
	for j in range(n):
		h += l[j]
		if h < 0:
			ans = max(ans, abs(h))
	print(ans)


def ans():
	N = int(input())
	for i in range(N):
		A = int(input())
		arr = input().split()
		b = 0
		ans = []
		for j in range(A):
			if int(arr[j]) >= 0 and b >= 0:
				b += int(arr[j])
			elif int(arr[j]) < 0 and b > 0:
				b = b + int(arr[j])
			if b < 0:
				b = 0
		print(b)
ans()


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	ans = 0
	s = 0
	for i in range(n):
		s += a[i]
		if s < 0 and abs(s) > ans:
			ans = abs(s)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	cnt = 0
	for (i, a) in enumerate(A):
		if a > 0:
			cnt += a
			A[i] = 0
		elif abs(a) <= cnt:
			A[i] = 0
			cnt -= abs(a)
		else:
			A[i] += cnt
			cnt = 0
	A[-1] += cnt
	cnt = 0
	ans = 0
	for a in A:
		if a < 0:
			ans += abs(a)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	for num in arr:
		res = max(0, res + num)
	print(res)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = [int(j) for j in input().split()]
	ans = 0
	s = 0
	for i in range(len(a)):
		s = s + a[i]
		if s < ans:
			ans = s
	print(-1 * ans)


for _ in range(0, int(input())):
	input()
	v = list(map(int, input().split()))
	p = 0
	t = 0
	for x in v:
		if x < 0:
			w = p + x
			p = max(0, p + x)
			if w < 0:
				t += w
		else:
			p += x
	print(abs(t))


t = int(input())
for i in range(0, t):
	m = int(input())
	arr = list(map(int, input().strip().split()))[:m]
	box = 0
	summ = 0
	for j in arr:
		if j < 0:
			box = box + j
			if box < 0:
				summ = summ + box
			if box < 0:
				box = 0
		else:
			box = box + j
	summ = abs(summ)
	print(summ)


import math as mt
import sys, string
from collections import Counter, defaultdict
input = sys.stdin.readline
I = lambda : int(input())
M = lambda : map(int, input().split())
ARR = lambda : list(map(int, input().split()))

def main():
	n = I()
	arr = ARR()
	cost = 0
	tmp = 0
	for i in range(n - 1, -1, -1):
		tmp += arr[i]
		cost = max(tmp, cost)
	print(cost)
tc = I()
for _ in range(tc):
	main()


import math
for test in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pos = 0
	neg = 0
	cost = 0
	for (idx, i) in enumerate(a):
		if i >= 0:
			pos += i
		elif pos >= math.fabs(i):
			pos = pos + i
		else:
			temp_cost = int(math.fabs(i) - pos)
			cost += temp_cost
			pos = 0
			for (idx2, j) in enumerate(reversed(a)):
				if idx2 == idx:
					break
				if j >= 0:
					if j >= temp_cost:
						a[idx2] = a[idx2] - temp_cost
						break
					else:
						temp_cost -= j
						a[idx2] = 0
	print(cost)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	temp = s.split()
	numbers = [int(i) for i in temp]
	summation = 0
	negativeSums = [0]
	for i in numbers:
		summation += i
		if summation < 0:
			negativeSums.append(summation)
	print(min(negativeSums) * -1)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	res = 0
	for i in range(len(l)):
		res = res + l[i]
		if res < 0:
			res = 0
	print(res)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	summ = arr[0]
	minn = arr[0]
	for i in range(1, n):
		summ += arr[i]
		minn = min(minn, summ)
	if minn > 0:
		print(0)
	else:
		print(abs(minn))


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split(' ')))
	cnt = 0
	sum_p = 0
	sum_n = 0
	for i in p:
		if i > 0:
			sum_p += i
		elif i < 0:
			sum_p = max(0, sum_p + i)
	print(sum_p)


for i in range(int(input())):
	n = input()
	l = list(map(int, input().split()))
	total = 0
	pos = 0
	balance = 0
	for i in l:
		if i > 0:
			total += i
			pos += i
		elif i < 0:
			var = min(pos, abs(i))
			pos -= var
			balance += var
	print(total - balance)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sm = 0
	for i in range(n - 1, -1, -1):
		if a[i] < 0:
			sm += a[i]
		else:
			a[i] += sm
			sm = min(0, a[i])
	sm = 0
	for i in a:
		if i > 0:
			sm += i
	print(sm)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	for (i, val) in enumerate(a):
		if val > 0:
			s += val
	i1 = n - 1
	i2 = n - 1
	while i1 >= 0 and i2 >= 0:
		if a[i2] >= 0:
			i2 -= 1
		if a[i1] <= 0 or i1 >= i2:
			i1 -= 1
		if a[i2] < 0 and a[i1] > 0 and (i1 < i2) and (i1 >= 0):
			tal = min(abs(a[i1]), abs(a[i2]))
			s -= tal
			a[i1] -= tal
			a[i2] += tal
	print(s)


from math import *
from collections import *
from itertools import *
from functools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(100000000)
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	temp = 0
	temp1 = 0
	ans = 0
	for i in range(len(l)):
		if l[i] < 0:
			temp = temp1 + l[i]
			if temp < 0:
				ans += -temp if temp < 0 else temp
				temp1 = 0
			else:
				temp1 += l[i]
		else:
			temp1 += l[i]
	print(ans)


import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
import sys
letters = ascii_letters
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	need = sum([i for i in arr if i >= 0])
	was = 0
	have = [0] * n
	for i in range(n):
		if i != 0:
			have[i] = have[i - 1]
		if arr[i] > 0:
			have[i] += arr[i]
	for i in range(n - 1, -1, -1):
		if arr[i] < 0:
			bf = min(abs(arr[i]), have[i] - was)
			was += bf
			need -= bf
		else:
			was = max(0, was - arr[i])
	print(need)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	res = 0
	temp = 0
	for i in a:
		if i + temp >= 0:
			temp += i
		else:
			res += abs(i) - temp
			temp = 0
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	mas = map(int, input().split())
	ans = 0
	for x in mas:
		ans = max(0, ans + x)
	print(ans)


import sys
from collections import defaultdict, Counter as CC
from math import sqrt, ceil, floor, log2
INF = float('inf')
MOD = 10 ** 9 + 7
read = lambda : [int(s) for s in sys.stdin.readline().split()]

def solve():
	(n,) = read()
	a = read()
	curr = 0
	for i in range(n):
		if a[i] > 0:
			curr += a[i]
		elif a[i] < 0:
			if curr > 0:
				curr = max(0, curr + a[i])
	print(curr)
_ = 1
_ = int(input())
while _:
	solve()
	_ -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	n = len(l)
	for i in range(n):
		if l[i] < 0:
			ans += l[i]
			if ans < 0:
				ans = 0
		else:
			ans += l[i]
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	for i in range(len(a)):
		if a[i] > 0:
			sum += a[i]
		elif sum < abs(a[i]):
			sum = 0
		else:
			sum -= abs(a[i])
	print(sum)


for i in range(int(input())):
	n = int(input())
	kol = 0
	mas = list(map(int, input().split()))
	ost = 0
	for i in mas:
		if i > 0:
			ost += 1
	ost1 = 0
	for i in range(n):
		if mas[i] < 0:
			if ost1 == 0:
				kol += abs(mas[i])
				mas[-1] -= abs(mas[i])
			elif ost1 < abs(mas[i]):
				k = abs(mas[i]) - ost1
				kol += k
				ost1 = 0
				mas[-1] -= k
			else:
				ost1 -= abs(mas[i])
		else:
			ost1 += mas[i]
	print(kol)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	x = []
	if n == 1:
		print(0)
	else:
		x = []
		temp = 0
		if arr[0] < 0:
			x = [arr[0]]
			for i in range(1, n):
				temp += arr[i]
				if temp < 0:
					x.append(temp)
					temp = 0
			x.append(temp)
		else:
			for i in range(0, n):
				temp += arr[i]
				if temp < 0:
					x.append(temp)
					temp = 0
			x.append(temp)
		s = 0
		for i in range(len(x)):
			if x[i] > 0:
				s += x[i]
		print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	s = 0
	m = 10 ** 15
	for i in range(n):
		s += l[i]
		if m > s:
			m = s
	ans = abs(m)
	print(ans)


import sys
sys.setrecursionlimit(10000)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	cost = 0
	toinc = 0
	for i in range(n):
		if a[i] < 0:
			if toinc >= abs(a[i]):
				toinc += a[i]
				a[i] = 0
			else:
				a[i] += toinc
				toinc = 0
				cost -= a[i]
				a[i] = 0
		elif a[i] > 0:
			toinc += a[i]
			a[i] = 0
	print(cost)


t = int(input())
for _ in range(t):
	n = int(input())
	lol = [int(n) for n in input().split()]
	ans = 0
	for i in range(n):
		if lol[i] > 0:
			ans += lol[i]
		elif lol[i] < 0:
			if ans > 0:
				if abs(ans) > abs(lol[i]):
					ans -= abs(lol[i])
					lol[i] = 0
				else:
					lol[i] += ans
					ans = 0
	print(ans)


for t in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split(' ')]
	p = 0
	n = 0
	for i in l:
		if i < 0:
			x = min([p, -1 * i])
			i += x
			p -= x
			n += i
		else:
			p += i
	print(-1 * n)


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	sm = 0
	for j in range(n):
		sm = max(0, sm + a[j])
	print(sm)


randuuid = '0dc8bb0d-3f6d-191e-a5bf-dc448f8da264'
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	mrp = -1
	mln = n
	for i in range(n):
		if i < mln and a[i] < 0:
			mln = i
		if i > mrp and a[i] > 0:
			mrp = i
	mon = 0
	for i in range(n):
		while a[i] != 0:
			if a[i] < 0:
				while a[mrp] <= 0 and mrp > 0:
					mrp -= 1
				x = min(-a[i], a[mrp])
				a[i] += x
				a[mrp] -= x
				if mrp > i:
					mon += x
			else:
				while a[mln] >= 0:
					mln += 1
				x = min(a[i], -a[mln])
				a[i] -= x
				a[mln] += x
				if mln < i:
					mon += x
	print(mon)


import math
for z in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	(pos, neg) = (0, 0)
	for i in range(n):
		if a[i] < 0:
			if pos > 0:
				k = min(pos, abs(a[i]))
				if k == pos:
					pos = 0
					neg += abs(a[i]) - k
				else:
					pos -= abs(a[i])
			else:
				neg += abs(a[i])
		else:
			pos += a[i]
	print(pos)


for _ in range(int(input())):
	n = int(input())
	v = list(map(int, input().split()))
	val = 0
	v = v[::-1]
	for i in range(len(v)):
		if v[i] > 0:
			if v[i] > abs(val):
				val = 0
			else:
				val += v[i]
		else:
			val += v[i]
	print(-1 * val)


num_t = int(input())
answers = list()
for test in range(num_t):
	n = int(input())
	arr = list(map(int, input().split()))
	minus = list()
	plus = list()
	for i in range(len(arr)):
		if arr[i] < 0:
			minus.append(i)
		elif arr[i] > 0:
			plus.append(i)
	while len(minus) > 0 and len(plus) > 0 and (minus[len(minus) - 1] > plus[0]):
		if plus[len(plus) - 1] > minus[len(minus) - 1]:
			plus.pop(len(plus) - 1)
			continue
		a = abs(arr[minus[len(minus) - 1]])
		b = arr[plus[len(plus) - 1]]
		if a > b:
			arr[plus[len(plus) - 1]] = 0
			plus.pop(len(plus) - 1)
			arr[minus[len(minus) - 1]] += b
		elif b > a:
			arr[minus[len(minus) - 1]] = 0
			minus.pop(len(minus) - 1)
			arr[plus[len(plus) - 1]] -= a
		else:
			arr[plus[len(plus) - 1]] = 0
			arr[minus[len(minus) - 1]] = 0
			minus.pop(len(minus) - 1)
			plus.pop(len(plus) - 1)
	summ = 0
	for i in arr:
		if i > 0:
			summ += i
	answers.append(summ)
for i in answers:
	print(i)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = s = sum(l)
	for e in l:
		s = s - e
		m = max(m, s)
	print(m)


import sys
import time as mt, bisect, sys
import math
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict

def si():
	return int(sys.stdin.readline())

def st():
	return stdin.readline()

def mi():
	return map(int, sys.stdin.readline().split())

def lis():
	return list(map(int, sys.stdin.readline().split()))

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)
t = si()
for _ in range(t):
	n = si()
	a = lis()
	min = 0
	s = 0
	for i in range(n):
		s = s + a[i]
		if s < min:
			min = s
	print(abs(min))


import os
import sys
from io import BytesIO, IOBase

def solution(arr, n):
	(pos, res) = ([], 0)
	for i in range(n):
		if arr[i] > 0:
			pos.append(i)
		while arr[i] < 0 and pos:
			idx = pos[-1]
			val = arr[idx]
			arr[idx] -= val
			arr[i] += val
			pos.pop()
			if arr[i] > 0:
				pos.append(i)
	for x in arr:
		if x > 0:
			break
		res += x
	write(-res)

def main():
	for _ in range(r_int()):
		n = r_int()
		arr = r_array()
		solution(arr, n)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


from sys import *
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	s = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		s += a[i]
		if s > 0:
			ans += s
			s = 0
	print(ans)


t = int(input())
while t:
	n = int(input())
	li = [int(x) for x in input().split()]
	s = 0
	m = 0
	for i in range(n - 1, -1, -1):
		s = s + li[i]
		if s > m:
			m = s
	print(m)
	t = t - 1


t = int(input())
for tc in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()][::-1]
	negsum = 0
	for i in range(n):
		if arr[i] < 0:
			negsum -= arr[i]
			arr[i] = 0
		elif arr[i] < negsum:
			negsum -= arr[i]
			arr[i] = 0
		else:
			arr[i] -= negsum
			negsum = 0
	print(negsum)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve(case):
	n = int(inp())
	a = lis()
	d = 0
	for i in range(n - 1, -1, -1):
		if a[i] == 0:
			continue
		if a[i] < 0:
			d += -a[i]
		elif d >= a[i]:
			d -= a[i]
			a[i] = 0
		else:
			a[i] -= d
			d = 0
	ans = 0
	for i in range(n):
		if a[i] < 0:
			continue
		ans += a[i]
	print(ans)
testcase(int(inp()))


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = [int(d) for d in sys.stdin.readline().split()]
	neg = 0
	ans = 0
	for i in range(len(arr) - 1, -1, -1):
		neg += abs(arr[i] if arr[i] < 0 else 0)
		if arr[i] > 0:
			if neg >= arr[i]:
				neg -= arr[i]
			else:
				ans += arr[i] - neg
				neg = 0
	print(ans)


t_case = int(input())
for _ in range(t_case):
	l = int(input())
	arr = list(map(int, input().split()))
	(i, j) = (0, 0)
	while i < l and j < l:
		if arr[i] > 0:
			if arr[j] < 0 and i < j:
				if arr[i] < -1 * arr[j]:
					arr[j] += arr[i]
					arr[i] = 0
				else:
					arr[i] += arr[j]
					arr[j] = 0
			else:
				j += 1
		else:
			i += 1
	cost = 0
	for i in range(l):
		if arr[i] > 0:
			cost += arr[i]
	print(cost)


def run():
	output = []
	for i in range(int(input())):
		_ = input()
		lst = [int(i) for i in input().split()]
		positive_sum = 0
		for (j, v) in enumerate(lst):
			if v > 0:
				positive_sum += v
			elif abs(v) > positive_sum:
				positive_sum = 0
			else:
				positive_sum -= abs(v)
		output.append(str(positive_sum))
	print('\n'.join(output))

def find_sum(n: list):
	c = 0
	for i in n:
		if i > 0:
			c += i
	return c
run()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = 0
	for i in range(n):
		if arr[i] > 0:
			pos += arr[i]
		elif pos > 0:
			if pos > -arr[i]:
				pos += arr[i]
				arr[i] = 0
			else:
				arr[i] += pos
				pos = 0
	sum = 0
	for i in range(n):
		if arr[i] < 0:
			sum += arr[i]
	print(-sum)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	positive = 0
	coin = 0
	for i in range(n):
		if ar[i] > 0:
			positive += ar[i]
		elif ar[i] < 0:
			if positive + ar[i] < 0:
				coin += positive + ar[i]
			positive += ar[i]
			if positive < 0:
				positive = 0
	print(abs(coin))


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	flag = 0
	ans = []
	sumA = 0
	for i in range(N):
		sumA += A[i]
		ans.append(sumA)
	print(abs(min(ans)))


def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	neg = 0
	pos = 0
	for (i, x) in enumerate(a):
		if x > 0:
			pos += x
		if x < 0:
			x = -x
			if pos < x:
				x -= pos
				pos = 0
				neg += x
			else:
				pos -= x
	return abs(neg)
T = int(input())
for case in range(T):
	print(solve())


for aww in range(int(input())):
	a = int(input())
	b = [*map(int, input().split())]
	c = 0
	if a == 1:
		print(b[0])
	else:
		for i in range(a):
			if b[i] > 0:
				c += b[i]
			else:
				c = max(0, c + b[i])
		print(c)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	curr = 0
	for i in arr:
		curr = max(0, curr + i)
	print(curr)


from itertools import accumulate
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	a = accumulate(a)
	print(max(0, max(a)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	neg_avail = 0
	pos_remain = 0
	for i in range(n - 1, -1, -1):
		if a[i] < 0:
			neg_avail += abs(a[i])
		else:
			take = min(neg_avail, a[i])
			neg_avail -= take
			pos_remain += a[i] - take
	print(pos_remain)


t = int(input())
for case in range(t):
	run_sum = 0
	aboves = 0
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	for i in range(1, n + 1):
		run_sum += a[-i]
		aboves = max(aboves, run_sum)
	print(aboves)


from bisect import bisect_right
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for j in range(n):
		if a[j] < 0:
			break
	for i in range(n):
		if a[i] > 0:
			j = max(i + 1, j)
			while True:
				if j == n:
					break
				if a[j] < 0:
					m = min(a[i], -a[j])
					a[i] -= m
					a[j] += m
					if a[i] == 0:
						break
				j += 1
			if j == n:
				break
	ans = 0
	for i in range(n):
		if a[i] > 0:
			ans += a[i]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	x = [int(x) for x in input().split()]
	a = [0] * n
	a[-1] = x[-1]
	for j in range(len(x) - 2, -1, -1):
		a[j] = x[j] + a[j + 1]
	print(max(a))


for _ in range(int(input())):
	n = int(input())
	array = [int(i) for i in input().split()]
	total_positives = 0
	needed_coins = 0
	for i in range(n):
		if array[i] >= 0:
			total_positives += array[i]
		elif array[i] + total_positives == 0:
			total_positives = 0
		elif -array[i] > total_positives:
			needed_coins -= array[i] + total_positives
			total_positives = 0
		else:
			total_positives += array[i]
	print(needed_coins)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	add = 0
	cost = 0
	for j in range(n):
		if l[j] < 0:
			l[j] = abs(l[j])
			if add <= l[j]:
				l[j] -= add
				add = 0
			else:
				add -= l[j]
				l[j] = 0
			cost += l[j]
		else:
			add += l[j]
	print(cost)


import math
import re
import itertools
import copy
import time
import sys

def input_linear(lamb=lambda x: int(x), seperator=' '):
	return map(lamb, input().split(seperator))

def input_array(lamb=lambda x: int(x), seperator=' '):
	return list(map(lamb, input().split(seperator)))

def input_matrix(rows, lamb=lambda x: int(x), seperator=' '):
	tmp_matrix = []
	for i in range(rows):
		tmp_matrix.append(input_array(lamb, seperator))
	return tmp_matrix

def input_graph_al_pair(vertices, lines, lamb=lambda x: int(x), seperator=' ', directional=False, remove_dup=False):
	temp_matrix = [x[:] for x in [[]] * vertices]
	if not directional:
		if not remove_dup:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				temp_matrix[ina].append(inb)
				temp_matrix[inb].append(ina)
		else:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				if inb not in temp_matrix[ina]:
					temp_matrix[ina].append(inb)
				if ina not in temp_matrix[inb]:
					temp_matrix[inb].append(ina)
		return temp_matrix
	else:
		if not remove_dup:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				temp_matrix[ina].append(inb)
		else:
			for i in range(lines):
				(ina, inb) = input_linear(lamb, seperator)
				if inb not in temp_matrix[ina]:
					temp_matrix[ina].append(inb)
		return temp_matrix
for _ in range(int(input())):
	n = int(input())
	a = input_array()
	p = 0
	ne = 0
	for i in range(n):
		if a[i] >= 0:
			p += a[i]
		else:
			pd = p
			p = max(0, a[i] + p)
			ne = ne + min(0, a[i] + pd)
	print(p)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	cost = 0
	pos = 0
	neg = 0
	for i in range(n - 1, -1, -1):
		if s[i] > 0:
			pos += s[i]
			if neg <= pos:
				pos -= neg
				neg = 0
			else:
				neg -= pos
				pos = 0
		elif s[i] < 0:
			neg += -1 * s[i]
			cost += pos
			pos = 0
	if pos:
		cost += pos
	print(cost)


from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
mo = 1000000007
for _ in range(inn()):
	n = inn()
	a = arr()
	j = n - 1
	bal = 0
	for i in range(n):
		if a[i] >= 0:
			bal += a[i]
			a[i] = 0
		else:
			mn = min(abs(a[i]), bal)
			a[i] += mn
			bal -= mn
	ans = bal
	for i in a:
		ans += abs(i)
	print(ans // 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = 0
	for i in range(n):
		if a[i] > 0:
			p += a[i]
			continue
		if p > 0:
			p += max(-p, a[i])
	print(p)


gans = []
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	cur = 0
	ans = 0
	for i in range(n):
		if u[i] == 0:
			continue
		if u[i] > 0:
			cur += u[i]
		else:
			u[i] = -u[i]
			if cur > u[i]:
				cur -= u[i]
			else:
				ans += u[i] - cur
				cur = 0
	gans.append(ans)
print('\n'.join(map(str, gans)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	tot = 0
	for i in range(n):
		if a[i] > 0:
			tot += a[i]
		elif a[i] < 0:
			if tot <= abs(a[i]):
				a[i] += tot
				tot = 0
			else:
				tot -= abs(a[i])
				a[i] = 0
	ans = 0
	for i in range(n):
		if a[i] < 0:
			ans += abs(a[i])
	print(ans)


try:
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		ans = 0
		count_pos = 0
		for i in l:
			if i >= 0:
				count_pos += i
			elif count_pos == 0:
				ans += abs(i)
			else:
				count_pos = count_pos + i
			if count_pos < 0:
				ans += abs(count_pos)
				count_pos = 0
		print(ans)
except:
	pass


import sys

def main():
	tn = int(input())
	for ti in range(tn):
		n = int(input())
		A = [int(a) for a in input().split()]
		ans = 0
		c = 0
		for (i, a) in enumerate(A):
			if i == n - 1:
				break
			c = a + c
			if c >= 0:
				continue
			ans += abs(c)
			c = 0
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = 0
	for j in l:
		x = x + j
		x = max(0, x)
	print(x)


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for i in arr:
		ans = max(ans + i, 0)
	print(ans)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	for i in a:
		if i < 0:
			c = max(0, c + i)
		else:
			c = c + i
	print(c)


import sys
from typing import List, Union

def rl(int_: bool=True, is_split: bool=True) -> Union[List[str], List[int]]:
	if int_:
		return [int(w) for w in sys.stdin.readline().split()]
	if is_split:
		return [w for w in sys.stdin.readline().split()]
	return sys.stdin.readline().strip()
for _ in range(rl()[0]):
	(n,) = rl()
	a = rl()
	coins = 0
	total = 0
	for x in a:
		total += x
		if total < 0:
			coins = max(coins, abs(total))
	print(coins)


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in range(n):
		s += A[i]
		if s < 0:
			ans += -s
			s = 0
	print(ans)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict, Counter
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def inp():
		return int(input())

	def minp():
		return map(int, input().split())
else:

	def inp():
		return int(stdin.readline())

	def minp():
		return map(int, stdin.readline().split())
for _ in range(inp()):
	n = inp()
	a = list(minp())
	new = [0] * n
	new[0] = a[0]
	for i in range(1, n):
		new[i] = a[i] + new[i - 1]
	print(abs(min(new)))


import sys
t = int(input())
for _ in range(t):
	n = sys.stdin.readline()
	arr = list(map(int, sys.stdin.readline().split()))
	saveNum = 0
	ans = 0
	for x in arr:
		if x >= 0:
			saveNum += x
		elif saveNum + x > 0:
			saveNum += x
			x = 0
		else:
			x += saveNum
			saveNum = 0
		if x < 0:
			ans += abs(x)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	f = 0
	c = 0
	for i in a:
		c += i
		if c < 0:
			l.append(-c)
			c = 0
	print(sum(l))


list_ans = []
for _ in range(int(input())):
	n = int(input())
	list1 = list(map(int, input().split()))
	a = 0
	b = 0
	for x in list1:
		a += x
		if a < b:
			b = a
	list_ans.append(-b)
for item in list_ans:
	print(item)


t = int(input())
while t:
	t -= 1
	n = int(input())
	A = [int(pi) for pi in input().split()]
	cummp = 0
	for ai in A:
		if ai == 0:
			continue
		if ai < 0:
			cummp = max(cummp + ai, 0)
			continue
		else:
			cummp += ai
	print(cummp)


for _ in range(int(input())):
	n = int(input())
	k = [int(j) for j in input().split()]
	l = []
	t = 0
	for j in range(n - 1, -1, -1):
		t = t + k[j]
		l.append(t)
	print(max(l))


import sys
t = int(input())
while t > 0:
	t -= 1
	n = int(sys.stdin.readline())
	input = sys.stdin.readline()
	arr = list(map(int, input.split()))
	ans = 0
	prev = 0
	for x in arr:
		prev = prev + x
		if prev < 0:
			ans += abs(prev)
			prev = 0
	print(ans)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(inputi())
	aa = [int(a) for a in inputi().split()]
	aas = 0
	aamn = 0
	for a in aa:
		aas += a
		aamn = min(aamn, aas)
	print(-aamn)

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


import sys, math, cmath, time
start_time = time.time()

def solve():
	n = inp()
	a = inlt()
	a.reverse()
	q = 0
	for i in range(len(a)):
		q = max(0, q - a[i])
	print(q)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


import sys
import math
input = sys.stdin.readline

def get_int():
	return int(input())

def get_int_list():
	return list(map(int, input().split()))

def get_char_list():
	return list(input())
cases = get_int()
for _ in range(cases):
	n = get_int()
	ints = get_int_list()
	pos = 0
	result = 0
	for v in ints:
		if v < 0:
			if -v > pos:
				result -= pos + v
				pos = 0
			else:
				pos += v
		else:
			pos += v
	print(result)


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	tot = 0
	for i in range(-1, -n - 1, -1):
		if lst[i] > 0:
			tot = max(tot - lst[i], 0)
		else:
			tot -= lst[i]
	print(tot)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	s = 0
	for i in a:
		s += i
		res = min(res, s)
	print(abs(res))


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	l = list(ac(zzz()))
	print(abs(min(l)))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	moves = 0
	for i in range(n - 1):
		if arr[i] < 0:
			moves += abs(arr[i])
			arr[-1] -= abs(arr[i])
		else:
			arr[i + 1] += abs(arr[i])
	print(moves)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	res = 0
	for i in range(n):
		c += a[i]
		if c < 0:
			res = max(res, abs(c))
	print(res)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	x = 0
	ans = 0
	for a in A:
		if x + a < 0:
			ans += -a - x
			x = 0
		else:
			x += a
	print(ans)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	if all((x == 0 for x in A)):
		return 0
	i = N - 1
	j = N - 1
	cost = 0
	while i >= 0:
		if A[i] > 0:
			while j > i and A[i]:
				if A[j] < 0:
					move = min(abs(A[i]), abs(A[j]))
					A[i] -= move
					A[j] += move
					if A[j] == 0:
						j -= 1
				else:
					j -= 1
		i -= 1
	return sum((x for x in A if x > 0))
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	nList = list(map(int, input().split()))
	for i in range(n - 1):
		if nList[i] > 0:
			nList[i + 1] += nList[i]
			nList[i] = 0
	ans = 0
	for i in nList:
		if i < 0:
			ans += -i
	print(ans)


t = int(input())
while t:
	n = int(input())
	c = []
	arr = [int(i) for i in input().split()]
	c.append(arr[-1])
	for i in range(n - 2, -1, -1):
		c.append(c[-1] + arr[i])
	print(max(c))
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	(c, ans) = (0, 0)
	for (k, v) in enumerate(A):
		c += v
		if c < 0:
			ans += abs(c)
			c = 0
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	r = 0
	for row in a:
		if row > 0:
			r += row
		else:
			r += row
		if r < 0:
			r = 0
	print(r)


import math
pi = math.acos(-1)
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	ans = 0
	for x in a:
		sum += x
		if sum < 0:
			ans = min(ans, sum)
	print(-1 * ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.reverse()
	mc = -10 ** 10
	c = 0
	for i in range(n):
		c += a[i]
		mc = max(c, mc)
	print(mc)


t = int(input())
for h in range(t):
	n1 = int(input())
	n = list(map(int, input().split()))
	srtp = 0
	srtn = 0
	for i in n:
		if i > 0:
			srtp += i
		if i < 0:
			if srtp > 0:
				if srtp - abs(i) < 0:
					srtn += abs(srtp - abs(i))
					srtp = 0
				else:
					srtp = srtp - abs(i)
			else:
				srtn += abs(i)
	print(srtn)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = b = 0
	for x in a:
		b += x
		if b < 0:
			ans -= b
			b = 0
	print(ans)


import math

def main():
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	cost = suffixSum = arr[-1]
	for i in reversed(range(n - 1)):
		suffixSum += arr[i]
		cost = max(cost, suffixSum)
	print(cost)
for _ in range(int(input())):
	main()


import sys, os, io
input = sys.stdin.readline
T = int(input())
ans = [0] * T
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	cost = 0
	yoyuu = 0
	for i in range(N):
		A[i] += yoyuu
		if A[i] < 0:
			cost -= A[i]
			yoyuu = 0
		else:
			yoyuu = A[i]
	ans[t] = cost
print(*ans, sep='\n')


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	t = 0
	for i in lst:
		t = max(0, t + i)
	print(t)


import sys
import random
from fractions import Fraction
from math import *

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def finput():
	return float(input())

def tinput():
	return input().split()

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

def fiinput():
	return map(float, tinput())

def flinput():
	return list(fiinput())

def rlinput():
	return list(map(int, input().split()))

def trinput():
	return tuple(rinput())

def srlinput():
	return sorted(list(map(int, input().split())))

def NOYES(fl):
	if fl:
		print('NO')
	else:
		print('YES')

def YESNO(fl):
	if fl:
		print('YES')
	else:
		print('NO')

def main():
	n = iinput()
	(ans, res) = (0, 0)
	for i in rlinput():
		ans += i * (i >= 0)
		if i < 0:
			ans -= min(ans, -i)
			res += (min(ans, -i) < -i) * (i + min(ans, -i))
	print(max(ans, res))
for TESTING in range(iinput()):
	main()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = a[::-1]
	ans = 0
	su = 0
	for i in range(n):
		if a[i] > 0:
			ans += a[i]
		else:
			temp = i
			break
	for i in range(temp, n):
		if su > 0:
			ans += su
			su = a[i]
		else:
			su += a[i]
	print(ans)


def solve():
	N = int(input())
	A = [int(s) for s in input().split()]
	best = 0
	cur = 0
	for i in range(N - 1, -1, -1):
		cur += A[i]
		best = max(best, cur)
	print(best)
	return
T = int(input())
for t in range(T):
	solve()


from math import gcd, ceil, log
from collections import Counter, deque
from heapq import heappush, heappop, heapify

def rl():
	return list(map(int, input().split()))

def arp(arr):
	return ''.join(arr)
for _ in range(int(input())):
	(n,) = rl()
	arr = rl()
	ans = 0
	for i in range(n):
		ans = max(0, ans + arr[i])
	print(ans)


for y in range(int(input())):
	a = int(input())
	finall = 0
	l = list(map(int, input().split()))
	for y in range(a - 1):
		if l[y + 1] >= 0 and l[y] >= 0:
			l[y + 1] = l[y] + l[y + 1]
			l[y] = 0
			continue
		if l[y] > 0 and l[y + 1] < 0:
			ans = l[y] + l[y + 1]
			if abs(l[y]) > abs(l[y + 1]):
				l[y] = ans
				l[y + 1] = 0
				if ans > 0:
					l[y + 1] = ans
					l[y] = 0
			else:
				l[y + 1] = ans
				l[y] = 0
	for y in l:
		if y > 0:
			finall += y
	print(finall)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = a[0]
	for i in range(1, n):
		if a[i] < 0:
			if x >= 0:
				x = x + a[i]
			else:
				x = 0
		elif x >= 0:
			x += a[i]
		else:
			x = a[i]
	print(x)


for p in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = 0
	for i in range(n):
		if a[i] > 0:
			b += a[i]
		else:
			b -= min(-a[i], b)
	print(b)


N = int(input())
Ans = []
for i in range(N):
	L = int(input())
	A = list(map(int, input().split()))
	B = A[::-1]
	total = 0
	c = 0
	for j in range(L):
		if B[j] < 0:
			total += abs(B[j])
		elif B[j] >= total:
			c += B[j] - total
			B[-1] += B[j] - total
			total = 0
		else:
			total -= B[j]
	Ans.append(c)
for i in Ans:
	print(i)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	for i in range(n - 1):
		if s[i] > 0:
			s[i + 1] += s[i]
			s[i] = 0
	print(max(s))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	res = 0
	for i in range(n):
		ans += l[i]
		if ans < 0:
			res += abs(ans)
			ans = 0
	print(res)


import sys
from math import sqrt
from collections import defaultdict
inpt = sys.stdin.readline

def inp():
	return int(inpt())

def inlist():
	return list(map(int, inpt().split()))

def instr():
	s = inpt()
	return list(s[:len(s) - 1])

def invr():
	return map(int, inpt().split())

class Graph:

	def __init__(self, n):
		self.n = n
		self.graph = defaultdict(list)
		self.marked = defaultdict(int)

	def addEdge(self, u, v):
		self.graph[u].append(v)
		self.graph[v].append(u)

	def dfs(self, u):
		self.marked[u] = 1
		for i in self.graph[u]:
			if self.marked[i] == 0:
				self.dfs(i)

def main():
	t = inp()
	for l in range(t):
		n = inp()
		a = inlist()
		ans = 0
		for i in range(n):
			ans += a[i]
			if ans < 0:
				ans = 0
		print(ans)
main()


import sys
import collections
T = int(sys.stdin.readline().strip())
for _ in range(T):
	n = int(sys.stdin.readline().strip())
	nums = list(map(int, sys.stdin.readline().strip().split()))
	pos = collections.deque([list(p) for p in enumerate(nums) if p[1] > 0])
	neg = collections.deque([[p[0], abs(p[1])] for p in enumerate(nums) if p[1] < 0])
	res = sum((n for n in nums if n > 0))
	while len(pos) and len(neg):
		(pos_ind, pos_n) = pos[0]
		(neg_ind, neg_n) = neg[0]
		if neg_ind <= pos_ind:
			neg.popleft()
			continue
		elif pos_n == 0:
			pos.popleft()
			continue
		elif neg_n == 0:
			neg.popleft()
			continue
		amt = min(pos_n, neg_n)
		res -= amt
		pos[0][1] -= amt
		neg[0][1] -= amt
	print(res)


def getIntList():
	return list(map(int, input().split()))

def getTransIntList(n):
	first = getIntList()
	m = len(first)
	result = [[0] * n for _ in range(m)]
	for i in range(m):
		result[i][0] = first[i]
	for j in range(1, n):
		curr = getIntList()
		for i in range(m):
			result[i][j] = curr[i]
	return result
t = int(input())
results = []
for _ in range(t):
	n = int(input())
	a = getIntList()
	result = 0
	currSum = 0
	for x in a:
		currSum += x
		result = min(currSum, result)
	results.append(-result)
for a in results:
	print(a)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = n - 1
	j = n - 1
	z = 0
	while 1:
		if a[j] >= 0 and j != 0:
			j -= 1
		if a[i] <= 0 and i != 0 or i > j:
			i -= 1
		if a[j] < 0 and a[i] > 0 and (j > i):
			c = min(abs(a[j]), abs(a[i]))
			a[j] += c
			a[i] -= c
		z += 1
		if z == 2 * n:
			break
	c = 0
	for i in range(n):
		c += abs(a[i])
	print(c // 2)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	pos = 0
	neg = 0
	net = 0
	cnt = 0
	for i in a:
		if i == 0:
			pass
		elif i > 0:
			pos += i
			net += i
		else:
			neg += i
			if net != 0:
				if net + i >= 0:
					net += i
				else:
					cnt += abs(i) - net
					net = 0
			else:
				cnt += abs(i)
	print(cnt)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = 0
	c = 0
	for j in a:
		b += j
		c = min(c, b)
	print(-c)


testcases = int(input())
lis = list()
for i in range(testcases):
	element = int(input())
	arr = list(map(int, input().split(' ')))
	lis.append(arr)

def coins(array):
	psums = []
	a = 0
	for i in range(len(array)):
		a += array[i]
		psums.append(a)
	return -min(psums)
for i in lis:
	print(coins(i))


import io, os
inp = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
inst = lambda : inp().decode().rstrip('\n\r')
_T_ = inin()
for _t_ in range(_T_):
	n = inin()
	a = inar()
	maxx = 0
	sum = 0
	for i in a[::-1]:
		sum += i
		maxx = max(maxx, sum)
	print(maxx)


def solve(n, arr):
	pos = 0
	for i in range(0, n):
		if arr[i] > 0:
			pos += arr[i]
		if arr[i] < 0:
			tmp = arr[i] + pos
			arr[i] = min(arr[i] + pos, 0)
			pos = max(0, tmp)
	ans = 0
	for i in range(len(arr)):
		if arr[i] < 0:
			ans += abs(arr[i])
	return ans
t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	print(solve(n, s))


for _ in range(int(input())):
	input()
	ans = 0
	for i in map(int, input().split()):
		ans = max(ans + i, 0)
	print(ans)


def tc():
	n = int(input())
	a = list(map(int, input().split()))
	pos = 0
	neg = 0
	ans = 0
	for x in a:
		if x >= 0:
			pos += x
		elif -x <= pos:
			pos -= -x
		else:
			ans += -x - pos
			(pos, neg) = (0, -x - pos)
	print(ans)
T = int(input())
for _ in range(T):
	tc()


from math import *
from cmath import *
from itertools import *
from decimal import *
from fractions import *
from sys import *
for _ in range(int(input())):
	n = int(input())
	cur = 0
	for x in input().split():
		cur = max(0, cur + int(x))
	print(cur)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		return
	ans = 0
	count = 0
	for x in range(n):
		count += a[x]
		if count < 0:
			ans -= count
			count = 0
	print(ans)
t = int(input())
for x in range(t):
	solve()


for _ in range(int(input())):
	w = int(input())
	myList = list(map(int, input().split()))
	for i in range(1, w):
		myList[i] += myList[i - 1]
	print(abs(min(myList)))


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = [int(x) for x in input().split()]
	prevpos = 0
	for i in range(n):
		if l1[i] > 0:
			prevpos += l1[i]
		elif l1[i] == 0:
			pass
		else:
			prevpos = max(0, prevpos + l1[i])
	print(prevpos)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	t = 0
	for i in range(n - 1):
		if a[i] >= 0:
			s += a[i]
		else:
			h = abs(a[i])
			if s - h < 0:
				a[-1] += s - h
				t += abs(s - h)
				s = 0
			else:
				s += a[i]
	print(t)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	neg_found = False
	neg = 0
	pos = 0
	for (i, a) in enumerate(arr[::-1], start=1):
		if a > 0:
			arr[-i] = 0 if neg + a < 0 else neg + a
			neg = neg + a if arr[-i] == 0 else 0
		else:
			neg = neg + (a if a < 0 else 0)
	sum = 0
	for a in arr:
		if a > 0:
			sum += a
	print(int(sum))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	sume = 0
	for i in range(n):
		if a[i] > 0:
			sume += a[i]
		else:
			sume = max(0, sume + a[i])
	print(sume)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	negative = 0
	free = 0
	maxi = 0
	for i in arr:
		if i > 0:
			maxi += i
	for i in range(1, len(arr) + 1):
		if arr[-i] < 0:
			negative += arr[-i]
		elif negative <= 0 and arr[-i] > abs(negative):
			free += abs(negative)
			negative = 0
		elif negative <= 0 and arr[-i] <= abs(negative):
			free += arr[-i]
			negative += arr[-i]
	print(str(maxi - free))


t = int(input())
q = 0
while q < t:
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	cur = 0
	while i < n:
		x = a[i]
		cur = max(0, cur + x)
		i += 1
	print(cur)
	q += 1


l = []
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = 0
	for i in range(n - 1, -1, -1):
		if m == 0 and a[i] >= 0:
			continue
		elif a[i] > 0 and m < 0:
			m = min(0, m + a[i])
		elif a[i] < 0:
			m = m + a[i]
	l.append(m)
for i in l:
	print(-i)


def readInt():
	return int(input())

def readList():
	return list(map(int, input().split()))

def readMap():
	return map(int, input().split())
inf = float('inf')
mod = 10 ** 9 + 7
import math

def solve(n, A):
	S = [A[n - 1]]
	for i in range(n - 2, -1, -1):
		S.append(S[-1] + A[i])
	S.reverse()
	return max(S)
t = readInt()
for _ in range(t):
	n = readInt()
	A = readList()
	print(solve(n, A))


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	cur = 0
	for i in range(n - 1, -1, -1):
		if l[i] >= 0:
			ans += max(0, l[i] + cur)
			cur += min(l[i], abs(cur))
		else:
			cur += l[i]
	print(ans)


t = int(input())
for i in range(t):
	l = int(input())
	arr = list(map(int, input().split()))
	bank = 0
	pos = 0
	coins = 0
	for _ in range(len(arr)):
		if arr[_] < 0:
			if pos >= abs(arr[_]):
				pos -= abs(arr[_])
			else:
				coins += abs(arr[_] - pos)
				bank += abs(arr[_] - pos)
				pos = 0
		elif arr[_] == 0:
			continue
		else:
			pos += arr[_]
	print(coins + pos - bank)


T = int(input())
for _ in range(T):
	n = int(input())
	x = list(map(int, input().split()))
	l = list()
	temp = 0
	for i in range(len(x)):
		if x[i] > 0:
			temp += x[i]
			continue
		if x[i] < 0 and temp == 0:
			l.append(x[i])
		elif x[i] < 0 and temp > 0:
			diff = temp - abs(x[i])
			if diff > 0:
				temp = diff
			else:
				l.append(temp - abs(x[i]))
				temp = 0
	print(abs(sum(l)))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()[:n]))
	ps = 0
	for item in a:
		if item < 0 and ps != 0:
			ps -= abs(item)
			if ps < 0:
				ps = 0
		elif item > 0:
			ps += item
	print(ps)


from sys import stdin

class Problem:

	def __init__(self, n, a):
		self.n = n
		self.a = a

	def solve(self):
		ri = 0
		ans = 0
		for le in range(n):
			if a[le] < 0:
				ans += abs(a[le])
			else:
				while ri <= le:
					ri += 1
				while ri < n and a[le] > 0:
					if a[ri] < 0:
						m = min(a[le], abs(a[ri]))
						a[le] -= m
						a[ri] += m
					if a[ri] >= 0:
						ri += 1
		return ans
cases = int(stdin.readline())
for test in range(cases):
	(n,) = list(map(int, stdin.readline().split()))
	a = list(map(int, stdin.readline().split()))
	ans = Problem(n, a).solve()
	print(ans)


from math import *
from collections import *
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	pos = 0
	neg = 0
	for i in range(n):
		if arr[i] > 0:
			pos += arr[i]
		if arr[i] < 0:
			if pos > 0:
				pos += arr[i]
				if pos < 0:
					pos = 0
	print(pos)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	av = 0
	for i in range(n):
		if a[i] < 0:
			cr = min(-1 * a[i], av)
			a[i] += cr
			av -= cr
			cnt += a[i]
			a[i] = 0
		else:
			av += a[i]
	print(-1 * cnt)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	currp = 0
	for i in arr:
		if i > 0:
			currp += i
		elif currp < abs(i):
			ans += abs(i) - currp
			currp = 0
		else:
			currp -= abs(i)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = map(int, input().split(' '))
	count = 0
	for i in a:
		count = max(0, count + i)
	print(count)


import sys
pprint = lambda s: print(' '.join(map(lambda x: str(x), s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
mod = 1000000007
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in a:
		if i < 0:
			d = max(0, s + i)
			ans += max(0, -s - i)
			s = d
		else:
			s += i
	print(ans)


t = int(input().split()[0])
for _ in range(t):
	n = input()
	a = [int(i) for i in input().split()]
	counter = 0
	cost = 0
	for v in a:
		if v > 0:
			counter += v
		else:
			if counter > 0:
				use = min(abs(v), counter)
				v += use
				counter -= use
			cost += abs(v)
	print(cost)


try:
	for _ in range(int(input())):
		n = int(input())
		A = list(map(int, input().split()))
		ans = 0
		tn = 0
		start = 1
		for i in A:
			if tn < 0:
				ans += abs(tn)
				tn = 0
			if i < 0:
				if tn <= 0:
					ans += abs(i)
				else:
					tn += i
			elif tn < 0:
				tn = i
			else:
				tn += i
		print(abs(ans))
except:
	pass


t = int(input())
test = []
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	test.append([n, arr])
for i in range(t):
	n = test[i][0]
	arr = test[i][1]
	price = 0
	gain = 0
	for j in range(n):
		if arr[j] < 0:
			a = -arr[j]
			price += abs(min(gain - a, 0))
			gain = max(gain - a, 0)
		else:
			gain += arr[j]
	print(price)


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()
for _ in range(int(input())):
	q = int(input())
	w = list(map(int, input().split()))
	k = 0
	for i in range(q):
		if k > 0 and w[i] < 0:
			k = max(0, k + w[i])
		if w[i] > 0:
			k += w[i]
	print(k)


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	remaining_posive = 0
	for val in arr:
		remaining_posive += val
		if remaining_posive < 0:
			ans += remaining_posive * -1
			remaining_posive = 0
	print(ans)


t = int(input())
for ii in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dec = 0
	pos = 0
	for i in range(n - 1, -1, -1):
		if a[i] > 0:
			if dec > a[i]:
				dec -= a[i]
				a[i] = 0
			else:
				a[i] -= dec
				dec = 0
				pos += a[i]
		else:
			dec += abs(a[i])
	print(pos)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(total_ve, total_ne) = (0, 0)
	start_ne = 0
	check = True
	for i in arr:
		if i <= 0:
			if check:
				start_ne += i
			else:
				total_ne += i
		else:
			total_ve += i
			check = False
		if total_ve > abs(total_ne):
			(total_ve, total_ne) = (total_ve + total_ne, 0)
		else:
			start_ne += total_ve + total_ne
			(total_ve, total_ne) = (0, 0)
			check = True
	print(abs(start_ne))


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	n = int(inp())
	arr = lmp()
	c = 0
	for i in range(n):
		if arr[i] > 0:
			c += arr[i]
		else:
			c -= min(c, -arr[i])
	print(c)


for _ in range(int(input())):
	num = int(input())
	arr = list(map(int, input().split()))
	count = 0
	for i in arr:
		count = max(0, i + count)
	print(count)


from math import ceil
from collections import deque
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	s = 0
	for i in range(n):
		if a[i] < s:
			ans += s - a[i]
			s = 0
		else:
			s -= a[i]
	print(ans)


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	for _ in range(ii()):
		n = ii()
		a = li()
		s = 0
		for i in range(n - 1, -1, -1):
			if a[i] < 0:
				s += abs(a[i])
			elif s > 0:
				s = max(s - a[i], 0)
		print(s)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	N = I()
	A = Neo()
	(inc, Ans) = (0, 0)
	for i in range(N):
		if A[i] >= 0:
			inc += A[i]
		else:
			inc += A[i]
			Ans += min(0, inc)
			inc = max(inc, 0)
	print(abs(Ans))


import sys
pin = sys.stdin.readline
for t in range(int(pin())):
	N = int(pin())
	A = [*map(int, pin().split())]
	pos = 0
	for i in range(N):
		if A[i] < 0:
			if pos + A[i] < 0:
				A[i] += pos
				pos = 0
			else:
				pos += A[i]
				A[i] = 0
		else:
			pos += A[i]
			A[i] = 0
	print(pos)


t = int(input())
for i in range(t):
	a = int(input())
	b = [int(s) for s in input().split()]
	s = 0
	t = 0
	for k in range(len(b)):
		if s + b[k] >= 0:
			s += b[k]
		else:
			t += abs(b[k]) - s
			s -= min(s, abs(b[k]))
	print(t)


def run_case():
	n = int(input())
	nums = list(map(int, input().split()))
	stack = []
	for i in range(n):
		if nums[i] < 0 and stack:
			while stack:
				if stack[-1] + nums[i] < 0:
					nums[i] += stack.pop()
				elif stack[-1] + nums[i] > 0:
					stack[-1] += nums[i]
					nums[i] = 0
					break
				else:
					nums[i] = 0
					stack.pop()
					break
		elif nums[i] > 0:
			stack.append(nums[i])
		else:
			continue
	print(-sum([i for i in nums if i < 0]))
flag = 1
if flag:
	t = int(input())
	for i in range(t):
		run_case()
else:
	run_case()


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		r = 0
		t = 0
		for c in a:
			t += c
			if t < 0:
				r -= t
				t = 0
		rr.append(r)
	return JA(rr, '\n')
print(main())


for case in range(int(input())):
	N = int(input())
	arr = list(map(int, input().split()))
	if N == 1:
		print(0)
	else:
		sol = []
		carry = 0
		for i in range(N):
			if carry == 0 and arr[i] < 0:
				sol.append(arr[i])
			elif carry != 0 and arr[i] < 0:
				carry += arr[i]
				if carry < 0:
					sol.append(carry)
					carry = 0
			elif arr[i] > 0:
				carry += arr[i]
		print(abs(sum(sol)))


t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	leftpositive = 0
	leftnegative = 0
	for j in range(len(b) - 1):
		if b[j] > 0 and b[j + 1] < 0:
			if abs(b[j]) > abs(b[j + 1]):
				b[j] = b[j] + b[j + 1]
				b[j + 1] = 0
				leftpositive = leftpositive + b[j]
			elif abs(b[j]) < abs(b[j + 1]):
				b[j + 1] = b[j + 1] + b[j]
				b[j] = 0
			else:
				b[j] = 0
				b[j + 1] = 0
		elif b[j] < 0 and leftpositive > 0:
			if abs(b[j]) > leftpositive:
				b[j] = b[j] + leftpositive
				leftnegative = leftnegative + b[j]
				leftpositive = 0
			elif abs(b[j]) < leftpositive:
				leftpositive = leftpositive + b[j]
				b[j] = 0
			else:
				leftpositive = 0
				b[j] = 0
		elif b[j] > 0 and b[j + 1] >= 0:
			leftpositive = leftpositive + b[j]
		else:
			leftnegative = leftnegative + b[j]
	print(abs(leftnegative), end='\n')


import sys
import itertools
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	print(-min(itertools.accumulate(map(int, input().split()))))


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	for i in range(n):
		if a[i] <= 0:
			if abs(a[i]) <= c:
				c += a[i]
				a[i] = 0
			else:
				a[i] += c
				c = 0
		else:
			c += a[i]
	c = 0
	for i in range(n):
		if a[i] < 0:
			c -= a[i]
	print(c)
	t -= 1


from sys import stdin, stdout
mp = lambda : map(int, stdin.readline().split())
li = lambda : list(map(int, stdin.readline().split()))
for _ in range(int(input())):
	n = int(input())
	a = li()
	(n, p) = (0, 0)
	for i in a:
		if i > 0:
			p += i
		elif i < 0:
			if p > 0:
				if p > i:
					p += i
				else:
					p = 0
					n = i + p
				if p < 0:
					n += p
					p = 0
			else:
				n += i
	print(max(abs(n), p))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = []
	c = 0
	for i in range(n):
		if a[i] > 0:
			c += a[i]
			b.append(a[i])
		if a[i] != []:
			if a[i] < 0:
				c -= min(c, abs(a[i]))
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = 0
	m = 0
	while True:
		while p < n and a[p] <= 0:
			p += 1
		if p == n:
			break
		while m < p:
			m += 1
		while m < n and a[m] >= 0:
			m += 1
		if m == n:
			break
		if a[p] + a[m] < 0:
			a[m] += a[p]
			a[p] = 0
		else:
			a[p] += a[m]
			a[m] = 0
	f = lambda i: max(i, 0)
	print(sum([f(i) for i in a]))


import sys
from operator import itemgetter
input = sys.stdin.readline
from collections import defaultdict as dc
from bisect import bisect_left, bisect_right
import math
for n_ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	for i in range(n):
		if l[i] < 0:
			c += abs(l[i])
		else:
			break
	j = i
	s = 0
	while j < n:
		s += l[j]
		j += 1
		while j < n and l[j] >= 0:
			s += l[j]
			j += 1
		p = 0
		while j < n and l[j] < 0:
			p += l[j]
			j += 1
		if abs(p) <= s:
			s = s - abs(p)
		else:
			c += abs(p) - s
			s = 0
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = n - 2
	for j in range(n - 1, -1, -1):
		if a[j] < 0:
			val = abs(a[j])
			p = min(p, j - 1)
			while p >= 0:
				if a[p] > 0 and val - a[p] >= 0:
					val -= a[p]
					a[p] = 0
				elif val - a[p] < 0:
					a[p] -= val
					val = 0
					break
				p -= 1
			a[j] = -1 * abs(val)
	cnt = 0
	for j in range(n):
		if a[j] > 0:
			cnt += a[j]
	print(cnt)


t = int(input())
while t > 0:
	t -= 1
	N = int(input())
	line = input().split(' ')
	numbers = []
	lp = 0
	ln = 0
	rp = 0
	rn = 0
	p = []
	n = []
	for i in line:
		num = int(i)
		numbers.append(num)
		if num > 0:
			lp += num
		if num < 0:
			m = min(lp, abs(num))
			lp -= m
			ln += num
			ln += m
	print(max(abs(lp), abs(ln)))


from math import ceil, floor, log, sqrt, factorial
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	temp = 0
	f = 0
	for i in range(n):
		if a[i] > 0 and i != n - 1:
			temp += a[i]
			a[i] = 0
			f = 1
		elif f and a[i] < 0:
			if temp + a[i] >= 0:
				temp += a[i]
				a[i] = 0
			else:
				a[i] += temp
				temp = 0
	c = 0
	for i in a:
		if i < 0:
			c += i
	print(abs(c))


from sys import stdin, stdout
for i in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().strip().split()))
	(r, l) = (0, 0)
	for i in range(n):
		if a[i] > 0:
			r = r + a[i]
		else:
			r = r + a[i]
			if r < 0:
				l = l + r
				r = 0
	print(abs(l))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		if arr[i] > 0:
			ans += arr[i]
		else:
			c = min(abs(ans), abs(arr[i]))
			ans -= c
			arr[i] += c
	ans = 0
	for i in arr:
		if i < 0:
			ans += abs(i)
	print(ans)


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def inin():
	return int(input())

def stin():
	return input()

def spin():
	return map(int, stin().split())

def lin():
	return list(map(int, stin().split()))

def matrix(n):
	return [list(map(int, input().split())) for i in range(n)]

def string2intlist(s):
	return list(map(int, s))

def calculate_sum(a, N):
	m = N / a
	sum = m * (m + 1) / 2
	ans = a * sum
	return ans

def series(N):
	return N * (N + 1) // 2

def count2Dmatrix(i, list):
	return sum((c.count(i) for c in list))

def modinv(n, p):
	return pow(n, p - 2, p)

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def LCM(x, y):
	return x * y // GCD(x, y)

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def isprime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
prime = []

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j
for _ in range(inin()):
	n = inin()
	a = lin()
	m = 0
	for i in a[::-1]:
		m += i
		if m >= 0:
			m = 0
	print(abs(m))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	for i in a:
		if i > 0:
			cnt += i
		else:
			cnt += i
			if cnt < 0:
				cnt = 0
	print(cnt)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	bud = 0
	coins = 0
	for i in range(n):
		if a[i] > 0:
			bud += a[i]
		elif bud > -a[i]:
			bud += a[i]
		else:
			coins += -a[i] - bud
			bud = 0
	print(coins)


def cost(arr):
	current = 0
	for i in range(len(arr)):
		if arr[i] > 0 or current != 0:
			current += arr[i]
		if current < 0:
			current = 0
	return current
T = int(input())
for t in range(T):
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	print(cost(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	for i in a:
		ans = max(0, ans + i)
	print(ans)


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	tot = 0
	ne = []
	for i in range(0, n):
		s += l[i]
		ne.append(s)
	tot = min(ne)
	print(abs(tot))
	t = t - 1


T = int(input())
while T:
	n = int(input())
	A = list(map(int, input().split()))
	i = 0
	S = 0
	while i < n:
		if S > 0 or (S == 0 and A[i] >= 0):
			S += A[i]
			S = max(S, 0)
		i = i + 1
	print(S)
	T = T - 1


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(s, mn) = (0, 0)
	for i in range(n):
		s = s + a[i]
		if s < mn:
			mn = s
	if mn < 0:
		mn = mn * -1
	print(mn)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for i in range(t):
	n = inp()
	l = inlt()
	s = 0
	res = float('inf')
	for num in l:
		s += num
		res = min(res, s)
	print(max(-res, 0))


def Test():
	t = int(input())
	while t > 0:
		t -= 1
		soln()

def soln():
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	su = 0
	cum = 0
	for i in range(n):
		cum += a[i]
		if cum < 0:
			su += cum
			cum = 0
	print(su * -1)
Test()


t = int(input())
for i in range(t):
	n = int(input())
	ar = [int(k) for k in input().split()]
	ans = 0
	sm = 0
	for k in ar:
		sm += k
		if sm < 0:
			ans += sm
			sm = 0
	print(-1 * ans)


for _ in range(int(input())):
	N = int(input())
	sum = 0
	List = [int(x) for x in input().split()]
	for i in List:
		if i > 0:
			sum += i
	Plus = 0
	count = 0
	for i in range(N):
		if List[i] > 0:
			Plus += List[i]
		elif List[i] < 0:
			if Plus > 0:
				if abs(List[i]) <= Plus:
					count += abs(List[i])
					Plus += List[i]
				else:
					count += Plus
					Plus = 0
	print(sum - count)


for _ in range(int(input())):
	n = int(input())
	arr = [int(j) for j in input().split()]
	arr.reverse()
	temp = []
	temp.append(arr[0])
	for i in range(1, n):
		temp.append(temp[i - 1] + arr[i])
	print(max(temp))


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	for i in l:
		sum += i
		if sum < 0:
			sum = 0
			continue
	print(sum)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	coins = 0
	for i in range(n - 1):
		if a[i] > 0:
			a[i + 1] += a[i]
			a[i] = 0
	print(a[-1])


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	neg = 0
	ans = 0
	for j in range(n - 1, -1, -1):
		if a[j] < 0:
			neg += abs(a[j])
		elif a[j] > 0:
			if a[j] > neg:
				ans += a[j] - neg
				neg = 0
			else:
				neg -= a[j]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	ar = [int(i) for i in input().split()]
	pos_cnt = 0
	for x in range(n):
		if ar[x] > 0:
			pos_cnt += ar[x]
		elif abs(ar[x]) > pos_cnt:
			pos_cnt = 0
		else:
			pos_cnt += ar[x]
	print(pos_cnt)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	remain = 0
	ans = 0
	for i in reversed(range(n)):
		if a[i] > 0:
			if a[i] + s < 0:
				s = a[i] + s
				continue
			ans += a[i] + s
			s = 0
		if a[i] < 0:
			s += a[i]
	print(ans)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	posi = 0
	for i in range(n):
		if a[i] > 0:
			posi += a[i]
		elif a[i] < 0:
			k = min(posi, -a[i])
			a[i] += k
			posi -= k
	posi = 0
	for i in range(n):
		if a[i] < 0:
			posi += abs(a[i])
	print(posi)
	t -= 1


for _ in range(int(input())):
	N = int(input())
	X = list(map(int, input().split()))
	(SUM, Total) = (0, 0)
	for i in range(N - 1, -1, -1):
		if X[i] < 0:
			Total += X[i]
		elif abs(Total) >= X[i]:
			Total += X[i]
		else:
			SUM += X[i] + Total
			Total = 0
	print(SUM)


t = int(input())
while t:
	n = int(input())
	v = list(map(int, input().split()))
	sum = 0
	ans = 0
	for i in v:
		sum += i
		ans = max(ans, -sum)
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	total = 0
	pos = 0
	for i in l:
		if i < 0:
			if pos > 0:
				if abs(i) < pos:
					pos += i
					total += i
				else:
					total -= pos
					pos = 0
			total += abs(i)
		else:
			pos += i
	print(total)


n = int(input())
for i in range(n):
	length = int(input())
	s = input().split(' ')
	L = [0] * length
	for j in range(length):
		L[j] = int(s[j])
	posi = 0
	nega = 0
	for j in range(length):
		if L[j] >= 0:
			posi += L[j]
		elif posi >= L[j] * -1:
			posi += L[j]
		else:
			posi = 0
			nega += L[j] + posi
	print(posi)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	z = 0
	ans = 0
	for x in a:
		z += x
		if z < 0:
			ans -= z
			z = 0
	print(ans)


def greedyCancel(arr: list):
	cost = 0
	neg = 0
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] > 0:
			neg += arr[i]
			if neg > 0:
				cost += neg
				neg = 0
		elif arr[i] < 0:
			neg += arr[i]
	return cost
t = int(input())
results = []
for i in range(t):
	n = int(input())
	array = input().split()
	array = list(map(int, array))
	results.append(greedyCancel(array))
for result in results:
	print(result)


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	pl = 0
	ans = 0
	for i in a:
		if i > 0:
			pl += i
		elif pl >= abs(i):
			pl -= abs(i)
		else:
			ans += abs(i) - pl
			pl = 0
	print(ans)


t = int(input())
for i in range(t):
	p = input()
	l = input().split()
	a = b = 0
	for x in l:
		b += int(x)
		if b < 0:
			a -= b
			b = 0
	print(a)


import sys
import bisect
import math as mt
input = sys.stdin.readline
t = int(input())
mod = 10 ** 9 + 7
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	maxn = 0
	cnt = 0
	for i in range(n - 1, -1, -1):
		if l[i] < 0:
			maxn += abs(l[i])
		if l[i] > 0:
			x = min(maxn, l[i])
			l[i] -= x
			maxn -= x
	print(maxn)


for t in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	pos = 0
	neg = 0
	for i in li:
		if i >= 0:
			pos += i
		elif pos + i >= 0:
			pos += i
		else:
			i += pos
			pos = 0
			neg += i
	print(-neg)


for _ in range(int(input())):
	n = int(input())
	num = [int(i) for i in input().split()]
	count = 0
	sumt = 0
	for i in range(n):
		sumt += abs(num[i])
	tt = 0
	for i in range(n - 1, -1, -1):
		if num[i] < 0:
			tt += num[i]
			continue
		if num[i] > 0:
			count += max(num[i] + tt, 0)
			tt = min(num[i] + tt, 0)
	print(count)


from itertools import accumulate
t = int(input())
for x in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	res = -min(accumulate(A))
	print(res)


import os
import sys
from io import BytesIO, IOBase
import math
import itertools
import bisect
import heapq

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] > arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def maxfac(n):
	root = int(n ** 0.5)
	for i in range(2, root + 1):
		if n % i == 0:
			return n // i
	return n

def p2(n):
	c = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	return c

def seive(n):
	primes = [True] * (n + 1)
	primes[1] = primes[0] = False
	for i in range(2, n + 1):
		if primes[i]:
			for j in range(i + i, n + 1, i):
				primes[j] = False
	p = []
	for i in range(0, n + 1):
		if primes[i]:
			p.append(i)
	return p

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def denofactinverse(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return pow(fac, m - 2, m)

def numofact(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return fac
for xyz in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	cs = 0
	for i in range(0, n):
		cs += l[i]
		if cs < 0:
			ans += abs(cs)
			cs = 0
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	total_neg_sum = 0
	for i in range(-1, -len(l), -1):
		if l[i] < 0:
			total_neg_sum += l[i]
			l[i] = 0
		elif total_neg_sum < 0:
			total_neg_sum += l[i]
			if total_neg_sum < 0:
				l[i] = 0
			else:
				l[i] = total_neg_sum
				total_neg_sum = 0
	ans = 0
	for i in range(-1, -len(l), -1):
		if l[i] > 0:
			ans += l[i]
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for rrr in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	r = 0
	ans = 0
	for i in range(len(arr)):
		if arr[i] < 0:
			r += arr[i]
			if r < 0:
				ans += abs(r)
			r = max(r, 0)
		else:
			r += arr[i]
	print(ans)


import math
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlst():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s) - 1])

def invar():
	return map(int, input().split())

def pbb(n, a):
	res = 0
	for i in range(n):
		if a[i] > 0:
			res += a[i]
		else:
			k = min(res, -a[i])
			a[i] += k
			res -= k
	res = 0
	for i in a:
		if i < 0:
			res -= i
	return res
for _ in range(inp()):
	n = inp()
	a = inlst()
	print(pbb(n, a))


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	an = 0
	s = 0
	for i in a:
		if s + i >= 0:
			s += i
		else:
			s += i
			an -= s
			s = 0
	print(an)
	t -= 1


T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	B = []
	C = []
	for i in range(n):
		if A[i] > 0:
			B.append(i)
		elif A[i] < 0:
			C.append(i)
	(j, k) = (0, 0)
	(lb, lc) = (len(B), len(C))
	while j < lb and k < lc:
		if B[j] < C[k]:
			if A[B[j]] <= -1 * A[C[k]]:
				A[C[k]] = A[C[k]] + A[B[j]]
				A[B[j]] = 0
				j += 1
				if A[C[k]] == 0:
					k += 1
			else:
				A[B[j]] = A[B[j]] + A[C[k]]
				A[C[k]] = 0
				k += 1
		else:
			k += 1
	sum1 = 0
	for i in range(n):
		if A[i] > 0:
			sum1 = sum1 + A[i]
	print(sum1)


import sys
import math
for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	su = 0
	mm = 0
	for i in range(n):
		su += a[i]
		mm = max(mm, abs(su))
	a = a[::-1]
	su = 0
	mm2 = 0
	for i in range(n):
		su += a[i]
		mm2 = max(mm2, su)
	print(min(mm, mm2))


for _ in range(int(input())):
	n = int(input())
	arr = [int(num) for num in input().split(' ')]
	ans = 0
	x = sum(arr)
	for item in arr:
		ans = max(ans, x - item)
		x = x - item
	print(ans)


for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	pos = neg = 0
	out = 0
	for i in range(n - 1, -1, -1):
		if num[i] == 0:
			continue
		if num[i] > 0:
			if neg >= num[i]:
				neg -= num[i]
			else:
				out += num[i] - neg
				pos += num[i] - neg
				neg = 0
		else:
			neg += abs(num[i])
	out += neg
	out -= pos
	print(out)


def solve(arr):
	n = len(arr)
	stk = []
	for i in arr:
		if len(stk) == 0:
			stk.append(i)
		elif stk[len(stk) - 1] > 0 and i < 0:
			stk[len(stk) - 1] += i
		elif stk[len(stk) - 1] > 0 and i > 0:
			stk[len(stk) - 1] += i
		elif stk[len(stk) - 1] < 0 and i > 0:
			stk.append(i)
		while len(stk) > 0 and stk[len(stk) - 1] == 0:
			stk.pop()
	ans = 0
	for i in stk:
		if i > 0:
			ans += i
	return ans
t = int(input())
while t > 0:
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))
	t -= 1


from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	st = 0
	for i in range(n):
		if l[i] > 0:
			st += l[i]
		elif st:
			mi = min(st, -l[i])
			st -= mi
	print(st)
	t -= 1


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : [int(x) for x in rr().split()]
from sys import stdin, stdout, setrecursionlimit
from math import ceil

def sol():
	n = rri()
	lis = rrm()
	res = 0
	for i in lis:
		res += i
		res = max(res, i)
	print(res)
	return

def main():
	T = rri()
	for t in range(1, T + 1):
		ans = sol()
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	su = 0
	for i in arr[::-1]:
		su += i
		if su >= 0:
			su = 0
	print(abs(su))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	first = False
	curr = 0
	for i in range(n):
		if arr[i] > 0 and (not first):
			first = True
		if first and curr >= 0:
			curr += arr[i]
		if first and curr < 0 and (arr[i] > 0):
			curr = arr[i]
	print(curr)


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	list_num = list(map(int, sys.stdin.readline().split()))
	count = [0, 0]
	for i in range(n):
		index = 1 if list_num[i] > 0 else 0
		if list_num[i] < 0:
			if count[1] > 0:
				count[1] += list_num[i]
				if count[1] < 0:
					count[0] += count[1]
					count[1] = 0
			else:
				count[0] += list_num[i]
		else:
			count[1] += list_num[i]
	print(count[1])


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	a = L()
	cur = 0
	ans = 0
	for i in range(n):
		if a[i] > 0:
			cur += a[i]
		elif a[i] < 0:
			cur += a[i]
			if cur < 0:
				cur = 0
	print(cur)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	j = 0
	while i < n and j < n:
		if l[i] > 0 and l[j] < 0 and (i < j):
			y = min(l[i], abs(l[j]))
			l[i] = l[i] - y
			l[j] = l[j] + y
		if l[i] == 0 and l[j] == 0:
			i = i + 1
			j = j + 1
		elif l[i] <= 0:
			i = i + 1
		else:
			j = j + 1
	coins = 0
	i = 0
	j = 0
	while i < n and j < n:
		if l[i] > 0 and l[j] < 0 and (i > j):
			y = min(l[i], abs(l[j]))
			coins = coins + y
			l[i] = l[i] - y
			l[j] = l[j] + y
		if l[i] == 0 and l[j] == 0:
			i = i + 1
			j = j + 1
		elif l[j] >= 0:
			j = j + 1
		else:
			i = i + 1
	print(coins)


t = 1
while t > 0:
	for i in range(int(input())):
		n = int(input())
		p = list(map(int, input().split()))
		s = 0
		x = len(p)
		for i in range(x):
			s = s + p[i]
			if s <= -1:
				s = 0
		print(s)
	t -= 1


from itertools import accumulate

def solve(n, arr):
	return -min(accumulate(arr))
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	i = 0
	j = 0
	pos = 0
	while i < n:
		pos = max(0, pos + arr[i])
		i += 1
	print(pos)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def NLI(n):
	return [[int(i) for i in input().split()] for i in range(n)]

def NLI_(n):
	return [[int(i) - 1 for i in input().split()] for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		(a1, ls) = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print((a1, a2), inp)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()
show_flg = False
show_flg = True
ans = 0

def lev(a):
	if len(a) == 1:
		return 0
	for i in range(n - 1, 0, -1):
		if a[i] < 0:
			a[i - 1] += a[i]
			a[i] = 0
	rt = 0
	for i in a:
		if i < 0:
			rt += i
	return -rt
for _ in range(I()):
	ans = 0
	n = I()
	a = LI()
	print(lev(a))


tt = int(input())
for _ in range(tt):
	n = int(input())
	a = input().split()
	sum = 0
	ans = 0
	for i in a[::-1]:
		sum += int(i)
		ans = max(ans, sum)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))[::-1]
	neg = 0
	rem = 0
	for i in ls:
		if i < 0:
			neg += i
		elif neg == 0:
			rem += i
		elif abs(neg) < i:
			rem += i + neg
			neg = 0
		else:
			neg += i
	print(rem)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = sum(map(abs, a)) / 2
	pct = 0
	negct = 0
	for i in a:
		if i < 0 and pct > 0:
			negct += min(pct, abs(i))
			pct -= min(pct, abs(i))
		elif i > 0:
			pct += i
	print(int(s - negct))


for _ in range(int(input())):
	input()
	arr = list(map(int, input().split()))
	n = len(arr)
	t = 0
	preArr = []
	s = 0
	for i in range(n):
		s += arr[i]
		preArr.append(s)
	print(-1 * min(preArr))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		arry = list(map(int, input().split()))
		overthere = 0
		ans = 0
		for i in range(n):
			if arry[i] < 0:
				if overthere == 0:
					ans += -arry[i]
				elif overthere + arry[i] < 0:
					ans += -(overthere + arry[i])
					overthere = 0
				else:
					overthere += arry[i]
			else:
				overthere += arry[i]
		yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


for _ in range(int(input())):
	n = int(input())
	arr = [int(d) for d in input().split()]
	t = 0
	s = 0
	for i in range(n):
		if arr[i] < 0 and s + arr[i] < 0:
			t += s + arr[i]
			s = 0
		elif arr[i] < 0 and s + arr[i] >= 0:
			s += arr[i]
		else:
			s += arr[i]
	print(-t)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from math import inf

def main():

	def solve():
		n = int(input())
		numbers = [int(x) for x in input().split()]
		(accumulated, coinsSpent) = (0, 0)
		for i in range(n):
			if numbers[i] > 0:
				accumulated += numbers[i]
			elif numbers[i] < 0:
				result = accumulated + numbers[i]
				if result < 0:
					coinsSpent -= result
				accumulated = max(result, 0)
		print(coinsSpent)
	t = int(input())
	for i in range(t):
		solve()
BUFFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = lambda : sys.stdin.readline().rstrip('\r\n')

def print(*args, **kwargs):
	sep = kwargs.pop('sep', ' ')
	file = kwargs.pop('file', sys.stdout)
	atStart = True
	for x in args:
		if not atStart:
			file.write(sep)
		file.write(str(x))
		atStart = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	acc = 0
	mx = 0
	for i in range(n - 1, -1, -1):
		acc += arr[i]
		mx = max(mx, acc)
	print(mx)


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	li = list(map(int, input().split()))
	c = 0
	p = 0
	ans = 0
	for i in range(0, n):
		if li[i] < 0:
			if p != 0:
				if p >= abs(li[i]):
					p = p + li[i]
				else:
					c += li[i] + p
					p = 0
			else:
				c += li[i]
		else:
			p += li[i]
	ans += p
	print(ans)


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	sum = 0
	for j in range(len(lst)):
		if lst[j] > 0:
			sum += lst[j]
		else:
			sum -= min(sum, abs(lst[j]))
	print(sum)


testCases = input()
for i in range(int(testCases)):
	n = int(input())
	arr = list(map(int, input().split()))
	minEle = 0
	Sum = 0
	for ele in arr:
		Sum += ele
		minEle = min(Sum, minEle)
	print(abs(minEle))


from sys import *
ws = lambda : map(int, stdin.readline().strip().split())
li = lambda : list(map(int, stdin.readline().strip().split()))
mod = 1000000007

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def prod(l):
	ans = 1
	for i in range(len(l)):
		ans = ans * l[i]
	return ans

def sortindex(l, a):
	c = []
	if a == -1:
		rev = True
	else:
		rev = False
	for i in range(len(l)):
		c.append([l[i], i])
	x = sorted(c, reverse=rev)
	print(x)
	c = []
	for i in range(len(l)):
		c.append(x[i][1])
	return c
for testcases in range(int(input())):
	num = int(input())
	lis = li()
	dec = 0
	inc = 0
	coin = 0
	for i in range(num):
		elem = lis[i]
		if i != 0:
			if elem > 0:
				inc += elem
			elif elem == 0:
				continue
			elif -1 * elem > inc:
				coin += -1 * elem - inc
				inc = 0
				dec += -1 * elem - inc
			else:
				inc -= -1 * elem
		elif elem < 0:
			coin += -1 * elem
			dec += -1 * elem
		else:
			inc += elem
	print(coin)


for _ in range(int(input())):
	n = input()
	a = list(map(int, input().split()))
	(nega, pos) = (0, 0)
	for i in a:
		if i < 0:
			nega = nega + i
			if pos > 0:
				add = min(pos, -i)
				nega = nega + add
				pos = pos - add
		if i > 0:
			pos += i
	print(pos)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(pos, neg) = (0, 0)
	while n > pos and n > neg:
		while n > pos and a[pos] <= 0:
			pos += 1
		while n > neg and (a[neg] >= 0 or neg < pos):
			neg += 1
		if n > pos and n > neg:
			diff = min(a[pos], -a[neg])
			a[neg] += diff
			a[pos] -= diff
	print(sum([max(0, i) for i in a]))


import sys, math, collections
from collections import defaultdict

def file():
	sys.stdin = open('input.py', 'r')
	sys.stdout = open('output.py', 'w')

def get_array():
	l = list(map(int, input().split()))
	return l

def get_2_ints():
	(a, b) = map(int, input().split())
	return (a, b)

def get_3_ints():
	(a, b, c) = map(int, input().split())
	return (a, b, c)

def sod(n):
	(n, c) = (str(n), 0)
	for i in n:
		c += int(i)
	return c

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def getFloor(A, x):
	(left, right) = (0, len(A) - 1)
	floor = -1
	while left <= right:
		mid = (left + right) // 2
		if A[mid] == x:
			return A[mid]
		elif x < A[mid]:
			right = mid - 1
		else:
			floor = A[mid]
			left = mid + 1
	return floor

def chk(l, n):
	s = 0
	o = 0
	for i in range(len(l)):
		s += l[i]
		if s > n:
			return 0
		if s == n:
			ind = i
			o = 1
			break
	if o:
		return sum(l[ind + 1:]) == n

def main():
	for tt in range(int(input())):
		n = int(input())
		l = get_array()
		t = [0] * n
		c = 0
		p = 0
		for i in range(n - 1, -1, -1):
			if l[i] < 0:
				c += l[i]
				t[i] = 0
			else:
				t[i] = c
		for i in range(n - 1, -1, -1):
			if l[i] > 0:
				t[i] += p
				if l[i] > abs(t[i]):
					p += abs(t[i])
					l[i] = l[i] + t[i]
				else:
					p += abs(l[i])
					l[i] = 0
		c = 0
		for i in l:
			if i > 0:
				c += i
		print(c)
main()


tt = int(input())
for t in range(tt):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.reverse()
	ans = 0
	cur_sum = 0
	for i in arr:
		cur_sum += i
		if cur_sum > 0:
			ans += cur_sum
			cur_sum = 0
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	neg = 0
	pos = 0
	while i < len(a):
		if a[i] < 0:
			left = pos + a[i]
			if left < 0:
				neg += left
				pos = 0
			else:
				pos = left
		else:
			pos += a[i]
		i += 1
	print(-neg)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	ind = 0
	for i in range(n):
		if arr[i] < 0:
			f = min(-arr[i], ind)
			ind -= f
			arr[i] += f
			if arr[i] < 0:
				ans -= arr[i]
		elif arr[i] > 0:
			ind += arr[i]
	print(ans)


def modinv(n, p):
	return pow(n, p - 2, p)

def main():
	for case in range(int(input())):
		n = int(input())
		arr = [int(x) for x in input().split()]
		c = 0
		i = n - 2
		j = n - 1
		while True:
			try:
				while arr[i] <= 0:
					i -= 1
				while arr[j] >= 0:
					j -= 1
				while i >= j or arr[i] <= 0:
					i -= 1
				if i < 0 or j < 0:
					break
				k = min(abs(arr[i]), abs(arr[j]))
				arr[i] -= k
				arr[j] += k
			except IndexError:
				break
		for x in arr:
			if x > 0:
				c += x
		print(c)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	lst = 0
	othr = 0
	for i in a:
		if i > 0:
			lst += i
		else:
			lst = max(0, lst + i)
	print(res + lst)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sm = 0
	p = 0
	for i in range(len(l)):
		if l[i] < 0:
			if p == 0:
				sm += -1 * l[i]
			elif p < -1 * l[i]:
				sm += -p - l[i]
				p = 0
			else:
				p += l[i]
		else:
			p += l[i]
	print(sm)


n = int(input())
for i in range(n):
	t = int(input())
	l = list(map(int, input().split()))
	i = 0
	x = 0
	while i != t:
		if l[i] > 0:
			x = x + l[i]
		elif x > 0:
			if abs(l[i]) > x:
				x = 0
			else:
				x = x + l[i]
		i += 1
	print(x)


t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in range(n):
		s += a[i]
		ans = min(ans, s)
	print(ans * -1)


from sys import stdin
import math
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	a = [int(x) for x in inp().split()]
	ans = 0
	total = 0
	for i in a:
		total += i
		if total < 0:
			ans = max(ans, abs(total))
	print(ans)


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		l = [int(i) for i in stdin.readline().split()]
		neg = 0
		pos = 0
		for i in l:
			if i > 0:
				pos += i
			elif i < 0:
				forward = min(abs(i), pos)
				pos -= forward
				neg += abs(i) - forward
		stdout.write('{}\n'.format(neg))
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	ps = 0
	c = 0
	for i in a:
		if i >= 0:
			ps += i
			a[c] = 0
		else:
			x = min(ps, -i)
			ps -= x
			a[c] += x
		c += 1
	print(-sum(a))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	while n > 1:
		i = n - 1
		if l[i - 1] >= 0 and l[i] < 0:
			l[i - 1] = l[i - 1] + l[i]
			l.pop(i)
			n -= 1
		elif l[i - 1] < 0 and l[i] >= 0:
			n = n - 1
		elif l[i - 1] >= 0 and l[i] >= 0 or (l[i - 1] < 0 and l[i] < 0):
			l[i - 1] = l[i - 1] + l[i]
			l.pop(i)
			n -= 1
	a = 0
	for i in range(len(l) - 1):
		if l[i] >= 0 and l[i + 1] < 0 or (l[i] < 0 and l[i + 1] >= 0):
			a += l[i]
			break
		else:
			a += l[i]
	if len(l) == 1:
		print(0)
	elif a >= 0:
		print(0)
	else:
		print(-a)


for k in range(int(input())):
	n = int(input())
	t = list(map(int, input().split()))
	f = []
	for i in range(n):
		if t[i] < 0:
			if len(f) == 0:
				pass
			else:
				while True:
					if len(f) > 0:
						a = min(abs(t[i]), f[-1][0])
						if a == abs(t[i]):
							f[-1][0] -= abs(t[i])
							t[f[-1][1]] -= abs(t[i])
							t[i] = 0
							break
						else:
							t[f[-1][1]] = 0
							t[i] += a
							f.pop()
					else:
						break
		else:
			f.append([t[i], i])
	p = 0
	for j in t:
		if j > 0:
			p += j
	print(p)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(i, j) = (0, 1)
	while i < n and j < n:
		if l[i] < 0:
			i += 1
			j += 1
			continue
		if l[j] > 0:
			j += 1
			continue
		if l[i] < abs(l[j]):
			t = l[i]
			l[i] = 0
			i += 1
			l[j] += t
		else:
			t = l[j]
			l[j] = 0
			j += 1
			l[i] += t
	c = 0
	for i in l:
		if i > 0:
			c += i
	print(c)


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
import io, os
input = sys.stdin.readline
from bisect import *
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().strip().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = rl()
	c = 0
	i = n - 1
	while i >= 0:
		if a[i] < 0:
			c = c + abs(a[i])
		elif a[i] > 0:
			if c >= a[i]:
				c = c - a[i]
				a[i] = 0
			else:
				a[i] = a[i] - c
				c = 0
		i = i - 1
	s = 0
	for i in range(0, n):
		if a[i] >= 0:
			s = s + a[i]
	print(s)


def solve(a, L):
	i = L - 1
	neg = 0
	while i >= 0:
		if a[i] > 0:
			if neg >= a[i]:
				neg -= a[i]
				a[i] = 0
			else:
				a[i] -= neg
				neg = 0
		elif a[i] < 0:
			neg += abs(a[i])
			a[i] = 0
		i -= 1
	print(neg)
	return
tcases = int(input())
for i in range(tcases):
	L = int(input())
	a = list(map(int, input().split()))
	solve(a, L)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = [0]
	for i in a:
		s.append(s[-1] + i)
	print(min(s) * -1)


T = int(input())
for case in range(T):
	i = int(input())
	ls = [int(x) for x in input().split()]
	negs = [n for (n, e) in enumerate(ls) if e < 0]
	pos = [n for (n, e) in enumerate(ls) if e > 0]
	ret = 0
	for i in range(len(ls) - 1, -1, -1):
		if ls[i] > 0:
			ls[0] += ls[i]
			ret += ls[i]
			ls[i] = 0
		elif ls[i] < 0:
			ls[i - 1] += ls[i]
			ls[i] = 0
	print(ret)


import sys

def swap(x, y, a):
	temp = a[x]
	a[x] = a[y]
	a[y] = temp
	return a

def solve():
	return None

def main():
	q = []
	for line in sys.stdin.readlines():
		q.append(line)
	for i in range(len(q)):
		q[i] = q[i].rstrip().split(' ')
		q[i] = [int(x) for x in q[i]]
	for i in range(1, len(q), 2):
		a = q[i + 1]
		i = 0
		j = 1
		while j != len(a):
			if a[i] > 0 and a[j] < 0:
				if a[i] >= abs(a[j]):
					a[i] += a[j]
					a[j] = 0
				else:
					a[j] += a[i]
					a[i] = 0
			if a[i] <= 0:
				i += 1
			if j <= i or a[j] >= 0:
				j += 1
		t = 0
		for i in range(len(a)):
			if a[i] > 0:
				t += a[i]
		print(t)
main()


def solve(n, ar):
	left = 0
	can = 0
	for i in range(n):
		if ar[i] > 0:
			can += ar[i]
			continue
		elif can > 0:
			if can + ar[i] >= 0:
				can += ar[i]
			else:
				left += ar[i] + can
				can = 0
		else:
			left += ar[i]
	print(abs(left))
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	solve(n, ar)


for _ in range(int(input())):
	p = int(input())
	cnt = 0
	a = []
	arr = list(map(int, input().split()))
	for i in range(p):
		if cnt <= 0:
			if arr[i] > 0:
				cnt = cnt + arr[i]
			if arr[i] < 0:
				cnt = 0
				a.append(arr[i])
		elif arr[i] > 0:
			cnt = cnt + arr[i]
		elif cnt >= -arr[i]:
			cnt = cnt + arr[i]
		else:
			a.append(arr[i] + cnt)
			cnt = 0
	a.append(cnt)
	su = 0
	for i in a:
		if i > 0:
			su = su + i
	print(su)


def checker(n, lst):
	(s, result) = (0, 0)
	for i in range(n):
		s += lst[i]
		result = min(result, s)
	return -result
for _ in range(int(input())):
	m = int(input())
	a = [int(i) for i in input().split()]
	print(checker(m, a))


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	ma = 0
	ne = 0
	for i in range(n - 1, -1, -1):
		if lis[i] < 0:
			ne += abs(lis[i])
		else:
			ma += lis[i]
			if ma >= ne:
				ne = 0
			else:
				ne -= ma
			ma = 0
	if ma > ne:
		ne = 0
	else:
		ne -= ma
	print(ne)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cmf = 0
	for i in range(len(arr)):
		if arr[i] < 0 and cmf != 0:
			cmf = max(0, cmf + arr[i])
		if arr[i] > 0:
			cmf += arr[i]
	print(cmf)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	for i in range(1, n):
		lst[i] += lst[i - 1]
	res = []
	for i in lst:
		if i <= 0:
			res.append(abs(i))
	print(max(res))


import sys as _sys

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = tuple(read_ints())
		result = find_min_cost_to_make_zeros(a)
		print(result)

def read_ints():
	return map(int, _sys.stdin.readline().split(' '))

def find_min_cost_to_make_zeros(sequence):
	min_cost = 0
	curr_plus_after = 0
	curr_minus_after = 0
	for x in sequence:
		if x == 0:
			continue
		elif x > 0:
			x = abs(x)
			curr_plus_after += x
			x = 0
		else:
			assert x < 0
			x = abs(x)
			if curr_plus_after >= x:
				curr_plus_after -= x
				x = 0
			else:
				x -= curr_plus_after
				curr_plus_after = 0
			curr_minus_after += x
			min_cost += x
			x = 0
	return min_cost
main()


t = int(input())
i = 0
while i < t:
	n = int(input())
	a = list(map(int, input().split()))
	minus = 0
	plus = 0
	for j in a:
		if j >= 0:
			plus += j
		elif plus > 0:
			if plus >= abs(j):
				plus -= abs(j)
			else:
				minus += j + plus
				plus = 0
		else:
			minus += j
	print(abs(minus))
	i += 1


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	neg = []
	d = {}
	for i in range(n - 1, -1, -1):
		if l[i] < 0:
			neg.append(i)
			d[i] = 1
	ans = 0
	for i in range(n):
		if l[i] > 0:
			while len(neg) > 0 and neg[-1] < i:
				neg.pop()
			while len(neg) > 0 and l[i] > 0:
				j = neg[-1]
				z = min(abs(l[j]), l[i])
				l[i] -= z
				l[j] += z
				if l[j] == 0:
					neg.pop()
		elif l[i] < 0 and d.get(i, 1) == 1:
			d[i] = 0
			neg.pop()
	ans = 0
	for i in range(n):
		if l[i] < 0:
			ans += abs(l[i])
	print(ans)


import collections
import math

def solve(lst):
	ans = 0
	mined = 9189218989218921
	for i in lst:
		ans += i
		mined = min(ans, mined)
	return abs(mined)
k = int(input())
for i in range(k):
	k = int(input())
	lst = list(map(int, input().split()))
	print(solve(lst))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	k = 0
	s = 0
	for i in range(n):
		if a[i] > 0:
			k += a[i]
		elif a[i] < 0:
			m = min(-a[i], k)
			a[i] += m
			k -= m
			s += -a[i]
	print(s)


u = int(input())
for i in range(u):
	n = int(input())
	l = list(map(int, input().split()))
	t = 0
	for j in range(len(l)):
		if l[j] < 0:
			p = min(t, -l[j])
			l[j] += p
			t -= p
		elif l[j] > 0:
			t += l[j]
	print(t)


for t in range(int(input())):
	n = int(input())
	arr = [int(k) for k in input().split()]
	posk = 0
	negk = 0
	peepos = 0
	ans = 0
	for ele in arr:
		if ele > 0:
			peepos += ele
	for ele in reversed(arr):
		mkzr = ele
		if mkzr > 0:
			diff = min(posk, mkzr)
			mkzr -= diff
			posk -= diff
			if mkzr > 0:
				peepos -= mkzr
				ans += mkzr
				negk += mkzr
		else:
			mkzr = -mkzr
			free = min(mkzr, peepos)
			mkzr -= free
			peepos -= free
			posk += free
			diff = min(negk, mkzr)
			mkzr -= diff
			negk -= diff
			if mkzr > 0:
				ans += mkzr
				posk += mkzr
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		if a[i] > 0:
			b.append(i)
	j = 0
	if len(set(a)) == 1:
		print(0)
	else:
		i = b[0] + 1
		while i != n:
			if a[i] >= 0:
				i += 1
				continue
			if a[b[j]] >= -a[i]:
				a[b[j]] += a[i]
				a[i] = 0
				i += 1
			else:
				a[i] += a[b[j]]
				a[b[j]] = 0
				j += 1
				if b[j] >= i:
					i = b[j] + 1
		s = 0
		a.sort()
		for i in a:
			if i >= 0:
				break
			s -= i
		print(s)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	val = 0
	for i in range(n):
		if arr[i] >= 0:
			val += arr[i]
		elif val + arr[i] < 0:
			ans += val + arr[i]
			val = 0
		else:
			val = val + arr[i]
	print(abs(ans))


import sys, os
from math import sqrt, gcd, ceil, log, floor, sin, pi
from math import factorial as fact
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
from itertools import permutations
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))
MOD = 10 ** 9 + 7

def main():
	ans_ = []
	for _ in range(int(input())):
		n = int(input())
		arr = read()
		s = 0
		for i in arr:
			if i <= 0:
				if s > 0:
					s = max(0, s + i)
			else:
				s += i
		ans_.append(s)
	for i in ans_:
		print(i)
main()


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	n = ip()
	arr = list(inp())
	stack = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		if arr[i] >= 0:
			if stack == 0:
				ans += arr[i]
			elif arr[i] >= stack:
				ans += arr[i] - stack
				stack = 0
			else:
				stack -= arr[i]
		else:
			stack += abs(arr[i])
	out(ans)


for t in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	al = 0
	for nu in nums:
		if nu >= 0:
			al += nu
		else:
			al = max(nu + al, 0)
	print(al)


from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math
import heapq

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		n = iin()
		l = ain()
		suma = 0
		for i in range(n - 1, -1, -1):
			suma += l[i]
			if suma > 0:
				suma = 0
		print(-1 * suma)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	min = 0
	s = 0
	for i in range(n):
		s = s + a[i]
		if s < min:
			min = s
	print(abs(min))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(i, j) = (0, 1)
	while i < n and j < n:
		while i < n and arr[i] <= 0:
			i += 1
		j = max(j, i + 1)
		while j < n and arr[j] >= 0:
			j += 1
		if not i < n or not j < n:
			break
		x = arr[i] - min(abs(arr[j]), arr[i])
		arr[j] = arr[j] + min(abs(arr[j]), arr[i])
		arr[i] = x
	cost = 0
	for i in range(-1, -len(arr) - 1, -1):
		if arr[i] > 0:
			cost += arr[i]
	print(cost)


import sys
import math
input = sys.stdin.readline
T = int(input())
for t in range(T):
	n = int(input())
	A = list(map(int, input().split(' ')))
	ans = 0
	sum_left = 0
	i = 0
	j = n - 1
	while i < j:
		while A[i] >= 0 and i < n - 1:
			sum_left += A[i]
			i += 1
		while A[j] <= 0 and j > 1:
			j -= 1
		if i >= j:
			break
		if sum_left != 0:
			y = min(sum_left, -A[i])
			A[i] += y
			sum_left -= y
		x = min(abs(A[i]), abs(A[j]))
		ans += x
		A[i] += x
		A[j] -= x
		if A[i] == 0:
			i += 1
		if A[j] == 0:
			j -= 1
	print(ans)


t = int(input())
for _ in range(t):
	n = input()
	arr = list(map(int, input().split()))
	pz = nz = 0
	for x in arr:
		if x < 0:
			if pz + x >= 0:
				pz += x
			else:
				x += pz
				pz = 0
				nz += x
		else:
			pz += x
	print(pz)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	p = 0
	out = 0
	for i in arr:
		if i < 0:
			if -1 * i < p:
				p += i
			else:
				out -= i + p
				p = 0
		else:
			p += i
	print(out)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	tot = 0
	for i in range(n):
		if a[i] > 0:
			tot += a[i]
		elif a[i] < 0 and tot > 0:
			x = min(abs(a[i]), tot)
			a[i] += x
			tot -= x
	ng = 0
	for i in a:
		if i < 0:
			ng += i
	print(max(abs(ng), tot))


def lst(x):
	return list(map(int, x.split()))
t = int(input())
for _ in range(t):
	n = int(input())
	ar = lst(input())
	(pos, neg) = (0, 0)
	for x in ar[::-1]:
		if x > 0:
			if x > neg:
				pos += x - neg
			neg -= min(neg, x)
		else:
			neg -= x
	print(pos)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(1, n):
		l[i] = l[i - 1] + l[i]
	i = 0
	ans = 0
	while i < n:
		ans = min(ans, l[i])
		i += 1
	print(abs(ans))


T = int(input())
for t in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	start = 1
	for i in range(n):
		if A[i] > 0:
			temp = []
			sum = 0
			for j in range(max(i + 1, start), n):
				if A[j] < 0:
					temp.append(j)
					sum -= A[j]
				if sum >= A[i]:
					break
			if sum >= A[i]:
				for k in temp:
					if A[i] + A[k] > 0:
						A[i] += A[k]
						A[k] = 0
					else:
						A[k] += A[i]
						A[i] = 0
						start = k
			else:
				A[i] -= sum
				for k in temp:
					A[k] = 0
				break
	ans = 0
	for i in range(n):
		if A[i] > 0:
			break
		ans -= A[i]
	print(ans)


t = int(input())
res = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n - 1):
		if a[i] > 0:
			a[i + 1] += a[i]
			a[i] = 0
	res.append(a[n - 1])
for i in res:
	print(i)


t = int(input())
for i in range(t):
	l = int(input())
	Nums = [int(i) for i in input().split()]
	numeral = 0
	for (j, i) in enumerate(Nums):
		if i > 0:
			numeral += i
		elif i < 0:
			Nums[j] = min(numeral + i, 0)
			numeral = max(0, numeral + i)
	Neg = [min(i, 0) for i in Nums]
	print(abs(sum(Neg)))


def _input():
	return map(int, input().split())

def f():
	n = int(input())
	lst = list(_input())
	i = 0
	while i < n - 1:
		if lst[i] > 0:
			lst[i + 1] += lst[i]
		i += 1
	print(lst[-1])
for _ in range(int(input())):
	f()


for _ in range(int(input())):
	n = int(input())
	pp = list(map(int, input().split()))
	c = 0
	p = 0
	for i in pp:
		if i > 0:
			p += i
		else:
			i = i * -1
			if p > i:
				p -= i
			else:
				c += i - p
				p = 0
	print(c)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	pos = 0
	for i in range(n):
		if arr[i] > 0:
			pos += arr[i]
		else:
			arr[i] = arr[i] * -1
			if pos > arr[i]:
				pos -= arr[i]
			else:
				c += arr[i] - pos
				pos = 0
	print(c)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
mod = int(1000000000.0) + 7
INF = 100000
for t in range(int(data())):
	n = int(data())
	a = mdata()
	p = 0
	n1 = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		if a[i] < 0:
			n1 -= a[i]
		else:
			p += max(a[i] - n1, 0)
			n1 = max(n1 - a[i], 0)
	out(p)


import os
from sys import stdin, stdout

def solve(tc):
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().split()))
	plus = 0
	for i in range(n):
		if seq[i] > 0:
			plus += seq[i]
		elif -seq[i] <= plus:
			plus += seq[i]
		else:
			plus = 0
	print(plus)
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


def solve(A, n):
	cur = 0
	for i in range(n):
		cur = max(0, cur + A[i])
	return cur
res = []
t = int(input())
while t:
	n = int(input())
	arr = input().split(' ')
	A = [int(n) for n in arr]
	res.append(solve(A, n))
	t -= 1
for r in res:
	print(r)


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	x = 0
	for i in range(n):
		if l[i] >= 0:
			x += l[i]
		elif abs(l[i]) > x:
			c += abs(l[i]) - x
			x = 0
		else:
			x += l[i]
	print(c)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	pos = 0
	for i in range(n):
		if l[i] >= 0:
			pos += l[i]
		elif pos > 0:
			if abs(l[i]) > pos:
				l[i] += pos
				pos = 0
			else:
				pos += l[i]
				l[i] = 0
	ans = 0
	for i in l:
		if i < 0:
			ans += abs(i)
	print(ans)


cases = int(input())
while cases:
	cases -= 1
	num = int(input())
	arr = list(map(int, input().split()))
	minn = 0
	summ = 0
	for num in arr:
		summ += num
		minn = min(minn, summ)
	print(-minn)


for _ in range(int(input())):
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	cost = a[-1]
	tot = a[-1]
	for i in range(n - 2, -1, -1):
		tot += a[i]
		cost = max(cost, tot)
	print(cost)


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	s = 0
	for i in range(n):
		k = a[i]
		if k > 0:
			s = s + k
		if k < 0:
			s = s + k
			if s < 0:
				ans = ans + abs(s)
				s = 0
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	cur = 0
	for i in a:
		cur = max(0, cur + i)
	print(cur)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip(' ').split(' ')))
	neg = 0
	pos = 0
	for i in a:
		if i >= 0:
			pos += i
		if i < 0:
			if pos > abs(i):
				pos = pos + i
			else:
				neg += abs(i) - pos
				pos = 0
	print(neg)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	total = 0
	for (i, num) in enumerate(arr):
		if num > 0:
			total += num
		else:
			k = min(total, abs(num))
			total -= min(total, abs(num))
			arr[i] += k
	ans = 0
	for num in arr:
		if num < 0:
			ans += abs(num)
	print(ans)


t = int(input())

def main():
	n = int(input())
	a = list(map(int, input().split()))
	tmp = 0
	ans = 0
	for e in a:
		if tmp + e < 0:
			ans += abs(tmp + e)
			tmp = 0
		else:
			tmp = tmp + e
	print(ans)
for i in range(t):
	main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	table = [0] * N
	res = 0
	for i in range(N - 1):
		table[i + 1] = table[i] + a[i] * (a[i] > 0)
	for x in a:
		res += x * (x > 0)
	x = table[-1]
	for i in range(N - 1, -1, -1):
		if a[i] < 0:
			res -= min(x, -a[i])
			x -= min(x, -a[i])
		elif a[i] > 0:
			x = min(x, table[i])
	print(res)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for j in range(1, len(a)):
		a[j] += a[j - 1]
	print(abs(min(a)))


t = int(input(''))
arr = []
for i in range(t):
	int(input(''))
	arr.append(list(map(int, input().split())))
for i in arr:
	n = len(i)
	cnt = 0
	rem = 0
	for j in range(n):
		if i[j] < 0:
			if rem >= abs(i[j]):
				rem -= abs(i[j])
			else:
				inc = abs(i[j]) - rem
				cnt += inc
				rem = 0
		else:
			rem += i[j]
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	m = 0
	A = [int(_) for _ in input().split()]
	c = 0
	for _ in A:
		c += _
		m = min(m, c)
	print(abs(m))


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	c = 0
	ans = 0
	for i in range(1, n + 1):
		c += s[-i]
		ans = max(ans, c)
	print(ans)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = map(int, input())
	l = list(map(int, input().split()))
	pos = 0
	res = 0
	for (i, v) in enumerate(l):
		if v < 0:
			if pos + v >= 0:
				pos += v
			else:
				res += -v - pos
				pos = 0
		else:
			pos += v
	print(res)


def findCoins(arr):
	preSumPositives = 0
	for num in arr:
		if num < 0:
			if abs(num) > preSumPositives:
				preSumPositives = 0
			else:
				preSumPositives += num
		else:
			preSumPositives += num
	return preSumPositives
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split(' ')))
	print(findCoins(arr))
	t -= 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	(tmp, neg, pos) = (0, 0, 0)
	arr = list(map(int, input().split()))
	for i in range(n - 1, -1, -1):
		if arr[i] < 0:
			neg += arr[i]
		elif arr[i] >= -1 * neg:
			pos += arr[i] + neg
			neg = 0
		else:
			neg += arr[i]
	print(max(pos, neg))


n = int(input())
for i in range(n):
	m = int(input())
	arr = list(map(int, input().split()))
	positive = 0
	for i in range(len(arr)):
		if arr[i] > 0:
			positive += arr[i]
		elif positive + arr[i] >= 0:
			positive += arr[i]
		else:
			arr[i] += positive
			positive = 0
	print(positive)


import math
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	pos = 0
	for i in range(n):
		if p[i] > 0:
			pos += p[i]
		else:
			pos -= min(pos, abs(p[i]))
	print(pos)


import random

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	curr = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		curr += a[i]
		if curr > 0:
			ans += curr
			curr = 0
	print(ans)


t = int(input())
for j in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	for l in range(n - 1, -1, -1):
		if a[l] < 0:
			c = c + a[l]
		elif c <= 0:
			c = min(0, c + a[l])
	print(abs(c))


from sys import stdin, stdout
q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, stdin.readline().split()))
	res = 0
	sum1 = 0
	for i in range(n):
		sum1 += p[i]
		if sum1 < 0:
			res += abs(sum1)
			sum1 = 0
	print(res)


from collections import deque
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	c = 0
	st = []
	for i in range(n):
		x = a[i]
		if a[i] == 0:
			continue
		if a[i] > 0:
			st.append(a[i])
		else:
			while st:
				v = st.pop()
				if x + v > 0:
					st.append(v + x)
					break
				x += v
				if x == 0:
					break
	print(sum(st))


from itertools import accumulate
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	max_neg = 0
	current = 0
	for i in range(n):
		current += a[i]
		max_neg = min(max_neg, current)
	print(abs(max_neg))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 0
	curr = 0
	for i in range(n):
		if arr[i] < 0:
			curr = max(0, curr + arr[i])
		else:
			curr = curr + arr[i]
	print(curr)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	p = 0
	for i in range(len(a)):
		if a[i] < 0:
			if p == abs(a[i]):
				p = 0
				a[i] = 0
			elif p < abs(a[i]):
				a[i] = a[i] + p
				p = 0
			else:
				p = p + a[i]
				a[i] = 0
		else:
			p += a[i]
	s = 0
	for i in a:
		if i < 0:
			s += abs(i)
	print(s)


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
for _ in range(int(I1())):
	n = int(I1())
	a = list(I())
	for i in range(n - 1, 0, -1):
		if a[i] < 0:
			a[i - 1] += a[i]
			a[i] = 0
	print(abs(a[0]))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sumPos = 0
	sumNeg = 0
	for number in arr:
		if number > 0:
			sumPos += number
		if number < 0:
			if sumPos >= abs(number):
				sumPos = sumPos + number
			else:
				sumNeg += abs(number - sumPos)
				sumPos = 0
	print(sumPos)


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	ans = 0
	for i in p:
		ans += i
		ans = max(ans, 0)
	print(ans)


for _ in range(int(input())):
	x = int(input())
	a = [*map(int, input().split())]
	n = [i for i in range(x) if a[i] < 0]
	p = [i for i in range(x) if a[i] > 0]
	ni = 0
	pi = 0
	while pi < len(p):
		while ni < len(n) and n[ni] < p[pi]:
			ni += 1
		if ni == len(n):
			break
		op = min(abs(a[n[ni]]), a[p[pi]])
		if op >= a[p[pi]]:
			a[p[pi]] = 0
			a[n[ni]] += op
			pi += 1
		else:
			a[p[pi]] -= op
			a[n[ni]] = 0
			ni += 1
	ans = sum((b for b in a if b > 0))
	print(ans)


for _ in range(int(input())):
	i = int(input())
	l = list(map(int, input().split()))
	ans = 0
	pos = 0
	for num in l:
		if pos == 0 and num < 0:
			ans += abs(num)
		elif num > 0:
			pos += num
		elif pos > 0 and num < 0:
			pos += num
			if pos < 0:
				ans += abs(pos)
				pos = 0
	print(ans)


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def MI1():
	return map(int1, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
dij = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for _ in range(II()):
	n = II()
	aa = LI()
	now = 0
	ans = 0
	for a in aa:
		if a > 0:
			now += a
		if a < 0:
			a = -a
			cur = min(a, now)
			ans += a - cur
			now -= cur
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = t = 0
	for i in l:
		t -= i
		m = max(t, m)
	print(m)


import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi

def input():
	return sys.stdin.readline()[:-1]
import string
from bisect import bisect_left, bisect_right
from functools import lru_cache
MOD = int(1000000000.0) + 7
INF = float('inf')

def solve():
	n = int(input())
	a = [int(x) for x in input().split()]
	neg = 0
	pos = 0
	for i in range(n - 1, -1, -1):
		if a[i] < 0:
			neg += -a[i]
		else:
			x = min(a[i], neg)
			a[i] -= x
			pos += a[i]
			neg -= x
	print((neg + pos) // 2)
t = 1
t = int(input())
for case in range(1, t + 1):
	ans = solve()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list([int(x) for x in input().split()])
	ans = 0
	sum = 0
	for i in range(n - 1, -1, -1):
		sum += l[i]
		ans = max(ans, sum)
	print(ans)


import sys
import itertools
import math
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(n,) = I()
while n:
	(k,) = I()
	arr = I()
	s = [0]
	for i in arr:
		k = s.pop()
		if k < 0:
			s.append(k)
			s.append(i)
		else:
			s.append(i + k)
	print(s.pop())
	n -= 1


import sys
input = sys.stdin.readline
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	c = 0
	for i in range(n):
		if lis[i] >= 0:
			c += lis[i]
			lis[i] = 0
		elif c >= abs(lis[i]):
			c += lis[i]
			lis[i] = 0
		else:
			lis[i] += c
			c = 0
	c = 0
	for i in lis:
		c += abs(i)
	print(c)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	b = []
	for i in a:
		b.append(s + i)
		s += i
	print(-min(b))


for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(p, c) = (0, 0)
	for jj in range(n):
		if a[jj] < 0:
			if p >= -a[jj]:
				p += a[jj]
			else:
				c += -a[jj] - p
				p = 0
		else:
			p += a[jj]
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	buff = 0
	for i in a:
		if i >= 0:
			buff += i
		else:
			buff -= min(buff, -i)
	print(buff)


import sys
import math
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		if s + a[i] <= 0:
			s += a[i]
		else:
			ans += a[i] + s
			s = 0
	print(ans)


from collections import Counter
from collections import defaultdict as dc
import sys
from heapq import *
import math
mod = 10 ** 9 + 7

def sinp():
	p = str(sys.stdin.readline())
	return p

def seive(n):
	p = [0] * (n + 1)
	for i in range(2, n):
		for j in range(2 * i, n + 1, i):
			p[i] = 1
	return p

def inp():
	p = int(sys.stdin.readline())
	return p

def line():
	p = list(map(int, sys.stdin.readline().split()))
	return p

def read_mat():
	n = inp()
	a = []
	for i in range(n):
		a.append(line())
	return a
from itertools import permutations as perm

def solve(t):
	n = inp()
	a = line()
	pos = 0
	for i in range(n):
		if a[i] >= 0:
			pos += a[i]
		elif pos > 0:
			pos = max(0, pos - abs(a[i]))
			a[i] = min(0, a[i] + pos)
	print(pos)
t = inp()
for test in range(t):
	solve(test)


for _ in range(int(input())):
	N = int(input())
	array = [int(x) for x in input().split()]
	prefix = []
	total = 0
	for i in array:
		total += i
		prefix.append(total)
	print(abs(min(prefix)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	suffix_sum = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		suffix_sum += a[i]
		ans = max(ans, suffix_sum)
	print(ans)


m = int(input())
for i in range(m):
	n = int(input())
	a = list(map(int, input().split(' ')))
	for j in range(0, n - 1):
		if a[j] > 0:
			a[j + 1] = a[j] + a[j + 1]
	print(a[n - 1])


test_cases = int(input())
t = []
for test in range(test_cases):
	sumt = 0
	n = int(input())
	str = input()
	str = str.split(' ')
	for i in range(len(str)):
		if int(str[i]) > 0:
			sumt = sumt + int(str[i])
		elif sumt > 0 and int(str[i]) < 0:
			if sumt + int(str[i]) > 0:
				sumt = sumt + int(str[i])
			else:
				sumt = 0
	t.append(sumt)
for i in t:
	print(i)


t = int(input())
for p in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	adds = [a[0]]
	for i in range(1, n):
		adds.append(a[i] + adds[i - 1])
	m = min(adds)
	print(abs(m))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for j in range(int(input())):
	n = int(input())
	vals = list(map(int, input().split()))
	positives = 0
	for s in range(n):
		if vals[s] > 0:
			positives += vals[s]
		else:
			positives -= min(abs(vals[s]), positives)
	print(positives)


import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
input = sys.stdin.readline
import random
mo = 10 ** 9 + 7

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def pref(a, n, f):
	pre = [0] * n
	if f == 0:
		pre[0] = a[0]
		for i in range(1, n):
			pre[i] = a[i] + pre[i - 1]
	else:
		pre[-1] = a[-1]
		for i in range(n - 2, -1, -1):
			pre[i] = pre[i + 1] + a[i]
	return pre
for _ in range(inin()):
	n = inin()
	l = ain()
	post = pref(l, n, 1)
	ma = max(post)
	print(ma)


t = int(input())
for j in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = 0
	sum = 0
	for i in range(n - 1, -1, -1):
		sum = sum + l[i]
		if m <= sum:
			m = sum
	print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	b = []
	c = []
	a = list(map(int, input().split()))
	for (index, value) in enumerate(a):
		if value > 0:
			b.append(index)
		elif value < 0:
			c.append(index)
	i = 0
	j = 0
	while i < len(b) and j < len(c):
		if b[i] < c[j]:
			if a[b[i]] > abs(a[c[j]]):
				a[b[i]] = a[b[i]] - abs(a[c[j]])
				a[c[j]] = 0
				j += 1
			elif a[b[i]] < abs(a[c[j]]):
				a[c[j]] = a[c[j]] + a[b[i]]
				a[b[i]] = 0
				i += 1
			else:
				a[b[i]] = 0
				a[c[j]] = 0
				i += 1
				j += 1
		elif b[i] > c[j]:
			j += 1
	s = 0
	for i in b:
		s += a[i]
	print(s)


import sys
import math
input = sys.stdin.readline

def inInt():
	return int(input())

def inStr():
	return input().strip('\n')

def inIList():
	return list(map(int, input().split()))

def inSList():
	return input().split()

def solve(n, p):
	best = 0
	sum = 0
	for x in p:
		sum = sum + x
		best = min(best, sum)
	return abs(best)
tasks = inInt()
for t in range(tasks):
	n = inInt()
	p = inIList()
	print(solve(n, p))


t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	j = n - 1
	neg = 0
	while j >= 0:
		if b[j] < 0:
			neg += abs(b[j])
		elif b[j] > abs(neg):
			neg = 0
		else:
			neg -= b[j]
		j += -1
	print(neg)


import sys, math
try:
	(sys.stdin, sys.stdout) = (open('input.txt', 'r'), open('out.txt', 'w'))
except:
	pass
from sys import stdin, stdout
mod = int(1000000000.0 + 7)
from statistics import mode
from collections import *
from math import ceil, floor, inf, factorial, gcd, log2, sqrt, log
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()

def lcm(a, b):
	return a * b // gcd(a, b)

def setbits(n):
	return bin(n).count('1')

def resetbits(n):
	return bin(n).count('0')

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r):
	(num, den) = (1, 1)
	r = min(n, n - r)
	for i in range(r):
		num *= n - i
		den *= i + 1
	return num // den

def ncr_p(n, r, p):
	(num, den) = (1, 1)
	r = min(r, n - r)
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * modinv(den, p) % p

def isPrime(num):
	if num <= 1:
		return False
	if num == 2 or n == 3:
		return True
	if num % 2 == 0 or num % 3 == 0:
		return False
	m = int(num ** 0.5) + 1
	for i in range(5, m, 6):
		if num % i == 0 or num % (i + 2) == 0:
			return False
	return True

def bin_search(s, low, high, val):
	while low <= high:
		mid = low + (high - low) // 2
		if s[mid] == val:
			return mid
		elif s[mid] < val:
			low = mid + 1
		else:
			high = mid - 1
	return -1

def sumofdigit(num):
	count = 0
	while num:
		count += num % 10
		num //= 10
	return count

def inputmatrix():
	(r, c) = iia()
	mat = [0] * r
	for i in range(r):
		mat[i] = iia()
	return (r, c, mat)

def prefix_sum(n, s):
	for i in range(1, n):
		s[i] += s[i - 1]
	return s

def binomial(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def divisors(n):
	res = []
	for i in range(1, ceil(sqrt(n)) + 1):
		if n % i == 0:
			if i == n // i:
				res.append(i)
			else:
				res.append(i)
				res.append(n // i)
	return res
for _ in range(ii1()):
	n = ii1()
	arr = iia()
	s = 0
	temp = [0] * n
	for i in range(n - 1, -1, -1):
		s += arr[i]
		temp[i] = s
	print(max(temp))


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	neg = 0
	for i in range(n - 1, -1, -1):
		if lis[i] > 0:
			neg -= min(neg, lis[i])
		else:
			neg -= lis[i]
	print(neg)


for _ in range(int(input())):
	n = int(input())
	k = [int(j) for j in input().split()]
	t = 0
	maxx = -1
	for j in range(n - 1, -1, -1):
		t = t + k[j]
		if maxx < t:
			maxx = t
	print(maxx)


def array_cancellation():
	prob = int(input())
	for _ in range(prob):
		l = int(input())
		arr = list(map(int, input().split()))
		arr_transform = list()
		pos = 0
		for v in arr:
			if v > 0:
				pos += v
				arr_transform.append(0)
			elif pos < 1:
				arr_transform.append(v)
			elif pos + v > 0:
				arr_transform.append(0)
				pos += v
			else:
				arr_transform.append(pos + v)
				pos = 0
		print(abs(sum([v for v in arr_transform if v < 0])))
array_cancellation()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	pre = 0
	for i in range(n):
		pre += a[i]
		ans = min(ans, pre)
	ans = abs(ans)
	print(ans)


import sys
import math

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	c = get_array()
	a = c[::-1]
	ans = [a[0]]
	for i in range(1, n):
		ans.append(a[i] + ans[-1])
	print(max(ans))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	ans = 0
	for j in range(n):
		c += a[j]
		if c < 0:
			ans -= c
			c = 0
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	stock = 0
	debt = 0
	for aa in a:
		if aa <= 0:
			if abs(aa) <= stock:
				stock -= abs(aa)
			else:
				debt += abs(aa) - stock
				stock = 0
		else:
			stock += aa
	print(debt)


T = int(input())
for _ in range(T):
	n = int(input())
	x = list(map(int, input().split()))
	idx = len(x) - 1
	s = 0
	res = 0
	while idx >= 0:
		s += x[idx]
		if s > 0:
			res += s
			s = 0
		idx -= 1
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	pos = []
	neg = []
	for i in range(n):
		if p[i] > 0:
			pos.append(i)
		else:
			neg.append(i)
	i = len(pos) - 1
	j = len(neg) - 1
	while i >= 0 and j >= 0:
		if pos[i] > neg[j]:
			i -= 1
		else:
			mn = min(p[pos[i]], abs(p[neg[j]]))
			p[pos[i]] -= mn
			p[neg[j]] += mn
			if p[pos[i]] == 0:
				i -= 1
			if p[neg[j]] == 0:
				j -= 1
	ans = 0
	for i in p:
		if i > 0:
			ans += i
	print(ans)


def nextp(i, a):
	while i < len(a):
		if a[i] <= 0:
			i += 1
		else:
			break
	return i

def nextn(i, a):
	while i < len(a):
		if a[i] >= 0:
			i += 1
		else:
			break
	return i
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = nextp(0, a)
	j = nextn(i, a)
	while i < n and j < n:
		if i < j:
			temp1 = a[i]
			a[i] = max(0, a[i] + a[j])
			a[j] = min(0, temp1 + a[j])
		else:
			j += 1
			j = nextn(j, a)
			if j == n:
				break
		if a[i] == 0:
			i = nextp(i, a)
			if i == n:
				break
		if a[j] == 0:
			j = nextn(j, a)
			if j == n:
				break
	ans = 0
	for i in range(n):
		ans += abs(a[i])
	print(ans // 2)


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	minuses = 0
	ans = 0
	for i in reversed(a):
		if i > 0:
			if minuses > i:
				minuses -= i
			else:
				ans += i - minuses
				minuses = 0
		else:
			minuses += -i
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	if n == 2:
		if A[0] >= A[1]:
			print(0)
			continue
	sump = 0
	sumn = 0
	B = []
	for i in range(n):
		if A[i] > 0:
			sump += A[i]
			if sumn != 0:
				B.append(sumn)
			sumn = 0
		else:
			sumn += A[i]
			if sump != 0:
				B.append(sump)
			sump = 0
	if sumn != 0:
		B.append(sumn)
	if sump != 0:
		B.append(sump)
	for i in range(0, len(B) - 1):
		if B[i] < 0:
			continue
		else:
			a = min(B[i], abs(B[i + 1]))
			B[i + 1] += a
			B[i] -= a
			if i + 2 != len(B):
				B[i + 2] += B[i]
				B[i] = 0
	ans = 0
	for i in B:
		if i > 0:
			ans += i
	print(abs(ans))


num = int(input())
for _ in range(num):
	x = int(input())
	l = list(map(int, input().split()))
	total = 0
	for i in range(x):
		if l[i] > 0:
			total += l[i]
		elif l[i] < 0:
			z = min(total, -l[i])
			l[i] += z
			total -= z
	total = 0
	for i in l:
		if i < 0:
			total += abs(i)
	print(total)


t = int(input())
for i in range(t):
	n = int(input())
	ai = list(map(int, input().split()))
	num = 0
	ans = 0
	for i in range(n):
		num += ai[i]
		if num < 0:
			ans -= num
			num = 0
	print(ans)


import sys, threading
sys.setrecursionlimit(10 ** 8)
threading.stack_size(10 ** 8)
from collections import defaultdict, Counter
import math

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		vals = list(map(int, input().split()))
		prevPos = []
		for i in range(n):
			if vals[i] > 0:
				prevPos.append(vals[i])
			while vals[i] < 0 and prevPos:
				pos = prevPos.pop()
				minVal = min(pos, abs(vals[i]))
				pos -= minVal
				vals[i] += minVal
				if pos > 0:
					prevPos.append(pos)
		print(sum((max(0, 0 - val) for val in vals)))
threading.Thread(target=main).start()


t = int(input())
while t:
	n = int(input())
	a = [int(i) for i in input().split()]
	redneg = 0
	for ele in a[::-1]:
		if ele > 0:
			if redneg > 0:
				redneg = max(redneg - ele, 0)
		else:
			redneg += abs(ele)
	print(redneg)
	t -= 1


def solve(arr, n):
	neg_to_right = [0 for i in range(n)]
	cost = 0
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			if arr[i] > 0:
				cost += arr[i]
			continue
		if arr[i + 1] < 0:
			neg_to_right[i] = neg_to_right[i + 1] + abs(arr[i + 1])
		else:
			neg_to_right[i] = neg_to_right[i + 1]
		if arr[i] > 0:
			if neg_to_right[i] >= arr[i]:
				neg_to_right[i] = neg_to_right[i] - arr[i]
			else:
				cost = cost + arr[i] - neg_to_right[i]
				neg_to_right[i] = 0
	return cost
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr, n))


test = int(input())

def getanswer(n, a):
	ans = 0
	res = 0
	for i in range(n):
		if a[i] > 0:
			res += a[i]
			a[i] = 0
		else:
			k = a[i]
			a[i] += res
			a[i] = min(0, a[i])
			res += k
			res = max(0, res)
	ans = 0
	a.sort()
	for i in range(n):
		if a[i] < 0:
			ans += a[i]
	print(abs(ans))
while test:
	test -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	getanswer(n, a)


def solve(arr, m):
	res = 0
	p = 0
	n = 0
	for i in range(m):
		if arr[i] >= 0:
			p += arr[i]
		else:
			n += arr[i]
		res = max(res, abs(n) - p)
	return res
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr, n))


for _ in range(int(input())):
	ans = 0
	z = 0
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n):
		if l[i] < 0:
			ans += l[i]
			if ans < 0:
				z = min(ans, z)
		else:
			ans += l[i]
	print(abs(z))


def solve(L):
	newL = []
	store = 0
	for i in L:
		store += i
		if store < 0:
			newL.append(store)
			store = 0
	newL.append(store)
	return newL
for i in ' ' * int(input()):
	n = int(input())
	print(solve(list(map(int, input().split())))[-1])


from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + '\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	curr = 0
	for i in range(n):
		if a[i] > 0:
			curr += a[i]
		if a[i] < 0 and curr:
			(a[i], curr) = (a[i] + min(abs(a[i]), curr), curr - min(abs(a[i]), curr))
	print(curr)


for test in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	for i in l:
		c = c + i
		if c < 0:
			c = 0
	print(c)


n = int(input())
for i in range(n):
	m = int(input())
	a = [int(i) for i in input().split()]
	flag = 0
	e = 0
	for j in range(0, len(a)):
		if a[j] > 0:
			e = e + a[j]
			flag = 1
		elif flag == 0:
			pass
		else:
			e = e + a[j]
			if e < 0:
				e = 0
			else:
				pass
	print(e)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	count = 0
	for i in l:
		sum += i
		if sum < 0:
			count += abs(sum)
		sum = max(sum, 0)
	print(count)


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	cn = 0
	for i in range(n):
		if a[i] > 0:
			for j in range(max(i + 1, cn), n):
				cn = j
				if a[j] < 0:
					x = min(abs(a[j]), a[i])
					a[i] -= x
					a[j] += x
				if a[i] == 0:
					break
	summ = 0
	for i in a:
		summ += max(i, 0)
	print(summ)


t = int(input())
for tc in range(t):
	n = int(input())
	line = input().split()
	arr = []
	for i in range(n):
		arr.append(int(line[i]))
	score1 = 0
	sum1 = 0
	for i in range(n):
		sum1 += arr[i]
		if sum1 < 0:
			score1 -= sum1
			sum1 = 0
	print(score1)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	s = 0
	p = 0
	for i in range(n - 1, -1, -1):
		if a[i] < 0:
			s += a[i]
		elif a[i] > 0:
			t = abs(s)
			if t > a[i]:
				s += a[i]
				a[i] = 0
			else:
				a[i] += s
				s = 0
				p += a[i]
	print(p)


for cnt in range(int(input())):
	n = int(input())
	P = list(map(int, input().split()))
	res = 0
	s = 0
	for p in P:
		s += p
		if s < 0:
			res += s
			s = 0
	print(abs(res))


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	bag = 0
	for j in range(n):
		if arr[j] > 0:
			bag += arr[j]
		elif arr[j] < 0:
			ab = abs(arr[j])
			if bag > ab:
				bag -= ab
				arr[j] = 0
			else:
				arr[j] += bag
				bag = 0
	print(bag)


import os
from heapq import heapify, heappop, heappush
import sys
import math
import operator
import bisect
from collections import defaultdict
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		ar = inpt()
		ok = False
		val = 0
		ct = 0
		for i in range(n - 1, -1, -1):
			val += ar[i]
			if val >= 0:
				ct += val
				val = 0
		print(ct)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout
from collections import deque as dq
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	c = []
	for i in a:
		if i > 0:
			b.append(i)
		elif i < 0:
			x = i
			while b and x < 0:
				r = min(b[-1], -x)
				x += r
				b[-1] -= r
				if b[-1] == 0:
					b.pop()
			if len(b) == 0:
				c.append(abs(x))
	print(sum(b))


def findarr1(arr):
	arr1 = []
	i = 0
	while i < len(arr):
		sum1 = 0
		j = i
		if arr[j] <= 0:
			while j < len(arr):
				if arr[j] <= 0:
					sum1 += arr[j]
				else:
					break
				j += 1
			arr1.append(sum1)
		elif arr[j] >= 0:
			while j < len(arr) and arr[j] >= 0:
				sum1 += arr[j]
				j += 1
			arr1.append(sum1)
		i = j
	return arr1
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sum1 = 0
	sum2 = 0
	arr = findarr1(arr)
	while True:
		if arr[0] <= 0:
			i = 1
			while i < len(arr):
				if i + 1 < len(arr):
					minm = min(abs(arr[i]), abs(arr[i + 1]))
					arr[i] -= minm
					arr[i + 1] += minm
				i += 2
		else:
			i = 0
			while i < len(arr):
				if i + 1 < len(arr):
					minm = min(abs(arr[i]), abs(arr[i + 1]))
					arr[i] -= minm
					arr[i + 1] += minm
				i += 2
		arr = findarr1(arr)
		if len(arr) == 2 and arr[0] <= 0 and (arr[1] >= 0):
			print((abs(arr[0]) + arr[1]) // 2)
			break
		elif len(arr) == 1:
			print(0)
			break


from collections import deque

def func():
	nn = int(input())
	for _ in range(nn):
		_ = int(input())
		values = list(map(int, input().split()))
		que = deque(values)
		res = 0
		cun = 0
		while que:
			m = que.pop()
			if m <= 0:
				cun += m
			else:
				x = m + cun
				if x <= 0:
					cun = x
					continue
				cun = 0
				res += x
		print(res)
func()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in range(n):
		s += arr[i]
		ans = min(ans, s)
	print(abs(ans))


from sys import stdout, stdin
from collections import defaultdict, deque
import math
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	l = list(map(int, stdin.readline().split()))
	(a, b) = (0, 0)
	z = 0
	for i in l[::-1]:
		if i < 0:
			b += -i
			a = 0
		else:
			a += i
			temp = min(b, a)
			b -= temp
			a -= temp
	print(b)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	j = 0
	cnt = 0
	while j < len(l) and i < len(l):
		if l[i] <= 0:
			i += 1
		if l[j] >= 0 or i > j:
			j += 1
		elif l[i] > 0 and l[j] < 0:
			temp1 = max(0, l[i] + l[j])
			temp2 = min(0, l[i] + l[j])
			l[i] = temp1
			l[j] = temp2
	for i in l:
		if i < 0:
			cnt += -1 * i
	print(cnt)


t = 1
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	summ = 0
	for el in reversed(a):
		summ += el
		ans = max(summ, ans)
	print(ans)


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def graph(n, m):
	adj = dict()
	for i in range(1, n + 1):
		adj.setdefault(i, 0)
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a] += 1
		adj[b] += 1
	return adj
import sys
import threading
from math import *
sys.setrecursionlimit(300000)
threading.stack_size(10 ** 8)
po = int(log(10 ** 18)) + 2
li = []
for i in range(po):
	li.append(3 ** i)
li = li[::-1]
s = sum(li)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	available = 0
	ans = 0
	for i in range(n):
		if a[i] > 0:
			available += a[i]
		else:
			value = abs(a[i])
			diff = min(available, value)
			available -= diff
			a[i] += diff
			ans += abs(a[i])
	print(ans)


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def get_input(a=str):
	return a(input())

def get_int_input():
	return get_input(int)

def get_input_arr(a):
	return list(map(a, input().split()))

def get_int_input_arr():
	return get_input_arr(int)

def solve():
	t = get_int_input()
	for _ in range(t):
		n = get_int_input()
		arr = get_int_input_arr()
		ptr_1 = 0
		ptr_2 = 0
		while ptr_1 < n:
			while ptr_1 < n and arr[ptr_1] <= 0:
				ptr_1 += 1
				ptr_2 = ptr_1 + 1
			while ptr_2 < n and arr[ptr_2] >= 0:
				ptr_2 += 1
			if ptr_2 == n:
				break
			if ptr_2 < n and arr[ptr_1] > 0:
				v = arr[ptr_1]
				arr[ptr_1] = max(0, arr[ptr_1] + arr[ptr_2])
				arr[ptr_2] = min(0, v + arr[ptr_2])
			if ptr_1 < n and arr[ptr_1] == 0:
				ptr_1 += 1
		res = 0
		for i in range(n):
			if arr[i] > 0:
				res += arr[i]
		cout << res << endl

def main():
	solve()
main()


def find_pos(a, i, m):
	while i < m:
		if a[i] > 0:
			return i
		else:
			i += 1
	return m

def main():
	n = int(input())
	for __ in range(n):
		m = int(input())
		a = list(map(int, input().split()))
		i = 0
		i = find_pos(a, i, m)
		neg = i + 1
		while neg < m:
			if a[neg] < 0:
				if a[i] + a[neg] >= 0:
					a[i] += a[neg]
					a[neg] = 0
					neg += 1
				else:
					a[neg] += a[i]
					a[i] = 0
					i = find_pos(a, i, m)
					if i > neg:
						neg = i + 1
			elif a[neg] >= 0 or neg <= i:
				neg += 1
		ans = 0
		for i in range(m):
			if a[i] > 0:
				ans += a[i]
		print(ans)
main()


def solution(n, a):
	(temp, min_) = (0, 0)
	for x in a:
		temp += x
		min_ = min(min_, temp)
	return -min_
t = int(input())
for _ in range(t):
	n = map(int, input().split(' '))
	p = list(map(int, input().split(' ')))
	print(solution(n, p))


t = int(input())
while t > 0:
	n = int(input())
	l = [int(s) for s in input().split()]
	s = c = 0
	for i in range(n):
		if s == 0 and l[i] < 0:
			c += abs(l[i])
		else:
			s += l[i]
		if s < 0:
			c += abs(s)
			s = 0
	print(c)
	t -= 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	j = 0
	while i < n and j < n:
		if a[i] > 0 and a[j] < 0 and (i < j):
			tmp = min(a[i], abs(a[j]))
			a[j] = a[j] + tmp
			a[i] = a[i] - tmp
			if a[j] == 0:
				j += 1
			else:
				i += 1
		elif a[i] <= 0:
			i += 1
		else:
			j += 1
	totalpositive = 0
	for e in a:
		if e > 0:
			totalpositive += e
	print(totalpositive)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	l = I()
	an = 0
	i = 0
	pr = 0
	le = 0
	re = 0
	while i < n:
		while i < n and l[i] >= 0:
			le += l[i]
			i += 1
		while i < n and l[i] <= 0:
			re += l[i]
			i += 1
		an += abs(min(0, le + re))
		if le + re >= 0:
			le = le + re
			re = 0
		else:
			re = 0
			le = 0
	print(an)


from fractions import Fraction
import bisect
import os
import io
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
from queue import Queue

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
t = iinput()
for i in range(t):
	n = iinput()
	a = rlinput()
	temp = 0
	ans = 0
	for i in range(n):
		if a[i] < 0:
			if temp + a[i] >= 0:
				temp += a[i]
			else:
				ans += -1 * (temp + a[i])
				temp = 0
		else:
			temp += a[i]
	print(ans)


def inp_n():
	return int(input())

def inp_list():
	return list(map(int, input().split()))

def inp_mul_num():
	return map(int, input().split())

def is_odd(n):
	if n & 1:
		return True
	return False

def is_prime(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
from collections import defaultdict
import math
for _ in range(inp_n()):
	n = inp_n()
	l = inp_list()
	s = 0
	for i in range(n):
		s += l[i]
		if s < 0:
			s = 0
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	negative_suffix = [0 for _ in range(n)]
	coins = 0
	if a[-1] < 0:
		negative_suffix[-1] = abs(a[-1])
	else:
		coins += a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] > 0:
			coins += max(a[i] - negative_suffix[i + 1], 0)
			negative_suffix[i] = max(negative_suffix[i + 1] - a[i], 0)
		else:
			negative_suffix[i] = abs(a[i]) + negative_suffix[i + 1]
	print(coins)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	c = 0
	for i in a:
		c += i
		b.append(c)
	print(abs(min(b)))


tst = int(input())
t = 1
while t <= tst:
	n = int(input())
	ls = list(map(int, input().split()))
	i = pos = neg = 0
	while i < n:
		x = ls[i]
		if x >= 0:
			pos += x
		elif pos + x >= 0:
			pos += x
		else:
			x += pos
			pos = 0
			neg += x
		i += 1
	print(-neg)
	t += 1


rr = lambda : input().strip()
rrm = lambda : map(int, rr().split())

def solve():
	n = int(rr())
	a = list(rrm())
	(ans, res) = (0, 0)
	for i in range(n - 1, -1, -1):
		res += a[i]
		if res > 0:
			ans += res
			res = 0
	return ans
T = int(rr())
for _ in range(T):
	ans = solve()
	print(ans)


import sys
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	for i in a:
		l.append(0 - i)
	sum = 0
	m = sys.maxsize
	for i in a:
		sum += i
		if m > sum:
			m = sum
	print(abs(m))


n = int(input())
for i in range(0, n):
	l = int(input())
	a = list(map(int, input().strip().split()))
	c = 0
	k = []
	for i in a:
		c += i
		k.append(c)
	t = min(k)
	print(abs(t))


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	p = 0
	ans = 0
	for i in range(n):
		if a[i] > 0:
			p += a[i]
		else:
			ans += max(0, -a[i] - p)
			p = max(0, p + a[i])
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	pos = 0
	for j in range(n):
		if l[j] > 0:
			pos += l[j]
		elif l[j] < 0:
			if pos > 0:
				if pos + l[j] >= 0:
					pos += l[j]
				else:
					pos = 0
	print(pos)


def maxSubArraySum(arr):
	a = 0
	b = 0
	x = 0
	y = 0
	for i in range(n):
		x += arr[i]
		if a > x:
			a = x
	for i in range(n - 1, -1, -1):
		y += arr[i]
		if b < y:
			b = y
	return max(abs(a), b)
for ad in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(maxSubArraySum(l))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	anspos = 0
	ansneg = 0
	for i in range(n):
		if l[i] < 0:
			if anspos != 0:
				num = abs(l[i]) - anspos
				if num > 0:
					anspos = 0
					ansneg = ansneg + num
				else:
					anspos = anspos - abs(l[i])
			else:
				ansneg = ansneg + l[i]
		else:
			anspos = anspos + l[i]
	print(max(anspos, abs(ansneg)))


import sys
input = sys.stdin.readline
read = lambda : map(int, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
from bisect import bisect_left

def solve():
	n = int(input())
	a = list(read())
	if n < 2:
		print(0)
		return
	for i in range(n - 1):
		if a[i] > 0:
			a[i + 1] += a[i]
			a[i] -= a[i]
	print(max(a))
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	m = [int(i) for i in input().split()]
	k = 0
	L = 0
	for i in range(len(m)):
		if m[i] > 0:
			L += m[i]
		elif m[i] < 0 and L > 0:
			L += m[i]
		else:
			k += abs(m[i])
		if L < 0:
			k += abs(L)
			L = 0
	k += L
	print(k // 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	for i in range(len(a)):
		s = s + a[i]
		if s < 0:
			s = 0
	print(s)


from sys import stdin, stdout

def find_cost(arr):
	i = 0
	j = i + 1
	cost = 0
	while j < len(arr):
		if arr[i] <= 0:
			cost += abs(arr[i])
			i += 1
			if j <= i:
				j += 1
			continue
		if arr[j] >= 0:
			j += 1
			continue
		ctz = min([arr[i], abs(arr[j])])
		arr[i] -= ctz
		arr[j] += ctz
	return cost
t = int(stdin.readline())
for i in range(t):
	n = stdin.readline()
	arr = [int(x) for x in stdin.readline().split()]
	print(find_cost(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n - 1):
		if a[i] > 0:
			a[i + 1] = a[i + 1] + a[i]
			a[i] = 0
	s = 0
	for j in a:
		s += abs(j)
	print(s // 2)


import math
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	co = 0
	for i in range(n - 1):
		if a[i] >= a[i + 1]:
			a[i + 1] += a[i]
			a[i] = 0
		elif a[i] > 0 and a[i + 1] > 0:
			a[i + 1] += a[i]
			a[i] = 0
		else:
			co += a[i]
	print(sum(a) - co)


def func(A):
	temp = []
	(pos, neg) = (0, 0)
	for i in range(len(A)):
		if A[i] > 0:
			break
		else:
			neg += A[i]
	if neg < 0:
		temp.append(neg)
	neg = 0
	for j in range(i, len(A)):
		if A[j] > 0:
			pos += A[j]
			if neg:
				temp.append(neg)
				neg = 0
		elif A[j] < 0:
			neg += A[j]
			if pos:
				temp.append(pos)
				pos = 0
	if neg:
		temp.append(neg)
	if pos:
		temp.append(pos)
	temp.append(0)
	new = []
	i = 0
	pre = 0
	new = []
	for i in range(len(temp)):
		pre += temp[i]
		if pre < 0:
			new.append(pre)
			pre = 0
	if pre < 0:
		new.append(pre)
	return -sum(new)
t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	print(func(A))


from math import ceil
from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	remainder = deque()
	for i in range(n):
		if a[i] > 0:
			remainder.append(i)
		elif a[i] < 0:
			j = 0
			n1 = len(remainder)
			while a[i] < 0 and j < n1:
				steps = -a[i]
				if steps == a[remainder[j]]:
					a[remainder[j]] = 0
					a[i] = 0
					remainder.popleft()
					break
				elif steps > a[remainder[j]]:
					a[i] += a[remainder[j]]
					a[remainder[j]] = 0
					remainder.popleft()
					j -= 1
					n1 -= 1
				else:
					a[remainder[j]] += a[i]
					a[i] = 0
					break
				j += 1
	ans = 0
	for i in range(n):
		if a[i] > 0:
			ans += a[i]
	print(ans)


import sys
t = int(sys.stdin.readline())
ans_arr = []
for i in range(t):
	n = int(sys.stdin.readline())
	a = [int(j) for j in sys.stdin.readline().split()]
	flag = False
	for w in range(n):
		if a[w] != 0:
			flag = True
			break
	if flag:
		pos = n
		neg = n
		for e in range(n):
			if a[e] > 0:
				pos = e
				break
		for f in range(pos + 1, n):
			if a[f] < 0:
				neg = f
				break
		while True:
			if pos == n or neg == n:
				break
			val = min(abs(a[neg]), a[pos])
			a[neg] += val
			a[pos] -= val
			if a[pos] == 0:
				cp = False
				for h in range(pos + 1, n):
					if a[h] > 0:
						pos = h
						cp = True
						break
				if cp == False:
					break
			if a[neg] == 0 or pos > neg:
				cn = False
				for h in range(max(pos, neg) + 1, n):
					if a[h] < 0:
						neg = h
						cn = True
						break
				if cn == False:
					break
		answer = 0
		for p in range(n):
			if a[p] > 0:
				answer += a[p]
		ans_arr.append(str(answer))
	else:
		ans_arr.append('0')
print('\n'.join(ans_arr))


import io, os
inp = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
inst = lambda : inp().decode().strip()
from collections import deque as que
_T_ = inin()
for _t_ in range(_T_):
	n = inin()
	a = inar()
	dq = que()
	for i in range(n):
		if a[i] < 0:
			dq.append(i)
	for i in range(n):
		while dq and a[i] > 0:
			j = dq.popleft()
			if j < i:
				pass
			else:
				minu = min(a[i], abs(a[j]))
				a[i] -= minu
				a[j] += minu
				if a[j] < 0:
					dq.appendleft(j)
	ans = 0
	for i in a:
		if i > 0:
			ans += i
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	lst = []
	lst.append(a[0])
	for i in range(1, n):
		a[i] += a[i - 1]
		lst.append(a[i])
	m = lst[0]
	for i in range(1, n):
		m = min(m, lst[i])
	print(m - 2 * m)


test = int(input())
for t in range(0, test):
	nn = int(input())
	a = [int(i) for i in input().split(' ')]
	p = 0
	n = 0
	i = nn - 1
	while i > 0:
		if a[i] > 0:
			n += a[i]
			if n > 0:
				p += n
				n = 0
		elif a[i] < 0:
			n += a[i]
		i -= 1
	print(p)


import math
t = int(input())
ans = []

def solve(arr, n):
	pos = 0
	neg = 0
	for j in range(n):
		if arr[j] > 0:
			pos += arr[j]
		else:
			pos -= min(pos, abs(arr[j]))
			arr[j] = abs(arr[j]) - min(pos, abs(arr[j]))
			neg += arr[j]
	return pos
	pass
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans.append(solve(arr, n))
for test in ans:
	print(test)


cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	for i in a:
		if s < 0:
			s = i
		else:
			s += i
	print(s)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = list(map(int, input().split()))
	pos = []
	neg = []
	for i in range(n):
		if a[i] > 0:
			pos += [i]
		if a[i] < 0:
			neg += [i]
	(i, j) = (0, 0)
	while i < len(pos) and j < len(neg):
		if pos[i] < neg[j]:
			x = min(a[pos[i]], -a[neg[j]])
			a[pos[i]] -= x
			a[neg[j]] += x
			if a[neg[j]] == 0:
				j += 1
			else:
				i += 1
		elif neg[j] > pos[i]:
			i += 1
		else:
			j += 1
	ans = 0
	for i in a:
		if i > 0:
			ans += i
	print(ans)


def answer(n, A):
	count = 0
	for i in range(n):
		if A[i] > 0:
			count += A[i]
		elif A[i] < 0:
			count = max(0, count + A[i])
	return count
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(answer(n, arr))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	pos = 0
	neg = 0
	i = n - 1
	while i >= 0:
		if l[i] > 0:
			a = min(neg, l[i])
			neg -= a
			l[i] -= a
			pos += l[i]
		else:
			neg -= l[i]
		i -= 1
	print(pos)


def main():
	t = int(input())
	for test in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		ans = 0
		sum = 0
		for i in range(n):
			sum += arr[i]
			if sum < 0:
				ans = max(ans, abs(sum))
		print(ans)
main()


import sys
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	j = 0
	ans = 0
	while i < n and j < n:
		if a[i] <= 0:
			i += 1
		else:
			if j <= i:
				j = i + 1
			while j < n and a[j] >= 0:
				j += 1
			if j < n and a[i] > 0 and (a[j] < 0):
				m = min(a[i], abs(a[j]))
				a[i] -= m
				a[j] += m
			if a[i] == 0:
				i += 1
			if j < n and a[j] == 0:
				j += 1
	neg = 0
	pos = 0
	for i in range(n):
		if a[i] < 0:
			neg += a[i]
		else:
			pos += a[i]
	print(max(abs(neg), pos))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in range(n):
		s += l[i]
		if s < 0:
			ans += abs(s)
			s = 0
	print(ans)


T = int(input())
for case in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	k = 0
	for i in a:
		if i < 0:
			if i + k <= 0:
				ans += -(i + k)
				k = 0
			else:
				k += i
		else:
			k += i
	print(ans)


for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	run = 0
	for i in range(n - 1, -1, -1):
		if arr[i] < 0:
			run += arr[i]
		elif run < 0:
			temp = arr[i]
			arr[i] = max(0, arr[i] + run)
			run = min(0, temp + run)
	res = 0
	for i in range(n):
		res += arr[i] if arr[i] > 0 else 0
	print(res)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	ps = ng = acc_ng = 0
	for v in a:
		if v >= 0:
			ps += v
		else:
			abs_neg = abs(v)
			if ps >= abs_neg:
				ps -= abs_neg
			else:
				acc_ng += abs_neg - ps
				ps = 0
	print(acc_ng)


t = int(input())
while t != 0:
	n = int(input())
	list1 = list(map(int, input().split()))
	ans = []
	s = 0
	for i in range(n):
		s = s + list1[i]
		if s >= 0:
			ans.append(s)
		else:
			ans.append(0)
			s = 0
	print(ans[-1])
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	for i in range(n):
		if a[i] < 0 and x > a[i]:
			y = min(x, -1 * a[i])
			x -= y
			a[i] += y
		if a[i] > 0:
			x += a[i]
	s = 0
	for i in range(n):
		if a[i] < 0:
			s -= a[i]
	print(s)


def posshift(l):
	k = 0
	c = len(l)
	for i in l:
		if k == c - 1:
			return i
		elif i > 0:
			l[k + 1] += i
		k += 1

def foo(x):
	x = x.split()
	for (i, v) in enumerate(x):
		x[i] = int(v)
	return x
i1 = int(input())
ls = []
for a in range(i1):
	r1 = input()
	r2 = input()
	ans = posshift(foo(r2))
	ls.append(ans)
for j in ls:
	print(j)


def fillPrefixSum(arr, n, prefixSum):
	prefixSum[0] = arr[0]
	for i in range(1, n):
		prefixSum[i] = prefixSum[i - 1] + arr[i]
	return prefixSum
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n):
		l[i] = -l[i]
	prefixSum = [0 for i in range(n + 1)]
	res = fillPrefixSum(l, n, prefixSum)
	m = 0
	for i in res:
		if i > 0 and i > m:
			m = i
	print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	res = 0
	for i in range(len(a)):
		res = res + a[i] if res + a[i] > 0 else 0
	print(res)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	a = li()
	s = 0
	c = 0
	for i in range(n - 1, -1, -1):
		if a[i] > 0 and s + a[i] > 0:
			c = max(c, s + a[i])
		s += a[i]
	print(c)
for _ in range(I()):
	solve()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	for i in range(n - 2, -1, -1):
		if l[i] < 0:
			c += l[i]
		elif l[i] >= -1 * c:
			c = 0
		else:
			c = c + l[i]
	if c < 0:
		print(-1 * c)
	else:
		print(c)


for _ in range(int(input())):
	input()
	arr = list(map(int, input().split()))
	(cost, i) = (0, 0)
	while i < len(arr) - 1:
		if arr[i] <= 0:
			arr[i] = abs(arr[i])
			i += 1
		else:
			arr[i + 1] = arr[i + 1] + arr[i]
			arr[i] = 0
			i += 1
	print(sum(arr) // 2)


a = int(input())
for i in range(a):
	b = int(input())
	l = list(map(int, input().split()))
	d = 0
	c = 0
	for i in range(b):
		c += l[i]
		if c < 0:
			d += abs(c)
			c = 0
	print(d)


for _ in range(int(input())):
	i_liczb = int(input())
	liczby = list(map(int, input().split()))
	freco = 0
	for x in liczby:
		if x > 0:
			freco += x
		else:
			freco = max(0, freco + x)
	print(freco)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = array()
	ans = 0
	neg = 0
	for i in range(n - 1, -1, -1):
		if a[i] < 0:
			neg += abs(a[i])
		else:
			neg -= min(neg, a[i])
	print(neg)


import sys
from collections import defaultdict
name = sys.stdin.read()
jobGrid = []
isJob = True
row = []
grid = name.split('\n')
grid = grid[:-1]
number_of_cases = int(grid[0])
for i in range(number_of_cases):
	l = list(map(int, grid[i * 2 + 2].split()))
	final = 0
	value = 0
	for j in l:
		if j > 0:
			value += j
		else:
			value += j
			if value < 0:
				final -= j
				final += value
				value = 0
			else:
				final -= j
	pos = []
	for i in l:
		if i > 0:
			pos.append(i)
	total = sum(pos)
	answer = total - final
	print(answer)


t = int(input())
for i in range(t):
	n = int(input())
	w = [int(k) for k in input().split()]
	(c, res) = (0, 0)
	for j in w:
		c += j
		if c < 0:
			res += abs(c)
			c = 0
	print(res)


def sp():
	return map(int, input().split())
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(sp())
	ans = 0
	for i in range(n):
		ans = max(ans, 0)
		ans += l[i]
	print(ans)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(ans, s) = (-pow(10, 10), 0)
	for i in range(n - 1, -1, -1):
		s += a[i]
		ans = max(ans, s)
	print(ans)


from math import *
from bisect import *
from collections import Counter, defaultdict, deque
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	n = I()
	a = LI()
	ans = 0
	c = 0
	for i in range(n):
		if a[i] < 0:
			if a[i] + c < 0:
				ans += abs(a[i] + c)
				c = 0
			else:
				c += a[i]
		else:
			c += a[i]
	if c > 0:
		ans = max(ans, c)
	print(ans)


def algo(n, arr):
	res = 0
	p = 0
	for i in arr:
		p += i
		if p < 0:
			res += p
			p = 0
	return abs(res)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = algo(n, arr)
	print(ans)


import sys

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		a = Ilist()
		total_pos_before = 0
		total_neg_before = 0
		ans = 0
		for ai in a:
			if ai < 0:
				if total_pos_before:
					t = min(total_pos_before, abs(ai))
					total_pos_before -= t
					ai += t
				ans += abs(ai)
			elif ai > 0:
				total_pos_before += ai
		print(ans)
en = enumerate

def debug(*args, c=6):
	pass
FIO = 10
BUFSIZE = 8 * 1024

def Istr():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import os

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	c = 0
	for i in range(n):
		if arr[i] < 0:
			c = max(c - abs(arr[i]), 0)
		else:
			c = c + arr[i]
	print(c)


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = inar()
		ans = 0
		sm = 0
		for i in range(n - 1, -1, -1):
			sm += arr[i]
			if sm > 0:
				ans += sm
				sm = 0
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


cases = int(input())
for _ in range(cases):
	n = int(input())
	c = list(map(int, input().split()))
	cur = 0
	for i in range(n):
		cur = max(0, cur + c[i])
	print(cur)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	sum1 = 0
	coins = 0
	for num in range(n):
		sum1 += l[num]
		if sum1 < 0:
			coins += abs(sum1)
			sum1 = 0
	print(coins)


def arr_cancel(arr):
	neg = 0
	pos = 0
	for i in range(len(arr)):
		if arr[i] < 0:
			if pos:
				m = min(abs(arr[i]), pos)
				pos -= m
				arr[i] += m
			neg += arr[i]
		elif arr[i] > 0:
			pos += arr[i]
	return pos
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(arr_cancel(arr))


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	l = 0
	for i in b:
		if i > 0:
			l += i
		else:
			l -= min(abs(i), l)
	print(l)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
for _ in range(ii()):
	n = ii()
	a = il()
	ans = []
	ps = 0
	for i in range(0, n):
		ps = ps + a[i]
		ans.append(ps)
	if min(ans) >= 0:
		print(0)
	else:
		print(abs(min(ans)))


def calc():
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	rans = 0
	for i in arr:
		ans += i
		if ans < 0:
			rans = min(ans, rans)
	print(-rans)
t = int(input())
while t:
	calc()
	t -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	for i in range(n):
		if l[i] > 0:
			sum += l[i]
		elif sum > 0:
			sum = sum + l[i]
			if sum < 0:
				sum = 0
	print(sum)


t = int(input())
for _ in range(t):
	lim = int(input())
	arr = list(map(int, input().split()))
	result = 0
	temp = 0
	for i in arr:
		if temp == 0 and i < 0:
			result -= i
		elif i > 0 and temp >= 0:
			temp += i
		elif i < 0 and temp > 0:
			if -i >= temp:
				temp = temp + i
				result -= temp
				temp = 0
			else:
				temp += i
	print(result)


import sys

def solve(arr):
	n = len(arr)
	pos = 0
	neg = 0
	coins = 0
	for i in range(n):
		if arr[i] < 0:
			spent = min(-arr[i], pos)
			pos -= spent
			arr[i] += spent
			if arr[i] < 0:
				coins += -arr[i]
				neg += arr[i]
		elif arr[i] > 0:
			pos += arr[i]
	return coins
caseCount = int(next(sys.stdin))
for i in range(caseCount):
	next(sys.stdin)
	x = next(sys.stdin)[:-1]
	arr = [int(j) for j in x.split(' ')]
	ans = solve(arr)
	print(ans)


import sys
import math

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def sinp():
	return sys.stdin.readline().strip()

def iinp():
	return int(input())

def ninp():
	return map(int, sys.stdin.readline().strip().split())

def linp():
	return list(map(int, sys.stdin.readline().strip().split()))

def p(a):
	print(a)
for _ in range(iinp()):
	n = iinp()
	a = linp()
	neg = 0
	pos = 0
	sum = 0
	for i in a:
		if i < 0:
			sum = pos + i
			if sum >= 0:
				pos = sum
			else:
				neg = neg + sum
				pos = 0
		elif i == 0:
			pass
		elif i > 0:
			pos = pos + i
	print(pos)


from itertools import accumulate
q = int(input())
for _ in range(q):
	n = int(input())
	s = list(map(int, input().split()))
	pos = [x for x in s if x > 0]
	res = sum(pos)
	acc = accumulate(s)
	r_acc = [-x for x in acc]
	print(max(r_acc))


h = int(input())
l = []
for i in range(h):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	big = 0
	small = 0
	b = [0] * n
	for i in range(len(a)):
		if a[i] == 0:
			continue
		if a[i] > 0:
			big += a[i]
		elif big + a[i] >= 0:
			big = big + a[i]
		else:
			small += a[i] + big
			big = 0
			sum -= small
			small = 0
	print(sum)


for _ in range(int(input())):
	input()
	a = list(map(int, input().split()))
	s = 0
	negsum = 0
	m = 0
	while len(a) > 0:
		i = a.pop()
		if i > 0:
			m += max(i + negsum, 0)
			negsum = min(i + negsum, 0)
		else:
			negsum += i
	print(m)


import atexit
import io
import sys
from collections import deque
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
for case in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = 0
	if n > 1:
		pos = deque()
		neg = deque()
		for (i, v) in enumerate(a):
			if v < 0:
				neg.append(i)
			elif v > 0:
				pos.append(i)
		while pos and neg:
			if neg[0] < pos[0]:
				while a[neg[0]]:
					_min = min(a[pos[-1]], abs(a[neg[0]]))
					ans += _min
					a[neg[0]] += _min
					a[pos[-1]] -= _min
					if not a[pos[-1]]:
						pos.pop()
				neg.popleft()
			else:
				while a[pos[0]]:
					_min = min(a[pos[0]], abs(a[neg[0]]))
					a[pos[0]] -= _min
					a[neg[0]] += _min
					if not a[neg[0]]:
						neg.popleft()
				pos.popleft()
	print(ans)


tc = int(input())
for _ in range(tc):
	n = int(input())
	a = list(map(int, input().split(' ')))
	cost = 0
	for e in a:
		if e > 0:
			cost += e
		else:
			cost -= min(cost, -e)
	print(cost)


def count_coinFunc(a_list):
	count = 0
	for i in range(len(a_list)):
		if a_list[i] >= 0:
			count += a_list[i]
			a_list[i] = 0
		else:
			min_val = 0
			min_val = min(-a_list[i], count)
			a_list[i] += min_val
			count -= min_val
	for j in range(len(a_list)):
		if a_list[i] >= 0:
			count += a_list[i]
	return count
t = int(input())
temp_arr = []
for i in range(t):
	n = int(input())
	a_list = list(map(int, input().split()))
	min_coins = count_coinFunc(a_list)
	temp_arr.append(min_coins)
for j in temp_arr:
	print(j)


t = int(input())
for z in range(t):
	stored = 0
	ans = 0
	length = int(input())
	array = input().split()
	for i in array:
		if int(i) < 0 and stored == 0:
			ans -= int(i)
		elif int(i) < 0 and stored > 0:
			stored += int(i)
			if stored < 0:
				ans -= stored
				stored = 0
		elif int(i) > 0:
			stored += int(i)
	print(ans)


from itertools import accumulate
q = int(input())
for _ in range(q):
	n = int(input())
	s = list(map(int, input().split()))
	acc = accumulate(s)
	r_acc = [-x for x in acc]
	print(max(r_acc))


t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	answer = 0
	for v in array:
		answer = max(0, answer + v)
	print(answer)


import sys, collections
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	ci = [arr[n - 1]]
	for i in range(n - 2, -1, -1):
		ci.append(ci[-1] + arr[i])
	ci.reverse()
	print(max(ci))


import sys
intinp = lambda : int(input())

def listinp(func=int):
	return list(map(func, input().split()))

def nsepline(n, func=str):
	return [func(input()) for _ in range(n)]

def printlist(li, glue=' '):
	return glue.join(list(map(str, li)))

def fop(s, end='\n'):
	sys.stdout.write(str(s) + end)

def fip():
	return sys.stdin.readline().strip()
fintinp = lambda : int(fip())

def flistinp(func=int):
	return list(map(func, fip().split()))

def fnsepline(n, func=str):
	return [func(fip()) for _ in range(n)]
from bisect import insort
for _ in range(fintinp()):
	n = fintinp()
	p = flistinp()
	pos = 0
	for i in range(n):
		if p[i] > 0:
			pos += p[i]
		elif p[i] < 0:
			if pos > 0:
				x = -p[i]
				if x <= pos:
					p[i] = 0
					pos -= x
				else:
					p[i] = -(x - pos)
					pos = 0
	fop(pos)


import sys
input = sys.stdin.readline

def inInt():
	return int(input())

def inStr():
	return input().strip('\n')

def inIList():
	return list(map(int, input().split()))

def inSList():
	return input().split()

def bsearch(nums, target):
	N = len(nums or [])
	l = 0
	r = N - 1
	while l <= r:
		mid = (l + r) // 2
		if nums[mid] < target:
			l = mid + 1
		elif nums[mid] > target:
			r = mid - 1
		else:
			return (None, mid, None)
	return (r if r >= 0 else None, None, l if l <= N - 1 else None)

def yesOrNo(val):
	print('YES' if val else 'NO')

def printSpacedArray(nums):
	for x in nums:
		print(x, end=' ')
	print()

def solve():
	n = inInt()
	case = inIList()
	csum = 0
	best = case[0]
	for x in case:
		csum += x
		best = min(best, csum)
	best = abs(best)
	print(best)
tests = inInt()
for case in range(tests):
	solve()


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	total = 0
	k = 0
	for j in range(n):
		if arr[j] > 0:
			total += arr[j]
		elif arr[j] < 0:
			k = min(total, -arr[j])
			arr[j] += k
			total -= k
	total = 0
	for j in range(n):
		if arr[j] < 0:
			total += abs(arr[j])
	print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if not any(a):
		print(0)
		continue
	cost = 0
	positive = 0
	while a[positive] <= 0:
		cost += -a[positive]
		positive += 1
	negative = positive + 1
	while negative < n and a[negative] >= 0:
		negative += 1
	while negative < n:
		operations = min(a[positive], -a[negative])
		a[positive] -= operations
		a[negative] += operations
		while positive < n and a[positive] <= 0:
			cost += -a[positive]
			positive += 1
		negative = max(negative, positive + 1)
		while negative < n and a[negative] >= 0:
			negative += 1
	while positive < n:
		cost += max(0, a[positive])
		positive += 1
	print(cost // 2)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	stacked = 0
	total = 0
	for x in a:
		if x > 0:
			stacked += x
		else:
			total += min(0, x + stacked)
			stacked += x
			stacked = max(0, stacked)
	print(abs(total))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	l = []
	for i in range(n):
		if a[i] == 0:
			continue
		elif len(l) == 0:
			l.append(a[i])
		elif l[-1] < 0:
			l.append(a[i])
		else:
			val = l[-1]
			l.pop()
			l.append(val + a[i])
	for i in range(len(l) - 1, -1, -1):
		if l[i] < 0:
			break
		ans += l[i]
	print(ans)


for item in [0] * int(input()):
	input()
	a = list(map(int, input().split()))
	count = 0
	for item in a:
		if item >= 0:
			count = count + item
		if item < 0:
			if count + item >= 0:
				count = count + item
			else:
				count = 0
	print(count)


for o in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	p = 0
	for i in reversed(range(n)):
		if s[i] < 0:
			p = p + s[i]
		if s[i] > 0:
			if p < 0:
				if s[i] <= abs(p):
					p = p + s[i]
				else:
					p = 0
	print(abs(p))


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = int_array()
	ans = 0
	prev_pos = 0
	for i in a:
		if i > 0:
			prev_pos += i
		elif prev_pos >= abs(i):
			prev_pos -= abs(i)
		else:
			i += prev_pos
			prev_pos = 0
			ans += abs(i)
	print(ans)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		ans = 0
		temp = 0
		for i in a:
			temp += i
			if temp < 0:
				ans += abs(temp)
				temp = 0
		print(ans)
main()


def solve(n, arr):
	currPos = 0
	for (i, num) in enumerate(arr):
		if num > 0:
			currPos += num
		elif num < 0:
			amount = min(currPos, abs(num))
			currPos -= amount
			arr[i] += amount
	ans = 0
	for num in arr:
		if num < 0:
			ans += abs(num)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [*map(int, input().split())]
	print(solve(n, arr))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	total = 0
	for i in range(n):
		if a[i] > 0:
			total += a[i]
		elif a[i] < 0:
			if total >= -a[i]:
				total += a[i]
				a[i] = 0
			elif total < -a[i]:
				a[i] = a[i] + total
				total = 0
	ans = 0
	for i in range(n):
		if a[i] < 0:
			ans += -a[i]
	print(ans)


for j in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	count = 0
	for j in b:
		if j < 0:
			if count > 0 and abs(j) > count:
				count = 0
			if count > 0 and abs(j) <= count:
				count += j
		else:
			count += j
	print(count)


tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	sum = 0
	flag = False
	for i in range(n):
		if arr[i] > 0:
			sum += arr[i]
			flag = True
		if flag and arr[i] < 0:
			sum += arr[i]
			if sum < 0:
				sum = 0
	print(sum)


def answer(n, a):
	cost = 0
	for i in range(n - 1):
		if a[i] <= 0:
			cost += abs(a[i])
			a[-1] += a[i]
		else:
			cost += 0
			a[i + 1] += a[i]
	return cost

def main():
	t = int(input())
	n = [0] * t
	a = [0] * t
	for j in range(t):
		n[j] = int(input())
		a[j] = [int(i) for i in input().split()]
	for j in range(t):
		print(answer(n[j], a[j]))
main()


import math
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	lst = []
	ans = 0
	lst = list(map(int, input().strip().split()))[:n]
	lst1 = []
	for i in range(n):
		ans = ans + lst[i]
		if ans < 0:
			lst1.append(ans)
			ans = 0
	print(abs(sum(lst1)))


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	val = 0
	for u in range(n):
		if a[-u - 1] < 0:
			val -= a[-u - 1]
		elif a[-u - 1] > val:
			val = 0
		else:
			val -= a[-u - 1]
	print(val)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	minus = 0
	plus = 0
	for i in a:
		if i == 0:
			continue
		elif i > 0:
			plus += i
		elif plus >= abs(i):
			plus += i
		else:
			plus = 0
			hg = abs(i) - plus
			minus -= hg
	print(abs(plus))


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	x = 0
	y = 0
	while y < n:
		if ar[x] <= 0:
			x += 1
			y = x
		elif ar[y] < 0:
			if ar[x] > abs(ar[y]):
				ar[x] += ar[y]
				ar[y] = 0
				y += 1
			else:
				ar[y] += ar[x]
				ar[x] = 0
				x += 1
		else:
			y += 1
	ans = 0
	for i in ar:
		if i > 0:
			ans += i
	print(ans)


for i in range(int(input())):
	n = int(input())
	ans = 0
	arr = list(map(int, input().split()))
	for j in arr:
		ans = max(ans + j, 0)
	print(ans)


for i in range(int(input())):
	s = int(input())
	e = [int(j) for j in input().split()]
	b = 0
	for k in range(len(e)):
		if e[k] >= 0:
			b += e[k]
		if e[k] < 0:
			if b + e[k] >= 0:
				b += e[k]
				e[k] == 0
			else:
				e[k] += b
				b = 0
	print(b)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	arr = []
	i = flag = t = 0
	while i < n:
		if l[i] < 0 and flag == 0:
			arr.append(l[i])
		elif l[i] < 0 and flag == 1:
			t = t + l[i]
			if t < 0:
				flag = 0
				arr.append(t)
				t = 0
			else:
				flag = 1
		elif l[i] > 0:
			t += l[i]
			flag = 1
		else:
			pass
		i += 1
	print(abs(sum(arr)))


t = int(input())

def dem(arr):
	for ele in arr:
		if ele != 0:
			return True
	return False
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	ct = 0
	total = 0
	for i in range(n):
		if x[i] > 0:
			total += x[i]
		else:
			gem = min(total, -x[i])
			x[i] += gem
			total -= gem
	ans = 0
	for i in range(n):
		if x[i] < 0:
			ans += abs(x[i])
	print(ans)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	positive = 0
	for i in range(n):
		if a[i] > 0:
			positive += a[i]
		if a[i] < 0 and positive > 0:
			if positive + a[i] >= 0:
				positive = positive + a[i]
				a[i] = 0
			else:
				positive = 0
				a[i] = positive + a[i]
	print(positive)


for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ans = 0
	a = 0
	b = 0
	for elem in ar:
		if elem < 0:
			if b > -elem:
				b += elem
				a += elem
			else:
				ans += abs(elem) - b
				b = 0
				a += abs(elem) - b
		else:
			b += elem
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	res = 0
	curSum = 0
	for num in a:
		if num < 0:
			if curSum == 0:
				res += abs(num)
				continue
			tmp = min(abs(num), curSum)
			curSum -= tmp
			num += tmp
			res += abs(num)
		if num > 0:
			curSum += num
	print(res)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	raw_nums = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	nums = []
	part_sum = 0
	for num in raw_nums:
		if part_sum == 0:
			part_sum = num
		elif part_sum > 0 and num >= 0 or (part_sum < 0 and num <= 0):
			part_sum += num
		else:
			nums.append(part_sum)
			part_sum = num
	if part_sum != 0:
		nums.append(part_sum)
	minus_sum = 0
	plus_sum = 0
	for num in nums:
		plus_sum += num
		if plus_sum < 0:
			minus_sum += plus_sum
			plus_sum = 0
	print(plus_sum)


t = int(input())
for t in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	b = [0]
	for i in range(n):
		b.append(b[-1] + a[i])
	mn = b[0]
	for x in b:
		mn = min(mn, x)
	print(abs(mn))


t = int(input())
for i in range(t):
	n = int(input())
	p = [int(i) for i in input().split()]
	j = 0
	for i in range(n):
		if j + p[i] <= 0:
			j = 0
		else:
			j += p[i]
	print(j)


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	cur = 0
	for x in l:
		cur = max(0, cur + x)
	return cur
for __ in range(int(input())):
	print(solve())


T = int(input())
for i in range(T):
	N = int(input())
	x = list(map(int, input().split()))
	total = 0
	for k in range(N):
		if x[k] > 0:
			total += x[k]
		else:
			y = min(total, -x[k])
			x[k] += y
			total -= y
	total = 0
	for s in range(N):
		if x[s] < 0:
			total += abs(x[s])
	print(total)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	positive = []
	for i in range(n):
		if a[i] > 0:
			positive.append(a[i])
		elif a[i] < 0:
			(s, t) = (0, -a[i])
			while len(positive) and s + positive[-1] <= t:
				s += positive.pop()
			if len(positive):
				positive[-1] -= t - s
	print(sum(positive))
for _ in range(int(input())):
	solve()


t = int(input())
while t > 0:
	n1 = int(input())
	a = list(map(int, input().split(' ')))
	sum = 0
	for x in a:
		if x < 0:
			if sum + x >= 0:
				sum = sum + x
			else:
				sum = 0
		else:
			sum = sum + x
	print(sum)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	i = 0
	while i < n:
		if a[i] > 0:
			while i < n:
				ans += a[i]
				if ans < 0:
					ans = 0
				i += 1
		i += 1
	print(ans)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	cost = 0
	curr = 0
	for i in range(0, n):
		if a[i] >= 0:
			curr = curr + a[i]
		elif abs(a[i]) > curr:
			cost = cost + abs(a[i]) - curr
			curr = 0
		else:
			curr = curr - abs(a[i])
	print(cost)
	t = t - 1


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	res = 0
	temp = 0
	for a in A:
		temp += a
		res = min(res, temp)
	print(-res)


import math
t = int(input())
for q in range(t):
	n = int(input())
	P = [int(i) for i in input().split()]
	c = 0
	res = 0
	for i in P:
		if i > 0:
			c += i
		elif i < 0:
			if i < -1 * c:
				res += abs(i + c)
				c = 0
			else:
				c += i
	print(res)


from functools import cmp_to_key
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(len(a) - 2, -1, -1):
		a[i] += a[i + 1]
	print(max(a))


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n - 1):
		if arr[i] > 0:
			arr[i + 1] = arr[i] + arr[i + 1]
			arr[i] = 0
	return arr[-1]
t = int(input())
for i in range(t):
	print(solve())


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = ria()
		ans = 0
		for i in a:
			if ans > 0:
				ans += i
			else:
				ans = i
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


from sys import stdin, stdout
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	s = 0
	ans = 0
	for i in a:
		s += i
		ans = min(ans, s)
	print(-ans)


for _ in ' ' * int(input()):
	a = int(input())
	p = 0
	c = 0
	for i in map(int, input().split()):
		if p + i < 0:
			c += p + i
			p = 0
		else:
			p += i
	print(max(p, c))


t = int(input())
ans = [0] * t
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ans = 0
	for i in range(n):
		s += a[i]
		if s < 0:
			ans += -s
			s = 0
	print(ans)


testc = int(input())
for ion in range(testc):
	l = int(input())
	p = input().split()
	for po in range(l):
		p[po] = int(p[po])
	if l == 2:
		if p[-1] < 0:
			p[-1] = 0
	else:
		for lp in range(1, l - 1):
			uy = p[lp]
			if uy < 0:
				p[0] = p[0] + uy
				p[lp] = 0
			elif uy > 0:
				p[lp + 1] = p[lp + 1] + uy
				p[lp] = 0
			else:
				continue
	if p[-1] < 0:
		p[-1] = 0
	print(p[-1])


import sys, math
input = sys.stdin.readline
t = int(input())
for r in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	cost = 0
	positive = 0
	for i in range(n):
		if l[i] < 0:
			if positive > 0:
				a = min(positive, abs(l[i]))
				positive -= a
				cost += abs(l[i]) - a
			else:
				cost += abs(l[i])
		elif l[i] > 0:
			positive += l[i]
	print(cost)