Recall that the sequence $b$ is a a subsequence of the sequence $a$ if $b$ can be derived from $a$ by removing zero or more elements without changing the order of the remaining elements. For example, if $a=[1, 2, 1, 3, 1, 2, 1]$, then possible subsequences are: $[1, 1, 1, 1]$, $[3]$ and $[1, 2, 1, 3, 1, 2, 1]$, but not $[3, 2, 3]$ and $[1, 1, 1, 1, 2]$.

You are given a sequence $a$ consisting of $n$ positive and negative elements (there is no zeros in the sequence).

Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.

In other words, if the maximum length of alternating subsequence is $k$ then your task is to find the maximum sum of elements of some alternating subsequence of length $k$.

You have to answer $t$ independent test cases.


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.

The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9, a_i \ne 0$), where $a_i$ is the $i$-th element of $a$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).


-----Output-----

For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of $a$.


-----Example-----
Input
4
5
1 2 3 -1 -2
4
-1 -2 -1 -3
10
-2 8 3 8 -4 -15 5 -2 -3 1
6
1 -1000000000 1 -1000000000 1 -1000000000

Output
2
-1
6
-2999999997



-----Note-----

In the first test case of the example, one of the possible answers is $[1, 2, \underline{3}, \underline{-1}, -2]$.

In the second test case of the example, one of the possible answers is $[-1, -2, \underline{-1}, -3]$.

In the third test case of the example, one of the possible answers is $[\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]$.

In the fourth test case of the example, one of the possible answers is $[\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]$.

Source: codeforces | Difficulty: EASY
Tags: Dynamic programming

Amortized analysis

Greedy algorithms


Solutions:
for _ in range(int(input())):
	n = int(input())
	seq = [int(x) for x in input().split()]
	sign = seq[0] > 0
	mmax = 0
	mmin = float('-inf')
	ans = 0
	for val in seq:
		if val > 0 and sign:
			mmax = max(mmax, val)
		elif val < 0 and (not sign):
			mmin = max(mmin, val)
		elif val > 0 and (not sign):
			ans += mmin
			mmin = float('-inf')
			sign = True
			mmax = val
		elif val < 0 and sign:
			ans += mmax
			mmax = 0
			sign = not True
			mmin = val
	if seq[-1] > 0:
		ans += mmax
	else:
		ans += mmin
	print(ans)


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()
	m = a[0]
	c = []
	for i in range(1, n):
		if a[i - 1] * a[i] > 0:
			m = max(m, a[i])
		else:
			c.append(m)
			m = a[i]
	c.append(m)
	print(sum(c))


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


from math import factorial as fac
from collections import defaultdict
from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
sys.setrecursionlimit(10 ** 6)

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

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

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
from collections import deque, defaultdict
import heapq

def bfs(V, E):
	s = set()
	print('\n'.join(map(str, E.items())))
	cnt = 0
	for v in V:
		if v in s:
			continue
		q = deque([])
		s.add(v)
		q.append(v)
		while len(q) > 0:
			x = q.popleft()
			for e in E[x]:
				if e not in s:
					s.add(e)
					q.append(e)
			print(q)
		cnt += 1
	return cnt

def regular_bs(L, x, l=None, r=None):
	l = 0 if l == None else l
	r = len(L) if r == None else r
	while r - l > 1:
		m = (r + l) // 2
		l = m if L[m] <= x else l
		r = m if L[m] > x else r
	return l if L[l] == x else -1
from typing import List, Tuple

class TreeNode:

	def __init__(self, x):
		self.val = x
		self.left = None
		self.right = None

	def __repr__(self):
		return f'({self.val}, {self.left.__repr__()}, {self.right.__repr__()})'

def helper(preorder: List[int], index, max_value=2 ** 300) -> Tuple[TreeNode, int]:
	if index >= len(preorder) or preorder[index] >= max_value:
		return (None, index)
	root_val = preorder[index]
	next_index = index + 1
	node = TreeNode(preorder[index])
	(node.left, next_index) = helper(preorder, next_index, root_val)
	(node.right, next_index) = helper(preorder, next_index, max_value)
	return (node, next_index)

def solution(L, n):
	pos = True if L[0] < 0 else False
	total = 0
	curr_opt = 0
	for x in L:
		if pos:
			if x > 0:
				curr_opt = max(curr_opt, x)
			else:
				total += curr_opt
				curr_opt = x
				pos = False
		elif x < 0:
			curr_opt = max(curr_opt, x)
		else:
			total += curr_opt
			curr_opt = x
			pos = True
	total += curr_opt
	return total

def main():
	T = read_int()
	for i in range(T):
		n = read_int()
		L = read_int_array()
		print(solution(L, n))
main()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	s = 0
	if x[0] > 0:
		f = 1
	else:
		f = 0
	mx = -1000000000000.0
	mn = -1000000000000.0
	for i in range(n):
		if x[i] < 0 and f == 1 and (i > 0):
			f = 0
			s = s + mx
			mn = -1000000000000.0
		if x[i] > 0 and f == 0 and (i > 0):
			f = 1
			s = s + mn
			mx = -1000000000000.0
		if f == 1:
			mx = max(mx, x[i])
		if f == 0:
			mn = max(mn, x[i])
	if x[-1] > 0:
		s = s + max(mx, x[-1])
	else:
		s = s + max(mn, x[-1])
	print(s)


def check(arr):
	k = min(arr)
	i = 0
	result = 0
	while i < len(arr):
		p = 0
		n = k
		if arr[i] < 0:
			while arr[i] < 0 and i < len(arr):
				if arr[i] > n:
					n = arr[i]
				i += 1
				if i == len(arr):
					break
			result += n
		if i >= len(arr):
			break
		if arr[i] > 0:
			while arr[i] > 0 and i < len(arr):
				if arr[i] > p:
					p = arr[i]
				i += 1
				if i == len(arr):
					break
			result += p
	return result
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	print(check(arr))
	t -= 1


from collections import Counter
import string
import math
import sys
from math import gcd as bltin_gcd

def coprime2(a, b):
	return bltin_gcd(a, b) == 1
from fractions import Fraction

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))

def printDivisors(n):
	divisors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				divisors.append(i)
			else:
				divisors.extend((i, n // i))
		i = i + 1
	return divisors

def countTotalBits(num):
	binary = bin(num)[2:]
	return len(binary)

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 dfs(node):
	global graph
	mex = 0
	size = 1
	for i in graph[node]:
		t = dfs(i)
		mex = max(mex, t[0])
		size += t[1]
	return [mex + size, size]

def gcd(p, q):
	while q != 0:
		(p, q) = (q, p % q)
	return p

def is_coprime(x, y):
	return gcd(x, y) == 1
for _ in range(vary(1)):
	n = vary(1)
	num = array_int()
	c = 1
	first = num[0]
	for i in range(1, n):
		if num[i] // abs(num[i]) * -1 == first // abs(first):
			c += 1
			first = num[i]
		else:
			continue
	flag = 0
	mini = float('-inf')
	sum = 0
	for i in range(n):
		if num[i] < 0:
			if flag == 2:
				sum += mini
				mini = float('-inf')
			flag = 1
			mini = max(mini, num[i])
			continue
		elif num[i] > 0:
			if flag == 1:
				sum += mini
				mini = float('-inf')
			flag = 2
			mini = max(mini, num[i])
	print(sum + mini)


test = int(input())
for i in range(test):
	size = int(input())
	lis = list(map(int, input().split()))
	pos = True if lis[0] > 0 else False
	maxi = lis[0]
	ans = 0
	for i in range(size):
		if pos != (lis[i] > 0):
			pos = not pos
			ans += maxi
			maxi = lis[i]
		elif lis[i] > maxi:
			maxi = lis[i]
	ans += maxi
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	s = 0
	val = arr[0]
	for i in range(1, n):
		if arr[i] > 0 and arr[i - 1] < 0:
			s += val
			val = arr[i]
		elif arr[i] < 0 and arr[i - 1] > 0:
			s += val
			val = arr[i]
		else:
			val = max(val, arr[i])
	s += val
	print(s)


T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	i = 0
	while i < n:
		k = 0
		while i < n and a[i] > 0:
			if a[i] > k:
				k = a[i]
			i += 1
		b.append(k)
		k = -10 ** 10
		while i < n and a[i] < 0:
			if a[i] > k:
				k = a[i]
			i += 1
		if k > -10 ** 10:
			b.append(k)
	print(sum(b))


get_sign = lambda y: y > 0

def get_max_sum(seq: list):
	n = len(seq)
	i = 0
	ans = 0
	while i < n:
		j = i
		mx = seq[i]
		while j < n and get_sign(seq[j]) == get_sign(seq[i]):
			mx = max(mx, seq[j])
			j += 1
		ans += mx
		i = j
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	seq = list(map(int, input().split(' ')))
	print(get_max_sum(seq))


num = int(input())
for i in range(num):
	n = int(input())
	l = list(map(int, input().split(' ')))
	length = 1
	for i in range(n - 1):
		if l[i] > 0 and l[i + 1] < 0 or (l[i] < 0 and l[i + 1] > 0):
			length += 1
	lists = []
	j = 0
	for i in range(length):
		temp = []
		if l[j] > 0:
			while j < n and l[j] > 0:
				temp.append(l[j])
				j += 1
		else:
			while j < n and l[j] < 0:
				temp.append(l[j])
				j += 1
		lists.append(temp)
	sum = 0
	for i in lists:
		sum += max(i)
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	isNeg = True
	if l[0] > 0:
		isNeg = False
	idx = []
	for i in range(n):
		if isNeg:
			if l[i] > 0:
				idx.append(i)
				isNeg = not isNeg
		if not isNeg:
			if l[i] < 0:
				idx.append(i)
				isNeg = not isNeg
	ans = 0
	if len(idx) > 0:
		ans += max(l[0:idx[0]])
	else:
		print(max(l))
		continue
	for i in range(len(idx) - 1):
		ans += max(l[idx[i]:idx[i + 1]])
	ans += max(l[idx[-1]:])
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	taken = 'P' if arr[0] >= 0 else 'N'
	temp = []
	temp.append(arr[0])
	for j in range(1, n):
		new_taken = 'P' if arr[j] >= 0 else 'N'
		if taken == new_taken:
			if arr[j] > temp[-1]:
				temp.pop()
				temp.append(arr[j])
		else:
			temp.append(arr[j])
			taken = 'P' if arr[j] >= 0 else 'N'
	print(sum(temp))


import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	minus = False
	plus = False
	for i in arr:
		if i < 0:
			minus = True
		else:
			plus = True
		if minus == True and plus == True:
			break
	brk = False
	ans = []
	i = 0
	flag = 'plus' if arr[0] > 0 else 'minus'
	if plus == True and minus == False or (plus == False and minus == True):
		print(max(arr))
	else:
		while i < len(arr):
			maxnum = 0
			while arr[i] > 0 and flag == 'plus':
				maxnum = max(maxnum, arr[i])
				i += 1
				if i >= len(arr):
					break
				elif arr[i] < 0:
					break
			if maxnum != 0:
				flag = 'minus'
				ans.append(maxnum)
			if i >= len(arr):
				break
			maxnum = -1000000001
			while arr[i] < 0 and flag == 'minus':
				maxnum = max(maxnum, arr[i])
				i += 1
				if i >= len(arr):
					break
				elif arr[i] > 0:
					break
			if maxnum != -1000000001:
				flag = 'plus'
				ans.append(maxnum)
		print(sum(ans))


t = int(input())
for tt in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	stack = []
	stack.append(l[0])
	top = 1
	for i in range(1, n):
		if stack[top - 1] > 0 and l[i] > 0:
			if stack[top - 1] < l[i]:
				stack[top - 1] = l[i]
		elif stack[top - 1] > 0 and l[i] < 0:
			stack.append(l[i])
			top += 1
		elif stack[top - 1] < 0 and l[i] < 0:
			if stack[top - 1] < l[i]:
				stack[top - 1] = l[i]
		elif stack[top - 1] < 0 and l[i] > 0:
			stack.append(l[i])
			top += 1
	print(sum(stack))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr[0] < 0:
		f = True
	else:
		f = False
	ans = 0
	m = -10 ** 10
	for x in arr:
		if f:
			if x < 0:
				m = max(m, x)
			else:
				f = False
				ans += m
				m = -10 ** 10
				m = max(m, x)
		elif x >= 0:
			m = max(m, x)
		else:
			f = True
			ans += m
			m = -10 ** 10
			m = max(m, x)
	print(ans + m)


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


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	ng = []
	p = []
	for i in l:
		if i < 0:
			ng.append(i)
			if len(p) > 0:
				s += max(p)
				p = []
		else:
			p.append(i)
			if len(ng) > 0:
				s += max(ng)
				ng = []
	if len(ng) == 0 and len(p) == 0:
		print(s)
	elif len(ng) != 0:
		s += max(ng)
		print(s)
	elif len(p) != 0:
		s += max(p)
		print(s)


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


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	mini = int(1000000000.0 + 7)
	maxi = -int(1000000000.0 + 7)
	sumi = 0
	flagi = -1
	for x in a:
		if flagi == -1:
			if x < 0:
				flagi = 0
				mini = x
			else:
				flagi = 1
				maxi = x
		elif flagi == 0:
			if x < 0:
				mini = max(mini, x)
			else:
				flagi = 1
				maxi = x
				sumi += mini
		elif x > 0:
			maxi = max(maxi, x)
		else:
			flagi = 0
			mini = x
			sumi += maxi
	if flagi == 0:
		sumi += mini
	else:
		sumi += maxi
	print(sumi)


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 factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

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 comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

def main():
	for _ in range(N()):
		n = N()
		arr = RLL()
		res = [arr[0]]
		for i in arr[1:]:
			if res[-1] > 0 and i < 0 or (res[-1] < 0 and i > 0):
				res.append(i)
			else:
				res[-1] = max(res[-1], i)
		print(sum(res))
main()


t = int(input())
for i in range(t):
	maxn = -1000000001
	maxp = 0
	sm = 0
	c = 0
	n = int(input())
	l = list(map(int, input().split()))
	res = l[0]
	if res > 0:
		c = 1
		maxp = res
	else:
		c = 0
		maxn = res
	if n == 1:
		print(l[0])
	else:
		for j in range(1, n):
			if l[j] > 0 and c == 1:
				maxp = max(maxp, l[j])
			elif l[j] < 0 and c == 0:
				maxn = max(maxn, l[j])
			elif l[j] > 0 and c == 0:
				sm += maxn
				maxn = -1000000001
				maxp = l[j]
				c = 1
			elif l[j] < 0 and c == 1:
				sm += maxp
				maxp = 0
				maxn = l[j]
				c = 0
		if maxp == 0 and maxn != -1000000001:
			print(sm + maxn)
		elif maxn == -1000000001 and maxp != 0:
			print(sm + maxp)
		else:
			print(sm)


for i in range(int(input())):
	n = int(input())
	a = [int(j) for j in input().split()]
	(s, mx) = (0, a[0])
	for j in range(n):
		if a[j] * mx < 0:
			s += mx
			mx = a[j]
		else:
			mx = max(mx, a[j])
	print(s + mx)


def is_positive(a):
	return False if a < 0 else True
k = int(input())
for i in range(k):
	num = int(input())
	my_list = list(map(int, input().split()))
	if num == 1:
		print(my_list[0])
	else:
		current_taken = my_list[0]
		new_list = []
		for n in range(len(my_list)):
			if is_positive(current_taken) == is_positive(my_list[n]):
				current_taken = max(my_list[n], current_taken)
			else:
				new_list.append(current_taken)
				current_taken = my_list[n]
		new_list.append(current_taken)
		print(sum(new_list))


def alternatingSubsequence(a):
	n = len(a)
	if n == 1:
		return a[0]
	max_sum = 0
	i = 0
	while i < n:
		j = i + 1
		while j < n and a[j] * a[i] > 0:
			j += 1
		max_sum += max(a[i:j])
		i = j
	return max_sum
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(alternatingSubsequence(a))


numcases = int(input())
for i in range(numcases):
	alength = int(input())
	a = list(map(int, input().split()))
	t = a[0]
	ans = []
	for i in range(alength - 1):
		if a[i] * a[i + 1] > 0:
			if a[i + 1] > t:
				t = a[i + 1]
		else:
			ans.append(t)
			t = a[i + 1]
	ans.append(t)
	print(sum(ans))


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')
testcases = int(input())
for j in range(testcases):
	n = int(input())
	nums = list(map(int, input().split()))
	outputlist = [nums[0]]
	changed = False
	for j in range(1, n):
		if nums[j] * outputlist[-1] < 0:
			outputlist.append(nums[j])
		elif nums[j] * outputlist[-1] > 0:
			if nums[j] > outputlist[-1]:
				outputlist[-1] = nums[j]
	print(sum(outputlist))


def main():
	T = int(input())
	for _ in range(T):
		_ = int(input())
		A = [int(i) for i in input().split()]
		ans = 0
		cur_max = A[0]
		posi = True if A[0] > 0 else False
		for a in A[1:]:
			if posi:
				if a > 0:
					cur_max = max(cur_max, a)
				else:
					ans += cur_max
					posi = False
					cur_max = a
			elif a < 0:
				cur_max = max(cur_max, a)
			else:
				ans += cur_max
				posi = True
				cur_max = a
		print(ans + cur_max)
main()


from sys import stdin
input = stdin.readline

def getSign(n):
	if n > 0:
		return 1
	else:
		return -1
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	j = 0
	s = 0
	while i < n:
		curr = a[i]
		j = i
		while j < n and getSign(a[i]) == getSign(a[j]):
			curr = max(curr, a[j])
			j += 1
		s += curr
		i = j
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	summ = 0
	count = 0
	l = 0
	for i in range(n):
		if i == 0:
			summ1 = summ + a[i]
		elif a[i] > 0 and a[i - 1] > 0 or (a[i] < 0 and a[i - 1] < 0):
			if summ + a[i] > summ1:
				summ1 = summ + a[i]
				if a[i] > 0:
					l = a[i]
		else:
			summ1 = summ1 + a[i]
			count += 1
			summ = summ1 - a[i]
	print(summ1)


t = int(input())
for tt in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	total = 0
	cur_max = 0
	prev_sign = None
	for i in arr:
		sign = i > 0
		if prev_sign == sign:
			cur_max = max(i, cur_max)
		else:
			prev_sign = sign
			total += cur_max
			cur_max = i
	total += cur_max
	print(total)


def func(n, a):
	s = 0
	i = 0
	while i < n:
		max_p = float('-inf')
		if a[i] > 0:
			while i < n and a[i] > 0:
				max_p = max(a[i], max_p)
				i += 1
			if max_p != float('-inf'):
				s += max_p
		else:
			min_n = float('-inf')
			while i < n and a[i] < 0:
				min_n = max(a[i], min_n)
				i += 1
			if min_n != float('inf'):
				s += min_n
	print(s)
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	func(n, l)


def solve():
	n = int(input())
	arr = list(map(int, input().split(' ')))
	temp = [arr[0]]
	sign = 1 if temp[0] > 0 else -1
	maxArr = []
	for i in range(1, n):
		if sign * arr[i] < 0:
			sign *= -1
			maxArr.append(max(temp))
			temp = []
		temp.append(arr[i])
	if len(temp) > 0:
		maxArr.append(max(temp))
	print(sum(maxArr))
t = int(input())
for i in range(t):
	solve()


T = int(input())
for tc in range(T):
	k = int(input())
	Q = list(map(int, input().split()))
	tmp = Q[0]
	if tmp > 0:
		t = 1
	else:
		t = 0
	ans = 0
	for i in Q:
		if t == 1 and i < 0:
			ans += tmp
			tmp = i
			t = 0
		elif t == 0 and i > 0:
			ans += tmp
			t = 1
		tmp = max(i, tmp)
	ans += tmp
	print(ans)


for _ in range(int(input())):
	answer = 0
	n = int(input())
	l = list(map(int, input().split()))
	pos = 0
	neg = -100000000000
	(pos_flag, neg_flag) = (0, 0)
	for i in range(n):
		if l[i] > 0:
			neg_flag = 1
			if neg_flag == 1 and l[i - 1] < 0 and (i != 0):
				answer += neg
				neg = -100000000000
				neg_flag = 0
			if l[i] > pos:
				pos = l[i]
		else:
			pos_flag = 1
			if pos_flag == 1 and l[i - 1] > 0 and (i != 0):
				answer += pos
				pos = 0
				pos_flag = 0
			if l[i] > neg:
				neg = l[i]
	if l[-1] < 0:
		answer += neg
	else:
		answer += pos
	print(answer)


t = int(input())
while t:
	t -= 1
	n = int(input())
	buff = 0
	x = list(map(int, input().strip().split()))
	if x[0] > 0:
		buff = 1
	else:
		buff = 2
	more = x[0]
	less = 0
	for i in range(1, n):
		if buff == 1 and x[i] > 0:
			more = max(more, x[i])
		elif buff == 1 and x[i] < 0:
			less += more
			more = x[i]
			buff = 2
		elif buff == 2 and x[i] > 0:
			less += more
			more = x[i]
			buff = 1
		elif buff == 2 and x[i] < 0:
			more = max(more, x[i])
	print(less + more)


import sys, collections
import io, os
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	maxim = arr[0]
	if arr[0] < 0:
		flag = 'neg'
	else:
		flag = 'pos'
	for i in range(1, n):
		if arr[i] > 0:
			if flag == 'neg':
				ans += maxim
				maxim = arr[i]
			maxim = max(maxim, arr[i])
			flag = 'pos'
		elif arr[i] < 0:
			if flag == 'pos':
				ans += maxim
				maxim = arr[i]
			maxim = max(maxim, arr[i])
			flag = 'neg'
	print(ans + maxim)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = [int(i) for i in input().split()]
	positions = [0]
	prev = nums[0] < 0
	i = 1
	for i in range(1, n):
		if (nums[i] < 0) != prev:
			positions.append(i)
		prev = nums[i] < 0
	max_s = 0
	for j in range(len(positions) - 1):
		pos = positions[j]
		next_pos = positions[j + 1]
		max_s += max(nums[pos:next_pos])
	j = positions[-1]
	max_s += max(nums[j:])
	print(max_s)


for _ in range(int(input())):
	n = int(input())
	aa = list(map(int, input().split()))
	ans = []
	now = aa[0]
	for a in aa:
		if now * a > 0:
			now = max(now, a)
		else:
			ans.append(now)
			now = a
	ans.append(now)
	print(sum(ans))


def main():
	t = int(input())
	while t:
		n = int(input())
		arr = list(map(int, input().split()))
		x = arr[0]
		(index, i) = (0, 0)
		ans = 0
		while i < len(arr):
			mac = arr[index]
			while index < len(arr) and arr[index] ^ x >= 0:
				if mac < arr[index]:
					mac = arr[index]
				x = arr[index]
				index += 1
			ans += mac
			i = index
			if index == len(arr):
				break
			x = arr[index]
		print(ans)
		t -= 1
main()


import sys

def solve(turn):
	list_b = [-1 if turn else 1]
	for i in range(n):
		num = list_num[i]
		if num * list_b[-1] < 0:
			list_b.append(num)
		elif num > list_b[-1]:
			list_b[-1] = num
	return (len(list_b) - 1, sum(list_b) + (1 if turn else -1))
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	list_num = list(map(int, sys.stdin.readline().split()))
	print(max(solve(0), solve(1))[1])


t = int(input())
for _ in range(t):
	l = int(input())
	a = list(map(int, input().split()))
	c = 0
	m = a[0]
	for i in range(l - 1):
		if a[i] * a[i + 1] > 0:
			m = max(m, a[i + 1])
		else:
			c += m
			m = a[i + 1]
	c += m
	if a[-1] * m < 0:
		c += a[-1]
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s1 = 0
	s2 = 0
	e1 = a[0]
	e2 = 0
	k1 = 1
	k2 = 0
	flag = False
	for i in range(1, n):
		if a[i] < 0 and e1 < 0 or (a[i] > 0 and e1 > 0):
			e1 = max(e1, a[i])
		else:
			k1 += 1
			if not flag:
				e2 = a[i]
				k2 = 1
				flag = True
			s1 += e1
			e1 = a[i]
		if flag:
			if a[i] < 0 and e2 < 0 or (a[i] > 0 and e2 > 0):
				e2 = max(e2, a[i])
			else:
				k2 += 1
				s2 += e2
				e2 = a[i]
	s1 += e1
	s2 += e2
	if k1 == k2:
		print(max(s1, s2))
	elif k1 > k2:
		print(s1)
	else:
		print(s2)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	suml = 0
	curm = l[0]
	for i in range(n - 1):
		if l[i] * l[i + 1] < 0:
			suml += curm
			curm = l[i + 1]
		elif curm < l[i + 1]:
			curm = l[i + 1]
	suml += curm
	print(suml)


def arrange(lis):
	dic = [0]
	for num in lis:
		if num < 0 and dic[-1] == 0:
			dic.append(num)
		elif num < 0 and dic[-1] < num and (dic[-1] < 0):
			dic[-1] = num
		elif num < 0 and dic[-1] > 0:
			dic.append(num)
		elif num > 0 and dic[-1] == 0:
			dic.append(num)
		elif num > 0 and dic[-1] < num and (dic[-1] > 0):
			dic[-1] = num
		elif num > 0 and dic[-1] < 0:
			dic.append(num)
	return sum(dic)
for t in range(int(input())):
	k = int(input())
	lis = list(map(int, input().split()))
	print(arrange(lis))


t = int(input())
for _ in range(t):
	n = int(input())
	positive = []
	negative = []
	seq = []
	alt = 0
	for a in input().split():
		ai = int(a)
		if ai > 0:
			positive.append(ai)
			if alt == 0:
				alt = 1
			elif alt == -1:
				alt = 1
				seq.append(max(negative))
				negative = []
		else:
			negative.append(ai)
			if alt == 0:
				alt = -1
			elif alt == 1:
				alt = -1
				seq.append(max(positive))
				positive = []
	if alt == 1:
		seq.append(max(positive))
	elif alt == -1:
		seq.append(max(negative))
	print(sum(seq))


t = int(input())
for a in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	positive = True
	if nums[0] < 0:
		positive = False
	highestPos = 0
	highestNeg = -10000000000000
	total = 0
	for num in nums:
		if positive:
			if num > 0:
				if num > highestPos:
					highestPos = num
			else:
				total += highestPos
				positive = False
				highestNeg = num
		elif num < 0:
			if num > highestNeg:
				highestNeg = num
		else:
			total += highestNeg
			positive = True
			highestPos = num
	if positive:
		total += highestPos
	else:
		total += highestNeg
	print(total)


t = int(input())
for m in range(t):
	n = int(input())
	lol = [int(x) for x in input().split()]
	flag = 0
	sum1 = 0
	for i in range(n):
		if lol[i] < 0:
			if flag == 0:
				s = -99999999999
			elif flag == 1:
				sum1 += s
				s = -99999999999
			if lol[i] > s:
				s = lol[i]
			flag = 2
		elif lol[i] > 0:
			if flag == 0:
				s = -99999999999
			elif flag == 2:
				sum1 += s
				s = -99999999999
			if lol[i] > s:
				s = lol[i]
			flag = 1
	sum1 += s
	print(sum1)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	s = 0
	while i < n:
		m1 = -100000000000
		m2 = -10000000000
		while i < n and l[i] > 0:
			if l[i] > m1:
				m1 = l[i]
			i += 1
		while i < n and l[i] < 0:
			if l[i] > m2:
				m2 = l[i]
			i += 1
		if m1 != -100000000000:
			s += m1
		if m2 != -10000000000:
			s += m2
	print(s)
	t -= 1


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


T = int(input())
for t in range(T):
	N = input()
	tmp_arr = input()
	arr = [int(x) for x in tmp_arr.split(' ')]
	fPos = True if arr[0] > 0 else False
	suma = [arr[0]]
	for x in arr[1:]:
		if fPos == True and x < 0:
			suma.append(x)
			fPos = False
		elif fPos == False and x > 0:
			suma.append(x)
			fPos = True
		elif x > suma[-1]:
			suma[-1] = x
	print(sum(suma))


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


t = int(input())
for tc in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	su = 0
	num = []
	j = 0
	for i in s:
		if j == 0:
			num.append(i)
			j += 1
		else:
			le = len(num) - 1
			if num[le] > 0:
				if num[le] >= i and i > 0:
					pass
				elif num[le] < i and i > 0:
					num.pop()
					num.append(i)
				else:
					num.append(i)
			elif num[le] >= i and i < 0:
				pass
			elif num[le] < i and i < 0:
				num.pop()
				num.append(i)
			else:
				num.append(i)
	print(sum(num))


t = int(input())
res = []
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().strip().split()]
	res_t = []
	t_g = []
	t_l = []
	for i in arr:
		if i < 0:
			if t_g:
				res_t.append(max(t_g))
				t_g = []
			t_l.append(i)
		else:
			if t_l:
				res_t.append(max(t_l))
				t_l = []
			t_g.append(i)
	if t_g:
		res_t.append(max(t_g))
	if t_l:
		res_t.append(max(t_l))
	res.append(res_t)
for r in res:
	print(sum(r))


from math import sqrt
from queue import Queue
import sys
sys.setrecursionlimit(10 ** 6)

def f(arr, start, n):
	if start == n:
		return (0, n)
	mmax = arr[start]
	i = start
	if arr[start] > 0:
		while i < n and arr[i] > 0:
			mmax = max(mmax, arr[i])
			i += 1
		return (mmax, i)
	while i < n and arr[i] < 0:
		mmax = max(mmax, arr[i])
		i += 1
	return (mmax, i)
nokfase = int(input())
for kase in range(nokfase):
	n = int(input())
	a = [int(i) for i in input().split()]
	sum = 0
	i = 0
	while i < n:
		(mmax, i) = f(a, i, n)
		sum += mmax
	print(sum)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = [a[0]]
	s = 0
	for i in range(1, n):
		if a[i - 1] < 0 and a[i] < 0 or (a[i - 1] > 0 and a[i] > 0):
			b.append(a[i])
		else:
			m = max(b)
			s += m
			b = [a[i]]
	m = max(b)
	s += m
	print(s)


def main():
	for _ in range(int(input())):
		input()
		poz = neg = (0, 0, 0)
		for x in map(int, input().split()):
			if x > 0:
				poz = max(poz, (neg[0] + 1, neg[1] + x, x))
			else:
				neg = max(neg, (poz[0] + 1, poz[1] + x, x))
		print(max(poz, neg)[1])
main()


def alternate(n, array):
	s = 0
	cm = array[0]
	for i in range(n):
		if array[i] / abs(array[i]) == cm / abs(cm) and array[i] > cm:
			cm = array[i]
			if i == n - 1:
				s += cm
		elif array[i] / abs(array[i]) != cm / abs(cm):
			s += cm
			cm = array[i]
			if i == n - 1:
				s += cm
		elif i == n - 1:
			s += cm
	return s
t = int(input())
array = []
for i in range(t):
	a = int(input())
	b = list(map(int, input().split(' ')))
	array.append([a, b])
for x in array:
	print(alternate(*x))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	arr = []
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			arr.append(a[i])
			continue
		if a[i] < 0:
			if arr[-1] < 0:
				arr[-1] = max(arr[-1], a[i])
			else:
				arr.append(a[i])
		elif arr[-1] > 0:
			arr[-1] = max(arr[-1], a[i])
		else:
			arr.append(a[i])
	if not arr:
		print(-1)
	else:
		print(sum(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 1 if arr[0] >= 0 else 0
	if flag == 1:
		mp = arr[0]
		mn = -99999999999
	else:
		mp = -99999999999
		mn = arr[0]
	s = 0
	for i in range(1, n):
		if flag == 1:
			mn = -99999999999
			if arr[i] < 0:
				s += mp
				mn = arr[i]
				flag = 0
			else:
				mp = max(arr[i], mp)
		elif flag == 0:
			mp = -99999999999
			if arr[i] >= 0:
				s += mn
				mp = arr[i]
				flag = 1
			else:
				mn = max(arr[i], mn)
	if flag == 1:
		print(s + mp)
	else:
		print(s + mn)


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	j = 0
	s = []
	z = []
	while j < n:
		count = 0
		while j < n and x[j] > 0:
			z.append(x[j])
			j = j + 1
			count += 1
			continue
		if count > 0:
			s.append(max(z))
		z = []
		count = 0
		while j < n and x[j] < 0:
			z.append(x[j])
			j = j + 1
			count = count + 1
			continue
		if count > 0:
			s.append(max(z))
	print(sum(s))


t = int(input())
for _ in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	res = 0
	if seq[0] > 0:
		flag = 0
	else:
		flag = 1
	t = -100000000000000000
	for i in range(n):
		if flag == 0:
			if seq[i] > 0 and seq[i] > t:
				t = seq[i]
			if seq[i] < 0:
				res += t
				t = seq[i]
				flag = 1
		else:
			if seq[i] < 0 and seq[i] > t:
				t = seq[i]
			if seq[i] > 0:
				res += t
				t = seq[i]
				flag = 0
	print(res + t)


t = int(input())
for testcase in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	pos = False
	mxpos = mxneg = 0
	if a[0] > 0:
		pos = True
		mxpos = a[0]
	else:
		mxneg = a[0]
	ans = []
	for i in range(1, n):
		if a[i] > 0:
			if not pos:
				ans.append(mxneg)
				mxpos = a[i]
				pos = True
			else:
				mxpos = max(mxpos, a[i])
		elif pos:
			ans.append(mxpos)
			mxneg = a[i]
			pos = False
		else:
			mxneg = max(mxneg, a[i])
	if len(ans) == 0:
		if mxpos == 0:
			ans.append(mxneg)
		else:
			ans.append(mxpos)
	elif ans[-1] > 0:
		ans.append(mxneg)
	else:
		ans.append(mxpos)
	print(sum(ans))


def findsign(x):
	if x > 0:
		return 1
	return 0
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sumi = 0
	i = 0
	while i < n:
		maxi = a[i]
		j = i + 1
		while j < n and findsign(a[i]) == findsign(a[j]):
			maxi = max(maxi, a[j])
			j += 1
		i = j
		sumi += maxi
	print(sumi)


test = int(input())
for peradaban in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	curmax = -10000000000.0
	ans = 0
	signum = False
	if a[0] < 0:
		signum = True
	for i in a:
		if signum == True:
			if i > 0:
				ans += curmax
				curmax = i
				signum = False
			else:
				curmax = max(curmax, i)
		elif i < 0:
			ans += curmax
			curmax = i
			signum = True
		else:
			curmax = max(curmax, i)
	ans += curmax
	print(int(ans))


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

def main():
	for _ in range(int(input())):
		n = int(input())
		a = [int(x) for x in input().split()]
		a.append(a[n - 1] * -1)
		k = 0
		maxp = 1
		maxn = -9999999999999999999
		ts = 0
		pos = 0
		neg = 0
		for i in range(n):
			if a[i] < 0:
				neg += 1
				maxn = max(maxn, a[i])
				if a[i + 1] > 0:
					ts += maxn
					maxn = -9999999999999999999
			if a[i] > 0:
				pos += 1
				maxp = max(maxp, a[i])
				if a[i + 1] < 0:
					ts += maxp
					maxp = 1
		print(ts)
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()


t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxlen = 0
	maxSum = 0
	curr = 0
	if a[0] > 0:
		maxlen += 1
		curr = a[0]
		status = 1
	else:
		maxlen += 1
		curr = a[0]
		status = 0
	if n == 1:
		maxSum += curr
	for i in range(1, n):
		if status == 1:
			if a[i] < 0:
				maxlen += 1
				status = 0
				maxSum += curr
				curr = a[i]
			else:
				curr = max(a[i], curr)
		if status == 0:
			if a[i] > 0:
				maxlen += 1
				status = 1
				maxSum += curr
				curr = a[i]
			else:
				curr = max(a[i], curr)
		if i == n - 1:
			maxSum += curr
	print(maxSum)


def func1(n, A):
	som = 0
	a = int(A[0])
	for i in range(1, n):
		inte_i = int(A[i])
		if a > 0 and inte_i > 0:
			if inte_i > a:
				a = inte_i
		elif a < 0 and inte_i < 0:
			if a < inte_i:
				a = inte_i
		else:
			som += a
			a = inte_i
	som += a
	print(som)
number_test = input()
for i in range(int(number_test)):
	size = input()
	array = input().split()
	func1(int(size), list(array))


for t in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	maxNum = s[0]
	maxSum = 0
	p = True if s[0] > 0 else False
	for i in range(1, n):
		if s[i] < 0 and p == True:
			maxSum += maxNum
			p = not p
			maxNum = s[i]
		elif s[i] > 0 and p == False:
			maxSum += maxNum
			p = not p
			maxNum = s[i]
		elif s[i] > maxNum:
			maxNum = s[i]
	print(maxSum + maxNum)


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		m = l[i]
		if l[i] < 0:
			while i < n:
				if l[i] < 0:
					i += 1
					if i < n:
						if l[i] > m and l[i] < 0:
							m = l[i]
				else:
					break
			s += m
		else:
			while i < n:
				if l[i] > 0:
					i += 1
					if i < n:
						if l[i] > m:
							m = l[i]
				else:
					break
			s += m
	print(s)
	t -= 1


def calculateMaxSum(n, li):
	array = []
	array.append(li[0])
	large = []
	for j in range(0, n - 1):
		if li[j] > 0 and li[j + 1] > 0:
			array.append(li[j + 1])
		elif li[j] > 0 and li[j + 1] < 0:
			large.append(max(array))
			array = []
			array.append(li[j + 1])
		elif li[j] < 0 and li[j + 1] > 0:
			large.append(max(array))
			array = []
			array.append(li[j + 1])
		else:
			array.append(li[j + 1])
	large.append(max(array))
	return sum(large)
t = int(input())
while t != 0:
	size = int(input())
	arr = [int(x) for x in input().split()]
	print(calculateMaxSum(size, arr))
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	(s, q) = (input().split(), 0)
	m = int(s[0])
	for j in s:
		if int(j) * m > 0:
			m = max(m, int(j))
		else:
			(q, m) = (q + m, int(j))
	print(q + m)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	l = [int(i) for i in input().split()]
	a = []
	a.append(l[0])
	j = 0
	for i in range(1, n):
		if l[i] > a[j] and (a[j] >= 0 and l[i] < 0 or (a[j] < 0 and l[i] >= 0)):
			a.append(l[i])
			j = j + 1
		elif l[i] > a[j] and (a[j] >= 0 and l[i] >= 0 or (a[j] < 0 and l[i] < 0)):
			a[j] = l[i]
		elif a[j] >= 0 and l[i] < 0 or (a[j] < 0 and l[i] >= 0):
			a.append(l[i])
			j = j + 1
	print(sum(a))


t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	elem = arr[0]
	s = 0
	for i in range(n - 1):
		if arr[i] > 0 and arr[i + 1] > 0:
			elem = max(elem, arr[i], arr[i + 1])
		if arr[i] < 0 and arr[i + 1] < 0:
			elem = max(elem, arr[i], arr[i + 1])
		if arr[i] > 0 and arr[i + 1] < 0:
			s += elem
			elem = arr[i + 1]
		if arr[i] < 0 and arr[i + 1] > 0:
			s += elem
			elem = arr[i + 1]
	s += elem
	print(s)


def helper(l, i, n, v):
	t = [l[i]]
	if v > 0:
		while i < n - 1 and l[i + 1] > 0:
			t.append(l[i + 1])
			i += 1
	else:
		while i < n - 1 and l[i + 1] < 0:
			t.append(l[i + 1])
			i += 1
	return (i, max(t))

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		if l[i] > 0:
			(i, a) = helper(l, i, n, 1)
		else:
			(i, a) = helper(l, i, n, -1)
		s += a
		i += 1
	return s
for __ in range(int(input())):
	print(solve())


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	prev_sign = a[0] > 0
	prev = a[0]
	s = a[0]
	for x in a[1:]:
		sign = x > 0
		if sign == prev_sign:
			if x > prev:
				s += x - prev
				prev = x
		else:
			prev_sign = sign
			prev = x
			s += x
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	dp = [0] * n
	dp[0] = arr[0]
	p = None
	sign = lambda a: 1 if a > 0 else -1
	j = 1
	while j < n:
		if sign(arr[j]) == sign(arr[j - 1]):
			dp[j] = max(dp[j - 1], arr[j])
		else:
			p = j - 1
			break
		j += 1
	while j < n:
		if sign(arr[j]) == sign(arr[j - 1]):
			dp[j] = max(dp[p] + arr[j], dp[j - 1])
		else:
			dp[j] = dp[j - 1] + arr[j]
			p = j - 1
		j += 1
	print(dp[n - 1])


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
import string

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

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

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

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

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

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
MAX = 10 ** 9
for _ in range(ii()):
	n = ii()
	d = li()
	i = 0
	s = 0
	while i < n:
		j = i
		if d[j] < 0:
			t = -MAX
			while j < n and d[j] < 0:
				t = max(t, d[j])
				j += 1
		else:
			t = 0
			while j < n and d[j] > 0:
				t = max(t, d[j])
				j += 1
		s += t
		i = j
	print(s)


from sys import stdin, stdout

def main():
	for _ in range(int(stdin.readline())):
		n = int(input())
		arr = [int(k) for k in input().split()]
		pos = 0
		neg = 0
		counter = 0
		result = 0
		for i in range(n):
			if arr[i] > 0 and counter == 0:
				pos = arr[i]
				counter = 1
				result += neg
				neg = 0
			elif arr[i] > 0 and counter == 1:
				pos = max(pos, arr[i])
			elif arr[i] < 0 and counter == 1:
				result += pos
				pos = 0
				counter = 0
				neg = arr[i]
			elif i == 0:
				neg = arr[i]
			else:
				neg = max(neg, arr[i])
		print(result + pos + neg)
main()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		nums = list(map(int, input().split()))
		sign = 1 if nums[0] > 0 else -1
		cur_max = float('-inf')
		ans = 0
		for x in nums:
			if x * sign > 0:
				cur_max = max(cur_max, x)
			else:
				ans += cur_max
				cur_max = x
				sign = -sign
		ans += cur_max
		print(ans)
main()


total = int(input())
for _ in range(total):
	n = list(map(int, input().split()))[0]
	seq = list(map(int, input().split()))
	current = float('-inf')
	best = 0
	for i in range(n - 1):
		if seq[i] * seq[i + 1] > 0:
			current = max(current, seq[i], seq[i + 1])
		elif seq[i] > 0 and seq[i + 1] < 0:
			current = max(current, seq[i])
			best += current
			current = float('-inf')
		else:
			current = max(current, seq[i])
			best += current
			current = float('-inf')
	if current == float('-inf'):
		print(best + seq[-1])
		continue
	print(best + current)


t = int(input())
for ii in range(t):
	n = int(input())
	i = 0
	a = list(map(int, input().rstrip().split()))
	neg = 0
	sum1 = 0
	posi = 1
	if a[i] < 0:
		neg = 0
	else:
		posi = 0
	flag = 0
	maxi = -1 * float('inf')
	while i < n:
		if neg == 0:
			maxi = -1 * float('inf')
			while a[i] < 0:
				if i == n:
					break
				maxi = max(maxi, a[i])
				i += 1
				if i == n:
					if maxi != -1 * float('inf'):
						sum1 += maxi
						flag = 1
					break
			if flag == 1:
				break
			if maxi != -1 * float('inf'):
				sum1 += maxi
			posi = 0
			neg = 1
		elif posi == 0:
			maxi = -1 * float('inf')
			while a[i] >= 0:
				if i == n:
					break
				maxi = max(maxi, a[i])
				i += 1
				if i == n:
					if maxi != -1 * float('inf'):
						sum1 += maxi
						flag = 1
					break
			if flag == 1:
				break
			if maxi != -1 * float('inf'):
				sum1 += maxi
			neg = 0
			posi = 1
	print(sum1)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	flag = arr[0] > 0
	result = 0
	temp = arr[0]
	for elem in arr:
		if flag == elem > 0:
			temp = max(temp, elem)
		elif flag and elem < 0:
			result += temp
			temp = elem
			flag = False
		elif not flag and elem > 0:
			result += temp
			temp = elem
			flag = True
		else:
			temp = max(temp, elem)
	print(result + temp)


def solution(t):
	for i in range(t):
		input()
		m1 = map(int, input().split())
		res = []
		sign = 1
		for v in m1:
			if res:
				t = 1 if v > 0 else -1
				if t != sign:
					res.append(v)
					sign = t
				elif v > res[-1]:
					res[-1] = v
			else:
				res.append(v)
				if v < 0:
					sign = -1
		print(sum(res))

def main():
	t = int(input())
	solution(t)
main()


from sys import stdin
import sys
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	summ = 0
	cur = 0
	while cur < n:
		if arr[cur] > 0:
			for i in range(cur, n):
				if arr[i] < 0:
					summ += max(arr[cur:i])
					cur = i
					break
			else:
				summ += max(arr[cur:n])
				cur = n
		elif arr[cur] < 0:
			for i in range(cur, n):
				if arr[i] > 0:
					summ += max(arr[cur:i])
					cur = i
					break
			else:
				summ += max(arr[cur:n])
				cur = n
	print(summ)


t = int(input())
for _ in range(t):
	n = int(input())
	prev_sign = 0
	prev_val = 0
	prev_max = 0
	sum = 0
	for a in input().split():
		a = int(a)
		sign = 1 if a > 0 else 2
		if sign != prev_sign:
			sum += prev_max
			prev_max = a
			prev_sign = sign
			continue
		prev_max = max(a, prev_max)
	sum += prev_max
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	maximalne = a[0]
	akt_zn = a[0] // abs(a[0])
	out = 0
	for i in range(1, n):
		nove_zn = a[i] // abs(a[i])
		if nove_zn == akt_zn:
			maximalne = max(maximalne, a[i])
		else:
			out = out + maximalne
			akt_zn = nove_zn
			maximalne = a[i]
	out = out + maximalne
	print(out)


n = int(input())
ans = []
for i in range(n):
	m = int(input())
	l = list(map(int, input().split()))
	sum = 0
	k = 0
	while k < m:
		s = l[k]
		k += 1
		while k < m and l[k] * l[k - 1] > 0:
			s = max(s, l[k])
			k += 1
		sum += s
	ans.append(sum)
for i in ans:
	print(i)


from sys import stdin
input = stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = []
	mx = a[0]
	for i in range(1, n):
		if a[i] * mx < 0:
			b.append(mx)
			mx = a[i]
		elif mx < a[i]:
			mx = a[i]
	b.append(mx)
	print(sum(b))


t = int(input())
for each_t in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	if l[0] < 0:
		current = 'n'
	else:
		current = 'p'
	s = []
	s.append(0)
	res = 0
	for i in range(1, n):
		if l[i] < 0 and current == 'p' or (l[i] > 0 and current == 'n'):
			s.append(i)
			if l[i] < 0 and current == 'p':
				current = 'n'
			elif l[i] > 0 and current == 'n':
				current = 'p'
	length = len(s)
	for i in range(length - 1):
		res = res + max(l[s[i]:s[i + 1]])
	print(res + max(l[s[-1]:]))


from sys import stdin, stdout

def si(a):
	if a < 0:
		return -1
	return 1

def f(a, n):
	i = 0
	ans = 0
	while i < n:
		x = []
		s = si(a[i])
		while i < n and si(a[i]) == s:
			x.append(a[i])
			i += 1
		ans += max(x)
	print(ans)
t = int(stdin.readline())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f(a, n)


import math
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	tek = 0
	i = 0
	a = list(map(int, input().split()))
	while i < n:
		mx = 0
		if a[i] > 0:
			mx = a[i]
			while i < n and a[i] > 0:
				mx = max(mx, a[i])
				i += 1
		else:
			mx = a[i]
			while i < n and a[i] < 0:
				mx = max(mx, a[i])
				i += 1
		tek += mx
	print(tek)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	su = i = mxx = 0
	while i <= n - 1:
		mxx = -pow(10, 18)
		if l[i] > 0:
			j = i
			while j < n and l[j] > 0:
				if mxx < l[j]:
					mxx = l[j]
				j += 1
			su += mxx
			i = j
		else:
			j = i
			while j < n and l[j] < 0:
				if mxx < l[j]:
					mxx = l[j]
				j += 1
			su += mxx
			i = j
	print(su)


test = int(input())
sol = []

def sqn(a: int):
	if a < 0:
		return False
	return True
while test:
	test -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	last = sqn(a[0])
	nr = a[0]
	sum = 0
	ok = False
	for i in range(n):
		if last == sqn(a[i]):
			if nr < a[i]:
				nr = a[i]
		else:
			sum += nr
			last = not last
			nr = a[i]
		if i == n - 1:
			sum += nr
	sol.append(sum)
for i in sol:
	print(i)


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = a[0]
	l = a[0]
	for i in range(1, n):
		if a[i] > 0 and l < 0 or (a[i] < 0 and l > 0):
			l = a[i]
			s += l
		elif l < a[i]:
			s += a[i] - l
			l = a[i]
	print(s)


t = int(input())

def same_sign(c, p):
	if c < 0:
		if p < 0:
			return 1
		else:
			return 0
	elif c > 0:
		if p > 0:
			return 1
		else:
			return 0
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = a[0]
	p = a[0]
	c = a[0]
	for i in range(1, len(a)):
		if same_sign(a[i], p) == 1:
			if a[i] > p:
				s = s - p + a[i]
				p = a[i]
		else:
			s = s + a[i]
			p = a[i]
	print(s)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cur = arr[0]
	ans = 0
	sign = arr[0] > 0
	i = 1
	while i < n:
		if not sign ^ (arr[i] > 0):
			cur = max(cur, arr[i])
		else:
			ans += cur
			sign = not sign
			cur = arr[i]
		i += 1
	ans += cur
	print(ans)


t = int(input())
for _ in range(0, t):
	n = int(input())
	mat = [int(z) for z in input().split()]
	dd = 1
	cc = 0
	if mat[-1] > cc:
		mat.append(-1)
	else:
		mat.append(dd)
	n = n + dd
	pp = -2
	pn = -2
	mp = -1000000001
	mn = -1000000001
	mps = cc
	mns = cc
	res = cc
	for i in range(0, n):
		if mat[i] > 0:
			mps = 1
			if pp == i - 1:
				mp = max(mp, mat[i])
			else:
				if mns > 0:
					res += mn
				mp = mat[i]
			pp = i
		else:
			mns = 1
			if pn == i - 1:
				mn = max(mn, mat[i])
			else:
				if mps > 0:
					res += mp
				mn = mat[i]
			pn = i
	print(res)


T = int(input())

def chech(h, e):
	if h == True and e > 0:
		return True
	elif h == False and e < 0:
		return True
	return False
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	h = True if arr[0] > 0 else False
	sadang = [arr[0]]
	sumi = 0
	for i in range(1, n):
		if chech(h, arr[i]) == True:
			sadang.append(arr[i])
		else:
			sumi += max(sadang)
			sadang = [arr[i]]
			if h == True:
				h = False
			else:
				h = True
	sumi += max(sadang)
	print(sumi)


import sys
tt = int(input())
while tt:
	n = int(input())
	A = [int(x) for x in input().split()]
	neg = []
	pos = []
	loc_min = -sys.maxsize
	flag = True
	for x in A:
		if x < 0:
			flag = False
			if x > loc_min:
				loc_min = x
		if x > 0 and flag == False:
			flag = True
			neg.append(loc_min)
			loc_min = -sys.maxsize
	if A[n - 1] < 0:
		neg.append(loc_min)
	loc_max = 0
	flag = True
	for x in A:
		if x > 0:
			if x > loc_max:
				flag = False
				loc_max = x
		if x < 0 and flag == False:
			pos.append(loc_max)
			loc_max = 0
			flag = True
	if A[n - 1] > 0:
		pos.append(loc_max)
	Sum = sum(pos) + sum(neg)
	print(Sum)
	tt -= 1


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

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

def sgn(x):
	if x > 0:
		return '+'
	else:
		return '-'

def solve(n, a):
	cur_sign = '0'
	last = 0
	ret = 0
	for i in range(n):
		if sgn(a[i]) == cur_sign:
			if a[i] > last:
				ret += a[i] - last
				last = a[i]
		else:
			ret += a[i]
			cur_sign = sgn(a[i])
			last = a[i]
	return ret

def main():
	T = int(input())
	for tt in range(T):
		n = int(input())
		a = list(invr())
		print(solve(n, a))
main()


alph = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(int(input())):
	n = int(input())
	arr = input().split()
	a = []
	for i in range(len(arr)):
		a.append(int(arr[i]))
	con = a[0]
	temp = a[0]
	for i in range(n - 1):
		if (a[i] > 0 and a[i + 1] > 0 or (a[i] < 0 and a[i + 1] < 0)) and a[i + 1] > temp:
			con = con + a[i + 1] - temp
			temp = a[i + 1]
		elif a[i] > 0 and a[i + 1] < 0 or (a[i] < 0 and a[i + 1] > 0):
			con = con + a[i + 1]
			temp = a[i + 1]
	print(con)


def main():
	n_s = int(input())
	for x in range(n_s):
		int(input())
		sec = list(map(int, input().split(' ')))
		ant = sec[0]
		max = ant
		sum = 0
		for n in sec[1:]:
			if ant * n > 0:
				if n > max:
					max = n
			else:
				sum += max
				max = n
			ant = n
		print(sum + max)
main()


for _ in range(int(input())):
	m = int(input())
	l = list(map(int, input().split()))
	q = []
	p = []
	i = 0
	res = 0
	while i < m:
		while i < m and l[i] > 0:
			q.append(l[i])
			i = i + 1
		while i < m and l[i] < 0:
			p.append(l[i])
			i += 1
		if len(q) == 0:
			y = 0
		else:
			y = max(q)
		if len(p) == 0:
			x = 0
		else:
			x = max(p)
		res = res + x + y
		p = []
		q = []
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(p) for p in input().split()]
	index = []
	for i in range(len(arr)):
		if i == 0:
			continue
		if arr[i] > 0 and arr[i - 1] < 0 or (arr[i] < 0 and arr[i - 1] > 0):
			index.append(i)
	i = 0
	s = 0
	index.append(n)
	for j in range(len(index)):
		s += max(arr[i:index[j]])
		i = index[j]
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	total = 0
	m = a[0]
	for i in range(1, n):
		if m > 0 and a[i] < 0 or (m < 0 and a[i] > 0):
			total += m
			m = a[i]
		else:
			m = max(m, a[i])
	print(total + m)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	if ar[0] > 0:
		ty = 'p'
	else:
		ty = 'n'
	li = []
	ans = 0
	for i in range(n):
		if ar[i] > 0:
			if ty == 'p':
				li.append(ar[i])
			else:
				ans += max(li)
				li = [ar[i]]
				ty = 'p'
		if ar[i] < 0:
			if ty == 'n':
				li.append(ar[i])
			else:
				ans += max(li)
				li = [ar[i]]
				ty = 'n'
	ans += max(li)
	print(ans)


def solve():
	sum = 0
	part_max = -10 ** 9
	pos = a[0] > 0
	for ai in a:
		if (ai > 0) == pos:
			part_max = max(part_max, ai)
		else:
			sum += part_max
			part_max = ai
			pos = not pos
	sum += part_max
	print(sum)
	return
t = int(input())
for i in range(1, t + 1):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	solve()


t = int(input())
for test in range(t):
	n = int(input())
	temp = input()
	temp = temp.split(' ')
	temp = list(map(int, temp))
	sign = temp[0] / abs(temp[0])
	res = []
	i = 0
	for x in temp:
		if x / abs(x) == sign:
			res.append(x)
			i += 1
			sign *= -1
		elif x > res[i - 1]:
			res[i - 1] = x
	sum = 0
	for x in res:
		sum = sum + x
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	x = []
	sign = l[0] > 0
	best = l[0]
	for i in l[1:]:
		if sign == (i > 0):
			best = max(i, best)
		else:
			x.append(best)
			sign = i > 0
			best = i
	x.append(best)
	print(sum(x))


import sys
stream = sys.stdin
t = int(stream.readline())
for n in stream:
	n = int(n)
	maxi = -float('inf')
	sums = 0
	prev_num = 0
	for num in stream.readline().split():
		num = int(num)
		if prev_num * num < 0:
			sums += maxi
			maxi = -float('inf')
		if maxi < num:
			maxi = num
		prev_num = num
	sums += maxi
	print(sums)


import sys
import math
from math import *
from collections import defaultdict
import os
from io import BytesIO, IOBase
from collections import deque

def check(d):
	if list(d.keys()) == ['a', 'b', 'c']:
		if d['a'] == d['c'] or d['b'] == d['c']:
			return True
	return False

def main(d):
	n = int(input())
	arr = list(map(int, input().split()))
	res = []
	i = 0
	while i < n:
		temp = [0]
		while i < n:
			if arr[i] < 0:
				break
			temp.append(arr[i])
			i += 1
		res.append(temp)
		temp = []
		while i < n:
			if arr[i] >= 0:
				break
			temp.append(arr[i])
			i += 1
		if len(temp) > 0:
			res.append(temp)
	sum = 0
	for i in res:
		sum += max(i)
	print(sum)
d = defaultdict(lambda : 0)
for i in range(int(input())):
	main(d)


t = int(input())
for x in range(t):
	n = int(input())
	ans = 0
	a = list(map(int, input().split()))
	flag = False if a[0] < 0 else True
	m = a[0]
	for i in range(1, n):
		if not (a[i] > 0) ^ flag:
			if a[i] > m:
				m = a[i]
		else:
			ans += m
			m = a[i]
			flag = not flag
	print(ans + m)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(j) for j in input().split()]
	minn = 0
	maxx = 0
	summ = 0
	for i in range(n):
		if a[i] > 0:
			npc = 0
			minn = 0
		else:
			npc = 1
			maxx = 0
		if npc % 2 == 0:
			if maxx < a[i]:
				summ -= maxx
				maxx = a[i]
				summ += maxx
			else:
				continue
		elif minn < a[i] or minn == 0:
			summ -= minn
			minn = a[i]
			summ += minn
		else:
			continue
	print(summ)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	array = list(map(int, sys.stdin.readline().split()))
	if array[0] > 0:
		signe = 1
	else:
		signe = -1
	maxi = -10 ** 9
	somme = 0
	for i in range(n):
		if signe == 1 and array[i] < 0:
			signe = -1
			somme += maxi
			maxi = -10 ** 9
		if signe == -1 and array[i] > 0:
			signe = 1
			somme += maxi
			maxi = -10 ** 9
		maxi = max(maxi, array[i])
	somme += maxi
	print(somme)


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


def main():
	t = int(input())
	get_sign = lambda x: 1 if x > 0 else -1
	for _ in range(t):
		n = int(input())
		seq = [int(i) for i in input().split()]
		cursor = seq[0]
		ans = 0
		for i in range(1, len(seq)):
			if seq[i] * seq[i - 1] < 0:
				ans += cursor
				cursor = seq[i]
			else:
				cursor = max(seq[i], cursor)
		ans += cursor
		print(ans)
main()


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = 0
	p = 0
	for e_s in input().split():
		e = int(e_s)
		if e * p <= 0:
			s += p
			p = e
			continue
		if e > p > 0:
			p = e
		elif p < e < 0:
			p = e
	s += p
	print(s)


Ntc = int(input())
for i in range(Ntc):
	_ = input()
	tc = list(map(int, input().split(' ')))
	ans = 0
	isLastPoolNegative = None
	last = 0
	for c in tc:
		if isLastPoolNegative is None:
			isLastPoolNegative = c < 0
			last = c
			continue
		if (c < 0) != isLastPoolNegative:
			ans += last
			isLastPoolNegative = c < 0
			last = c
			continue
		if c > last:
			last = c
	ans += last
	print(ans)


for _ in range(int(input())):
	x = int(input())
	(*li,) = map(int, input().split())
	(ans, i) = (0, 0)
	while i < x:
		sum1 = li[i]
		j = i
		while j < x and li[i] * li[j] > 0:
			sum1 = max(sum1, li[j])
			j += 1
		ans += sum1
		i = j
	print(ans)


t = int(input())
for t1 in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	max = 0
	i = 0
	while i < n:
		max = a[i]
		it = (-1, 1)[a[i] > 0]
		i += 1
		while i < n and it * a[i] > 0:
			if it > 0 and max < a[i] or (it < 0 and max < a[i]):
				max = a[i]
			i += 1
		sum += max
	print(sum)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	somme = 0
	mini = 0
	maxi = 0
	for i in a:
		if i > 0:
			somme += mini
			mini = 0
			if i > maxi:
				maxi = i
		else:
			somme += maxi
			maxi = 0
			if mini == 0:
				mini = i
			elif i > mini:
				mini = i
	print(somme + mini + maxi)


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	lip = []
	lin = []
	c = 0
	(nn, pp) = (0, 0)
	flag = True
	p = 0
	i = 0
	t = 0
	while i < n:
		t = li[i]
		q = 1
		flag = True
		if li[i] < 0:
			flag = False
		while flag == False and li[i] < 0 or (flag == True and li[i] > 0):
			t = max(t, li[i])
			i += 1
			if i >= n:
				break
		c += t
		if i >= n:
			break
		flag = True
	print(c)


t = int(input())
n = [0 for _ in range(t)]
a = [0 for _ in range(t)]
for i in range(t):
	n[i] = int(input())
	a[i] = list(map(int, input().split()))
for test in range(t):
	sum_t = 0
	pos = False
	neg = False
	maxi = 0
	mini = float('-inf')
	for terms in range(n[test]):
		if a[test][terms] > 0:
			pos = True
			if neg:
				maxi = a[test][terms]
				sum_t += mini
				neg = False
			if a[test][terms] > maxi:
				maxi = a[test][terms]
		else:
			neg = True
			if pos:
				mini = a[test][terms]
				sum_t += maxi
				pos = False
			if a[test][terms] > mini:
				mini = a[test][terms]
	if a[test][terms] > 0:
		sum_t += maxi
	else:
		sum_t += mini
	print(sum_t)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split())) + [0]
	arr = []
	pos = 0
	neg = -float('inf')
	for i in range(n):
		if a[i] >= 0:
			pos = max(pos, a[i])
			if a[i + 1] <= 0:
				arr.append(pos)
				pos = 0
		elif a[i] < 0:
			neg = max(neg, a[i])
			if a[i + 1] >= 0:
				arr.append(neg)
				neg = -float('inf')
	print(sum(arr))


import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *

class Scanner:

	@staticmethod
	def int():
		return int(sys.stdin.readline().rstrip())

	@staticmethod
	def string():
		return sys.stdin.readline().rstrip()

	@staticmethod
	def map_int():
		return [int(x) for x in Scanner.string().split()]

	@staticmethod
	def string_list(n):
		return [Scanner.string() for i in range(n)]

	@staticmethod
	def int_list_list(n):
		return [Scanner.map_int() for i in range(n)]

	@staticmethod
	def int_cols_list(n):
		return [Scanner.int() for i in range(n)]

class Math:

	@staticmethod
	def gcd(a, b):
		if b == 0:
			return a
		return Math.gcd(b, a % b)

	@staticmethod
	def lcm(a, b):
		return a * b // Math.gcd(a, b)

	@staticmethod
	def divisor(n):
		res = []
		i = 1
		for i in range(1, int(n ** 0.5) + 1):
			if n % i == 0:
				res.append(i)
				if i != n // i:
					res.append(n // i)
		return res

	@staticmethod
	def round_up(a, b):
		return -(-a // b)

	@staticmethod
	def is_prime(n):
		if n < 2:
			return False
		if n == 2:
			return True
		if n % 2 == 0:
			return False
		d = int(n ** 0.5) + 1
		for i in range(3, d + 1, 2):
			if n % i == 0:
				return False
		return True

def pop_count(x):
	x = x - (x >> 1 & 6148914691236517205)
	x = (x & 3689348814741910323) + (x >> 2 & 3689348814741910323)
	x = x + (x >> 4) & 1085102592571150095
	x = x + (x >> 8)
	x = x + (x >> 16)
	x = x + (x >> 32)
	return x & 127
MOD = int(1000000000.0) + 7
INF = int(1000000000000000.0)

def solve():
	N = Scanner.int()
	A = Scanner.map_int()
	ans = 0
	m = A[0]
	for i in range(1, N):
		if A[i] > 0 and A[i - 1] > 0:
			m = max(A[i], m)
		elif A[i] < 0 and A[i - 1] < 0:
			m = max(A[i], m)
		elif A[i] > 0 and A[i - 1] < 0:
			ans += m
			m = A[i]
		else:
			ans += m
			m = A[i]
	ans += m
	print(ans)

def main():
	T = Scanner.int()
	for _ in range(T):
		solve()
main()


t = int(input())

def f(n, l):
	a = l[0]
	sum = 0
	for i in l:
		if a // i < 0:
			sum += a
			a = i
		elif a < i:
			a = i
		else:
			continue
	print(sum + a)
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f(n, l)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	temp = l[0]
	for i in range(1, n):
		if temp > 0 and l[i] > 0:
			temp = max(temp, l[i])
		elif temp < 0 and l[i] < 0:
			temp = max(temp, l[i])
		elif temp < 0 and l[i] > 0:
			l1.append(temp)
			temp = l[i]
		elif temp > 0 and l[i] < 0:
			l1.append(temp)
			temp = l[i]
	l1.append(temp)
	print(sum(l1))


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	nums = list(map(int, sys.stdin.readline().split()))
	tot = 0
	neg = nums[0] < 0
	maxn = nums[0]
	nums += [-nums[-1]]
	for num in nums:
		if num < 0:
			if neg:
				if maxn < num:
					maxn = num
			else:
				neg = True
				tot += maxn
				maxn = num
		elif not neg:
			if maxn < num:
				maxn = num
		else:
			neg = False
			tot += maxn
			maxn = num
	print(tot)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	l2 = []
	k = []
	for i in range(n):
		if l[i] < 0:
			if l1:
				k.append(max(l1))
			l1 = []
			l2.append(l[i])
		else:
			if l2:
				k.append(max(l2))
			l2 = []
			l1.append(l[i])
	if l1:
		k.append(max(l1))
	if l2:
		k.append(max(l2))
	print(sum(k))


for _ in range(int(input())):
	n = int(input())
	li = list(map(int, input().split(' ')))
	m = []
	m.append(li[0])
	for i in range(1, n):
		if li[i] * li[i - 1] < 0:
			m.append(li[i])
		elif li[i] > m[-1]:
			m.pop()
			m.append(li[i])
	print(sum(m))


import math
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	total = 0
	for i in range(len(a)):
		if a[i] < 0:
			if flag == 0:
				s = -math.inf
			elif flag == 1:
				total += s
				s = -math.inf
			if a[i] > s:
				s = a[i]
			flag = 2
		elif a[i] > 0:
			if flag == 0:
				s = -math.inf
			elif flag == 2:
				total += s
				s = -math.inf
			if a[i] > s:
				s = a[i]
			flag = 1
	total += s
	print(total)


from sys import stdin

def func():
	return
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	(pos, neg) = (0, 0)
	for i in range(n):
		if arr[i] < 0:
			neg += 1
		else:
			pos += 1
	if neg == 0 or pos == 0:
		print(max(arr))
	else:
		(a, b) = ([], [])
		for i in range(n):
			if i == 0:
				if arr[i] < 0:
					flag = 1
					a.append(arr[i])
				else:
					flag = 0
					b.append(arr[i])
			elif flag == 0:
				if arr[i] > 0:
					b[-1] = max(b[-1], arr[i])
				else:
					flag = 1
					a.append(arr[i])
			elif arr[i] < 0:
				a[-1] = max(a[-1], arr[i])
			else:
				flag = 0
				b.append(arr[i])
		print(sum(a) + sum(b))


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


def s():
	n = int(input())
	a = list(map(int, input().split(' ')))
	pos = False
	neg = False
	pos_max = -1
	neg_max = -100000000000.0
	sm = 0
	for x in a:
		if x * -1 > 0:
			if pos_max != -1:
				sm += pos_max
				pos_max = -1
			neg = True
			pos = False
		else:
			if neg_max != -100000000000.0:
				sm += neg_max
				neg_max = -100000000000.0
			pos = True
			neg = False
		if pos:
			pos_max = max(x, pos_max)
		else:
			neg_max = max(x, neg_max)
	if pos:
		sm += pos_max
	else:
		sm += neg_max
	print(sm)
t = int(input())
for i in range(t):
	s()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	i = 0
	while i < n:
		(flag_n, flag_p) = (False, False)
		pos = -1
		neg = -10 ** 9 - 1
		while i < n and a[i] > 0:
			pos = max(pos, a[i])
			i += 1
			flag_p = True
		while i < n and a[i] < 0:
			neg = max(neg, a[i])
			i += 1
			flag_n = True
		if flag_n:
			res += neg
		if flag_p:
			res += pos
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	flag1 = 0
	flag2 = 0
	s1 = 0
	s2 = 0
	d = 0
	e = 0
	f = 0
	g = 0
	c1 = 0
	c2 = 0
	for j in range(n):
		if flag1 == 0 and a[j] > 0:
			s1 += a[j]
			d = a[j]
			flag1 = 1
			c1 += 1
		elif flag1 == 1 and a[j] > 0:
			if d != 0 and d < a[j]:
				s1 = s1 + a[j] - d
				d = a[j]
		elif flag1 == 1 and a[j] < 0:
			s1 += a[j]
			e = a[j]
			flag1 = 0
			c1 += 1
		elif flag1 == 0 and a[j] < 0:
			if e != 0 and e < a[j]:
				s1 = s1 + a[j]
				e *= -1
				s1 += e
				e = a[j]
		if flag2 == 0 and a[j] < 0:
			s2 += a[j]
			f = a[j]
			flag2 = 1
			c2 += 1
		elif flag2 == 1 and a[j] < 0:
			if f != 0 and f < a[j]:
				s2 = s2 + a[j]
				f *= -1
				s2 += f
				f = a[j]
		elif flag2 == 1 and a[j] > 0:
			s2 += a[j]
			g = a[j]
			flag2 = 0
			c2 += 1
		elif flag2 == 0 and a[j] > 0:
			if g != 0 and g < a[j]:
				s2 += a[j] - g
				g = a[j]
	if c1 > c2:
		print(s1)
	elif c2 > c1:
		print(s2)
	else:
		print(max(s1, s2))


from itertools import *
for s in [*open(0)][2::2]:
	print(sum((max(map(int, g)) for (k, g) in groupby(s.split(), key=str.isdigit))))


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	res = []
	i = 0
	while i < n:
		temp = []
		if li[i] >= 0:
			while i < n and li[i] >= 0:
				temp.append(li[i])
				i += 1
		else:
			while i < n and li[i] < 0:
				temp.append(li[i])
				i += 1
		res.append(temp)
	sol = 0
	for x in res:
		sol += max(x)
	print(sol)


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		if a[i] > 0:
			c = i + 1
			d = a[i]
			while c < n:
				if a[c] > 0:
					d = max(d, a[c])
					c += 1
				else:
					break
			s += d
			i = c
		else:
			c1 = i + 1
			d1 = a[i]
			while c1 < n:
				if a[c1] < 0:
					d1 = max(d1, a[c1])
					c1 += 1
				else:
					break
			s += d1
			i = c1
	print(s)


N = int(input())
for _ in range(N):
	L = int(input())
	list1 = list(map(int, input().split()))
	max_length_seq = []
	if list1[0] > 0:
		max_finder_positive = True
	else:
		max_finder_positive = False
	current_max = None
	for num in list1:
		if (num >= 0) == max_finder_positive:
			if current_max is None:
				current_max = num
			elif num > current_max:
				current_max = num
		else:
			max_finder_positive = not max_finder_positive
			max_length_seq.append(current_max)
			current_max = num
	max_length_seq.append(current_max)
	print(sum(max_length_seq))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(ppos, npos, ans) = (-1, -1, 0)
	for i in range(n):
		if arr[i] > 0 and ppos == -1:
			ppos = i
		if arr[i] < 0 and npos == -1:
			npos = i
		if ppos != -1 and npos != -1:
			ans += max(arr[min(npos, ppos):max(npos, ppos)])
			if ppos < npos:
				ppos = -1
			else:
				npos = -1
	ans += max(arr[max(npos, ppos):])
	print(ans)


def check(p, q):
	if p * q > 0:
		return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	l = [a[0]]
	for i in range(1, n):
		if check(l[-1], a[i]):
			if l[-1] < a[i]:
				l[-1] = a[i]
		else:
			l.append(a[i])
	print(sum(l))


t = int(input())
for j in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	max = arr[0]
	sum = 0
	for i in range(1, n):
		if arr[i] * arr[i - 1] > 0:
			if arr[i] > max:
				max = arr[i]
		if arr[i] * arr[i - 1] < 0:
			sum += max
			max = arr[i]
	sum += max
	print(sum)


t = int(input())
for i in range(t):
	x = int(input())
	l = list(map(int, input().split()))
	j = 0
	k = []
	count = 0
	while j < x:
		if l[j] > 0:
			while j < x and l[j] > 0:
				k.append(l[j])
				j += 1
		else:
			while j < x and l[j] < 0:
				k.append(l[j])
				j += 1
		count += max(k)
		k = []
	print(count)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	aa = a + [-1 * a[-1]]
	g = []
	mx = aa[0]
	for i in range(1, n + 1):
		if a[i - 1] > mx:
			mx = aa[i - 1]
		if (aa[i] > 0) != (aa[i - 1] > 0):
			g.append(mx)
			mx = aa[i]
	print(sum(g))


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().split())
inl = lambda : list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))
ans = []
t = ini()
for _ in range(t):
	n = ini()
	a = inl()
	count = 0
	tmp = a[0]
	prev = True if a[0] > 0 else False
	for i in range(1, n):
		actual = True if a[i] > 0 else False
		if actual == prev:
			tmp = max(tmp, a[i])
		else:
			count += tmp
			tmp = a[i]
		prev = actual
	ans.append(count + tmp)
print('\n'.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = []
	i = 0
	while i < n:
		if l[i] > 0:
			mx = 0
			while i < n and l[i] > 0:
				mx = max(mx, l[i])
				i += 1
			p.append(mx)
		else:
			mx = -10 ** 9
			while i < n and l[i] < 0:
				mx = max(mx, l[i])
				i += 1
			p.append(mx)
	print(sum(p))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	p = n = 0
	ps = 0
	ns = -10 ** 9
	if a[0] > 0:
		ps = a[0]
	else:
		ns = a[0]
	for i in range(1, len(a)):
		if a[i] > 0:
			if ps == 0:
				l.append(ns)
				ns = -10 ** 9
				ps = a[i]
			elif ps < a[i]:
				ps = a[i]
		elif ns == -10 ** 9:
			l.append(ps)
			ps = 0
			ns = a[i]
		elif ns < a[i]:
			ns = a[i]
	if ps != 0:
		l.append(ps)
	else:
		l.append(ns)
	print(sum(l))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	x = a[0]
	i = 0
	b = []
	b.append(a[0])
	y = a[0]
	for i in range(1, n):
		if x > 0 and a[i] < 0:
			x = a[i]
			y = a[i]
			b.append(a[i])
		elif x < 0 and a[i] > 0:
			x = a[i]
			b.append(a[i])
		elif x > 0 and a[i] > 0:
			if a[i] > x:
				b.remove(x)
				b.append(a[i])
				x = a[i]
		elif x < 0 and a[i] < 0:
			if a[i] > y:
				b.remove(y)
				b.append(a[i])
				y = a[i]
	print(sum(b))


def solution():
	from sys import stdout, stdin
	(_input, _print) = (stdin.readline, stdout.write)
	for _ in range(int(input())):
		n = int(input())
		arr = [int(i) for i in input().split()]
		flag = arr[0] > 0
		current = 0
		summary = 0
		for (i, v) in enumerate(arr):
			if flag:
				if v > 0:
					if current:
						if v > current:
							current = v
					else:
						current = v
				else:
					summary += current
					current = v
					flag = False
			elif v < 0:
				if current:
					if v > current:
						current = v
				else:
					current = v
			else:
				summary += current
				current = v
				flag = True
		summary += current
		print(summary)
solution()


import math
from decimal import *
getcontext().prec = 30
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	x = list()
	for i in range(n):
		if a[i] < 0:
			if i:
				if a[i - 1] < 0:
					a[i] = max(a[i], a[i - 1])
		elif i:
			if a[i - 1] > 0:
				a[i] = max(a[i], a[i - 1])
	for i in range(1, n):
		if a[i] * a[i - 1] < 0:
			x.append(a[i - 1])
	x.append(a[n - 1])
	print(sum(x))


def solve(n, arr):
	maxi = float('-inf')
	res = 0
	arr.append(arr[-1] * -1)
	for i in range(n):
		maxi = max(maxi, arr[i])
		if arr[i] * arr[i + 1] < 0:
			res += maxi
			maxi = float('-inf')
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))
assert solve(5, [1, 2, 3, -1, -2]) == 2
assert solve(4, [-1, -2, -1, -3]) == -1
assert solve(10, [-2, 8, 3, 8, -4, -15, 5, -2, -3, 1]) == 6
assert solve(6, [1, -1000000000, 1, -1000000000, 1, -1000000000]) == -2999999997
assert solve(5, [-9, -8, -3, 8, 2]) == 5


for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = 0
	s = 0
	v = -99999999999
	for i in range(n):
		if i == 0:
			if a[i] > 0:
				c = 1
				v = max(v, a[i])
			else:
				c = 0
				v = max(v, a[i])
		elif a[i] > 0:
			if c == 1:
				v = max(v, a[i])
			else:
				c = 1
				s = s + v
				v = a[i]
		elif c == 0:
			v = max(v, a[i])
		else:
			c = 0
			s = s + v
			v = a[i]
	s = s + v
	print(s)


import sys
input = sys.stdin.readline
for q in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		sign = 1
	else:
		sign = 0
	Len = 1
	res = []
	res.append(a[0])
	for i in range(1, n):
		if sign == 1:
			if a[i] < 0:
				res.append(a[i])
				sign = 0
			elif a[i] > res[-1]:
				res[-1] = a[i]
		elif a[i] > 0:
			res.append(a[i])
			sign = 1
		elif a[i] > res[-1]:
			res[-1] = a[i]
	print(sum(res))


try:
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		l.append(0)
		m = min(l) - 1
		k = min(l) - 1
		sum = 0
		for i in range(n):
			if l[i] > 0 and l[i + 1] > 0 or (l[i] < 0 and l[i + 1] < 0):
				m = max(m, l[i], l[i + 1])
			elif m == k:
				sum = sum + l[i]
			else:
				sum = sum + m
				m = k
		print(sum)
except:
	pass


t = int(input())
for test in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	temp = []
	ans = 0
	val = l[0] // abs(l[0])
	for i in range(len(l)):
		if l[i] // abs(l[i]) == val:
			temp.append(l[i])
		else:
			ans += max(temp)
			temp = [l[i]]
			val = -val
	if temp:
		ans = ans + max(temp)
	print(ans)


for _ in range(int(input())):
	sgn = lambda x: 1 if x > 0 else -1
	lens = int(input())
	arrs = [int(x) for x in input().split()]
	sums = 0
	i = 0
	while i < lens:
		now = arrs[i]
		j = i
		while j < lens and sgn(now) == sgn(arrs[j]):
			now = max(now, arrs[j])
			j += 1
		sums += now
		i = j
	print(sums)


import math
t = int(input())
for g in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	i = 0
	if a[0] > 0:
		while i < n:
			b = []
			while i < n and a[i] > 0:
				b.append(a[i])
				i = i + 1
			sum += max(b)
			if i > n - 1:
				break
			b = []
			while i < n and a[i] < 0:
				b.append(a[i])
				i = i + 1
			sum += max(b)
			if i > n - 1:
				break
	else:
		while i < n:
			b = []
			while i < n and a[i] < 0:
				b.append(a[i])
				i = i + 1
			sum += max(b)
			if i > n - 1:
				break
			b = []
			while i < n and a[i] > 0:
				b.append(a[i])
				i = i + 1
			sum += max(b)
			if i > n - 1:
				break
	print(sum)


def convert(list):
	n = len(list)
	if n == 1:
		return list[0]
	else:
		s = 0
		i = 0
		while i < n - 1:
			if list[i] * list[i + 1] < 0:
				s += list[i]
				i += 1
			elif list[i] < 0:
				min = list[i]
				j = 1
				while i + j < n and list[i + j] < 0:
					if list[i + j] > min:
						min = list[i + j]
						j += 1
					else:
						j += 1
				else:
					s += min
					i += j
			else:
				max = list[i]
				j = 0
				while i + j < n and list[i + j] > 0:
					if list[i + j] > max:
						max = list[i + j]
						j += 1
					else:
						j += 1
				else:
					s += max
					i += j
		else:
			if list[-1] * list[-2] < 0:
				s += list[-1]
				return s
			else:
				return s
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	ans = convert(arr)
	print(ans)


m = []
for i in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	a = l[0]
	k = 0
	for j in range(len(l)):
		if l[j] * a < 0:
			k += a
			a = l[j]
		else:
			a = max(l[j], a)
	print(k + a)


def alternateSequence(sequence: list, length):
	alternate_sequence = [sequence[0]]
	flag = sequence[0] > 0
	for num in sequence:
		if flag and num > 0 or (not flag and num < 0):
			alternate_sequence[-1] = max(alternate_sequence[-1], num)
		elif not flag and num > 0 or (flag and num < 0):
			alternate_sequence.append(num)
			flag = num > 0
	print(sum(alternate_sequence))
testcases = int(input())
sequences = []
for _ in range(testcases):
	length = int(input())
	sequences.append([length, list(map(int, input().split()))])
for (length, sequence) in sequences:
	alternateSequence(sequence, length)


def negative_picker(a):
	N = len(a)
	(xsum, i, xlen) = (0, 0, 0)
	while i < N and a[i] > 0:
		i += 1
	while i < N:
		nmax = a[i]
		while i < N and a[i] < 0:
			nmax = max(nmax, a[i])
			i += 1
		xsum += nmax
		xlen += 1
		if i >= N:
			break
		pmax = a[i]
		while i < N and a[i] > 0:
			pmax = max(pmax, a[i])
			i += 1
		xsum += pmax
		xlen += 1
	return (xsum, xlen)

def postive_picker(a):
	N = len(a)
	(xsum, i, xlen) = (0, 0, 0)
	while i < N and a[i] < 0:
		i += 1
	while i < N:
		pmax = a[i]
		while i < N and a[i] > 0:
			pmax = max(pmax, a[i])
			i += 1
		xsum += pmax
		xlen += 1
		if i >= N:
			break
		nmax = a[i]
		while i < N and a[i] < 0:
			nmax = max(nmax, a[i])
			i += 1
		xsum += nmax
		xlen += 1
	return (xsum, xlen)
for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	(lo, hi) = (0, N - 1)
	(psum, nsum) = (postive_picker(a), negative_picker(a))
	out = max(psum, nsum, key=lambda x: x[1])
	print(out[0])


n = int(input())
for i in range(n):
	x = int(input())
	l = list(map(int, input().split()))
	ml = []
	maxi = []
	mini = []
	for j in range(len(l)):
		if l[j] < 0:
			if maxi != []:
				ml.append(max(maxi))
				maxi.clear()
			mini.append(l[j])
		else:
			if mini != []:
				ml.append(max(mini))
				mini.clear()
			maxi.append(l[j])
	if maxi != []:
		ml.append(max(maxi))
	if mini != []:
		ml.append(max(mini))
	print(sum(ml))


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	a.append(-a[len(a) - 1])
	s = 0
	c = -1000000001
	for i in range(n):
		if a[i] > c:
			c = a[i]
		if a[i] >= 0 and a[i + 1] <= 0 or (a[i + 1] >= 0 and a[i] <= 0):
			s = s + c
			c = c = -1000000001
	print(s)


t = int(input())
for z in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	g = []
	sum = 0
	c = a[n - 1]
	if c < 0:
		while i < n:
			if a[i] < 0:
				g.append(a[i])
			else:
				if g != []:
					sum += max(g)
					g.clear()
				while i < n and a[i] > 0:
					g.append(a[i])
					i += 1
				i -= 1
				if g != []:
					sum += max(g)
					g.clear()
			i += 1
		if g != []:
			sum += max(g)
		print(sum)
	else:
		while i < n:
			if a[i] > 0:
				g.append(a[i])
			else:
				if g != []:
					sum += max(g)
					g.clear()
				while i < n and a[i] < 0:
					g.append(a[i])
					i += 1
				i -= 1
				sum += max(g)
				g.clear()
			i += 1
		if g != []:
			sum += max(g)
		print(sum)


def test_c():
	input()
	seq = [int(subseq) for subseq in input().split(' ')]
	sum_ = 0
	pos = seq[0] < 0
	sub_num = 0
	for num in seq:
		if pos:
			if num >= 0:
				sub_num = max(sub_num, num)
			else:
				sum_ += sub_num
				sub_num = num
		elif num >= 0:
			sum_ += sub_num
			sub_num = num
		else:
			sub_num = max(sub_num, num)
		pos = num >= 0
	print(sum_ + sub_num)
t = int(input())
for _ in range(t):
	test_c()


tc = int(input())
for i in range(tc):
	res = 0
	n = int(input())
	lis = list(map(int, input().split()))
	temp_lis = []
	for i in lis:
		if len(temp_lis) == 0:
			temp_lis.append(i)
			parity = 0 if i > 0 else 1
		else:
			new_p = 0 if i > 0 else 1
			if new_p == parity:
				temp_lis.append(i)
			else:
				res += max(temp_lis)
				temp_lis = [i]
				parity = 0 if i > 0 else 1
	if len(temp_lis) != 0:
		res += max(temp_lis)
	print(res)


import math
for w in range(int(input())):
	x = int(input())
	a = list(map(int, input().split()))
	lista = [a[0]]
	for i in range(1, len(a)):
		if a[i] > 0 and lista[-1] > 0:
			if a[i] > lista[-1]:
				lista[-1] = a[i]
		elif a[i] < 0 and lista[-1] < 0:
			if a[i] > lista[-1]:
				lista[-1] = a[i]
		else:
			lista.append(a[i])
	print(sum(lista))


n = int(input())
for i in range(n):
	t = int(input())
	mas = list(map(int, input().split()))
	answer = []
	znak = True
	c = 1
	Sum = mas[0]
	Max = 0
	for i in range(t - 1):
		if mas[i] * mas[i + 1] < 0:
			c += 1
			answer.append(mas[i])
		else:
			mas[i + 1] = max(mas[i], mas[i + 1])
	answer.append(mas[-1])
	print(sum(answer))


def sign(n):
	return n < 0
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	si = sign(l[0])
	s = 0
	m = l[0]
	for i in l:
		if si != sign(i):
			s += m
			m = i
			si = sign(i)
		else:
			m = max(m, i)
	s += m
	print(s)


t = int(input())
while t > 0:
	n = int(input())
	arr = input().split(' ')
	arr = [int(x) for x in arr]
	(p, o) = ([], [])
	(mx, mn) = (0, 0)
	ans = 0
	(c, d) = (0, 0)
	for i in arr:
		if i > 0:
			if d:
				ans += mx
				mx = 0
			mn = max(mn, i)
			(c, d) = (1, 0)
		else:
			if c:
				ans += mn
				mn = 0
			if mx < 0:
				mx = max(mx, i)
			else:
				mx = min(mx, i)
			(d, c) = (1, 0)
	ans += mx + mn
	print(ans)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = a[0] > 0
	current = a[0]
	reduced = []
	for x in a:
		if pos and x > 0:
			current = max(x, current)
		elif pos:
			reduced.append(current)
			current = x
			pos = False
		elif not pos and x < 0:
			current = max(x, current)
		else:
			reduced.append(current)
			current = x
			pos = True
	reduced.append(current)
	print(sum(reduced))


ans = []
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, input().split()))
	k = u[0] // abs(u[0])
	mx = u[0]
	ansi = 0
	for i in range(1, n):
		if u[i] // abs(u[i]) == k:
			mx = max(mx, u[i])
		else:
			k = -k
			ansi += mx
			mx = u[i]
	ansi += mx
	ans.append(ansi)
print('\n'.join(map(str, ans)))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	sm = 0
	lr = 0
	k = 0
	p = []
	if a[0] < 0:
		k = 1
	if k:
		s = a[0]
	else:
		l = a[0]
	for j in range(1, n):
		if a[j - 1] < 0 and a[j] > 0:
			p.append(s)
			l = a[j]
			k = 0
		elif a[j - 1] > 0 and a[j] < 0:
			p.append(l)
			s = a[j]
			k = 1
		elif k:
			s = max(s, a[j])
		else:
			l = max(l, a[j])
	if a[n - 1] < 0:
		s = max(s, a[n - 1])
		p.append(s)
	else:
		l = max(l, a[n - 1])
		p.append(l)
	print(sum(p))


t = int(input())
for test in range(t):
	n = int(input())
	ans = []
	arr = [int(x) for x in input().split()]
	if arr[0] < 0:
		mode = 1
	else:
		mode = 2
	import sys
	minneg = -sys.maxsize + 1
	maxpos = 0
	for i in range(n):
		if mode == 1 and arr[i] > 0:
			mode = 2
			ans.append(minneg)
			minneg = -sys.maxsize + 1
			if arr[i] > maxpos:
				maxpos = arr[i]
		elif mode == 2 and arr[i] > 0:
			if arr[i] > maxpos:
				maxpos = arr[i]
		elif mode == 2 and arr[i] < 0:
			mode = 1
			ans.append(maxpos)
			maxpos = 0
			if arr[i] > minneg:
				minneg = arr[i]
		elif mode == 1 and arr[i] < 0:
			if arr[i] > minneg:
				minneg = arr[i]
	if mode == 1:
		ans.append(minneg)
	if mode == 2:
		ans.append(maxpos)
	print(sum(ans))


for T in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a1 = []
	a1.append(arr[0])
	i = 1
	x = 1
	while i < n:
		if a1[x - 1] > 0 and arr[i] > 0:
			if a1[x - 1] < arr[i]:
				a1.pop()
				a1.append(arr[i])
		elif a1[x - 1] > 0 and arr[i] < 0:
			a1.append(arr[i])
			x += 1
		elif a1[x - 1] < 0 and arr[i] < 0:
			if a1[x - 1] < arr[i]:
				a1.pop()
				a1.append(arr[i])
		else:
			a1.append(arr[i])
			x += 1
		i += 1
	print(sum(a1))


def findsum(numberList):
	bigList = []
	sum = 0
	pos = 0
	neg = 0
	if numberList[0] > 0:
		pos = numberList[0]
	else:
		neg = numberList[0]
	for i in range(0, len(numberList) - 1):
		if pos < numberList[i + 1] and numberList[i] * numberList[i + 1] > 0 and (numberList[i] > 0) or (numberList[i + 1] > neg and numberList[i] * numberList[i + 1] > 0 and (numberList[i] < 0)):
			if numberList[i] > 0:
				pos = numberList[i + 1]
			else:
				neg = numberList[i + 1]
		elif numberList[i] < 0 and numberList[i + 1] > 0 or (numberList[i] > 0 and numberList[i + 1] < 0):
			if numberList[i] > 0:
				sum += pos
				bigList.append(pos)
				pos = 0
				neg = numberList[i + 1]
			else:
				sum += neg
				bigList.append(neg)
				neg = 0
				pos = numberList[i + 1]
	sum += pos
	sum += neg
	print(sum)
a = int(input())
for i in range(a):
	random = input()
	numbers = input().split()
	numbers = [int(i) for i in numbers]
	findsum(numbers)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.append(0)
	P = 0
	N = 0
	maxP = 0
	maxN = -1 * 10 ** 9
	summ = 0
	if a[0] > 0:
		maxP = a[0]
		P = 1
	else:
		maxN = a[0]
		N = 1
	for i in range(n):
		if a[i] > maxP and P == 1:
			maxP = a[i]
		elif a[i] > maxN and N == 1:
			maxN = a[i]
		if a[i + 1] > 0 and N == 1:
			summ += maxN
			maxN = -1 * 10 ** 9
			P = 1
			N = 0
		elif a[i + 1] < 0 and P == 1:
			summ += maxP
			maxP = 0
			P = 0
			N = 1
		elif a[i + 1] == 0:
			if P == 1:
				summ += maxP
			elif N == 1:
				summ += maxN
	print(summ)


def calc(srr):
	s = 0
	i = 0
	j = 0
	while i <= len(srr) and j <= len(srr):
		p = srr[i]
		while j < len(srr) and (srr[i] < 0 and srr[j] < 0 or (srr[i] > 0 and srr[j] > 0)):
			p = max(p, srr[j])
			j += 1
		i = j
		s = s + p
		if i >= len(srr):
			break
	return s
try:
	t = int(input())
	for i in range(t):
		n = int(input())
		srr = [int(x) for x in input().split()]
		print(calc(srr))
except:
	pass


t = int(input())

def sign(number):
	if number > 0:
		return 1
	else:
		return -1
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	j = 0
	while j < n:
		cur = a[j]
		k = j
		while k < n and sign(a[j]) == sign(a[k]):
			cur = max(cur, a[k])
			k += 1
		sum += cur
		j = k - 1
		j += 1
	print(sum)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	seq = []
	pt1 = 0
	pt2 = 0
	i = 0
	sign = 0
	if a[0] > 0:
		sign = 1
	else:
		sign = -1
	while i < n:
		pt1 = i
		while i < n and sign * a[i] > 0:
			pt2 = i
			i += 1
		seq.append(max(a[pt1:pt2 + 1]))
		sign = -sign
	print(sum(seq))


for q in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	som = 0
	maxm = arr[0]
	(i, j) = (0, 0)
	while i < n and j < n:
		if arr[i] >= 0:
			maxm = -10 ** 19
			while j < n and arr[j] >= 0:
				maxm = max(maxm, arr[j])
				j += 1
			som += maxm
			i = j
		else:
			maxm = -10 ** 19
			while j < n and arr[j] < 0:
				maxm = max(maxm, arr[j])
				j += 1
			som += maxm
			i = j
	print(som)


t = int(input())
for i in range(t):
	n = int(input())
	lis = list(map(int, input().split()))
	neg = 0
	pos = 0
	s = 0
	j = 0
	while j < n:
		if lis[j] < 0:
			neg = lis[j]
			while j < n and lis[j] < 0:
				neg = max(neg, lis[j])
				j = j + 1
			s = s + neg
		else:
			pos = lis[j]
			while j < n and lis[j] > 0:
				pos = max(lis[j], pos)
				j = j + 1
			s = s + pos
	print(s)


t = int(input())

def maxsum(l):
	positive = []
	negative = []
	s = 0
	if l[0] > 0:
		positive.append(l[0])
		if l[1] < 0:
			s += l[0]
			positive = []
	else:
		negative.append(l[0])
		if l[1] > 0:
			s += l[0]
			negative = []
	for i in range(1, len(l) - 1):
		if l[i] > 0:
			positive.append(l[i])
			if l[i + 1] < 0:
				s += max(positive)
				positive = []
		else:
			negative.append(l[i])
			if l[i + 1] > 0:
				s += max(negative)
				negative = []
	if l[-1] > 0:
		positive.append(l[-1])
		if l[-2] < 0:
			s += l[-1]
		else:
			s += max(positive)
	else:
		negative.append(l[-1])
		if l[-2] > 0:
			s += l[-1]
		else:
			s += max(negative)
	return s
for i in range(t):
	n = int(input())
	List = list(map(int, input().split()))
	if len(List) == 1:
		print(List[0])
	else:
		print(maxsum(List))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.append(0)
	b = []
	maxv = a[0]
	for i in range(n):
		maxv = max(maxv, a[i])
		if a[i] > 0 and a[i + 1] < 0:
			b.append(maxv)
			maxv = -100000000000000000
		elif a[i] < 0 and a[i + 1] > 0:
			b.append(maxv)
			maxv = 0
	b.append(maxv)
	print(sum(b))


for _ in range(int(input())):
	_ = input()
	a = list(map(int, input().split()))
	t = a[0]
	total = 0
	for x in a:
		if t > 0 and x > 0 or (t < 0 and x < 0):
			t = max(t, x)
		elif t > 0 and x < 0 or (t < 0 and x > 0):
			total += t
			t = x
	total += t
	print(total)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sign = 1 if a[0] > 0 else -1
	(i, sum) = (0, 0)
	while i < n:
		curr_no = a[i]
		curr_sign = 1 if curr_no > 0 else -1
		p_mx = 0
		n_mx = -1000000000000
		flag = False
		sign = curr_sign
		while sign == curr_sign:
			flag = True
			if sign == 1:
				p_mx = max(p_mx, curr_no)
				n_mx = 0
			if sign == -1:
				n_mx = max(n_mx, curr_no)
				p_mx = 0
			i += 1
			if i < n:
				curr_no = a[i]
				curr_sign = 1 if curr_no > 0 else -1
			else:
				break
		sum = sum + p_mx + n_mx
		if not flag:
			i += 1
	print(sum)


def solve(a):
	max_ = a[0]
	prev_max = a[0]
	switch_counter = 0
	if len(a) > 1:
		sum_ = 0
		for i in range(1, len(a)):
			current_item = a[i]
			previous_item = a[i - 1]
			if (current_item > max_) & (current_item * previous_item >= 0):
				prev_max = max_
				max_ = current_item
			if current_item * previous_item < 0:
				if current_item > max_:
					prev_max = max_
				sum_ += max_
				max_ = current_item
				prev_max = max_
				switch_counter += 1
			if i == len(a) - 1:
				if current_item >= max_:
					max_ = current_item
				sum_ += max_
	else:
		sum_ = a[0]
	print(sum_)
t = int(input())
for i in range(t):
	len_ = int(input())
	a = list(map(lambda x: int(x), input().split(' ')))
	solve(a)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	new = []
	pos = False
	neg = False
	if a[0] > 0:
		pos = True
	else:
		neg = True
	val = -math.inf
	for i in a:
		if pos:
			if i > 0:
				val = max(val, i)
			else:
				new.append(val)
				val = i
				pos = False
				neg = True
		elif i < 0:
			val = max(val, i)
		else:
			new.append(val)
			val = i
			neg = False
			pos = True
	new.append(val)
	print(sum(new))


L = []

def main():
	for i in range(int(input())):
		n = int(input())
		L = list(map(int, input().split()))
		s = 0
		i = 0
		while i < len(L):
			mi = -1 * (10 ** 9 + 1)
			ma = 0
			if L[i] > 0:
				while i < len(L) and L[i] > 0:
					ma = max(ma, L[i])
					i += 1
				s += ma
			else:
				while i < len(L) and L[i] < 0:
					mi = max(mi, L[i])
					i += 1
				s += mi
		print(s)
		L = []
main()


from itertools import groupby

def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))
(POSITIVE, NEGATIVE) = (0, 1)

def get_key(a):
	return POSITIVE if a > 0 else NEGATIVE

def solve():
	n = read_int()
	arr = read_ints()
	S = 0
	for (_, numbers) in groupby(arr, key=get_key):
		S += max(numbers)
	return S
T = read_int()
for _ in range(T):
	print(solve())


def same_sign(a, b):
	if a < 0 and b < 0:
		return True
	if a > 0 and b > 0:
		return True
	return False

def solve(arr, n):
	(i, s) = (0, 0)
	while i < n:
		mx = arr[i]
		j = i
		while j < n and same_sign(arr[i], arr[j]):
			mx = max(arr[j], mx)
			j += 1
		s += mx
		i = j
	return s
test_cases = int(input())
res = []
for i in range(test_cases):
	n = int(input())
	line = input().split()
	arr = [int(n) for n in line]
	res.append(solve(arr, n))
for r in res:
	print(r)


def more(lis, li, j, che):
	if li[j] > lis[che]:
		lis[che] = li[j]
	return lis
n = int(input())
for i in range(0, n):
	le = int(input())
	li = list(map(int, input().split()))
	lis = []
	lis.append(li[0])
	(ma, mi) = (0, 0)
	che = 0
	for j in range(1, le):
		if lis[che] > 0 and li[j] > 0:
			lis = more(lis, li, j, che)
		elif lis[che] < 0 and li[j] > 0:
			lis.append(li[j])
			che = che + 1
		elif lis[che] > 0 and li[j] < 0:
			lis.append(li[j])
			che = che + 1
		elif lis[che] < 0 and li[j] < 0:
			lis = more(lis, li, j, che)
	print(sum(lis))


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	sign = 'p' if a[0] > 0 else 'n'
	it = 0
	maxi = -float('inf')
	mini = maxi
	while it < n:
		if sign == 'p' and a[it] > 0:
			maxi = max(maxi, a[it])
		elif sign == 'n' and a[it] < 0:
			mini = max(mini, a[it])
		else:
			break
		it += 1
	ans = maxi if sign == 'p' else mini
	if it < n:
		sign = 'p' if a[it] > 0 else 'n'
	while it < n:
		flag1 = 0
		maxi = -float('inf') if sign == 'p' else maxi
		while sign == 'p' and a[it] > 0:
			maxi = max(maxi, a[it])
			it += 1
			flag1 = 1
			if it == n:
				break
		if flag1:
			ans += maxi
			flag1 = 0
		if it == n:
			break
		sign = 'p' if a[it] > 0 else 'n'
		flag2 = 0
		mini = -float('inf') if sign == 'n' else mini
		while sign == 'n' and a[it] < 0:
			mini = max(mini, a[it])
			it += 1
			flag2 = 1
			if it == n:
				break
		if flag2:
			ans += mini
			flag2 = 0
		if it == n:
			break
		sign = 'p' if a[it] > 0 else 'n'
	print(ans)
	t = t - 1


from collections import defaultdict

def find_k(arr):
	z = local_max = arr[0]
	res = 0
	for x in arr[1:]:
		if z // abs(z) + x // abs(x) == 0:
			res += local_max
			z = local_max = x
		else:
			local_max = max(local_max, x)
	res += local_max
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(find_k(a))


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

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

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect

def sign(x):
	return math.copysign(1, x)
for _ in range(I()):
	n = I()
	a = L()
	s = sign(a[0])
	ss = set()
	ans = 0
	cnt = 1
	for i in range(n + 1):
		if i == n or sign(a[i]) != s:
			s *= -1
			ans += max(ss)
			ss.clear()
			cnt += 1
		if i == n:
			break
		ss.add(a[i])
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = []
	ps = 0
	ns = 0
	x = []
	y = []
	for i in range(n):
		if a[i] > 0:
			x.append(a[i])
			if len(y) > 0:
				b.append(y)
			y = []
			ns = 0
			ps = 1
		if a[i] < 0:
			y.append(a[i])
			if len(x) > 0:
				b.append(x)
			x = []
			ps = 0
	if len(x) > 0:
		b.append(x)
	if len(y) > 0:
		b.append(y)
	c = []
	for i in range(len(b)):
		c.append(max(b[i]))
	print(sum(c))


t = int(input())

def sign(i):
	if i < 0:
		return -1
	return 1
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	g = []
	i = 0
	si = sign(a[0])
	while i < n:
		j = i
		m = a[i]
		while j < n and sign(a[j]) == si:
			m = max(m, a[j])
			j += 1
		g.append(m)
		i = j
		if i < n:
			si = sign(a[i])
	print(sum(g))


for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	s = 0
	maximum = a[0]
	for i in range(n):
		if a[i] * maximum < 0:
			s += maximum
			maximum = a[i]
		else:
			maximum = max(maximum, a[i])
	print(s + maximum)


from itertools import *
for i in range(int(input())):
	input()
	print(sum([max(list(map(int, x))) for (_, x) in groupby(input().split(), str.isdigit)]))


t = int(input())
for case_num in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = []
	for i in a:
		if len(b) == 0 or b[-1][0] * i < 0:
			b.append([i])
		else:
			b[-1].append(i)
	print(sum([max(interval) for interval in b]))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	sum = 0
	prev = 0
	max_neg = 0
	max_pos = 0
	for j in a:
		if j < 0:
			if prev >= 0:
				sum += prev
				(prev, max_neg) = (j, j)
			elif prev <= 0:
				if j >= max_neg:
					(prev, max_neg) = (j, j)
		elif prev <= 0:
			sum += prev
			(prev, max_pos) = (j, j)
		elif prev >= 0:
			if j >= max_pos:
				(prev, max_pos) = (j, j)
	print(sum + prev)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(el) for el in input().split()]
	s = 0
	max_plus = 0
	max_otr = 0
	for el in a:
		if el > 0:
			s += max_otr
			max_otr = 0
			max_plus = max(max_plus, el)
		else:
			s += max_plus
			max_plus = 0
			if max_otr < 0:
				max_otr = max(max_otr, el)
			else:
				max_otr = el
	s += max_plus + max_otr
	print(s)


t = int(input())
for gfsgd in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	ele_sum = 0
	i = 0
	while i < n:
		cur = a[i]
		j = i
		while j < n and (cur > 0 and a[j] > 0 or (cur < 0 and a[j] < 0)):
			cur = max(cur, a[j])
			j = j + 1
		ele_sum += cur
		i = j
	print(ele_sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] < 0:
		f = -1
	else:
		f = 1
	ans = 0
	temp = a[0]
	for k in range(1, n):
		if f == 1 and a[k] < 0:
			ans += temp
			f = -1
			temp = a[k]
		elif f == 1 and a[k] > 0:
			temp = max(temp, a[k])
		elif f == -1 and a[k] > 0:
			ans += temp
			f = 1
			temp = a[k]
		else:
			temp = max(temp, a[k])
	ans += temp
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n - 1):
		if l[i] > 0 and l[i + 1] < 0 or (l[i] < 0 and l[i + 1] > 0):
			continue
		else:
			l[i + 1] = max(l[i], l[i + 1])
			l[i] = 0
	res = 0
	for li in l:
		res += li
	print(res)


t = int(input())
for z in range(t):
	n = int(input())
	data = input().split()
	array = []
	for i in data:
		array.append(int(i))
	j = 0
	total = 0
	while j < n:
		maximum = 0
		minimum = 0
		while j < n and array[j] > 0:
			if maximum < array[j]:
				maximum = array[j]
			j += 1
		total += maximum
		while j < n and array[j] < 0:
			if minimum < array[j]:
				minimum = array[j]
			elif minimum == 0:
				minimum = array[j]
			j += 1
		total += minimum
	print(total)


t = int(input())
for ig in range(t):
	n = int(input())
	lst = [int(x) for x in input().split()]
	ans = []
	m = 0
	i = 0
	while i < n:
		if lst[i] > 0:
			m = lst[i]
			while i < n and lst[i] > 0:
				m = max(m, lst[i])
				i += 1
		else:
			m = lst[i]
			while i < n and lst[i] < 0:
				m = max(m, lst[i])
				i += 1
		ans.append(m)
	print(sum(ans))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = []
	i = 0
	j = 0
	while True:
		flag = False
		while j < n and arr[j] > 0:
			flag = True
			j += 1
		if i != j:
			ans.append(max(arr[i:j]))
			i = j
		while j < n and arr[j] < 0:
			flag = True
			j += 1
		if i != j:
			ans.append(max(arr[i:j]))
			i = j
		if flag == False or i >= n:
			break
	print(sum(ans))


t = int(input())

def sign(n):
	if n > 0:
		return 1
	else:
		return -1
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	chunk = []
	for i in range(n):
		if not chunk or sign(a[i]) == sign(chunk[0]):
			chunk.append(a[i])
		else:
			b.append(chunk)
			chunk = [a[i]]
	b.append(chunk)
	res = 0
	for i in range(len(b)):
		res += max(b[i])
	print(res)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	now = a[0]
	maxx = a[0]
	ans = []
	summ = 0
	for j in a[1:]:
		if maxx < 0 and j > 0:
			summ += maxx
			maxx = j
		elif maxx > 0 and j < 0:
			summ += maxx
			maxx = j
		elif j > maxx:
			maxx = j
	summ += maxx
	print(summ)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	tmp = a[0]
	for j in range(1, n - 1):
		if tmp * a[j] < 0:
			ans += tmp
			tmp = a[j]
		else:
			tmp = max(tmp, a[j])
	if tmp * a[-1] < 0:
		ans += tmp + a[-1]
	else:
		ans += max(tmp, a[-1])
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	j = 0
	while j < n:
		if l[j] > 0:
			a = l[j]
			while l[j] > 0:
				a = max(l[j], a)
				j += 1
				if j == n:
					break
			l1.append(a)
		else:
			b = l[j]
			while l[j] < 0:
				b = max(l[j], b)
				j += 1
				if j == n:
					break
			l1.append(b)
	print(sum(l1))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	summ = [l[0]]
	if l[0] > 0:
		prev = 1
	else:
		prev = 0
	for i in l:
		if i > 0 and prev == 1:
			if i > summ[-1]:
				summ[-1] = i
		elif i < 0 and prev == 0:
			if i > summ[-1]:
				summ[-1] = i
		else:
			summ.append(i)
			prev = 0 if prev == 1 else 1
	print(sum(summ))


def sign(x):
	if x > 0:
		return True
	else:
		return False
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	l = []
	maxx = 0
	while i < len(a):
		cur = a[i]
		j = i
		while j < n and sign(a[i]) == sign(a[j]):
			cur = max(cur, a[j])
			j += 1
		l.append(maxx)
		maxx += cur
		i = j
	print(maxx)


for _ in range(int(input())):
	input()
	a = [int(x) for x in input().split()]
	sum = 0
	sign = -1
	subsequence = []
	for i in range(len(a)):
		if i == 0:
			sign = a[i] > 0
			subsequence.append(a[i])
		else:
			subsequence.append(a[i - 1])
			if sign and a[i] < 0 or (not sign and a[i] > 0):
				sign = not sign
				sum += max(subsequence)
				subsequence.clear()
			subsequence.append(a[i])
	sum += max(subsequence)
	print(sum)


t = int(input())
for weer in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	lis = [1] * n
	prev = 0
	temp = []
	i = 0
	for i in range(1, n):
		lis[i] = lis[i - 1]
		if li[i] * li[i - 1] < 0:
			lis[i] = lis[i - 1] + 1
	temp = []
	prev = -1
	c = 1
	ma = li[0]
	for i in range(n):
		if lis[i] == c + 1:
			c += 1
			temp.append(ma)
			ma = li[i]
		else:
			ma = max(ma, li[i])
	temp.append(ma)
	print(sum(temp))


for i in range(int(input())):
	n = int(input())
	L = list(map(int, input().split()))
	sum = 0
	a = L[0]
	for i in L:
		if a // i < 0:
			sum += a
			a = i
		if a < i:
			a = i
	print(sum + a)


from collections import *
from sys import *
inp = stdin.readline
flag = 1
if not flag:
	out = stdout.write
	str = str
else:
	out = print

def linp(x):
	return list(map(int, x.split()))

def minp(x):
	return map(int, x.split())
t = int(inp())
while t > 0:
	n = int(inp())
	l = linp(inp()) + [-float('INF')]
	n += 1
	i = 0
	arr = []
	while i != n - 1:
		if l[i] < 0:
			max1 = l[i]
			i += 1
			while l[i] < 0 and i != n - 1:
				if l[i] > max1:
					max1 = l[i]
				i += 1
			arr.append(max1)
		elif l[i] > 0:
			max2 = l[i]
			i += 1
			while l[i] > 0 and i != n - 1:
				if l[i] > max2:
					max2 = l[i]
				i += 1
			arr.append(max2)
	print(sum(arr))
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(k) for k in input().split()]
	m = 0
	m1 = -10000000000
	s = 0
	for j in a:
		if j > 0:
			if m1 != -10000000000:
				s += m1
			m1 = -10000000000
			m = max(m, j)
		if j < 0:
			if m != 0:
				s += m
			m = 0
			m1 = max(m1, j)
	if a[n - 1] > 0:
		print(s + m)
	if a[n - 1] < 0:
		print(s + m1)


def solve():
	T = int(input())
	for t in range(T):
		n = int(input())
		nums = list(map(int, input().split()))
		s_idx = [0]
		for i in range(1, n):
			if nums[i - 1] * nums[i] < 0:
				s_idx.append(i)
		s_idx.append(n)
		arr = []
		for i in range(1, len(s_idx)):
			arr.append(max(nums[s_idx[i - 1]:s_idx[i]]))
		print(sum(arr))
solve()


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	now = []
	for i in range(n - 1):
		now.append(a[i])
		if a[i] * a[i + 1] < 0:
			l.append(max(now))
			now = []
	now.append(a[-1])
	l.append(max(now))
	print(sum(l))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	final_arr = 0
	flag = 0
	m = 0
	for i in l:
		if i < 0:
			if flag == 1:
				final_arr += m
			if flag == -1:
				m = max(m, i)
			else:
				m = i
				flag = -1
		else:
			if flag == -1:
				final_arr += m
			if flag == 1:
				m = max(m, i)
			else:
				m = i
				flag = 1
	final_arr += m
	print(final_arr)


for test in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	maxpos = 0
	maxneg = -float('inf')
	if n == 1:
		print(arr[0])
		continue
	posi = True if arr[0] > 0 else False
	finale = 0
	for i in range(n):
		if arr[i] > 0:
			if posi:
				maxpos = max(maxpos, arr[i])
			else:
				finale += maxneg
				maxpos = 0
				maxpos = max(maxpos, arr[i])
			posi = True
		else:
			if not posi:
				maxneg = max(maxneg, arr[i])
			else:
				finale += maxpos
				maxneg = -float('inf')
				maxneg = max(maxneg, arr[i])
			posi = False
	if arr[-1] > 0:
		finale += maxpos
	else:
		finale += maxneg
	print(finale)


def ispos(n):
	if n < 0:
		return False
	else:
		return True

def isneg(n):
	if n < 0:
		return True
	else:
		return False
for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ll = [0] * n
	ans = 0
	temp = [l[0]]
	for i in range(n):
		if l[i] > 0:
			ll[i] = 1
	for i in range(1, n):
		if ll[i] != ll[i - 1]:
			ans = ans + max(temp)
			temp = []
			temp.append(l[i])
		else:
			temp.append(l[i])
	if len(temp) != 0:
		ans += max(temp)
	print(ans)


from sys import stdin
import math
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()

def cost(x):
	c = 0
	for i in range(n // 2):
		if a[i] + a[n - 1 - i] == x:
			continue
		elif min(a[i], a[n - 1 - i]) >= x or max(a[i], a[n - 1 - i]) + k < x:
			c += 2
		else:
			c += 1
	return c
for _ in range(t):
	(n,) = gil()
	a = gil()
	ans = 0
	prev = a[0]
	a.append(-a[-1])
	for i in range(n + 1):
		if a[i] * prev > 0:
			prev = max(prev, a[i])
		else:
			ans += prev
			prev = a[i]
	print(ans)


import math, sys

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

def ls():
	return list(map(int, list(input())))

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

def isPrime(n):
	i = 2
	if n == 1:
		return False
	while i <= int(math.sqrt(n)):
		if n % i == 0:
			return False
		i = i + 1
	return True

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = ii()
for _ in range(t):
	nn = ii()
	a = li()
	pos = []
	neg = []
	p = []
	n = []
	if a[0] > 0:
		p.append(a[0])
	else:
		n.append(a[0])
	for i in range(1, nn):
		if a[i - 1] < 0 and a[i] > 0:
			neg.append(max(n))
			p = []
			n = []
		elif a[i - 1] > 0 and a[i] < 0:
			pos.append(max(p))
			p = []
			n = []
		if a[i] > 0:
			p.append(a[i])
		else:
			n.append(a[i])
	if n != []:
		neg.append(max(n))
	if p != []:
		pos.append(max(p))
	total = 0
	if neg != []:
		total += sum(neg)
	if pos != []:
		total += sum(pos)
	print(total)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mx = arr[0]
	s = 0
	for j in range(n):
		if arr[j] * mx < 0:
			s += mx
			mx = arr[j]
		else:
			mx = max(mx, arr[j])
	print(s + mx)


def solve():
	for i in range(int(input())):
		n = int(input())
		l = list(map(int, input().strip().split()))
		if n == 1:
			print(l[0])
			continue
		r = []
		if l[0] > 0:
			pas = 1
		else:
			pas = 0
		ma = l[0]
		for i in range(1, n):
			if l[i] > 0:
				pre = 1
			else:
				pre = 0
			if pre == pas and pre == 1:
				ma = max(l[i], ma)
			elif pre == pas and pre == 0:
				ma = max(l[i], ma)
			else:
				r.append(ma)
				pas = pre
				ma = l[i]
			if i == n - 1:
				r.append(ma)
		print(sum(r))
solve()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	l = 1
	s = prev = arr[0]
	for i in range(1, n):
		if prev > 0 and prev < arr[i]:
			s -= prev
			s += arr[i]
			prev = arr[i]
		elif 0 > arr[i] > prev < 0:
			s -= prev
			s += arr[i]
			prev = arr[i]
		elif arr[i] > 0 > prev or arr[i] < 0 < prev:
			s += arr[i]
			l += 1
			prev = arr[i]
	print(s)


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	i = 0
	while i < n:
		if a[i] < 0:
			mx_val = float('-inf')
			while i < n and a[i] < 0:
				mx_val = max(mx_val, a[i])
				i += 1
			ans += mx_val
		else:
			mx_val = float('-inf')
			while i < n and a[i] > 0:
				mx_val = max(mx_val, a[i])
				i += 1
			ans += mx_val
	print(ans)


i = int(input())
for _ in range(i):
	n = int(input())
	a = list(map(int, input().split()))
	out = 0
	sign = True if a[0] > 0 else False
	temp = 0 if sign else -10 ** 9
	for i in a:
		if sign:
			if i < 0:
				out += temp
				temp = i
				sign = not sign
			else:
				temp = max(temp, i)
		elif i > 0:
			out += temp
			temp = i
			sign = not sign
		else:
			temp = max(temp, i)
	out += temp
	print(out)


from collections import Counter
from _collections import defaultdict
import math
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	mx = -9999999999
	ans = 0
	for i in range(0, n):
		if i == 0:
			if a[i] > 0:
				sign = 1
				mx = max(mx, a[i])
			else:
				sign = -1
				mx = max(mx, a[i])
		elif sign == 1 and a[i] < 0:
			ans = ans + mx
			sign = -1
			mx = a[i]
		elif sign == -1 and a[i] > 0:
			ans = ans + mx
			sign = 1
			mx = a[i]
		else:
			mx = max(a[i], mx)
	ans = ans + mx
	print(ans)


from sys import stdin, stdout
import math
mod = 10 ** 9 + 7
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	ans = 0
	temp = True
	t1 = arr[0]
	t2 = 0
	if arr[0] < 0:
		temp = False
	for i in range(n):
		if t1 >= 0:
			if arr[i] < 0:
				ans += t1
				t1 = arr[i]
			else:
				t1 = max(t1, arr[i])
		elif t1 < 0:
			if arr[i] >= 0:
				ans += t1
				t1 = arr[i]
			else:
				t1 = max(t1, arr[i])
	ans += t1
	stdout.write(str(ans) + '\n')


import sys
for t in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	a = tuple(map(int, sys.stdin.readline().split()))
	sex = set(a)
	for i in range(n):
		if a[i] > 0:
			j = i
			break
	else:
		print(max(sex))
		continue
	strick = set()
	sum_ = 0
	plus = a[0] > 0
	for i in range(n):
		if plus and a[i] < 0:
			sum_ += max(strick)
			plus = False
			strick = set()
		elif not plus and a[i] > 0:
			sum_ += max(strick)
			plus = True
		strick.add(a[i])
	sum_ += max(strick)
	print(sum_)


t = int(input())
for _ in range(t):
	r = []
	n = int(input())
	l = list(map(int, input().split()))
	p = l[0]
	for x in l:
		if x * p < 0:
			r.append(p)
			p = x
		elif x > p:
			p = x
	r.append(p)
	print(sum(r))


MOD = 10 ** 9 + 7
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

def main():
	test = int(input())
	for iteration in range(test):
		n = int(input())
		arr = [int(i) for i in input().split()]
		sum = 0
		if arr[0] > 0:
			max_pos = arr[0]
			max_neg = MOD * -1
			prev = 'pos'
		else:
			max_neg = arr[0]
			max_pos = 0
			prev = 'neg'
		for i in range(1, n):
			if arr[i] > 0:
				if prev == 'pos':
					if arr[i] > max_pos:
						max_pos = arr[i]
				else:
					sum += max_neg
					max_pos = arr[i]
					max_neg = MOD * -1
				prev = 'pos'
			else:
				if prev == 'neg':
					if arr[i] > max_neg:
						max_neg = arr[i]
				else:
					sum += max_pos
					max_neg = arr[i]
					max_pos = 0
				prev = 'neg'
		if prev == 'pos':
			sum += max_pos
		else:
			sum += max_neg
		print(sum)
main()


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split()
	for j in range(n):
		s[j] = int(s[j])
	count = 0
	mp = 0
	mn = -100000000
	if s[0] > 0:
		mp = s[0]
		count = count + mp
	elif s[0] < 0:
		mn = s[0]
		count = count + mn
	for j in range(n - 1):
		if s[j] > 0 and s[j + 1] < 0:
			mp = 0
			mn = s[j + 1]
			count = count + mn
		elif s[j] < 0 and s[j + 1] > 0:
			mn = -1000000000
			mp = s[j + 1]
			count = count + mp
		elif s[j] > 0 and s[j + 1] > 0:
			if s[j + 1] > mp:
				count = count - mp
				mp = s[j + 1]
				count = count + mp
		elif s[j] < 0 and s[j + 1] < 0:
			if s[j + 1] > mn:
				count = count - mn
				mn = s[j + 1]
				count = count + mn
	print(count)


t = int(input())
for ti in range(t):
	k = int(input())
	s = [int(i) for i in input().split()]
	pos = 0
	neg = 0
	if s[0] > 0:
		pos += 1
	else:
		neg += 1
	indices = [0]
	for i in range(1, len(s)):
		if s[i] > 0 and s[i - 1] < 0:
			pos += 1
			indices.append(i)
		elif s[i] < 0 and s[i - 1] > 0:
			neg += 1
			indices.append(i)
	indices.append(len(s))
	max_sum = 0
	if pos + neg > 0:
		for i in range(len(indices) - 1):
			max_sum += max(s[indices[i]:indices[i + 1]])
	print(max_sum)


t = int(input())
for q in range(t):
	n = int(input())
	a = [int(a) for a in input().split()]
	sum = 0
	temp = 0
	for i in range(1, n):
		if a[i] / a[i - 1] < 0:
			sum += max(a[temp:i])
			temp = i
	sum += max(a[temp:])
	print(sum)


t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	sum1 = 0
	while i < n:
		if l[i] < 0:
			j = i
			while l[i] < 0:
				i += 1
				if i == n:
					break
			sum1 += max(l[j:i])
		else:
			j = i
			while l[i] > 0 and i < n:
				i += 1
				if i == n:
					break
			sum1 += max(l[j:i])
	print(sum1)
	t -= 1


def subseq(n, A):
	count = 0
	state = 0
	last = A[0]
	if A[0] > 0:
		state = 1
	for i in range(1, n):
		if state == 0:
			if A[i] > 0:
				count += last
				last = A[i]
				state = 1
			elif A[i] > last:
				last = A[i]
		elif A[i] > 0:
			if A[i] > last:
				last = A[i]
		else:
			count += last
			last = A[i]
			state = 0
	return count + last
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(subseq(n, arr))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	cur_pos = min(arr) - 1
	cur_neg = min(arr) - 1
	is_last_pos = arr[0] > 0
	new_arr = [arr[0]]
	for i in range(len(arr)):
		if is_last_pos:
			if arr[i] < 0:
				new_arr.append(arr[i])
				is_last_pos = False
			elif arr[i] > 0:
				if arr[i] > new_arr[len(new_arr) - 1]:
					new_arr[len(new_arr) - 1] = arr[i]
		elif arr[i] < 0:
			if arr[i] > new_arr[len(new_arr) - 1]:
				new_arr[len(new_arr) - 1] = arr[i]
		elif arr[i] > 0:
			new_arr.append(arr[i])
			is_last_pos = True
	if len(new_arr) == 1:
		print(max(arr))
	else:
		print(sum(new_arr))


import sys
inp = sys.stdin.readline
read = lambda : list(map(int, inp().split()))

def a():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		s = '11'
		while n % int(s, 2) != 0:
			s += '1'
		ans += str(n // int(s, 2)) + '\n'
	print(ans)

def b():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		if not n // 2 % 2:
			ans += 'YES\n'
			arr = [[], []]
			for i in range(1, n + 2):
				if i != (n + 2) // 2:
					arr[i % 2].append(str(i))
			ans += ' '.join(arr[0] + arr[1]) + '\n'
		else:
			ans += 'NO\n'
	print(ans)

def c():
	ans = ''
	for _ in range(int(inp())):
		n = int(inp())
		arr = read() + [0]
		s = 0
		m = arr[0]
		for i in range(1, n + 1):
			if arr[i] < 0 < arr[i - 1] or arr[i] > 0 > arr[i - 1] or arr[i] == 0:
				s += m
				m = arr[i]
			else:
				m = max(arr[i], m)
		ans += str(s) + '\n'
	print(ans)
c()


def solve():
	n = int(input())
	ar = [int(i) for i in input().split()]
	s = 0
	m = ar[0]
	for i in ar[1:]:
		if m > 0 and i > 0 or (m < 0 and i < 0):
			if m < i:
				m = i
		else:
			s += m
			m = i
	s += m
	return s
tc = int(input())
for i in range(tc):
	print(solve())


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		z = a[i]
		j = i
		while j < n and a[i] * a[j] >= 0:
			z = max(z, a[j])
			j += 1
		s += z
		i = j
	print(s)


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


k = int(input())
for j in range(k):
	n = int(input())
	a = [int(x) for x in input().split()]
	na = [a[0]]
	for i in range(1, len(a)):
		if a[i] // na[-1] < 0:
			na.append(a[i])
		elif a[i] > na[-1]:
			na.pop(-1)
			na.append(a[i])
	ans = 0
	for i in na:
		ans += i
	print(ans)


def dp(arr):
	lst = []
	lst.append([arr[0]])
	c = 0
	maxi = 0
	for i in range(1, len(arr)):
		if arr[i] * lst[c][-1] > 0:
			lst[c].append(arr[i])
		else:
			temp = [arr[i]]
			lst.append(temp)
			c += 1
	for i in lst:
		maxi += max(i)
	return maxi
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(dp(arr))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	subs = []
	sub = []
	for i in a:
		if sub and i // abs(i) != sub[-1] // abs(sub[-1]):
			subs.append(sub)
			sub = []
		sub.append(i)
	if sub:
		subs.append(sub)
	ans = 0
	for sub in subs:
		ans += max(sub)
	print(ans)


def right_pl(past, current):
	return past > 0 and current > 0 or (past < 0 and current < 0)
for item in range(int(input())):
	input()
	mass = list(map(int, input().split()))
	buff = []
	ans = []
	mass.append(0)
	for iter in range(1, len(mass)):
		if right_pl(mass[iter], mass[iter - 1]):
			buff += [mass[iter], mass[iter - 1]]
		else:
			buff.append(mass[iter - 1])
			ans.append(buff)
			buff = []
	print(sum([max(iter) for iter in ans]))


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

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

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 res

def primefs(n):
	primes = {}
	while n % 2 == 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:
			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):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	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(1000000.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 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()
	sign = None
	ans = 0
	prev = None
	for i in a:
		if sign == None:
			if i > 0:
				sign = 1
			else:
				sign = 0
			ans = i
			prev = i
			continue
		if i > 0:
			if sign == 1:
				if i > prev:
					ans -= prev
					ans += i
					prev = i
			else:
				ans += i
				prev = i
				sign = 1
		elif sign == 1:
			ans += i
			prev = i
			sign = 0
		elif i > prev:
			ans -= prev
			ans += i
			prev = i
	print(ans)


from sys import stdin, exit
input = stdin.readline

def i():
	return input()

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

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

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

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	n = ii()
	a = liis()
	soma = 0
	positive = True
	maxi = 0
	for i in a:
		if positive:
			if i > 0:
				maxi = max(maxi, i)
			else:
				soma += maxi
				positive = False
				maxi = i
		elif i < 0:
			maxi = max(maxi, i)
		else:
			soma += maxi
			positive = True
			maxi = i
	soma += maxi
	print(soma)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = []
	i = 0
	while i < n:
		a = [l[i]]
		j = i + 1
		while j < n:
			if l[i] * l[j] > 0:
				a.append(l[j])
				j = j + 1
			else:
				k.append(a)
				break
		else:
			k.append(a)
		i = j
	s = 0
	for i in range(len(k)):
		s = s + max(k[i])
	print(s)


case = int(input())
for i in range(case):
	n = int(input())
	list1 = [int(v) for v in input().split()]
	ans = 0
	temp = list1[0]
	for j in range(1, len(list1)):
		if list1[j] * list1[j - 1] > 0:
			if list1[j] > temp:
				temp = list1[j]
			else:
				pass
		else:
			ans += temp
			temp = list1[j]
	print(ans + temp)


import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from collections import deque
t = int(input())
for ii in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	if max(l) < 0 or min(l) > 0:
		print(max(l))
		continue
	if l[0] > 0:
		f = 1
	else:
		f = -1
	m = l[0]
	z = []
	for i in range(n - 1):
		if l[i] * l[i + 1] < 0:
			z.append(max(m, l[i]))
			m = -10 ** 10
		else:
			m = max(m, l[i])
	if l[-1] * z[-1] > 0:
		z[-1] = max(z[-1], max(m, l[-1]))
	else:
		z.append(max(m, l[-1]))
	print(sum(z))
sys.exit()
N = 5
es = [[1, 2, 3], [0, 2], [0, 1], [0, 4], [3]]
colors = [0 for i in range(N)]

def dfs(v, color):
	print(v, color)
	colors[v] = color
	for to in es[v]:
		if colors[to] == color:
			return False
		if colors[to] == 0 and (not dfs(to, -color)):
			return False
	return True

def is_bipartite():
	return dfs(0, 1)
print(is_bipartite())


el = int(input())
for x in range(el):
	nums = int(input())
	ch = 0
	tempval = 0
	count = 0
	for y in input().split(' '):
		temp = int(y)
		if ch == -1:
			if temp > 0:
				ch = 1
				count = count + tempval
				tempval = temp
			elif temp > tempval:
				tempval = temp
		elif ch == 1:
			if temp < 0:
				ch = -1
				count = count + tempval
				tempval = temp
			elif temp > tempval:
				tempval = temp
		else:
			tempval = temp
			if temp > 0:
				ch = 1
			else:
				ch = -1
	print(count + tempval)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = [l[0]]
	for i in range(1, len(l)):
		if l[i - 1] < 0 and l[i] > 0 or (l[i] < 0 and l[i - 1] > 0):
			ans.append(l[i])
		else:
			ans[-1] = max(ans[-1], max(l[i], l[i - 1]))
	print(sum(ans))


t = int(input())
for _ in range(t):
	S = 0
	n = int(input())
	m = input().split()
	sign = '-' in m[0]
	M = -1000000000000.0
	for i in m:
		i = int(i)
		if (i < 0) == sign:
			M = max(M, i)
		else:
			sign = 1 - sign
			S += M
			M = i
	S += M
	print(S)


for test in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())] + [0]
	answer = 0
	counter = 0
	element = None
	old = None
	for element in a:
		if element == 0:
			answer += old
		if old == None:
			old = element
		elif old * element < 0:
			answer += old
			old = element
		else:
			old = max(old, element)
	print(answer)


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 0
	cur = A[0]
	max_ = A[0]
	for i in range(n):
		if A[i] * cur > 0:
			max_ = max(max_, A[i])
		else:
			ans += max_
			cur = A[i]
			max_ = A[i]
	else:
		ans += max_
	print(ans)


def alt_subsequence(arr, n):
	int_list = list(map(lambda x: 'P' if x > 0 else 'N', arr))
	idx_list = []
	sum_list = []
	if all(('P' == i for i in int_list)):
		return max(arr)
	elif all(('N' == i for i in int_list)):
		return max(arr)
	else:
		for i in range(n - 1):
			if int_list[i] == 'N' and int_list[i + 1] == 'P':
				idx_list.append(i + 1)
			elif int_list[i] == 'P' and int_list[i + 1] == 'N':
				idx_list.append(i + 1)
		sum_list.append(max(arr[:idx_list[0]]))
		for i in range(len(idx_list) - 1):
			sum_list.append(max(arr[idx_list[i]:idx_list[i + 1]]))
		sum_list.append(max(arr[idx_list[-1]:]))
		return sum(sum_list)
t = int(input())
for t_itr in range(t):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	result = alt_subsequence(arr, n)
	print(result)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if l[0] > 0:
		positive = True
	else:
		positive = False
	ans = [l[0]]
	for i in l[1:]:
		if positive == True and i < 0:
			ans.append(i)
			positive = False
		elif positive == True and ans[-1] < i:
			ans[-1] = i
		elif positive == False and i > 0:
			ans.append(i)
			positive = True
		elif positive == False and ans[-1] < i:
			ans[-1] = i
	print(sum(ans))


import heapq as hp
import collections
import bisect

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def s_unpack(func=int):
	return sorted(map(func, input().split()))

def range_n():
	return range(int(input()))

def counter(a):
	d = {}
	for x in a:
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	return d

def getint():
	return int(input())

def same_sign(sym, x):
	return sym > 0 and x > 0 or (sym < 0 and x < 0)

def main():
	for _ in range_n():
		n = getint()
		a = l_unpack()
		sym = 1 if a[0] > 0 else -1
		ans = 0
		i = 0
		while i < n:
			temp = float('-inf')
			while i < n and same_sign(sym, a[i]):
				temp = max(temp, a[i])
				i += 1
			else:
				sym = 1 if sym < 0 else -1
			ans += temp
		print(ans)
main()


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


kl = int(input())
for l in range(kl):
	n = int(input())
	t = 0
	s = 0
	for i in input().split():
		i = int(i)
		if t * i > 0:
			t = max(t, i)
		else:
			s += t
			t = i
	print(s + t)


t = int(input())
for p in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 0
	ne = 0
	for i in range(n):
		if A[i] < 0:
			ne += 1
	if ne == n:
		print(max(A))
		continue
	elif ne == 0:
		print(max(A))
		continue
	N = []
	P = []
	for i in range(0, n):
		if A[i] < 0:
			N.append(A[i])
			if len(P) == 0:
				continue
			ans += max(P)
			P = []
		if A[i] > 0:
			P.append(A[i])
			if len(N) == 0:
				continue
			ans += max(N)
			N = []
	if len(P) != 0:
		ans += max(P)
	if len(N) != 0:
		ans += max(N)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	A = [int(i) for i in input().split()]
	m = -float('INF')
	op = 1
	if A[0] < 0:
		op = -1
	ans = 0
	for i in A:
		if op == 1:
			if i > 0:
				if i > m:
					m = i
			else:
				ans += m
				m = i
				op = -1
		elif i < 0:
			if i > m:
				m = i
		else:
			ans += m
			m = i
			op = 1
	ans += m
	print(ans)


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 main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI()
		t = a[0]
		r = 0
		for c in a[1:]:
			if (t < 0) == (c < 0):
				if t < c:
					t = c
			else:
				r += t
				t = c
		r += t
		rr.append(r)
	return JA(rr, '\n')
print(main())


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = tuple(map(int, input().split()))
		sgn = A[0] > 0
		ans = [A[0]]
		for a in A:
			if sgn == (a > 0):
				ans[-1] = max(ans[-1], a)
			else:
				ans.append(a)
				sgn = a > 0
		print(sum(ans))
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.append(-a[-1])
	j = 0
	b = []
	for i in range(1, n + 1):
		if a[i - 1] < 0 < a[i] or a[i - 1] > 0 > a[i]:
			s = max(a[j:i])
			b.append(s)
			j = i
	print(sum(b))


def alt(l, n):
	u = l[-1]
	p = 0
	for k in range(n - 1, -1, -1):
		if u < 0 and l[k] < 0:
			u = max(u, l[k])
		elif u > 0 and l[k] > 0:
			u = max(u, l[k])
		else:
			p += u
			u = l[k]
	p += u
	return p
for k in range(int(input())):
	n = int(input())
	t = list(map(int, input().split()))
	print(alt(t, n))


from math import *
inf = -10 ** 20
for zz in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	segs = []
	pos = False
	if a[0] > 0:
		pos = True
	segs = [inf]
	for i in range(n):
		if a[i] > 0 and (not pos) or (a[i] < 0 and pos):
			pos = not pos
			segs.append(inf)
		segs[-1] = max(segs[-1], a[i])
	print(sum(segs))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	stack = [l[0]]
	for i in l[1:]:
		if len(stack) and (stack[-1] < 0 and i > 0) or (stack[-1] > 0 and i < 0):
			stack.append(i)
		elif len(stack) > 0 and stack[-1] > 0 and (i > 0) and (i > stack[-1]):
			while stack and i >= stack[-1] and (stack[-1] > 0):
				stack.pop()
			stack.append(i)
		elif len(stack) > 0 and stack[-1] < 0 and (i < 0) and (i > stack[-1]):
			while stack and i >= stack[-1] and (stack[-1] < 0):
				stack.pop()
			stack.append(i)
	print(sum(stack))


t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	negative = []
	positive = []
	ans = 0
	pre_num = array[0]
	for i in range(1, n):
		if pre_num * array[i] < 0:
			ans += pre_num
			pre_num = array[i]
		else:
			pre_num = max(pre_num, array[i])
	ans += pre_num
	print(ans)


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

def lisdiv():
	return list(map(int, list(input())))
t = int(input())
for _ in range(t):
	n = int(input())
	elem = linput()
	flag = 1
	if elem[0] < 0:
		flag = -1
	k = elem[0]
	sum = 0
	i = 1
	while i < n:
		if flag == 1:
			if elem[i] < 0:
				flag = -1
				sum = sum + k
				k = elem[i]
				i = i + 1
			else:
				if elem[i] > k:
					k = elem[i]
				i = i + 1
		elif flag == -1:
			if elem[i] > 0:
				flag = 1
				sum = sum + k
				k = elem[i]
				i = i + 1
			else:
				if elem[i] > k:
					k = elem[i]
				i = i + 1
	sum = sum + k
	print(sum)


t = int(input())
for t1 in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	minus = 0
	plus = 0
	x = []
	y = []
	for i in l:
		if i > 0:
			if len(y) > 0 and plus == 0:
				s = s + max(y)
				y = []
			x.append(i)
			plus = 1
		else:
			if len(x) > 0 and plus == 1:
				s = s + max(x)
				x = []
			y.append(i)
			plus = 0
	if plus == 0:
		s = s + max(y)
	else:
		s = s + max(x)
	print(s)


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


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(l[0])
		continue
	(m, s) = (l[0], 0)
	f = 1 if l[0] > 0 else 0
	for i in l:
		if f == 1 and i > 0:
			if i > m:
				m = i
		elif f == 1 and i < 0:
			s += m
			m = i
			f = 0
		elif f == 0 and i < 0:
			if i > m:
				m = i
		elif f == 0 and i > 0:
			s += m
			m = i
			f = 1
	s += m
	print(s)


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		i = 1
		p = 1 if a[0] > 0 else 0
		maxi = a[0] if a[0] > 0 else 0
		mini = a[0] if a[0] < 0 else -10 ** 10
		l = []
		while i < n:
			if p == 0 and a[i] > 0:
				l.append(mini)
				p = 1
				maxi = a[i]
			elif p == 1 and a[i] < 0:
				l.append(maxi)
				p = 0
				mini = a[i]
			if p == 0:
				if mini < a[i]:
					mini = a[i]
			elif p == 1:
				if maxi < a[i]:
					maxi = a[i]
			i += 1
		if p == 0:
			l.append(mini)
		else:
			l.append(maxi)
		print(sum(l))
main()


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


import math
n1 = int(input())
for j in range(n1):
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	k = 1
	b = []
	b.append(a[0])
	for i in range(1, n):
		if a[i] * b[k - 1] > 0:
			if a[i] > b[k - 1]:
				b[k - 1] = a[i]
		elif a[i] * b[k - 1] < 0:
			b.append(a[i])
			k += 1
	print(sum(b))


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


def solve():
	n = int(input())
	lis = list(map(int, input().split()))
	j = 0
	ans = 0
	for (i, v) in enumerate(lis[1:], 1):
		if v * lis[j] < 0:
			ans += max(lis[j:i])
			j = i
	ans += max(lis[j:])
	print(ans)
t = int(input())
for i in range(t):
	solve()


for _ in range(int(input())):
	j = int(input())
	a = list(map(int, input().split()))
	l = 0
	x = -10000000000
	k = 0
	sum1 = 0
	p = True
	n = True
	t = []
	m = []
	for i in range(0, j):
		if a[i] >= l:
			l = a[i]
			p = False
			if n == False:
				m.append(k)
				n = True
				k = 0
				x = -10000000000
		elif a[i] > x and a[i] < 0:
			x = a[i]
			k = x
			n = False
			if p == False:
				t.append(l)
				p = True
				l = 0
	print(sum(t) + sum(m) + l + k)


import sys

def bestSubSequence(arr):
	bestSum = 0
	tam = len(arr)
	flgPosi = 0
	ultSigno = 0
	arrAux = []
	for i in range(tam):
		if i == 0:
			if arr[i] > 0:
				ultSigno = 1
			else:
				ultSigno = 0
		if arr[i] >= 0:
			flgPosi = 1
		else:
			flgPosi = 0
		if ultSigno == flgPosi and i != tam - 1:
			arrAux.append(arr[i])
		elif ultSigno != flgPosi and i != tam - 1:
			bestSum += max(arrAux)
			arrAux = []
			arrAux.append(arr[i])
		elif ultSigno == flgPosi and i == tam - 1:
			arrAux.append(arr[i])
			bestSum += max(arrAux)
		else:
			bestSum += max(arrAux)
			bestSum += arr[i]
		ultSigno = flgPosi
	return bestSum
t = int(sys.stdin.readline())
for i in range(t):
	l = []
	n = int(sys.stdin.readline())
	a = sys.stdin.readline().split()
	for j in range(n):
		b = int(a[j])
		l.append(b)
	print(bestSubSequence(l))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	max_pos_so_far = (0, -float('inf'))
	max_neg_so_far = (0, -float('inf'))
	max_so_far = (0, -float('inf'))
	for i in range(n):
		if a[i] < 0:
			max_neg_ending_here = max((1, a[i]), (max_pos_so_far[0] + 1, max_pos_so_far[1] + a[i]))
			max_neg_so_far = max(max_neg_so_far, max_neg_ending_here)
		else:
			max_pos_ending_here = max((1, a[i]), (max_neg_so_far[0] + 1, max_neg_so_far[1] + a[i]))
			max_pos_so_far = max(max_pos_so_far, max_pos_ending_here)
		max_so_far = max(max_so_far, max_pos_so_far, max_neg_so_far)
	print(max_so_far[1])


import sys
import math
import bisect

def solve(A):
	n = len(A)
	B = []
	for a in A:
		if len(B) == 0:
			B.append(a)
		elif B[-1] * a < 0:
			B.append(a)
		else:
			B[-1] = max(B[-1], a)
	return sum(B)

def main():
	for _ in range(int(input())):
		n = int(input())
		A = list(map(int, input().split()))
		print(solve(A))
main()


import sys
import math

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

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')

def main():
	n = ri()
	a = ria()
	b = []
	mx = -1 * 10000000000.0
	sum = 0
	i = 0
	while i < n:
		p = i
		mx = -1 * 10000000000.0
		while i < n and a[i] < 0:
			mx = max(mx, a[i])
			i += 1
		if mx != -1 * 10000000000.0:
			sum += mx
		mx = -1 * 10000000000.0
		while i < n and a[i] > 0:
			mx = max(mx, a[i])
			i += 1
		if mx != -1 * 10000000000.0:
			sum += mx
	wi(sum)
t = ri()
for _ in range(t):
	main()


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	groups = []
	i = 0
	while i < len(A):
		t = []
		while i < len(A) and A[i] < 0:
			t.append(A[i])
			i += 1
		if t:
			groups.append(max(t))
		t = []
		while i < len(A) and A[i] > 0:
			t.append(A[i])
			i += 1
		if t:
			groups.append(max(t))
	print(sum(groups))


t = int(input())
for test in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.append(0)
	ans = 0
	i = 0
	while i < n:
		m = a[i]
		while (a[i] > 0) == (a[i + 1] > 0) and i < n - 1:
			m = max(m, a[i + 1])
			i += 1
		ans += m
		i += 1
	print(ans)


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 findmin(x, y):
	if x < y:
		return x
	return y

def findmax(x, y):
	if x > y:
		return x
	return y
t = inp()
for i in range(0, t):
	n = inp()
	a = list(invr())
	neg = -1000000001
	pos = -1000000001
	result = 0
	for j in range(n):
		if j - 1 >= 0 and a[j - 1] > 0 and (a[j] < 0):
			result = result + pos
			pos = -1000000001
		if j - 1 >= 0 and a[j - 1] < 0 and (a[j] > 0):
			result = result + neg
			neg = -1000000001
		if a[j] < 0 and a[j] > neg:
			neg = a[j]
		if a[j] > 0 and a[j] > pos:
			pos = a[j]
	if pos != -1000000001:
		result = result + pos
	if neg != -1000000001:
		result = result + neg
	print(result)


import sys
sys.setrecursionlimit(10 ** 6)
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 LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

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

def main():
	for _ in range(II()):
		inf = 10 ** 16
		n = II()
		aa = LI()
		pos = aa[0] > 0
		mx = -inf
		s = 0
		for a in aa:
			if pos:
				if a > 0:
					mx = max(mx, a)
				else:
					s += mx
					mx = a
					pos = False
			elif a < 0:
				mx = max(mx, a)
			else:
				s += mx
				mx = a
				pos = True
		s += mx
		print(s)
main()


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	positive = []
	negative = []
	for i in range(n - 1):
		if l[i] > 0 and l[i + 1] < 0:
			positive.append(l[i])
			sum += max(positive)
			positive.clear()
		elif l[i] < 0 and l[i + 1] > 0:
			negative.append(l[i])
			sum += max(negative)
			negative.clear()
		elif l[i] > 0:
			positive.append(l[i])
		else:
			negative.append(l[i])
	if l[n - 2] > 0 and l[n - 1] < 0 or (l[n - 2] < 0 and l[n - 1] > 0):
		sum += l[n - 1]
	elif l[n - 1] > 0:
		positive.append(l[n - 1])
		sum += max(positive)
		positive.clear()
	else:
		negative.append(l[n - 1])
		sum += max(negative)
		negative.clear()
	print(sum)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 0 or n == 1:
		print(sum(a))
	else:
		s = 0
		p = 0
		ne = -1000000000
		fp = 0
		fn = 0
		for i in range(n):
			if a[i] < 0:
				if fn == 1:
					ne = max(ne, a[i])
				else:
					ne = a[i]
				fn = 1
				if fp == 1:
					s += p
				fp = 0
			else:
				if fp == 1:
					p = max(p, a[i])
				else:
					p = a[i]
				fp = 1
				if fn == 1:
					s += ne
				fn = 0
		if a[n - 1] < 0:
			s += ne
		else:
			s += p
		print(s)


t = int(input())
while t > 0:
	l = int(input())
	arr = [int(x) for x in input().split(' ')]
	qi = 0
	q = [[arr[0]]]
	for i in range(1, l):
		if arr[i] * arr[i - 1] > 0:
			q[qi].append(arr[i])
		else:
			q.append([arr[i]])
			qi += 1
	s = 0
	for subsequence in q:
		s += max(subsequence)
	print(s)
	t -= 1


t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = list(map(int, input().split()))
	pos = []
	neg = []
	summ = 0
	for x in arr:
		if x > 0:
			pos.append(x)
			if len(neg) > 0:
				summ = summ + max(neg)
			neg.clear()
		if x < 0:
			neg.append(x)
			if len(pos) > 0:
				summ = summ + max(pos)
			pos.clear()
	if len(neg) > 0:
		summ = summ + max(neg)
	if len(pos) > 0:
		summ = summ + max(pos)
	print(summ)
	i = i + 1


all_ans = []
for _ in range(int(input())):
	n = int(input())
	(*a,) = map(int, input().split())
	pre = a[0]
	ans = 0
	for i in range(1, n):
		now = a[i]
		if pre * now < 0:
			ans += pre
			pre = now
		else:
			pre = max(pre, now)
	else:
		ans += pre
	all_ans.append(ans)
print('\n'.join(map(str, all_ans)))


def sign(num):
	return num > 0
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	i = 0
	while i < n:
		j = i
		mx = arr[i]
		while j < n and sign(arr[i]) == sign(arr[j]):
			mx = max(arr[j], mx)
			j += 1
		i = j - 1
		ans += mx
		i += 1
	print(ans)


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	j = 0
	l = []
	while j < a:
		if b[j] > 0:
			p = b[j]
			while b[j] > 0:
				if p < b[j]:
					p = b[j]
				j += 1
				if j >= a:
					break
			l.append(p)
		else:
			p = b[j]
			while b[j] < 0:
				if p < b[j]:
					p = b[j]
				j += 1
				if j >= a:
					break
			l.append(p)
	print(sum(l))


t = int(input())
for pp in range(0, t):
	n = int(input())
	ip = list(map(int, input().split()))
	if ip[0] > 0:
		arr = []
		curr = ip[0]
		sign = 1
		for i in range(1, len(ip)):
			num = ip[i]
			if num > 0 and sign == 1 and (num > curr):
				curr = num
			elif num < 0 and sign == 1:
				arr.append(curr)
				curr = num
				sign = -1
			elif sign == -1:
				if num < 0 and num > curr:
					curr = num
				if num > 0:
					arr.append(curr)
					curr = num
					sign = 1
		arr.append(curr)
	else:
		arr = []
		curr = ip[0]
		sign = -1
		for i in range(1, len(ip)):
			num = ip[i]
			if num > 0 and sign == 1 and (num > curr):
				curr = num
			elif num < 0 and sign == 1:
				arr.append(curr)
				curr = num
				sign = -1
			elif sign == -1:
				if num < 0 and num > curr:
					curr = num
				if num > 0:
					arr.append(curr)
					curr = num
					sign = 1
		arr.append(curr)
	ss = sum(arr)
	print(ss)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	ans = 0
	i = 0
	while True:
		m = -float('Inf')
		while i < n and a[i] > 0:
			m = max(m, a[i])
			i += 1
		if m != -float('Inf'):
			ans += m
		m = -float('Inf')
		while i < n and a[i] < 0:
			m = max(m, a[i])
			i += 1
		if m != -float('Inf'):
			ans += m
		if i == n:
			break
	print(ans)


def sign(z):
	if int(z) > 0:
		return 1
	else:
		return 0
t = int(input())
for t in range(0, t):
	n = int(input())
	a = [int(x) for x in input().split()]
	x = 0
	i = 0
	while i < n:
		j = i
		mx = a[j]
		while j < n and sign(a[j]) == sign(a[i]):
			mx = max(a[j], mx)
			j = j + 1
		i = j - 1
		x = x + mx
		i = i + 1
	print(x)


t = int(input())
for tests in range(t):
	n = int(input())
	sp = list(map(int, input().split()))
	pol = sp[0] > 0
	maxsum = 0
	maxdobav = sp[0]
	i = 1
	for i in range(1, n):
		gb = sp[i] > 0
		if gb == pol:
			maxdobav = max(maxdobav, sp[i])
		else:
			maxsum += maxdobav
			pol = sp[i] > 0
			maxdobav = sp[i]
	gb = sp[i - 1] > 0
	if gb == pol:
		maxdobav = max(maxdobav, sp[i - 1])
		maxsum += maxdobav
	else:
		maxsum += maxdobav
	print(maxsum)


import sys
from collections import deque
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 print_list_int(l):
	str_list = list(map(str, list(map(int, out))))
	print(' '.join(str_list))
t = inp()
for test in range(t):
	n = inp()
	arr = inlt()
	stack = deque([arr[0]])
	for i in arr[1:]:
		last = stack.pop()
		if (i < 0) == (last < 0):
			stack.append(max(last, i))
		else:
			stack.append(last)
			stack.append(i)
	print(int(sum(stack)))


for _ in range(int(input())):
	n = int(input())
	N = list(map(int, input().split()))
	sumIdx = [0]
	sumVal = 0
	i = 1
	while i < n:
		if N[i] * N[sumIdx[-1]] < 0:
			sumIdx.append(i)
		elif N[i] > N[sumIdx[-1]]:
			sumIdx.pop()
			sumIdx.append(i)
		i += 1
	for i in range(len(sumIdx)):
		sumVal += N[sumIdx[i]]
	print(sumVal)


def task(n, a):
	total = 0
	positive = a[0] > 0
	mx = a[0]
	for i in range(0, n):
		if positive:
			if a[i] > 0:
				mx = max(mx, a[i])
			else:
				total += mx
				positive = False
				mx = a[i]
		elif a[i] < 0:
			mx = max(mx, a[i])
		else:
			total += mx
			positive = True
			mx = a[i]
	total += mx
	print(total)
t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	task(n, a)


from collections import Counter as C, defaultdict as D, deque as Q
from operator import itemgetter as I
from itertools import product as P
from bisect import bisect_left as BL, bisect_right as BR
from heapq import heappush as HPUSH, heappop as HPOP
from math import floor as MF, ceil as MC, gcd as MG, factorial as F, sqrt as SQRT, inf as INFINITY, log as LOG
from sys import stdin, stdout
INPUT = stdin.readline
PRINT = stdout.write

def player1():
	print('')

def player2():
	print('')

def isPrime(n):
	for i in range(2, MC(SQRT(n)) + 1):
		if n % i == 0:
			return False
	return True

def factors(x):
	ans = []
	for i in range(1, MC(SQRT(x)) + 1):
		if x % i == 0:
			ans.append(i)
			if x % (x // i) == 0:
				ans.append(x // i)
	return ans

def main():
	for _ in range(int(INPUT())):
		n = int(input())
		a = list(map(int, INPUT().split()))
		block = []
		for v in a:
			if not block:
				block.append([v])
				continue
			if block[-1][0] < 0 and v < 0:
				block[-1].append(v)
			elif block[-1][0] > 0 and v > 0:
				block[-1].append(v)
			else:
				block.append([v])
		ans = 0
		for v in block:
			ans += max(v)
		PRINT(str(ans) + '\n')
main()


def solution(sub):
	arr = []
	for i in range(len(sub)):
		if sub[i] > 0:
			if not arr or arr[-1] < 0:
				arr.append(sub[i])
			elif arr[-1] < sub[i]:
				arr.pop()
				arr.append(sub[i])
		elif sub[i] < 0:
			if not arr or arr[-1] > 0:
				arr.append(sub[i])
			elif arr[-1] < sub[i]:
				arr.pop()
				arr.append(sub[i])
	return sum(arr)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(solution(a))


def RA():
	l = input().split()
	return [int(s) for s in l]

def solve():
	n = int(input())
	a = RA()
	isPositive = a[0] > 0
	m = a[0]
	result = 0
	for num in a:
		if isPositive:
			if num > 0:
				m = max(m, num)
			else:
				result += m
				m = num
				isPositive = False
		elif num < 0:
			m = max(m, num)
		else:
			result += m
			m = num
			isPositive = True
	print(result + m)
t = int(input())
for i in range(t):
	solve()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sign_change = [0]
	vals = []
	for j in range(1, n):
		if a[j] * a[j - 1] < 0:
			sign_change.append(j)
	sign_change.append(n)
	for j in range(1, len(sign_change)):
		subarray = a[sign_change[j - 1]:sign_change[j]]
		vals.append(max(subarray))
	print(sum(vals))


import math
t = input()
t = int(t)
while t:
	t -= 1
	n = input()
	n = int(n)
	arr = list(map(int, input().split()))
	s = 0
	pos = arr[0]
	if pos > 0:
		want = '+'
	else:
		want = '-'
	for i in range(n):
		if arr[i] > 0:
			sign = '+'
		else:
			sign = '-'
		if want == '+' and sign == '+':
			if arr[i] > pos:
				pos = arr[i]
		elif want == '+' and sign == '-':
			s = s + pos
			pos = arr[i]
			want = '-'
		elif want == '-' and sign == '-':
			if arr[i] > pos:
				pos = arr[i]
		else:
			s = s + pos
			pos = arr[i]
			want = '+'
	s = s + pos
	print(s)


def sgn(x):
	return -1 if x < 0 else 1
t = int(input())
for _t in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	(stx, csgn) = ([[A[0]]], sgn(A[0]))
	for i in range(1, n):
		if sgn(A[i]) == csgn:
			stx[-1].append(A[i])
		else:
			stx.append([A[i]])
			csgn = sgn(A[i])
	ans = 0
	for row in stx:
		ans += max(row)
	print(ans)


def maximumsub(l):
	summ = 0
	choose = None
	for i in l:
		if choose == None:
			choose = i
		elif choose * i < 0:
			summ += choose
			choose = i
		elif i > choose:
			choose = i
	summ += choose
	return summ
t = int(input())
for i in range(t):
	n = int(input())
	print(maximumsub([int(i) for i in input().split()]))


no = int(input())
for ll in range(no):
	n = int(input())
	a = [i for i in map(int, input().split())]
	ans = []
	x = 0
	ans = a[0]
	last = a[0]
	for i in range(1, n):
		if last > 0:
			if a[i] > 0 and a[i] > last:
				ans -= last
				ans += a[i]
				last = a[i]
			if a[i] < 0:
				ans += a[i]
				last = a[i]
		else:
			if a[i] < 0 and a[i] > last:
				ans -= last
				ans += a[i]
				last = a[i]
			if a[i] > 0:
				ans += a[i]
				last = a[i]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	mx = 0
	if l[0] > 0:
		pol = 0
	else:
		pol = 1
	tmax = l[0]
	ans = []
	for i in range(1, len(l)):
		if l[i] > 0 and pol:
			ans.append(tmax)
			tmax = l[i]
			pol = 0
			mx += 1
		elif l[i] > 0 and (not pol) and (l[i] > tmax):
			tmax = l[i]
		elif l[i] < 0 and (not pol):
			ans.append(tmax)
			tmax = l[i]
			pol = 1
			mx += 1
		elif l[i] < 0 and pol and (l[i] > tmax):
			tmax = l[i]
	ans.append(tmax)
	print(sum(ans))


from itertools import groupby
t = int(input())
for _ in range(0, t):
	n = int(input())
	aa = [int(i) for i in input().split()]
	ss = 0
	ans = [[]]
	if aa[0] > 0:
		k = True
	else:
		k = False
	for i in range(0, n):
		if aa[i] > 0 and k:
			ans[-1].append(aa[i])
		elif aa[i] < 0 and (not k):
			ans[-1].append(aa[i])
		elif aa[i] > 0 and (not k):
			k = True
			ans.append([])
			ans[-1].append(aa[i])
		elif aa[i] < 0 and k:
			k = False
			ans.append([])
			ans[-1].append(aa[i])
	for i in ans:
		ss += max(i)
	print(ss)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if l[0] > 0:
		positive = True
	else:
		positive = False
	curr_el = l[0]
	curr_sum = l[0]
	t_sum = [l[0]]
	if n > 1:
		for x in range(1, n):
			if l[x] > 0 and positive == True:
				if l[x] > t_sum[-1]:
					t_sum[-1] = l[x]
			elif l[x] < 0 and positive == False:
				if l[x] > t_sum[-1]:
					t_sum[-1] = l[x]
			elif l[x] > 0 and positive == False:
				positive = True
				t_sum.append(l[x])
			elif l[x] < 0 and positive == True:
				positive = False
				t_sum.append(l[x])
	print(sum(t_sum))


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 and i < n - 1:
			if a[i + 1] > 0:
				c.append(a[i])
			else:
				c.append(a[i])
				b.append(max(c))
				c = []
		if a[i] > 0 and i == n - 1:
			c.append(a[i])
			b.append(max(c))
		elif a[i] < 0 and i < n - 1:
			if a[i + 1] < 0:
				c.append(a[i])
			else:
				c.append(a[i])
				b.append(max(c))
				c = []
		if a[i] < 0 and i == n - 1:
			c.append(a[i])
			b.append(max(c))
	print(sum(b))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	g = []
	x = []
	for i in range(len(l)):
		if l[i] > 0:
			if l[i - 1] < 0:
				g.append(x)
				x = []
			x.append(l[i])
		else:
			if l[i - 1] > 0:
				g.append(x)
				x = []
			x.append(l[i])
	g.append(x)
	s = 0
	if len(g[0]) == 0:
		g.pop(0)
	for i in g:
		s += max(i)
	print(s)


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


for i in range(int(input())):
	n = int(input())
	arr = []
	last = 0
	for i in map(int, input().split()):
		if last == 0:
			last = i
		elif (i > 0) != (last > 0):
			arr.append(last)
			last = i
		else:
			last = max(last, i)
	arr.append(last)
	print(sum(arr))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(s, m) = (0, l[0])
	for i in range(1, n):
		if m * l[i] < 0:
			s += m
			m = l[i]
		elif m < l[i]:
			m = l[i]
	s += m
	print(s)


for _ in range(int(input())):
	(n, a) = (int(input()), [int(x) for x in input().split()])
	(x, i) = ([a[0]], 0)
	for row in a[1:]:
		if row < 0 and x[i] < 0 or (row >= 0 and x[i] >= 0):
			x[i] = max(x[i], row)
		else:
			i += 1
			x += [row]
	print(sum(x))


import math

def solve(n, arr):
	seg = -math.inf
	neg = arr[0] < 0
	total = 0
	for a in arr:
		if a < 0 and neg:
			seg = max(seg, a)
		elif a > 0 and (not neg):
			seg = max(seg, a)
		elif a < 0 and (not neg) or (a > 0 and neg):
			total += seg
			seg = a
		neg = a < 0
	return total + seg
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	print(solve(n, arr))


from math import *
from bisect import bisect_left, bisect_right
from queue import Queue
from sys import stdin, stdout
from collections import Counter
input = lambda : stdin.readline().strip()
print = stdout.write
t = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().split()))
	M = ls[0]
	s = 0
	flag = 0 if ls[0] > 0 else 1
	for i in range(n):
		if ls[i] > 0 and flag == 0 or (ls[i] < 0 and flag == 1):
			M = max(M, ls[i])
		else:
			s += M
			M = ls[i]
			flag = 1 - flag
	s += M
	print(str(s) + '\n')


t = int(input())
for j in range(t):
	A = []
	s = 0
	n = int(input())
	A += list(map(int, input().split()))
	mx = A[0]
	for i in range(1, n):
		if A[i - 1] * A[i] < 0:
			s += mx
			mx = A[i]
		mx = max(mx, A[i])
	s += mx
	print(s)


for k in range(int(input())):
	n = int(input())
	elements = list(map(int, input().split(' ')))
	max_pos = 0
	min_neg = 0
	if elements[0] > 0:
		max_pos = elements[0]
	else:
		min_neg = elements[0]
	sum_e = 0
	for i in range(1, n):
		if elements[i] > 0 and elements[i - 1] < 0:
			sum_e += min_neg
			min_neg = 0
			max_pos = elements[i]
		elif elements[i] < 0 and elements[i - 1] > 0:
			sum_e += max_pos
			max_pos = 0
			min_neg = elements[i]
		elif max_pos == 0:
			if abs(elements[i]) < abs(min_neg):
				min_neg = elements[i]
		elif elements[i] > max_pos:
			max_pos = elements[i]
	sum_e += min_neg
	sum_e += max_pos
	print(sum_e)


t = int(input())
inf = -10 ** 10
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	plus = a[0] > 0
	m = a[0]
	ans = 0
	for i in range(1, n):
		if plus and a[i] < 0:
			ans += m
			plus = False
			m = inf
		elif not plus and a[i] > 0:
			ans += m
			plus = True
			m = inf
		m = max(a[i], m)
	print(ans + m)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	mini = -10000000000
	maxu = 0
	tougle = 0
	for i in range(n):
		if i == 0:
			if arr[i] > 0:
				maxu = arr[i]
				tougle = 1
			else:
				mini = arr[i]
				tougle = -1
		else:
			if tougle == 1 and arr[i] < 0:
				tougle = -1
				ans += maxu
				maxu = 0
			elif tougle == -1 and arr[i] > 0:
				tougle = 1
				ans += mini
				mini = -10000000000
			if tougle == 1:
				if arr[i] > maxu:
					maxu = arr[i]
			elif arr[i] > mini:
				mini = arr[i]
	if tougle == 1:
		ans += maxu
	else:
		ans += mini
	print(ans)


from sys import stdin, stdout
from collections import defaultdict, Counter
from functools import lru_cache
from math import gcd, floor, ceil

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

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

def istr():
	return stdin.readline().strip()
t = iint()
for _x in range(t):
	n = iint()
	arr = ilist()
	posMax = 0
	negMax = -float('inf')
	if arr[0] < 0:
		pos = False
	else:
		pos = True
	(i, n) = (0, len(arr))
	res = []
	while i < n:
		if pos:
			negMax = -float('inf')
			while i < n and arr[i] > 0:
				posMax = max(posMax, arr[i])
				i += 1
			pos = False
			res.append(posMax)
		else:
			posMax = 0
			while i < n and arr[i] < 0:
				negMax = max(negMax, arr[i])
				i += 1
			res.append(negMax)
			pos = True
	print(sum(res))


t = int(input())
for i in range(t):
	N = int(input())
	A = list(map(int, input().split()))
	(ans, tmp) = (0, A[0])
	for j in range(1, N):
		if A[j] * A[j - 1] > 0:
			tmp = max(tmp, A[j])
		if A[j] * A[j - 1] < 0:
			ans += tmp
			tmp = A[j]
	print(ans + tmp)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	ma = -1
	mi = min(a)
	s = 1 if a[0] > 0 else -1
	i = 0
	while i <= n - 1:
		if a[i] > 0:
			if s < 0:
				l.append(mi)
				ma = a[i]
				s = 1
				i = i + 1
			else:
				ma = max(ma, a[i])
				i = i + 1
		elif s > 0:
			l.append(ma)
			mi = a[i]
			s = -1
			i = i + 1
		else:
			mi = max(mi, a[i])
			i = i + 1
	if s > 0:
		l.append(ma)
	else:
		l.append(mi)
	print(sum(l))


t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	flagn = False
	flagp = False
	lp = 0
	ln = 0
	res = []
	if arr[0] > 0:
		lp = arr[0]
		flagp = True
	else:
		ln = arr[0]
		flagn = True
	for i in range(1, n):
		if flagp:
			if arr[i] > 0:
				lp = max(lp, arr[i])
			else:
				res.append(lp)
				flagn = True
				flagp = False
				ln = arr[i]
		elif arr[i] < 0:
			ln = max(ln, arr[i])
		else:
			res.append(ln)
			flagp = True
			flagn = False
			lp = arr[i]
	if flagp:
		res.append(lp)
	if flagn:
		res.append(ln)
	print(sum(res))


t = int(input())
for _ in range(t):
	n = int(input())
	(*a,) = map(int, input().split())
	a2 = []
	tmp = []
	for i in range(n):
		if tmp:
			if tmp[-1] * a[i] < 0:
				a2.append(tmp)
				tmp = [a[i]]
			else:
				tmp.append(a[i])
		else:
			tmp.append(a[i])
	a2.append(tmp)
	ans = 0
	for s in a2:
		ans += max(s)
	print(ans)


for i in range(int(input())):
	n = int(input())
	nums = [int(i) for i in input().split()]
	s = 0
	ps = 1 if nums[0] > 0 else 0
	pmax = nums[0]
	for i in range(1, n):
		if nums[i] > 0:
			if ps == 0:
				s += pmax
				ps = 1
				pmax = nums[i]
			elif nums[i] > pmax:
				pmax = nums[i]
		elif ps == 1:
			s += pmax
			ps = 0
			pmax = nums[i]
		elif nums[i] > pmax:
			pmax = nums[i]
	s += pmax
	print(s)


import math
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	res = [l[0]]
	for i in range(1, len(l)):
		if abs(res[-1]) + abs(l[i]) == abs(l[i] + res[-1]):
			if res[-1] < l[i]:
				res[-1] = l[i]
		else:
			res.append(l[i])
	print(sum(res))


T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split(' ')))
	k = 1
	res = 0
	best = a[0]
	prev = 1 if a[0] > 0 else -1
	for val in a[1:]:
		cur = 1 if val > 0 else -1
		if prev != cur:
			res += best
			best = val
			k += 1
			prev = cur
		else:
			best = max(best, val)
	res += best
	print(res)


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()

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

def li():
	return list(mi())
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 main():
	for _ in range(ii()):
		n = ii()
		a = li()
		(f, f1) = (0, 0)
		ans = 0
		x = -mod
		y = -mod
		for i in range(n):
			if a[i] >= 0:
				x = max(x, a[i])
				f = 1
				if f1 == 1:
					ans += y
					y = -mod
					f1 = 0
			else:
				y = max(y, a[i])
				f1 = 1
				if f == 1:
					ans += x
					x = -mod
					f = 0
		if f == 1:
			ans += x
		if f1 == 1:
			ans += y
		print(ans)
main()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		dp = [1 for i in range(n)]
		lastp = -1
		lastn = -1
		for (idx, i) in enumerate(a):
			if i > 0:
				lastp = idx
				if lastn == -1:
					continue
				else:
					dp[idx] = dp[lastn] + 1
			else:
				lastn = idx
				if lastp == -1:
					continue
				else:
					dp[idx] = dp[lastp] + 1
		i = 0
		j = 0
		ans = 0
		while i < n:
			elem = -float('inf')
			while j < n and dp[j] == dp[i]:
				elem = max(elem, a[j])
				j += 1
			ans += elem
			i = j
		print(ans)
	return
main()


T = int(input())
for x in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	maxSum = 0
	lastNo = A[0]
	for a in A:
		if lastNo < 0 and a > 0:
			maxSum += lastNo
			lastNo = a
		elif lastNo > 0 and a < 0:
			maxSum += lastNo
			lastNo = a
		elif a > lastNo:
			lastNo = a
	maxSum += lastNo
	print(maxSum)


for _ in range(int(input())):
	x: int = int(input())
	arr = input().split(' ')
	arr = list(map(int, arr))
	prev_sign = 0
	output = 0
	sum = 0
	for i in range(x):
		if i != 0:
			if arr[i] < 0:
				cur_sign = -1
			else:
				cur_sign = 1
			if cur_sign == prev_sign:
				if arr[i] > arr[output]:
					output = i
			else:
				sum += arr[output]
				output = i
		else:
			output = 0
			if arr[i] < 0:
				cur_sign = -1
			else:
				cur_sign = 1
		prev_sign = cur_sign
	sum += arr[output]
	print(sum)


def sign(n):
	if abs(n) == n:
		return True
	return False

def func(arr):
	sum = 0
	last_added = arr[0]
	sum = arr[0]
	for el in arr[1:]:
		if sign(el) == sign(last_added) and el > last_added:
			sum = sum - last_added + el
			last_added = el
		elif sign(el) != sign(last_added):
			sum += el
			last_added = el
	print(sum)
data = []
n = int(input())
for _ in range(n):
	_ = input()
	data.append(list(map(int, input().split(' '))))
for d in data:
	func(d)


t = int(input())
ans = []
j = 0
while j < t:
	n = int(input())
	seq = input()
	A = [int(s) for s in seq.split(' ')]
	saidai = []
	sub = []
	i = 0
	while i < len(A):
		if i == 0:
			sub.append(A[i])
		else:
			if sub[0] > 0 and A[i] < 0 or (sub[0] < 0 and A[i] > 0):
				saidai.append(max(sub))
				sub.clear()
			sub.append(A[i])
		i += 1
	saidai.append(max(sub))
	ans.append(sum(saidai))
	j += 1
for a in ans:
	print(a)


for test in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	stack = []
	stack.append(arr[0])
	for i in range(0, n):
		if stack[-1] < 0 and arr[i] > 0:
			stack.append(arr[i])
		elif stack[-1] > 0 and arr[i] > 0:
			if stack[-1] < arr[i]:
				stack.pop(-1)
				stack.append(arr[i])
		if stack[-1] > 0 and arr[i] < 0:
			stack.append(arr[i])
		if stack[-1] < 0 and arr[i] < 0:
			if stack[-1] < arr[i]:
				stack.pop(-1)
				stack.append(arr[i])
	print(sum(stack))


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	flag = A[0] // abs(A[0])
	tmp = A[0]
	res = 0
	for (i, a) in enumerate(A[1:]):
		if a * flag > 0:
			if a > tmp:
				tmp = a
		else:
			flag = -flag
			res += tmp
			tmp = a
	res += tmp
	print(res)


for i in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	gg = []
	for i in ar:
		if len(gg) == 0:
			gg.append(i)
		elif gg[-1] * i > 0:
			gg[-1] = max(gg[-1], i)
		elif gg[-1] * i < 0:
			gg.append(i)
	print(sum(gg))


import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, UserDict, defaultdict, deque, namedtuple
from functools import cmp_to_key, lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import chain, combinations, combinations_with_replacement, permutations, product
sys.setrecursionlimit(10 ** 5 + 1)
input = sys.stdin.buffer.readline
MOD = 10 ** 9 + 7
T = int(input())
for _ in range(T):
	n = int(input())
	nums = [int(ch) for ch in input().split()]
	ans = nums[0]
	pre = nums[0]
	for cur in nums[1:]:
		res = cur * pre
		if res > 0 and cur > pre:
			ans += -pre + cur
			pre = cur
		elif res < 0:
			ans += cur
			pre = cur
	print(ans)


import math
n = int(input())
for i in range(n):
	j = int(input())
	s = [int(i) for i in input().split()]
	m = s[0]
	su = []
	for j in s:
		if j * m > 0 and j > m:
			m = j
		if j * m < 0:
			su.append(m)
			m = j
	su.append(m)
	print(sum(su))


def getNegativeSegments(a):
	negSegments = set()
	(start, end) = (0, 0)
	insegment = False
	for i in range(len(a)):
		if a[i] < 0 and insegment == False:
			insegment = True
			start = i
		if a[i] > 0 and insegment == True:
			insegment = False
			end = i
			negSegments.add((start, end))
	if insegment == True:
		negSegments.add((start, len(a)))
	return negSegments

def getPositiveSegments(a):
	posSegments = set()
	(start, end) = (0, 0)
	insegment = False
	for i in range(len(a)):
		if a[i] > 0 and insegment == False:
			insegment = True
			start = i
		if a[i] < 0 and insegment == True:
			insegment = False
			end = i
			posSegments.add((start, end))
	if insegment == True:
		posSegments.add((start, len(a)))
	return posSegments
for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	negSegments = getNegativeSegments(a)
	posSegments = getPositiveSegments(a)
	ans = 0
	for seg in negSegments:
		ans += max(a[seg[0]:seg[1]])
	for seg in posSegments:
		ans += max(a[seg[0]:seg[1]])
	print(ans)


import collections

def solve(n, arr):
	positives = []
	negatives = []
	lastEncountered = None
	for (i, num) in enumerate(arr):
		if num > 0:
			if not lastEncountered or lastEncountered == -1:
				positives.append([num])
			else:
				positives[-1].append(num)
			lastEncountered = 1
		else:
			if not lastEncountered or lastEncountered == 1:
				negatives.append([num])
			else:
				negatives[-1].append(num)
			lastEncountered = -1
	consideringPos = False
	if arr[0] > 0:
		consideringPos = True
	(i, j) = (0, 0)
	lenPos = len(positives)
	lenNeg = len(negatives)
	maxAnsPossible = 0
	while True:
		if consideringPos:
			if i >= lenPos:
				break
			maxAnsPossible += max(positives[i])
			i += 1
		else:
			if j >= lenNeg:
				break
			maxAnsPossible += max(negatives[j])
			j += 1
		consideringPos = not consideringPos
	return maxAnsPossible
t = int(input().strip())
for i in range(t):
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	print(solve(n, arr))


def sol(n, l):
	val = l[0]
	max_ = l[0]
	i = 1
	sum = 0
	while i < n:
		if l[i] > 0 and val > 0 or (l[i] < 0 and val < 0):
			max_ = max(max_, l[i])
		else:
			sum += max_
			max_ = l[i]
		val = l[i]
		i += 1
	sum += max_
	return sum
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(sol(n, l))


import math
t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = []
	neg = []
	for i in range(len(a)):
		if len(pos) != 0:
			if pos[-1] * a[i] > 0:
				pos[-1] = max(a[i], pos[-1])
			else:
				pos.append(a[i])
		if len(neg) != 0:
			if neg[-1] * a[i] > 0:
				neg[-1] = max(a[i], neg[-1])
			else:
				neg.append(a[i])
		if a[i] > 0 and len(pos) == 0:
			pos.append(a[i])
		elif a[i] < 0 and len(neg) == 0:
			neg.append(a[i])
	if len(neg) > len(pos):
		print(sum(neg))
	elif len(pos) > len(neg):
		print(sum(pos))
	else:
		print(max(sum(pos), sum(neg)))


t = int(input())
for k in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	s = 0
	if a[0] > 0:
		i = 0
		while i < n:
			ma = a[i]
			while i < n and a[i] > 0:
				if a[i] > ma:
					ma = a[i]
				i += 1
			s += ma
			if i < n:
				mi = a[i]
				while i < n and a[i] < 0:
					if a[i] > mi:
						mi = a[i]
					i += 1
				s += mi
		print(s)
	else:
		i = 0
		while i < n:
			mi = a[i]
			while i < n and a[i] < 0:
				if a[i] > mi:
					mi = a[i]
				i += 1
			s += mi
			if i < n:
				ma = a[i]
				while i < n and a[i] > 0:
					if a[i] > ma:
						ma = a[i]
					i += 1
				s += ma
		print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	ins = a[0] / abs(a[0])
	s = 0
	l = []
	for i in a:
		tmp = i / abs(i)
		if ins == tmp:
			l.append(i)
		else:
			ins = tmp
			s += max(l)
			l.clear()
			l.append(i)
	s += max(l)
	print(s)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	m = -999999999999999999999999
	for j in range(n):
		if j == 0:
			m = l[j]
			continue
		elif l[j] * l[j - 1] > 0:
			m = max(m, l[j])
		else:
			s += m
			m = l[j]
	s = s + m
	print(s)


from sys import stdin
a = int(stdin.readline())
for b in range(0, a):
	A = int(stdin.readline())
	B = stdin.readline().split()
	MAX = int(B[0])
	sum = 0
	for c in range(0, A):
		if int(B[c]) * MAX < 0:
			sum += MAX
			MAX = int(B[c])
			if c == A - 1:
				sum += MAX
		else:
			MAX = max(MAX, int(B[c]))
			if c == A - 1:
				MAX = max(MAX, int(B[c]))
				sum += MAX
	print(sum)


from sys import stdin, stdout
for query in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	b = a[0] > 0
	changes = []
	for x in range(n):
		d = a[x] > 0
		e = b
		if d != e:
			b = not b
			changes.append(x)
	if len(changes) > 0:
		total = max(a[:changes[0]])
		y = len(changes)
		for x in range(y):
			if x == y - 1:
				total += max(a[changes[x]:])
			else:
				total += max(a[changes[x]:changes[x + 1]])
		stdout.write(str(total) + '\n')
	else:
		stdout.write(str(max(a)) + '\n')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	m = a[0]
	for v in a:
		if m * v < 0:
			s += m
			m = v
		m = max(m, v)
	s += m
	print(s)


from sys import stdin, exit
from bisect import *
from heapq import heappush, heappop
from itertools import combinations
from collections import deque
input = stdin.readline
lmi = lambda : list(map(int, input().split()))
mi = lambda : map(int, input().split())
si = lambda : input().strip('\n')
ssi = lambda : input().strip('\n').split()
for _ in range(int(input())):
	n = int(input())
	arr = lmi()
	i = 0
	tmp = []
	ans = 0
	while i < n:
		tmp.append(arr[i])
		i += 1
		if i >= n or (arr[i] > 0) != (arr[i - 1] > 0):
			ans += max(tmp)
			tmp = []
	print(ans)


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


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))[:n]
	if arr[0] > 0:
		flag = 1
	else:
		flag = 0
	ans = 0
	change = 0
	for i in range(n):
		if flag == 0:
			if arr[i] < 0:
				ans += change
				change = arr[i]
				flag = 1
			else:
				change = max(change, arr[i])
		elif arr[i] > 0:
			ans += change
			change = arr[i]
			flag = 0
		else:
			change = max(change, arr[i])
	ans += change
	print(ans)


for w in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(c, i, s) = (0, 0, 0)
	l2 = []
	while c != n:
		l1 = []
		if l[i] > 0:
			for j in range(i, n):
				if l[j] > 0:
					l1.append(l[j])
					c += 1
				else:
					i = j
					break
		else:
			for j in range(i, n):
				if l[j] < 0:
					l1.append(l[j])
					c += 1
				else:
					i = j
					break
		l2.append(l1)
	for i in l2:
		s = s + max(i)
	print(s)


def solve(a):
	switches = []
	for i in range(1, len(a)):
		if a[i] > 0 and a[i - 1] < 0 or (a[i] < 0 and a[i - 1] > 0):
			switches.append(i)
	pieces = []
	prev = 0
	for switch_index in switches:
		pieces.append(a[prev:switch_index])
		prev = switch_index
	pieces.append(a[prev:])
	return sum(map(max, pieces))
for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(solve(a))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	l1.append(l[0])
	for j in l:
		le = len(l1) - 1
		if l1[le] > 0:
			if l1[le] >= j and j > 0:
				pass
			elif l1[le] < j and j > 0:
				l1.pop()
				l1.append(j)
			else:
				l1.append(j)
		elif l1[le] >= j and j < 0:
			pass
		elif l1[le] < j and j < 0:
			l1.pop()
			l1.append(j)
		else:
			l1.append(j)
	print(sum(l1))


def run():

	def isb(n):
		return 1 if n > 0 else -1
	n = int(input())
	nums = [int(i) for i in input().split()]
	stack = [nums[0]]
	for i in range(1, n):
		if isb(stack[-1]) == isb(nums[i]):
			if nums[i] > stack[-1]:
				stack.pop()
				stack.append(nums[i])
			else:
				continue
		else:
			stack.append(nums[i])
	print(sum(stack))
t = int(input())
for i in range(t):
	run()


def max_val(lst):
	val = lst[0]
	for i in lst[1:]:
		if val < i:
			val = i
	return val
T = int(input())
for t in range(T):
	N = int(input())
	lst = list(map(int, input().split()))
	ind = 0
	temp = []
	temp_index = 0
	while ind < N:
		if lst[ind] > 0:
			for i in range(ind, N):
				if lst[i] > 0:
					temp_index = i + 1
				else:
					break
			temp.append(max_val(lst[ind:temp_index]))
			ind = temp_index
		else:
			for i in range(ind, N):
				if lst[i] < 0:
					temp_index = i + 1
				else:
					break
			temp.append(max_val(lst[ind:temp_index]))
			ind = temp_index
	print(sum(temp))


(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 = zzz()
	(r, c) = (0, 0)
	for i in l:
		if c * i > 0:
			c = max(c, i)
		else:
			r += c
			c = i
	print(r + c)


import sys
cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, input().split()))
	prev = a[0]
	s = 0
	m = prev
	for i in a[1:]:
		if i * prev > 0:
			m = max(m, i)
		else:
			s += m
			m = i
		prev = i
	s += m
	print(s)


def minutes_before():
	for i in range(int(input())):
		x = int(input())
		a = list(map(int, input().split()))
		sum_number = 0
		negative = 0
		positive = 0
		for i in range(x):
			if a[i] < 0:
				if negative == 0:
					negative = a[i]
				else:
					negative = max(a[i], negative)
			elif a[i] > 0:
				if positive == 0:
					positive = a[i]
				else:
					positive = max(a[i], positive)
			if a[i] > 0 and a[i - 1] < 0:
				sum_number += negative
				negative = 0
			elif a[i] < 0 and a[i - 1] > 0:
				sum_number += positive
				positive = 0
		if negative:
			sum_number += max(negative, a[-1])
		elif positive:
			sum_number += max(positive, a[-1])
		print(sum_number)
minutes_before()


t = int(input())
import math
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	pos = 2 * int(l[0] > 0) - 1
	ind = 0
	s = 0
	while ind < n:
		maxi = l[ind]
		while ind < n and pos * l[ind] >= 0:
			maxi = max(maxi, l[ind])
			ind += 1
		s += maxi
		pos *= -1
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = [int(__) for __ in input().split()]
	max1 = a[0]
	ans = 0
	if n == 1:
		print(a[0])
		continue
	for i in range(n - 1):
		if a[i] * a[i + 1] > 0:
			if a[i] > max1:
				max1 = a[i]
		else:
			if a[i] > max1:
				max1 = a[i]
			ans += max1
			max1 = a[i + 1]
	if a[-1] * a[-2] < 1:
		ans += a[-1]
	else:
		ans += max(max1, a[-1])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		prev = 'pos'
	else:
		prev = 'neg'
	temp = a[0]
	cur = prev
	ans = []
	i = 1
	while i < n:
		if a[i] > 0:
			cur = 'pos'
		else:
			cur = 'neg'
		if cur == prev:
			temp = max(temp, a[i])
			i += 1
		else:
			ans.append(temp)
			prev = cur
			temp = a[i]
	ans.append(temp)
	print(sum(ans))


import sys
import math
t = int(sys.stdin.readline())
while t:
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	posnum = 0
	negnum = -math.inf
	ans = 0
	findpos = True
	findneg = True
	prev = ''
	for i in range(n):
		if arr[i] > 0:
			if prev == 'neg':
				ans += negnum
				negnum = -math.inf
			if arr[i] > posnum:
				posnum = arr[i]
			prev = 'pos'
		elif arr[i] < 0:
			if prev == 'pos':
				ans += posnum
				posnum = 0
			if arr[i] > negnum:
				negnum = arr[i]
			prev = 'neg'
	if prev == 'neg':
		ans += negnum
	else:
		ans += posnum
	print(ans)
	t -= 1


def startPostve(arr):
	sum = 0
	max = 0
	max1 = -float('inf')
	for i in range(len(arr)):
		if arr[i] > 0:
			if max1 != -float('inf'):
				sum += max1
				max1 = -float('inf')
			if max < arr[i]:
				max = arr[i]
		else:
			if max != 0:
				sum += max
				max = 0
			if max1 < arr[i]:
				max1 = arr[i]
	if max != 0:
		sum += max
	if max1 != -float('inf'):
		sum += max1
	return sum

def startNegtve(arr):
	sum = 0
	max = 0
	max1 = -float('inf')
	for i in range(len(arr)):
		if arr[i] < 0:
			if max != 0:
				sum += max
				max = 0
			if max1 < arr[i]:
				max1 = arr[i]
		else:
			if max1 != -float('inf'):
				sum += max1
				max1 = -float('inf')
			if max < arr[i]:
				max = arr[i]
	if max != 0:
		sum += max
	if max1 != -float('inf'):
		sum += max1
	return sum

def alternatingSub(arr):
	if arr[0] > 0:
		print(startPostve(arr))
	else:
		print(startNegtve(arr))

def main():
	t = int(input())
	for i in range(t):
		input()
		arr = list(map(int, input().split()))
		alternatingSub(arr)
main()


inp = input
li = list
iinp = lambda : int(inp())
ent = lambda : map(int, inp().split())
lient = lambda : [int(i) for i in inp().split()]
li0 = lambda x: [0 for i in range(x)]
stent = lambda : [i for i in inp()]
ma = max
l = len
t = iinp()
for _ in range(t):
	n = iinp()
	numeros = lient()
	pos = []
	neg = []
	res = 0
	for i in numeros:
		if i > 0:
			if l(neg) > 0:
				res += ma(neg)
				neg = []
			pos.append(i)
		else:
			if l(pos) > 0:
				res += ma(pos)
				pos = []
			neg.append(i)
	if l(neg) > 0:
		res += ma(neg)
	if l(pos) > 0:
		res += ma(pos)
	print(res)


t = int(input())
for rep in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = -1
	maxi = 0
	s = 0
	for i in range(n):
		if f == -1:
			if a[i] > 0:
				maxi = a[i]
				f = 1
			else:
				maxi = a[i]
				f = 0
		elif f == 1:
			if a[i] > 0:
				if a[i] > maxi:
					maxi = a[i]
			else:
				s += maxi
				maxi = a[i]
				f = 0
		elif f == 0:
			if a[i] < 0:
				if a[i] > maxi:
					maxi = a[i]
			else:
				s += maxi
				maxi = a[i]
				f = 1
	s += maxi
	print(s)


from sys import stdin, stdout
from collections import defaultdict, Counter
from functools import lru_cache
from math import gcd, floor, ceil

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

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

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

def solve(a):
	n = len(a)
	res = 0
	prev = 0
	for i in range(1, n):
		if a[i - 1] * a[i] < 0:
			res += max(a[prev:i])
			prev = i
	res += max(a[prev:])
	return res
t = iint()
for _ in range(t):
	n = iint()
	a = ilist()
	print(solve(a))


T = int(input())
for _ in range(T):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print(arr[0])
	else:
		if arr[0] >= 0:
			j = 1
		else:
			j = -1
		check = arr[0]
		count = 0
		for i in range(1, n):
			if j == 1:
				if arr[i] > check:
					check = arr[i]
				if arr[i] < 0:
					count += check
					check = arr[i]
					j = -1
			if j == -1:
				if arr[i] > check and arr[i] < 0:
					check = arr[i]
				if arr[i] > 0:
					count += check
					check = arr[i]
					j = 1
			if i == n - 1:
				count += check
		print(count)


t = int(input())

def sign(number):
	return number / abs(number)
for i in range(t):
	n = int(input())
	numbers = list(map(int, input().split()))
	answer = 0
	current_subseq = []
	current_sign = numbers[0]
	for x in numbers:
		if current_sign * x > 0:
			current_subseq.append(x)
		else:
			answer += max(current_subseq)
			current_sign *= -1
			current_subseq = [x]
	answer += max(current_subseq)
	print(answer)


import sys
input = sys.stdin.readline

def check(n):
	return 0 < n
tc = int(input())
for _ in range(tc):
	n = int(input())
	l = list(map(int, input().split()))
	maximum = l[0]
	ans = 0
	cnt = 0
	for i in range(n - 1):
		if check(l[i]) == check(l[i + 1]):
			maximum = max(l[i], l[i + 1], maximum)
		else:
			ans += maximum
			maximum = l[i + 1]
	print(ans + maximum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	p = 0
	s = 0
	for i in a:
		if p == 0:
			s += i
			p = i
		elif p > 0 and i > 0 or (p < 0 and i < 0):
			if i > p:
				s -= p
				s += i
				p = i
		else:
			p = i
			s += i
	print(s)


import sys
input = sys.stdin.buffer.readline

def sign(x):
	return 1 if x > 0 else 0
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	stack = []
	for i in arr:
		if not stack or sign(stack[-1]) != sign(i):
			stack.append(i)
		else:
			k = stack[-1]
			if i > k:
				stack.pop()
				stack.append(i)
	print(sum(stack))


t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	i = 0
	while i < len(l) - 1:
		j = 1
		while l[i + j] * l[i] > 0:
			if l[i + j] > l[i]:
				l[i] = 0
			else:
				l[i + j] = 0
			if i + j == n - 1:
				break
			j += 1
		i += 1
	print(sum(l))


def sgn(x):
	return 1 if x > 0 else -1
for _ in range(int(input())):
	n = int(input())
	ans = 0
	prev = -10 ** 9 - 1
	for x in [int(x) for x in input().split()]:
		if prev == -10 ** 9 - 1 or sgn(x) == sgn(prev):
			prev = max(prev, x)
		else:
			ans += prev
			prev = x
	ans += prev
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	maxsum = 0
	if l[0] < 0:
		sign = False
	else:
		sign = True
	i = 0
	currmax = l[0]
	while i < n:
		if sign:
			if l[i] > 0:
				currmax = max(currmax, l[i])
			else:
				maxsum += currmax
				currmax = l[i]
				sign = False
		elif l[i] < 0:
			currmax = max(currmax, l[i])
		else:
			maxsum += currmax
			currmax = l[i]
			sign = True
		i += 1
	print(maxsum + currmax)


cnt = int(input())
for _ in range(cnt):
	n = int(input())
	A = list(map(int, input().split()))
	i = 0
	l = 0
	s = 0
	while i < n:
		if A[i] > 0:
			best = -100000000000000
			while i < n and A[i] > 0:
				best = max(best, A[i])
				i += 1
			l += 1
			s += best
		else:
			best = -100000000000000
			if i < n and A[i] < 0:
				while i < n and A[i] < 0:
					best = max(best, A[i])
					i += 1
				l += 1
				s += best
	print(s)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(i, n) = (0, len(arr))
	max_sum = 0
	while i < n:
		if arr[i] < 0:
			minn = -1 * (10 ** 9 + 1)
			while i < n:
				if arr[i] > 0:
					break
				minn = max(minn, arr[i])
				i += 1
			max_sum += minn
		else:
			maxx = -1 * (10 ** 9 + 1)
			while i < n:
				if arr[i] < 0:
					break
				maxx = max(maxx, arr[i])
				i += 1
			max_sum += maxx
	print(max_sum)


for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	left = 0
	right = 0
	bestSeq = 0
	while left < n:
		sign = 1
		if nums[left] < 0:
			sign = 0
		if sign:
			right = left
			bestValue = 0
			while right < n and nums[right] > 0:
				bestValue = max(nums[right], bestValue)
				right += 1
			bestSeq += bestValue
		else:
			right = left
			bestValue = float('-inf')
			while right < n and nums[right] < 0:
				bestValue = max(nums[right], bestValue)
				right += 1
			bestSeq += bestValue
		left = right
	print(bestSeq)


t = int(input())
for i in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	sum = 0
	flag = 0
	max = arr[0]
	for j in range(1, n):
		if arr[j - 1] < 0 and arr[j] < 0 or (arr[j - 1] > 0 and arr[j] > 0):
			if arr[j] > max:
				max = arr[j]
		else:
			sum = sum + max
			max = arr[j]
	sum = sum + max
	print(sum)


q = int(input())
for q0 in range(q):
	n = int(input())
	str1 = input()
	list4 = str1.split()
	list1 = [int(o) for o in list4]
	list3 = []
	i = list1[0]
	for w in range(n):
		if i * list1[w] > 0:
			i = max(i, list1[w])
		else:
			list3.append(i)
			i = list1[w]
	list3.append(i)
	print(sum(list3))


def main():
	t = int(input())
	for tt in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		a.append(0)
		sp = 0
		sn = -10 ** 10
		b = []
		for i in range(0, len(a)):
			if a[i] > 0:
				sp = max(sp, a[i])
				if sn >= -10 ** 9:
					b.append(sn)
					sn = -10 ** 10
			elif a[i] < 0:
				b.append(sp)
				sp = 0
				sn = max(sn, a[i])
			elif a[i] == 0:
				b.append(sp)
				if sn >= -10 ** 9:
					b.append(sn)
		print(sum(b))
main()


t = int(input())
while t > 0:
	l = int(input())
	n = input()
	arr = list(map(int, n.split(' ')))
	sum = 0
	prev = arr[0]
	for i in range(1, l):
		if arr[i] * prev > 0:
			prev = max(arr[i], prev)
		else:
			sum += prev
			prev = arr[i]
	sum += prev
	print(sum)
	t -= 1


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	for (key, group) in groupby(s.split(), str.isdigit):
		ans += max(map(int, group))
	print(ans)


import math
import sys
t = int(input())
for cs in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	br = []
	ans = 0
	idx = 0
	while idx < n:
		mx = ar[idx]
		prev = idx
		for j in range(idx, n):
			if ar[j] * ar[idx] < 0:
				idx = j
				break
			mx = max(mx, ar[j])
		br.append(mx)
		ans += mx
		if idx == prev:
			break
	print(ans)


import sys

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

def print(*argv):
	n = len(argv)
	for i in range(n):
		if i == n - 1:
			sys.stdout.write(str(argv[i]) + '\n')
		else:
			sys.stdout.write(str(argv[i]) + ' ')

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

def solve(n, lst):
	if n == 1:
		return lst[0]
	post = 1
	i = 0
	summ = 0
	while i < n - 1:
		maxx = float('-inf')
		if lst[i] > 0 and lst[i + 1] > 0:
			for j in range(i, n):
				if lst[j] < 0:
					break
				else:
					maxx = max(maxx, lst[j])
			i = j
			summ += maxx
		elif lst[i] < 0 and lst[i + 1] < 0:
			for j in range(i, n):
				if lst[j] > 0:
					break
				else:
					maxx = max(maxx, lst[j])
			i = j
			summ += maxx
		else:
			summ += lst[i]
			i += 1
	if n >= 2:
		if lst[-1] / lst[-2] < 0:
			summ += lst[-1]
	return summ
for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	print(solve(n, lst))


def solve():
	n = int(input())
	l = [int(e) for e in input().split()]
	l = [l[0] // abs(l[0]) * -1] + l + [l[-1] // abs(l[-1]) * -1]
	a = 0
	s = 0
	for i in range(1, n + 2):
		e = l[i]
		p = l[i - 1]
		if e // abs(e) != p // abs(p):
			s += a
			a = -10 ** 9
		a = max(a, e)
	print(s)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


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


t = int(input())
i = 0
while i < t:
	n = int(input())
	arr = list(map(int, input().split()))
	(sum, size, j, tmp) = (0, 0, 0, 0)
	while j < n:
		if j < n and arr[j] < 0:
			tmp = -100000000000000000
			while j < n and arr[j] < 0:
				tmp = max(tmp, arr[j])
				j += 1
			size += 1
			sum += tmp
			tmp = -10000000000000000
		if j < n and arr[j] > 0:
			while j < n and arr[j] > 0:
				tmp = max(tmp, arr[j])
				j += 1
			size += 1
			sum += tmp
	print(sum)
	i += 1


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	l1.append(l[0])
	for i in range(1, len(l)):
		if l[i] < 0:
			if l1[-1] < 0:
				if l[i] > l1[-1]:
					l1[-1] = l[i]
			else:
				l1.append(l[i])
		elif l1[-1] > 0:
			if l[i] > l1[-1]:
				l1[-1] = l[i]
		else:
			l1.append(l[i])
	print(sum(l1))


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

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 bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

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

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		n = inp()
		a = inlt()
		dp = [[0 for i in range(2)] for j in range(n)]
		itr_p = -1
		itr_n = -1
		for i in range(n):
			if a[i] > 0:
				if itr_n == -1:
					dp[i][0] = 1
					dp[i][1] = a[i]
				else:
					dp[i][0] = 1 + dp[itr_n][0]
					dp[i][1] = dp[itr_n][1] + a[i]
				if dp[itr_p][0] == dp[i][0]:
					if a[itr_p] < a[i]:
						itr_p = i
				else:
					itr_p = i
			else:
				if itr_p == -1:
					dp[i][0] = 1
					dp[i][1] = a[i]
				else:
					dp[i][0] = 1 + dp[itr_p][0]
					dp[i][1] = dp[itr_p][1] + a[i]
				if dp[itr_n][0] == dp[i][0]:
					if a[itr_n] < a[i]:
						itr_n = i
				else:
					itr_n = i
		m_size = 0
		m_sum = -1 * sys.maxsize
		for i in range(n):
			if dp[i][0] > m_size:
				m_size = dp[i][0]
				m_sum = dp[i][1]
			elif dp[i][0] == m_size:
				m_sum = max(dp[i][1], m_sum)
		print(m_sum)
	return 0
main()


import math as m
i = input()
inp = []
for a in range(0, int(i)):
	input()
	inp += [[int(x) for x in input().split()]]
for arr in inp:
	s = 0
	seg = [arr[0]]
	pos = arr[0] > 0
	for i in arr[1:]:
		if (i > 0) == pos:
			seg += [i]
		else:
			pos = not pos
			s += max(seg)
			seg = [i]
	s += max(seg)
	print(s)


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans1 = 0
	a1 = 0
	ans2 = 0
	a2 = 0
	c1 = 0
	c2 = 0
	m = 0
	g = 0
	while c1 != n:
		if a[c1] > 0 and a1 % 2 == 0:
			a1 += 1
			m = a[c1]
			ans1 += g
			g = 0
		if a[c1] > 0 and a1 % 2 == 1:
			if a[c1] > m:
				m = a[c1]
		if a[c1] < 0 and a1 % 2 == 1:
			a1 += 1
			g = a[c1]
			ans1 += m
			m = 0
		if a[c1] < 0 and a1 % 2 == 0:
			if a[c1] > g:
				g = a[c1]
		c1 += 1
	ans1 += m
	ans1 += g
	(m, g) = (0, 0)
	while c2 != n:
		if a[c2] < 0 and a2 % 2 == 0:
			g = a[c2]
			a2 += 1
			ans2 += m
			m = 0
		if a[c2] > 0 and a2 % 2 == 1:
			a2 += 1
			m = a[c2]
			ans2 += g
			g = 0
		if a[c2] > 0 and a2 % 2 == 0:
			if m < a[c2]:
				m = a[c2]
		if a[c2] < 0 and a2 % 2 == 1:
			if g < a[c2]:
				g = a[c2]
		c2 += 1
	ans2 += g
	ans2 += m
	if a1 > a2:
		print(ans1)
	elif a2 > a1:
		print(ans2)
	else:
		print(max(ans1, ans2))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	final = []
	temp = a[0]
	for i in a:
		if i < 0 and temp < 0 or (i > 0 and temp > 0):
			b.append(i)
		else:
			final.append(b)
			b = []
			b.append(i)
		temp = i
	final.append(b)
	sum = 0
	for i in final:
		sum += max(i)
	print(sum)


def solve(n, a):
	ans = 0
	pre = 0
	tmp = -10 ** 10
	for i in a:
		if pre * i >= 0:
			tmp = max(tmp, i)
		else:
			ans += tmp
			tmp = i
		pre = i
	ans += tmp
	print(ans)
t = int(input())
args = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	args.append((n, a))
for (n, a) in args:
	solve(n, a)


t = int(input())
for i in range(t):
	n = int(input())
	arr = map(int, input().split(' '))
	i = 0
	chosen = -1
	summ = 0
	pos = False
	for a in arr:
		if i == 0:
			pos = a > 0
			chosen = a
			i += 1
		elif pos and a < 0:
			pos = False
			summ += chosen
			chosen = a
		elif not pos and a > 0:
			pos = True
			summ += chosen
			chosen = a
		elif a > chosen:
			chosen = a
	summ += chosen
	print(summ)


import math

def sign(a):
	if a < 0:
		return -1
	return 1
t = int(input())
for tt in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	ans = 0
	while i < len(arr):
		j = i
		mm = -10000000000
		while j < len(arr) and sign(arr[i]) == sign(arr[j]):
			mm = max(arr[j], mm)
			j += 1
		i = j
		ans += mm
	print(ans)


def answer():
	s = 0
	if a[0] >= 0:
		p = True
	else:
		p = False
	m = a[0]
	for v in a[1:]:
		if v < 0 and p:
			s += m
			m = v
			p = False
		if v > 0 and (not p):
			s += m
			m = v
			p = True
		if v > 0:
			m = max(m, v)
		else:
			m = max(m, v)
	s += m
	return s
for T in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(answer())


t = int(input())
for _ in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	answer = [0] * n
	length = [0] * n
	maxNeg = None
	maxPos = None
	negLen = 0
	posLen = 0
	for i in range(n):
		if L[i] > 0:
			if maxNeg is None:
				answer[i] = L[i]
			else:
				answer[i] = maxNeg + L[i]
			length[i] = negLen + 1
			if length[i] > posLen:
				maxPos = answer[i]
			else:
				maxPos = max(maxPos, answer[i])
			posLen = max(posLen, length[i])
		else:
			if maxPos is None:
				answer[i] = L[i]
			else:
				answer[i] = maxPos + L[i]
			length[i] = posLen + 1
			if length[i] > negLen:
				maxNeg = answer[i]
			else:
				maxNeg = max(maxNeg, answer[i])
			negLen = max(negLen, length[i])
	maxLen = max(length)
	m = min(answer)
	for i in range(n):
		if length[i] == maxLen:
			m = max(m, answer[i])
	print(m)


T = int(input())
for _ in range(T):
	n = int(input())
	a = []
	a = list(map(int, input().split()))
	psum = -10 ** 20
	ps = 0
	while a[ps] < 0:
		ps = ps + 1
		if ps == n:
			break
	ng = -1
	pos = ps
	pk = 0
	if ps < n:
		psum = a[ps]
	for i in range(ps, n):
		if pos > ng:
			if a[i] > 0:
				if a[pos] < a[i]:
					psum = psum - a[pos] + a[i]
					pos = i
			else:
				pk += 1
				psum = psum + a[i]
				ng = i
		elif a[i] > 0:
			pk += 1
			psum = psum + a[i]
			pos = i
		elif a[ng] < a[i]:
			psum = psum - a[ng] + a[i]
			ng = i
	neg = 0
	nk = 0
	while a[neg] > 0:
		neg += 1
		if neg == n:
			break
	nsum = -10 ** 20
	if neg < n:
		nsum = a[neg]
	ng = neg
	ps = -1
	for i in range(neg, n):
		if ng > ps:
			if a[i] > 0:
				nk += 1
				nsum += a[i]
				ps = i
			elif a[i] > a[ng]:
				nsum = nsum - a[ng] + a[i]
				ng = i
		elif a[i] > 0:
			if a[i] > a[ps]:
				nsum = nsum - a[ps] + a[i]
				ps = i
		else:
			nk += 1
			nsum += a[i]
			ng = i
	if nk > pk:
		print(nsum)
	elif pk > nk:
		print(psum)
	else:
		print(max(nsum, psum))


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


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().strip().split()))
	s = 0
	p = 0
	if l[0] > 0:
		p = 1
	i = 0
	while i < n:
		if p == 1:
			big = l[i]
			while i < n and l[i] > 0:
				big = max(big, l[i])
				i += 1
			s += big
			p = 0
		else:
			big = l[i]
			while i < n and l[i] < 0:
				big = max(big, l[i])
				i += 1
			s += big
			p = 1
	print(s)


arr = [[], []]

def out1(x, n, m):
	if x <= 0:
		return True
	if n <= 0:
		if x - 10 * m > 0:
			return False
		else:
			return True
	if m <= 0:
		return False
	return out1(x // 2 + 10, n - 1, m) or out1(x - 10, n, m - 1)

def out(x):
	arr = list(map(int, input().split()))
	ans = 0
	flag = True if arr[0] < 0 else False
	maxi = arr[0]
	for i in range(x):
		if flag and arr[i] < 0 or (not flag and arr[i] > 0):
			maxi = max(maxi, arr[i])
		else:
			ans += maxi
			maxi = arr[i]
			flag = not flag
	print(ans + maxi)
for i in range(int(input())):
	x = int(input())
	out(x)


from sys import stdin, stdout
Pi = lambda x: stdout.write(str(x) + '\n')
Ps = lambda x: stdout.write(str(x))
S = lambda x: x * (x + 1) // 2
I = lambda x: 1 + 2 * x
R = lambda : stdin.readline()
Ri = lambda x: map(int, x.split())
Rs = lambda x: map(str, x.split())
Rf = lambda x: map(float, x.split())
MaxN = 1 << 60

def f(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return 1
	return 0

def main():
	test = int(R())
	for t in range(test):
		n = int(R())
		A = list(Ri(R()))
		ans = 1
		signo = 0
		if A[0] > 0:
			signo = 1
		B = [A[0]]
		for x in A:
			if signo == 0 and x < 0 or (signo and x > 0):
				B[-1] = max(x, B[-1])
			if x > 0 and signo == 0:
				ans += 1
				signo = 1
				B.append(x)
			if x < 0 and signo:
				ans += 1
				signo = 0
				B.append(x)
		Pi(sum(B))
main()


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


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


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
tokens = []
tokens_next = 0

def next_str():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = input().split()
		tokens_next = 0
	tokens_next += 1
	return tokens[tokens_next - 1]

def nextInt():
	return int(next_str())

def f(a, st):
	myMax = a[st]
	j = st
	for j in range(st, len(a)):
		if a[j] * a[st] < 0:
			return (myMax, j)
		myMax = max(myMax, a[j])
	return (myMax, len(a))
t = nextInt()
for _ in range(t):
	n = nextInt()
	a = [nextInt() for _ in range(n)]
	sum = 0
	i = 0
	while i < len(a):
		(curSegmentMax, nextInd) = f(a, i)
		sum += curSegmentMax
		i = nextInd
	print(sum)


def get_positive_sub_seq(current_index, n):
	start_index = current_index
	while current_index < len(n):
		if n[current_index] > 0:
			current_index += 1
		else:
			break
	return (max(n[start_index:current_index]), current_index - 1)

def get_negative_sub_seq(current_index, n):
	start_index = current_index
	while current_index < len(n):
		if n[current_index] < 0:
			current_index += 1
		else:
			break
	return (max(n[start_index:current_index]), current_index - 1)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	current_index = 0
	sum = 0
	while current_index < n:
		e = a[current_index]
		if e > 0:
			(max_num, current_index) = get_positive_sub_seq(current_index, a)
		elif e < 0:
			(max_num, current_index) = get_negative_sub_seq(current_index, a)
		sum += max_num
		current_index += 1
	print(sum)


def sol():
	n = int(input())
	a = list(map(int, input().split()))
	sum = 0
	k = 0
	while k <= n - 1:
		max = a[k]
		while k < n - 1 and a[k] * a[k + 1] > 0:
			if a[k + 1] > max:
				max = a[k + 1]
			k += 1
		sum += max
		k += 1
	print(sum)
t = int(input())
for i in range(t):
	sol()


T = int(input())
results = []
for case in range(1, T + 1):
	n = int(input())
	a = [int(i) for i in input().split()]
	prev = [a[0]]
	new = []
	su = 0
	for i in range(1, n):
		num = a[i]
		if num > 0:
			if a[i - 1] < 0:
				new_num = max(prev)
				new.append(new_num)
				su += new_num
				prev = []
			prev.append(num)
		elif num < 0:
			if a[i - 1] > 0:
				new_num = max(prev)
				new.append(new_num)
				su += new_num
				prev = []
			prev.append(num)
	new_num = max(prev)
	su += new_num
	results.append(su)
for result in results:
	print(result)


import sys
input = sys.stdin.readline

def compress(string):
	string.append('#')
	n = len(string)
	(begin, end, cnt) = (0, 1, 1)
	ans = []
	while end < n:
		if string[begin] == string[end]:
			(end, cnt) = (end + 1, cnt + 1)
		else:
			ans.append((string[begin], cnt))
			(begin, end, cnt) = (end, end + 1, 1)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	sign = [0] * n
	for i in range(n):
		if a[i] < 0:
			sign[i] = -1
		else:
			sign[i] = 0
	sign = compress(sign)
	l = 0
	ans = 0
	for (_, length) in sign:
		ans += max(a[l:l + length])
		l += length
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	sub_arr = [arr[0]]
	for i in range(1, len(arr)):
		if arr[i] > 0 and sub_arr[-1] > 0 or (arr[i] < 0 and sub_arr[-1] < 0):
			if arr[i] > sub_arr[-1]:
				sub_arr.pop()
				sub_arr.append(arr[i])
		else:
			sub_arr.append(arr[i])
	print(sum(sub_arr))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	isn = a[0] < 0
	(s, i, m) = (0 if n > 1 else a[0], 1, a[0])
	while i < n:
		while i < n and isn == (a[i] < 0):
			m = max(m, a[i])
			i += 1
		s += m
		isn = not isn
		if i < n:
			m = a[i]
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = a[0]
	s = 0
	for num in a:
		if x * num < 0:
			s += x
			x = num
		else:
			x = max(x, num)
	print(s + x)


import sys
input = sys.stdin.buffer.readline

def solve(n, arr):
	pos = [0, 0, 0]
	neg = [0, 0, 0]
	for i in arr:
		if i > 0:
			if i > pos[0]:
				pos[2] = pos[2] - pos[0] + i
				pos[0] = i
			if neg[1] + 1 > pos[1] or (neg[1] + 1 == pos[1] and neg[2] + i > pos[2]):
				pos = list(neg)
				pos[2] += i
				pos[0] = i
				pos[1] += 1
		elif i < 0:
			if i > neg[0]:
				neg[2] = neg[2] - neg[0] + i
				neg[0] = i
			if pos[1] + 1 > neg[1] or (pos[1] + 1 == neg[1] and pos[2] + i > neg[2]):
				neg = list(pos)
				neg[2] += i
				neg[0] = i
				neg[1] += 1
	if pos[1] > neg[1]:
		return pos[2]
	elif pos[1] < neg[1]:
		return neg[2]
	else:
		return max(pos[2], neg[2])
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = solve(n, arr)
	print(ans)


for _ in range(int(input())):
	a = int(input())
	lis = list(map(int, input().split()))

	def checksi(i):
		if i > 0:
			return '+'
		else:
			return '-'
	i = 0
	ak = lis[0]
	summ = 0
	fag = False
	while i < a - 1:
		if checksi(lis[i + 1]) == checksi(lis[i]):
			if checksi(lis[i]) == '+':
				ak = max(ak, lis[i], lis[i + 1])
			elif checksi(lis[i]) == '-':
				ak = max(ak, lis[i], lis[i + 1])
			if i == a - 2:
				summ += ak
		elif i == a - 2:
			fag = True
			summ += lis[-1]
			summ += ak
		else:
			fag = True
			summ += ak
			if i < a - 1:
				ak = lis[i + 1]
		i += 1
	if len(lis) == 1:
		print(lis[0])
	elif fag:
		print(summ)
	else:
		print(max(lis))


test = int(input())
for t in range(test):
	n = int(input())
	L = list(map(int, input().split()))
	sumpos = 0
	sumneg = -99999999999
	maxsum = []
	for i in range(n - 1):
		if L[i] > 0:
			if sumpos < L[i]:
				sumpos = L[i]
			if L[i + 1] < 0:
				maxsum.append(sumpos)
				sumpos = 0
		else:
			if sumneg < L[i]:
				sumneg = L[i]
			if L[i + 1] > 0:
				maxsum.append(sumneg)
				sumneg = -99999999999
	if L[n - 1] > 0 and L[n - 2] > 0 or (L[n - 1] < 0 and L[n - 2] < 0):
		if L[n - 1] > 0:
			maxsum.append(max(sumpos, L[n - 1]))
		else:
			maxsum.append(max(sumneg, L[n - 1]))
	else:
		maxsum.append(L[n - 1])
	print(sum(maxsum))


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	flag = 0
	f = 0
	l = []
	l.append(a[0])
	for i in a:
		if l[flag] > 0 and i > 0:
			if i > l[flag]:
				l[flag] = i
		elif l[flag] < 0 and i < 0:
			if i > l[flag]:
				l[flag] = i
		else:
			flag = flag + 1
			l.append(i)
	print(sum(l))


test = int(input())
from collections import defaultdict
for i in range(test):
	n = int(input())
	nums = [int(i) for i in input().split()]
	cnt = defaultdict(list)
	i = 0
	flag = 0
	cnt[0].append(nums[0])
	for k in range(1, n):
		if nums[k] > 0 and nums[k - 1] < 0 or (nums[k] < 0 and nums[k - 1] > 0):
			flag = 1
		if flag:
			i += 1
			cnt[i].append(nums[k])
			flag = 0
		else:
			cnt[i].append(nums[k])
	ans = 0
	for t in cnt.values():
		ans += max(t)
	print(ans)


for i in range(int(input())):
	n = int(input())
	x = [int(q) for q in input().split()]
	s = 0
	e = 0
	for i in range(n):
		if x[i] > 0:
			s += 1
		if x[i] < 0:
			e += 1
	if s == n or e == n:
		print(max(x))
	else:
		l = 0
		ans = 0
		a = x[0]
		if x[0] > 0:
			l = 1
		if x[0] < 0:
			l = -1
		for i in range(1, n):
			if l == -1 and x[i] < 0:
				a = max(x[i], a)
				continue
			if l == 1 and x[i] > 0:
				a = max(x[i], a)
				continue
			ans += a
			a = x[i]
			if x[i] > 0:
				l = 1
			if x[i] < 0:
				l = -1
		print(ans + a)


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	str = input()
	numbers = str.split(' ')
	numbers.append(0)
	last = None
	sum = 0
	for i in numbers:
		number = int(i)
		if last == None:
			last = number
		elif last < 0 and number < 0 or (last > 0 and number > 0):
			last = max(last, number)
		else:
			sum += last
			last = number
	print(sum)


import sys
input = sys.stdin.readline
a = int(input())
for x in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	j = 0
	for y in range(b):
		if c[y] < 0:
			j += 1
	if j == b:
		print(max(c))
	else:
		n = -1
		if c[0] > 0:
			n = 0
		s = -9999999999999
		p = 0
		for y in range(b):
			if n == 0:
				if c[y] > 0:
					if s < c[y]:
						s = c[y]
				elif c[y] < 0:
					p += s
					s = c[y]
					n = -1
			elif n == -1:
				if c[y] < 0:
					if c[y] > s:
						s = c[y]
				else:
					p += s
					s = c[y]
					n = 0
		p += s
		print(p)


t = int(input())
for j in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	sarr = []
	total = 0
	for i in arr:
		if i > 0:
			sarr.append(1)
		else:
			sarr.append(-1)
	i = 0
	while i < n:
		curr = arr[i]
		j = i
		while j < n and sarr[i] == sarr[j]:
			curr = max(curr, arr[j])
			j += 1
		total += curr
		i = j
	print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	positive = a[0] > 0
	temp = a[0]
	for x in a:
		if positive:
			if x > 0:
				temp = max(temp, x)
			else:
				res += temp
				temp = x
				positive = False
		elif x < 0:
			temp = max(temp, x)
		else:
			res += temp
			temp = x
			positive = True
	res += temp
	print(res)


t = int(input())
anslist = []
for i in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	key = li[0]
	total = li[0]
	for i in range(1, n):
		if li[i] > 0 and li[i - 1] > 0 or (li[i] < 0 and li[i - 1] < 0):
			if li[i] > key:
				total = total + li[i] - key
				key = li[i]
		else:
			total = total + li[i]
			key = li[i]
	anslist.append(total)
for i in anslist:
	print(i)


n = int(input())
for i in range(n):
	k = int(input())
	h = list(map(int, input().rstrip().split()))
	l = 0
	s = 0
	v = h[0]
	if h[0] > 0:
		p = +1
	else:
		p = -1
	for i in range(1, k):
		if h[i] * p > 0:
			if h[i] > v:
				v = h[i]
		else:
			p *= -1
			l += 1
			s += v
			v = h[i]
	l += 1
	s += v
	print(s)


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	v = groupby(a, lambda x: x < 0)
	print(sum([max(i[1]) for i in v]))


T = int(input())
for t in range(T):
	n = int(input())
	A = [int(i) for i in input().split()]
	switch_sign = [i for i in range(1, n) if A[i] * A[i - 1] < 0]
	switch_sign.append(n)
	ans = max(A[:switch_sign[0]])
	for j in range(1, len(switch_sign)):
		ans += max(A[switch_sign[j - 1]:switch_sign[j]])
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
from collections import deque
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
stdin = sys.stdin
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
t = ni()
for i in range(t):
	n = ni()
	a = na()
	ans = 0
	t = a[0]
	for j in range(1, n):
		if a[j] * t > 0:
			t = max(a[j], t)
		else:
			ans += t
			t = a[j]
	ans += t
	print(ans)


import os
import sys
from io import BytesIO, IOBase
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 main():
	try:
		for _ in range(inp()):
			n = inp()
			a = inlt()
			(ans, temp) = (0, [])
			for i in range(n):
				if temp == []:
					temp.append(a[i])
				elif temp[-1] < 0 and a[i] < 0:
					temp.append(a[i])
				elif temp[-1] < 0 and a[i] > 0:
					ans += max(temp)
					temp = [a[i]]
				elif temp[-1] > 0 and a[i] > 0:
					temp.append(a[i])
				elif temp[-1] > 0 and a[i] < 0:
					ans += max(temp)
					temp = [a[i]]
			ans += max(temp)
			print(ans)
	except Exception as e:
		raise e
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 = int(input())
	a = list(map(int, input().split()))
	i = 0
	ans = 0
	while i < n:
		if a[i] < 0:
			t = a[i]
			while i + 1 < n and a[i + 1] < 0:
				if t < a[i + 1]:
					t = a[i + 1]
				i += 1
			ans += t
		else:
			t = a[i]
			while i + 1 < n and a[i + 1] > 0:
				if t < a[i + 1]:
					t = a[i + 1]
				i += 1
			ans += t
		i += 1
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = a[0]
	c = 0
	for i in range(1, n):
		if s // a[i] < 0:
			c += s
			s = a[i]
		elif a[i] > s:
			s = a[i]
	print(s + c)


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = 1
	if arr[0] < 0:
		pos = 0
		mn = arr[0]
	else:
		mx = arr[0]
	su = 0
	for a in arr[1:]:
		if pos == 1:
			if a > 0 and mx < a:
				mx = a
			elif a < 0:
				su += mx
				mx = 0
				pos = 0
				mn = a
		elif a < 0 and mn < a:
			mn = a
		elif a > 0:
			su += mn
			mn = -10000000000000
			pos = 1
			mx = a
	if pos == 1:
		su += mx
	else:
		su += mn
	print(su)


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve(n, arr):
	tot = 0
	i = 0
	check = True
	val = arr[0]
	if val < 0:
		check = False
	for i in range(n):
		curr = True
		if arr[i] < 0:
			curr = False
		if check == curr:
			val = max(arr[i], val)
		else:
			tot += val
			val = arr[i]
		check = curr
	print(tot + val)
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	solve(n, arr)


for i in range(int(input())):
	length = int(input())
	l = list(map(int, input().split()))
	sec = [[l[0]]]
	s = 0
	index = 0
	for j in range(1, length):
		if sec[-1][-1] < 0 and l[j] < 0 or (sec[-1][-1] > 0 and l[j] > 0):
			sec[-1].append(l[j])
		else:
			s += max(sec[-1])
			sec.append([l[j]])
	s += max(sec[-1])
	print(s)


import math
num_case = int(input())
for i in range(num_case):
	len_arr = int(input())
	datain1 = input().split(' ')
	datain = []
	for e in datain1:
		datain.append(int(e))
	sum1 = 0
	i = 0
	max_po = 0
	max_ne = -999999999999999
	while i < len(datain):
		if datain[i] > 0:
			j = i
			while datain[j] > 0 and j < len(datain) - 1:
				if datain[j] > max_po:
					max_po = datain[j]
				j += 1
			if j == len(datain) - 1:
				if datain[j] < 0:
					sum1 += max_po
					sum1 += datain[j]
				elif max_po < datain[j]:
					sum1 += datain[j]
				else:
					sum1 += max_po
				i = j
				i += 1
			else:
				i = j
				sum1 += max_po
				max_po = 0
		else:
			j = i
			while datain[j] < 0 and j < len(datain) - 1:
				if datain[j] > max_ne:
					max_ne = datain[j]
				j += 1
			if j == len(datain) - 1:
				if datain[j] > 0:
					sum1 += max_ne
					sum1 += datain[j]
				elif max_ne < datain[j]:
					sum1 += datain[j]
				else:
					sum1 += max_ne
				i = j
				i += 1
			else:
				i = j
				sum1 += max_ne
				max_ne = -999999999999999
	print(sum1)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, sys.stdin.readline().split(' ')))
	a = min(arr)
	b = max(arr)
	mx = min(arr)
	mn = max(arr)
	s = 0
	num = arr[0]
	for i in range(n):
		if arr[i] > 0 and num > 0:
			num = max(arr[i], num)
		elif arr[i] > 0 and num < 0:
			s += num
			num = arr[i]
		elif arr[i] < 0 and num < 0:
			num = max(arr[i], num)
		else:
			s += num
			num = arr[i]
	print(s + num)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(j) for j in input().split(' ')]
	partitions = [[arr[0]]]
	index = 0
	for j in range(1, n):
		if arr[j] * arr[j - 1] > 0:
			partitions[index].append(arr[j])
		else:
			partitions.append([arr[j]])
			index += 1
	sum = 0
	for partition in partitions:
		sum += max(partition)
	print(sum)


import sys

class l(list):

	def __init__(self, type):
		self.type = type

	def push(self, x):
		if not len(self):
			if ~self.type and x >= 0:
				self.append(x)
			elif not ~self.type and x < 0:
				self.append(x)
			return
		t = self[-1]
		if t < 0:
			if 0 > x > t:
				self[-1] = x
			elif x >= 0:
				self.append(x)
		elif x > t > 0:
			self[-1] = x
		elif x < 0:
			self.append(x)

def main():
	ans = ''
	for _ in range(int(input())):
		n = int(input())
		x = [int(i) for i in input().split()]
		neg_first = l(-1)
		pos_first = l(1)
		for xi in x:
			neg_first.push(xi)
			pos_first.push(xi)
		m = max(len(neg_first), len(pos_first))
		if m == len(neg_first):
			c_ans = sum(neg_first)
		else:
			c_ans = sum(pos_first)
		ans += f'{c_ans}\n'
	sys.stdout.write(ans)
main()


for i in range(int(input())):
	a = input()
	seq = [int(x) for x in input().strip().split()]
	subseq = [[seq[0]]]
	index = 0
	for i in range(1, len(seq)):
		if seq[i - 1] > 0 and seq[i] > 0 or (seq[i - 1] < 0 and seq[i] < 0):
			subseq[index].append(seq[i])
		else:
			subseq.append([seq[i]])
			index += 1
	total = 0
	for ss in subseq:
		total += max(ss)
	print(total)


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


t = int(input())
for j in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	s = l[0]
	last_added = l[0]
	for i in range(1, n):
		if l[i] > 0 and l[i - 1] > 0:
			if l[i] > last_added:
				s = s - last_added + l[i]
				last_added = l[i]
			else:
				continue
		elif l[i] < 0 and l[i - 1] < 0:
			if l[i] > last_added:
				s = s - last_added + l[i]
				last_added = l[i]
			else:
				continue
		else:
			s = s + l[i]
			last_added = l[i]
	print(s)


def solve():
	n = int(input())
	arr = [int(v) for v in input().split()]
	i = 0
	j = 0
	ans = 0
	while i < n:
		add = arr[i]
		while j < n and (arr[j] < 0) == (arr[i] < 0):
			add = max(add, arr[j])
			j += 1
		ans += add
		i = j
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = 1
	mn = 0
	mc = a[0]
	p = a[0] < 0
	while k < n:
		if a[k] > 0 and p or (a[k] < 0 and (not p)):
			mn += mc
			mc = a[k]
			p = a[k] < 0
		else:
			mc = max(mc, a[k])
		k += 1
	mn += mc
	print(mn)


t = int(input())
for i in range(t):
	p = int(input())
	A = list(map(int, input().split()))
	su = 0
	(f1, f2) = (-1, -1)
	(fv1, fv2) = (-1, -10 ** 9)
	ans = 0
	last = 2
	for i in range(len(A)):
		if A[i] >= 0:
			if A[i] > fv1:
				f1 = i
				fv1 = A[i]
			if last == 4:
				ans += fv2
				fv2 = -10 ** 9
			last = 3
		if A[i] < 0:
			if A[i] > fv2:
				f2 = i
				fv2 = A[i]
			if last == 3:
				ans += fv1
				fv1 = -1
			last = 4
	if last == 4:
		ans += fv2
	if last == 3:
		ans += fv1
	print(ans)


def is_element_suits(prev, current, sum_of):
	if current * prev < 0:
		return (True, sum_of + current)
	elif current > prev:
		return (True, sum_of - prev + current)
	return (False, sum_of)
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	list_positive = [-1]
	list_negative = [1]
	sum_positive = 0
	sum_negative = 0
	for element in a:
		(is_opposite, sum_positive) = is_element_suits(list_positive[-1], element, sum_positive)
		if is_opposite:
			list_positive.append(element)
		(is_opposite, sum_negative) = is_element_suits(list_negative[-1], element, sum_negative)
		if is_opposite:
			list_negative.append(element)
	if len(list_positive) > len(list_negative):
		print(sum_positive)
	elif len(list_positive) < len(list_negative):
		print(sum_negative)
	else:
		print(max(sum_negative, sum_positive))


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	e = []
	d = []
	c = 0
	for i in range(len(a)):
		if a[i] < 0:
			d.append(a[i])
			if len(e) > 0:
				ee = max(e)
				c = c + ee
				e = []
		else:
			if len(d) > 0:
				aa = max(d)
				c = c + aa
			d = []
			e.append(a[i])
	if len(e) > 0:
		ee = max(e)
		c = c + ee
	if len(d) > 0:
		aa = max(d)
		c = c + aa
	print(c)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	(x, y) = ([], [])
	for i in range(n):
		if l[i] > 0:
			if len(y) > 0:
				s += max(y)
				y = []
			x.append(l[i])
		else:
			if len(x) > 0:
				s += max(x)
				x = []
			y.append(l[i])
	if len(x) > 0:
		s += max(x)
	if len(y) > 0:
		s += max(y)
	print(s)


from bisect import bisect as br
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if a[-1] > 0:
		a.append(-1)
	else:
		a.append(1)
	(f, fin) = ([], [])
	for i in range(len(a) - 1):
		if a[i] * a[i + 1] < 0:
			f.append(a[i])
			fin.append(f)
			f = []
		else:
			f.append(a[i])
	ss = 0
	for x in fin:
		ss += max(x)
	print(ss)


t = int(input())
for i in range(t):
	n = int(input())
	a = input().split()
	k = 0
	s = 0
	a = [int(u) for u in a]
	for j in range(n):
		if j == 0:
			k = a[0]
		else:
			if k * a[j] > 0:
				if a[j] > k:
					k = a[j]
					pass
				pass
			else:
				s += k
				k = a[j]
				pass
			pass
		pass
	s += k
	print(s)
	pass


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = [l[0]]
	s = 0
	for i in range(n - 1):
		if l[i] * l[i + 1] > 0:
			m.append(l[i + 1])
		else:
			s += max(m)
			m = [l[i + 1]]
	s += max(m)
	print(s)


def read():
	return list(map(int, input().strip().split()))
for i in range(int(input())):
	n = int(input())
	arr = read()
	a = arr[0]
	s = 0
	for j in arr:
		if a * j < 0:
			s += a
			a = j
		else:
			a = max(a, j)
	print(s + a)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = [[] for i in range(n)]
	l[0].append(a[0])
	j = 0
	for i in range(1, n):
		if l[j][-1] > 0 and a[i] < 0 or (l[j][-1] < 0 and a[i] > 0):
			j += 1
		l[j].append(a[i])
	for i in range(len(l)):
		if len(l[i]) == 0:
			l = l[:i]
			break
	s = 0
	for i in range(len(l)):
		s += max(l[i])
	print(s)


for _ in range(int(input())):
	n = int(input())
	x = 0
	s = 0
	for y in map(int, input().split()):
		if (y < 0) == (x < 0):
			x = max(x, y)
		else:
			s += x
			x = y
	print(s + x)


from sys import stdin
for j in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	s1 = a[0]
	s = 0
	x = 0
	for i in range(n):
		if a[i] * s1 > 0:
			s1 = max(s1, a[i])
		else:
			x = 1
			s += s1
			s1 = a[i]
	s += s1
	if x == 0:
		print(max(a))
	else:
		print(s)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	l = list(map(int, input().split()))
	max_ending_here = 0
	(lp, ln) = (0, 0)
	for i in range(n):
		if l[i] < 0 and i == 0:
			sign = 'n'
			max_ending_here += l[i]
			ln = l[i]
		elif l[i] > 0 and i == 0:
			sign = 'p'
			max_ending_here += l[i]
			lp = l[i]
		elif sign == 'n' and l[i] > 0:
			max_ending_here += l[i]
			sign = 'p'
			lp = l[i]
		elif sign == 'n' and l[i] < 0:
			if l[i] > ln:
				max_ending_here -= ln
				max_ending_here += l[i]
				ln = l[i]
		elif sign == 'p' and l[i] < 0:
			max_ending_here += l[i]
			sign = 'n'
			ln = l[i]
		elif sign == 'p' and l[i] > 0:
			if l[i] > lp:
				max_ending_here -= lp
				max_ending_here += l[i]
				lp = l[i]
	print(max_ending_here)


def funct(n, c):
	i = 0
	maxi = c[i]
	s = 0
	for i in range(1, n):
		if maxi * c[i] < 0:
			s += maxi
			maxi = c[i]
		else:
			maxi = max(maxi, c[i])
	print(s + maxi)
test_cases = int(input())
for i in range(test_cases):
	n = int(input())
	c = list(map(int, input().split()))
	funct(n, c)


for q in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	now = a[0]
	for i in range(1, n):
		if a[i] * a[i - 1] > 0:
			now = max(now, a[i])
		else:
			b.append(now)
			now = a[i]
	b.append(now)
	print(sum(b))


import sys

def sign(x):
	return 1 if x > 0 else -1

def single_solve(n, a):
	total = 0
	i = 0
	while i < n:
		current = a[i]
		j = i
		while j < n and sign(a[j]) == sign(a[i]):
			current = max(current, a[j])
			j += 1
		total += current
		i = j
	return f'{total}'

def solve(input_path=None):
	if input_path is None:
		f = sys.stdin
	else:
		f = open(input_path, 'r')
	golds = list()
	t = int(f.readline())
	for _ in range(t):
		n = int(f.readline())
		a = list(map(int, f.readline().split()))
		golds.append(single_solve(n, a))
	return golds

def main():
	for line in solve():
		print(f'{line}')
main()


def calculateMaxSum(n, li):
	ar = []
	ar.append(li[0])
	large = []
	for j in range(0, n - 1):
		if li[j] > 0 and li[j + 1] > 0:
			ar.append(li[j + 1])
		elif li[j] > 0 and li[j + 1] < 0:
			large.append(max(ar))
			ar = []
			ar.append(li[j + 1])
		elif li[j] < 0 and li[j + 1] > 0:
			large.append(max(ar))
			ar = []
			ar.append(li[j + 1])
		else:
			ar.append(li[j + 1])
	large.append(max(ar))
	return sum(large)
answer = []
for _ in range(int(input())):
	n = int(input())
	array = list(map(int, input().split()))[:n]
	answer.append(calculateMaxSum(n, array))
for i in answer:
	print(i)


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


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	index = 0
	now = a[0]
	mx = a[0]
	while index < n:
		while index < n and now * a[index] > 0:
			mx = max(mx, a[index])
			index += 1
		ans += mx
		if index == n:
			break
		now = a[index]
		mx = a[index]
	print(ans)


import sys

def input():
	return sys.stdin.readline()[:-1]
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	if a[0] > 0:
		sgn = 1
	else:
		sgn = -1
	tmp = a[0]
	for x in a[1:]:
		if (sgn > 0) == (x > 0):
			tmp = max(tmp, x)
		else:
			ans += tmp
			sgn *= -1
			tmp = x
	ans += tmp
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	li = [int(i) for i in input().split(' ')]
	a = li[0]
	res = 0
	for (j, i) in enumerate(li):
		if j == 0:
			continue
		if i * a > 0:
			a = max(i, a)
		else:
			res += a
			a = i
	res += a
	print(res)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

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
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	z = -10 ** 18
	ans = 0
	while i < n:
		if l[i] > 0:
			maxi = 0
			while l[i] > 0:
				maxi = max(maxi, l[i])
				i += 1
				if i == n:
					break
			ans += maxi
		if i >= n:
			break
		if l[i] < 0:
			maxi = z
			while l[i] < 0:
				maxi = max(l[i], maxi)
				i += 1
				if i == n:
					break
			ans += maxi
	print(ans)


import math

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

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

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
t = inp()
for _ in range(t):
	n = inp()
	a = inlist()
	subseq = []
	cur = a[0]
	res = 0
	i = 0
	while i < n:
		tempMax = a[i]
		d = a[i]
		for j in range(i + 1, n):
			if d * a[j] > 0:
				tempMax = max(tempMax, a[j])
				i = j
			else:
				i = j - 1
				break
		res += tempMax
		i += 1
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	seg_max = []
	max_ele = a[0]
	out = 0
	for j in range(n - 1):
		if a[j] > 0 and a[j + 1] > 0:
			max_ele = max(a[j], a[j + 1], max_ele)
		elif a[j] < 0 and a[j + 1] < 0:
			max_ele = max(a[j], a[j + 1], max_ele)
		elif max_ele == 0:
			seg_max.append(a[j])
			out += a[j]
		else:
			seg_max.append(max_ele)
			out += max_ele
			max_ele = a[j + 1]
	seg_max.append(max_ele)
	out += max_ele
	print(out)


def sign(n):
	return n // abs(n)
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sum = 0
	maxele = arr[0]
	for i in range(1, n):
		if sign(arr[i]) != sign(arr[i - 1]):
			sum += maxele
			maxele = arr[i]
		else:
			maxele = max(maxele, arr[i])
	sum += maxele
	print(sum)


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
for _ in range(t):
	n = int(inp().strip())
	array = [int(x) for x in inp().strip().split()]
	ans = []
	prev = array[0]
	max1 = prev
	for i in range(1, n):
		if array[i] > 0 and prev < 0 or (array[i] < 0 and prev > 0):
			ans.append(max1)
			if array[i] > 0:
				max1 = 0
			else:
				max1 = -1 * 10 ** 9
		max1 = max(array[i], max1)
		prev = array[i]
	ans.append(max1)
	print(sum(ans))


import sys
input = sys.stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		n = int(input())
		ara = [int(num) for num in input().split()]
		if ara[0] < 0:
			sign = -1
		else:
			sign = 1
		ans = 0
		current = ara[0]
		for num in ara:
			if num * sign > 0:
				current = max(current, num)
			else:
				sign *= -1
				ans += current
				current = num
		ans += current
		print(ans)
main()


t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	x = []
	y = 0
	z = 0
	w = 0
	ww = 0
	for i in range(0, n):
		if a[i] >= 0:
			z += 1
			y = i + 1
			if w == 0:
				continue
			else:
				d = int(max(a[ww - w:ww]))
				x.append(d)
				w = 0
		else:
			w += 1
			ww = i + 1
			if z == 0:
				continue
			else:
				c = max(a[y - z:y])
				x.append(c)
				z = 0
	if z > 0:
		c = max(a[y - z:y])
		x.append(c)
	if w > 0:
		d = max(a[ww - w:ww])
		x.append(d)
	print(int(sum(x)))


t = int(input())

def f(n, a):
	ans = 0
	candidate = a[0]
	for i in range(1, n):
		if a[i] * candidate > 0:
			candidate = max(a[i], candidate)
		else:
			ans += candidate
			candidate = a[i]
	ans += candidate
	return ans
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(f(n, a))


def dkwpwn(list1):
	ans = 0
	temp = []
	list2 = []
	temp.append(list1[0])
	for i in range(1, len(list1)):
		if temp[0] > 0 and list1[i] < 0 or (temp[0] < 0 and list1[i] > 0):
			list2.append(temp)
			temp = []
		temp.append(list1[i])
	list2.append(temp)
	for i in list2:
		ans += max(i)
	return ans
t = int(input())
for _ in range(t):
	asd = input()
	list1 = list(map(int, input().split()))
	print(dkwpwn(list1))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	c = 0
	ans = 0
	cm = arr[0]
	if arr[-1] > 0:
		arr.append(-1)
	else:
		arr.append(float('inf'))
	for i in range(1, n + 1):
		if arr[i] > 0 and arr[i - 1] < 0 or (arr[i] < 0 and arr[i - 1] > 0):
			c += 1
			ans += cm
			if arr[i] > 0:
				cm = -1
			else:
				cm = -float('inf')
			if arr[i] > cm:
				cm = arr[i]
		elif arr[i] > cm:
			cm = arr[i]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	a.append(0)
	summ = 0
	c = []
	for i in range(n):
		c.append(a[i])
		if a[i + 1] > 0 and a[i] > 0 or (a[i + 1] < 0 and a[i] < 0):
			c.append(a[i + 1])
		else:
			summ += max(c)
			c = []
	print(summ)


f = lambda x: 1 if x > 0 else -1
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	cur = f(s[0])
	max_ = -10 ** 10
	ans = 0
	for i in range(n):
		if cur == 0:
			cur = f(s[i])
		if cur * s[i] > 0:
			max_ = max(s[i], max_)
		else:
			ans += max_
			cur = f(s[i])
			max_ = s[i]
	ans += max_
	print(ans)


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

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

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

def invr():
	return map(int, input().split())
entries = inp()
for i in range(entries):
	n = inp()
	s = inlt()
	sign = s[0]
	ind = 0
	r = []
	val = s[0]
	while ind < n:
		while s[ind] / sign > 0 and ind < n:
			if s[ind] > val:
				val = s[ind]
			ind += 1
			if ind == n:
				break
		r.append(val)
		sign *= -1
		val = -10 ** 9 - 1
	print(sum(r))


def find(arr, N):
	i = 0
	r = 0
	while i < N:
		m = arr[i]
		i += 1
		if m > 0:
			while i < N and arr[i] > 0:
				m = max(arr[i], m)
				i += 1
		else:
			while i < N and arr[i] < 0:
				m = max(arr[i], m)
				i += 1
		r += m
	return r

def main():
	for i in range(int(input())):
		N = int(input())
		arr = list(map(int, input().split()))
		print(find(arr, N))
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	t = a[0]
	temp = a[0]
	for i in range(1, n):
		if t > 0 and a[i] > 0:
			temp = max(temp, a[i])
		elif t > 0 and a[i] < 0:
			s += temp
			temp = a[i]
			t = a[i]
		elif t < 0 and a[i] < 0:
			temp = max(a[i], temp)
		elif t < 0 and a[i] > 0:
			s += temp
			temp = a[i]
			t = a[i]
	print(s + temp)


def sign(n):
	return abs(n) // n
t = int(input())
for case in range(t):
	n = int(input())
	numbers = [int(x) for x in input().split()]
	cur_state = sign(numbers[0])
	numbers.append(sign(numbers[n - 1]) * -1)
	cur_set = []
	total = 0
	for i in range(n + 1):
		k = numbers[i]
		if sign(k) == cur_state:
			cur_set.append(k)
		else:
			copycur_set = cur_set.copy()
			total += max(copycur_set)
			cur_state = cur_state * -1
			cur_set = [k]
	print(total)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 0 or n == 1:
		print(sum(a))
	else:
		seo = 0
		pup = 0
		nek = -1000000000
		fp = 0
		fn = 0
		hjsd = 0
		bdjs = 8
		sui = 0
		klo = 9
		for i in range(n):
			sui += 1
			if a[i] < 0:
				if fn == 1:
					nek = max(nek, a[i])
				else:
					nek = a[i]
				fn = 1
				if fp == 1:
					seo += pup
				fp = 0
			else:
				klo += 1
				if fp == 1:
					pup = max(pup, a[i])
				else:
					pup = a[i]
				fp = 1
				if fn == 1:
					seo += nek
				fn = 0
		if a[n - 1] < 0:
			seo += nek
		else:
			seo += pup
		hjsd += 1
		print(seo)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 1
	(b, tmp) = ([], [a[0]])
	while i < n:
		if a[i] * tmp[0] > 0:
			tmp.append(a[i])
		else:
			b.append(tmp)
			tmp = [a[i]]
		i += 1
	b.append(tmp)
	s = 0
	for bb in b:
		s += max(bb)
	print(s)


for _ in range(int(input())):
	input()
	s = x = 0
	for a in map(int, input().split()):
		if a * x > 0:
			x = max(x, a)
		else:
			s += x
			x = a
	print(s + x)


from sys import stdin, stdout
from sys import maxsize

def solve(a, n):
	i = 0
	ans = []
	while i < n:
		flag = True
		j = i + 1
		p = a[i]
		while j < n and a[j] * a[i] > 0:
			p = max(p, a[j])
			j += 1
		i = j
		ans.append(p)
	print(sum(ans))
test = 1
test = int(input())
for t in range(0, test):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = solve(arr, n)


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


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = [0] + list(map(int, input().split()))
	res = []
	maxPlus = -1
	maxMinus = -99999999999999999
	dp = [[0, 0] for _ in range(n + 1)]
	param = 0
	for j in range(1, n + 1):
		if a[j] > 0:
			dp[j] = [dp[j - 1][-1] + 1, dp[j - 1][-1]]
			param = 1
			if dp[j][0] != dp[j - 1][0]:
				res.append(maxMinus)
				maxMinus = -99999999999999999
			if a[j] > maxPlus:
				maxPlus = a[j]
		else:
			dp[j] = [dp[j - 1][0], dp[j - 1][0] + 1]
			param = -1
			if dp[j][-1] != dp[j - 1][-1]:
				res.append(maxPlus)
				maxPlus = -1
			if a[j] > maxMinus:
				maxMinus = a[j]
	k = dp[n][0] > dp[n][1]
	print(sum(res[1:] + [maxPlus if k else maxMinus]))


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	ans = []
	alt = 0
	temp = 0
	for i1 in s:
		if i1 > 0:
			if temp <= 0:
				temp = 1
				alt += 1
		elif temp >= 0:
			temp = -1
			alt += 1
	i = 0
	while i < len(s):
		if s[i] < 0:
			max1 = s[i]
			while s[i] < 0 and i < n:
				if s[i] > max1:
					max1 = s[i]
				i += 1
				if i == n:
					break
			ans.append(max1)
			if len(ans) == alt:
				break
		else:
			max1 = s[i]
			while s[i] > 0 and i < n:
				if s[i] > max1:
					max1 = s[i]
				i += 1
				if i == n:
					break
			ans.append(max1)
			if len(ans) == alt:
				break
	print(sum(ans))


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


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


for _ in range(int(input())):
	size = int(input())
	array = list(map(int, input().split()))
	bag = [array[0]]
	flag = 1 if bag[0] > 0 else -1
	for x in range(1, size):
		if flag == 1:
			if array[x] > 0:
				if array[x] > bag[-1]:
					bag[-1] = array[x]
			else:
				bag.append(array[x])
				flag = -1
		elif flag == -1:
			if array[x] < 0:
				if array[x] > bag[-1]:
					bag[-1] = array[x]
			else:
				bag.append(array[x])
				flag = 1
	print(sum(bag))


for _ in range(int(input())):
	w = int(input())
	data = list(map(int, input().split()))
	maxx = None
	if data[0] > 0:
		maxx = 0
	else:
		maxx = -1000000001
	sum = 0
	for i in range(w):
		if data[i] > 0:
			if i - 1 >= 0 and data[i - 1] < 0:
				sum += maxx
				maxx = 0
			if data[i] > maxx:
				maxx = data[i]
		else:
			if i - 1 >= 0 and data[i - 1] > 0:
				sum += maxx
				maxx = -1000000001
			if data[i] > maxx:
				maxx = data[i]
	sum += maxx
	print(sum)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	positive = a[0] > 0
	sum = 0
	maxi = a[0]
	a.append(-a[-1])
	for num in a:
		if (num > 0) == positive:
			maxi = max(maxi, num)
		else:
			sum += maxi
			maxi = num
			positive = not positive
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		sign = 1
	else:
		sign = -1
	count = 1
	checkpos = [0]
	for i in range(1, n):
		if a[i] * sign < 0:
			sign = -sign
			count += 1
			checkpos.append(i)
	sum = 0
	checkpos.append(n)
	for i in range(count):
		sum += max(a[checkpos[i]:checkpos[i + 1]])
	print(sum)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		N = int(input())
		A = [int(x) for x in input().split()]
		ans = 0
		pf = False
		if A[0] > 0:
			pf = True
		tmp = []
		for a in A:
			if pf:
				if a < 0:
					pf = False
					ans += max(tmp)
					tmp.clear()
					tmp.append(a)
				else:
					tmp.append(a)
			elif a > 0:
				pf = True
				ans += max(tmp)
				tmp.clear()
				tmp.append(a)
			else:
				tmp.append(a)
		if tmp:
			ans += max(tmp)
		print(ans)
main()


t = int(input())
for i in range(0, t):
	n = int(input())
	lst = list((int(num) for num in input().split()))[:n]
	sum1 = 0
	initial = 0
	for j in range(1, n):
		if lst[j] * lst[j - 1] < 0:
			sum1 += max(lst[initial:j])
			initial = j
	if initial + 1 == n:
		print(sum1 + lst[n - 1])
	else:
		print(sum1 + max(lst[initial:j + 1]))


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


class inp:

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def ilist():
		return list(map(int, input().split()))

def main():
	t = inp.int()
	for _ in range(t):
		n = inp.int()
		a = inp.ilist()
		pos = None
		ans = 0
		toadd = 0
		for (i, ai) in enumerate(a):
			if ai > 0:
				if pos == True:
					toadd = max(ai, toadd)
					continue
				ans += toadd
				toadd = ai
				pos = True
			else:
				if pos == False:
					toadd = max(ai, toadd)
					continue
				ans += toadd
				toadd = ai
				pos = False
		print(ans + toadd)
main()


def maximum_subsequence(l):
	n = len(l)
	b = [[]]
	b[-1].append(l[0])
	if l[0] > 0:
		a = 1
	else:
		a = -1
	for i in range(1, n):
		if a == 1:
			if l[i] > 0:
				b[-1].append(l[i])
			else:
				b.append([l[i]])
				a = -1
		elif l[i] < 0:
			b[-1].append(l[i])
		else:
			b.append([l[i]])
			a = 1
	s = 0
	for j in b:
		s += max(j)
	return s
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(maximum_subsequence(l))


for _ in range(int(input())):
	n = int(input())
	data = [int(x) for x in input().split()]
	ans = 0
	if data[0] > 0:
		flag = 0
	else:
		flag = 1
	large = data[0]
	for q in range(1, n):
		if data[q] < 0:
			temp = 1
		else:
			temp = 0
		if temp == flag:
			if data[q] > large:
				large = data[q]
		else:
			ans += large
			large = data[q]
		flag = temp
	ans += large
	print(ans)


import math
test = int(input())

def pos(arr, n):
	pos_ind = -1
	for i in range(n):
		if arr[i] > 0:
			pos_ind = i
			break
	res = []
	if pos_ind > -1:
		res.append(arr[pos_ind])
		for i in range(pos_ind + 1, n):
			if res[-1] > 0:
				if arr[i] > 0 and arr[i] > res[-1]:
					res[-1] = arr[i]
				elif arr[i] < 0:
					res.append(arr[i])
			elif arr[i] < 0 and arr[i] > res[-1]:
				res[-1] = arr[i]
			elif arr[i] > 0:
				res.append(arr[i])
	return res

def neg(arr, n):
	neg_ind = 0
	for i in range(n):
		if arr[i] < 0:
			neg_ind = i
			break
	res = []
	res.append(arr[neg_ind])
	for i in range(neg_ind + 1, n):
		if res[-1] > 0:
			if arr[i] > 0 and arr[i] > res[-1]:
				res[-1] = arr[i]
			elif arr[i] < 0:
				res.append(arr[i])
		elif arr[i] < 0 and arr[i] > res[-1]:
			res[-1] = arr[i]
		elif arr[i] > 0:
			res.append(arr[i])
	return res
while test:
	number = int(input())
	arr = list(map(int, input().split()))
	pa = pos(arr, number)
	na = neg(arr, number)
	if len(pa) > len(na):
		print(sum(pa))
	elif len(pa) < len(na):
		print(sum(na))
	else:
		print(max(sum(na), sum(pa)))
	test -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag1 = 0
	flag2 = 0
	sumk = 0
	for i in a:
		if i >= 0:
			if flag1 == 0:
				pos = i
				flag1 = 1
				if flag2 == 1:
					sumk += neg
			elif flag3 == 1 and i > pos:
				pos = i
			elif flag3 == -1:
				sumk += neg
				pos = i
			flag3 = 1
		else:
			if flag2 == 0:
				neg = i
				flag2 = 1
				if flag1 == 1:
					sumk += pos
			elif flag3 == -1 and i > neg:
				neg = i
			elif flag3 == 1:
				sumk += pos
				neg = i
			flag3 = -1
	if flag3 == -1:
		sumk += neg
	elif flag3 == 1:
		sumk += pos
	print(sumk)


t = int(input())
for _ in range(t):
	i = -1
	j = -1
	var = 0
	ans = 0
	n = int(input())
	arr = list(map(int, input().split()))
	while var < n:
		if arr[var] < 0:
			if i < j or i == -1:
				i = var
				ans += arr[var]
			elif arr[i] < arr[var]:
				ans += arr[var] - arr[i]
				i = var
		elif j < i or j == -1:
			j = var
			ans += arr[var]
		elif arr[j] < arr[var]:
			ans += arr[var] - arr[j]
			j = var
		var += 1
	print(ans)


from sys import stdin, stdout
n_tests = int(input())
for k in range(n_tests):
	input()
	a = stdin.readline().split()
	a = list(map(int, a))
	sum = 0
	maxEl = a[0]
	for element in a:
		if maxEl > 0 and element < 0 or (maxEl < 0 and element > 0):
			sum += maxEl
			maxEl = element
		maxEl = max(maxEl, element)
	sum += maxEl
	stdout.write(str(sum) + '\n')


t = int(input())
ans = ''
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(sum, last) = (0, arr[0])
	for j in range(1, n, 1):
		if arr[j] * last > 0:
			last = max(last, arr[j])
		else:
			sum += last
			last = arr[j]
	sum += last
	ans += str(sum) + '\n'
print(ans)


t = int(input())
ultimate = []
for z in range(t):
	n = int(input())
	ar = [int(i) for i in input().split()]
	ar.append(0)
	final = []
	sum = 0
	big = ar[0]
	for j in range(n):
		if big < ar[j]:
			big = ar[j]
		if j == n - 1:
			final.append(big)
			break
		if ar[j] * ar[j + 1] < 0:
			final.append(big)
			big = ar[j + 1]
	for k in final:
		sum += int(k)
	ultimate.append(sum)
for p in range(t):
	print(ultimate[p])


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = float('-inf')
	neg = float('-inf')
	is_pos = a[0] > 0
	ans = 0
	for elem in a:
		if elem > 0:
			if is_pos:
				pos = max(pos, elem)
			else:
				ans += neg
				is_pos = True
				pos = elem
				neg = float('-inf')
		elif is_pos:
			ans += pos
			is_pos = False
			neg = elem
			pos = float('-inf')
		else:
			neg = max(neg, elem)
	last = pos if is_pos else neg
	ans += last
	print(ans)


def checkl(a, i):
	p = a[i]
	x = i
	while x < len(a) and a[x] * a[i] > 0:
		p = max(p, a[x])
		x += 1
	return [p, x]
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = 0
	ans = 0
	while k != len(a):
		(r, s) = checkl(a, k)
		ans += r
		k = s
	print(ans)


t = int(input())
import math
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	r = []
	q = []
	for i in range(n):
		if a[i] > 0:
			q.append(a[i])
		else:
			r.append(q)
			q = []
	r.append(q)
	q = []
	for i in range(n):
		if a[i] < 0:
			q.append(a[i])
		else:
			r.append(q)
			q = []
	r.append(q)
	ans = 0
	for i in r:
		if i == []:
			continue
		else:
			ans += max(i)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	currmax = l[0]
	sum = 0
	for j in range(n):
		if l[j] * currmax < 0:
			sum += currmax
			currmax = l[j]
		else:
			currmax = max(currmax, l[j])
	print(sum + currmax)


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


t = int(input())
for i in range(t):
	n = int(input())
	sequence = list(map(int, input().split()))
	maximum = 0
	minimum = 0
	summation = 0
	flag = True
	if sequence[0] > 0:
		maximum = sequence[0]
		flag = True
	else:
		minimum = sequence[0]
		flag = False
	i = 1
	while i < n:
		if flag:
			if sequence[i] > maximum:
				maximum = sequence[i]
			elif sequence[i] < 0:
				minimum = sequence[i]
				flag = False
				summation += maximum
				maximum = 0
		elif minimum < sequence[i] < 0:
			minimum = sequence[i]
		elif sequence[i] > 0:
			maximum = sequence[i]
			flag = True
			summation += minimum
			minimum = 0
		i += 1
	summation += minimum + maximum
	print(summation)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	c = 0
	f = A[0]
	for i in A:
		if f * i < 0:
			c += f
			f = i
		else:
			f = max(f, i)
	print(f + c)


T = int(input())
for _ in range(T):
	n = int(input())
	li = input().split()
	max1 = 0
	max2 = 0
	flag = 0
	final_sum = 0
	for i in range(n):
		li[i] = int(li[i])
	if li[0] < 0:
		max2 = li[0]
		flag = 1
	else:
		max1 = li[0]
	for i in range(1, n):
		if li[i] < 0 and li[i - 1] < 0:
			max2 = max(li[i], max2)
		elif li[i] > 0 and li[i - 1] > 0:
			max1 = max(li[i], max1)
		elif li[i] > 0 and li[i - 1] < 0:
			final_sum += max2
			max1 = li[i]
		else:
			final_sum += max1
			max2 = li[i]
	if li[n - 1] > 0 and li[n - 2] > 0:
		final_sum += max1
	elif li[n - 1] < 0 and li[n - 2] < 0:
		final_sum += max2
	elif li[n - 1] > 0 and li[n - 2] < 0:
		final_sum += li[n - 1]
	else:
		final_sum += li[n - 1]
	print(final_sum)


from bisect import bisect_left as lower_bound, bisect_right as upper_bound
from sys import stdin, stdout
from collections import defaultdict
inf = 10 ** 17

def solve(arr, n):
	pass

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().strip().split()))
		(pos, neg) = (-inf, -inf)
		for (i, num) in enumerate(arr):
			if num > 0:
				new_pos = num + (neg if neg != -inf else 0)
				if i > 0 and arr[i - 1] > 0:
					pos = max(pos, new_pos)
				else:
					pos = new_pos
			else:
				new_neg = num + (pos if pos != -inf else 0)
				if i > 0 and arr[i - 1] < 0:
					neg = max(neg, new_neg)
				else:
					neg = new_neg
		if arr[-1] < 0:
			print(neg)
		else:
			print(pos)
main()


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	i = 0
	ans = 0
	while i < n:
		if lst[i] > 0:
			j = i + 1
			maxPos = lst[i]
			while j < n and lst[j] > 0:
				maxPos = max(maxPos, lst[j])
				j += 1
			ans += maxPos
			i = j
		else:
			j = i + 1
			maxNeg = lst[i]
			while j < n and lst[j] < 0:
				maxNeg = max(maxNeg, lst[j])
				j += 1
			ans += maxNeg
			i = j
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	prevPos = False
	s = 0
	max_ = arr[0]
	if max_ > 0:
		prevPos = True
	for j in range(1, n):
		i = arr[j]
		if i > 0:
			if prevPos:
				max_ = max(max_, i)
			else:
				prevPos = True
				s = s + max_
				max_ = i
		elif not prevPos:
			max_ = max(max_, i)
		else:
			prevPos = False
			s = s + max_
			max_ = i
	print(s + max_)


import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest

def STR():
	return list(input())

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

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

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

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

def sortListWithIndex(listOfTuples, idx):
	return sorted(listOfTuples, key=lambda x: x[idx])
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
	t = INT()
	while t != 0:
		n = INT()
		a = LIST()
		currmax = a[0]
		ans = []
		currSign = 0
		if a[0] > 0:
			currSign = 1
		for i in range(1, n):
			if a[i] > 0 and currSign == 1:
				if a[i] > currmax:
					currmax = a[i]
			elif a[i] > 0 and currSign == 0:
				currSign = 1
				ans.append(currmax)
				currmax = a[i]
			elif a[i] < 0 and currSign == 1:
				currSign = 0
				ans.append(currmax)
				currmax = a[i]
			elif a[i] > currmax:
				currmax = a[i]
		ans.append(currmax)
		print(sum(ans))
		t -= 1
main()


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ty = -1
	k = 0
	z = 0
	result = 0
	for x in a:
		if ty == -1:
			if x > 0:
				ty = 1
			else:
				ty = 0
			k += 1
			z = x
		elif ty == 1:
			if x < 0:
				ty = 0
				k += 1
				result += z
				z = x
			elif x > z:
				z = x
		elif x > 0:
			ty = 1
			k += 1
			result += z
			z = x
		elif x > z:
			z = x
	result += z
	print(result)


import sys

def main():
	t = int(input())
	for _ in range(t):
		n = int(sys.stdin.readline().strip())
		nums = list(map(int, sys.stdin.readline().split()))
		negFlag = False
		if nums[0] < 0:
			negFlag = True
		tempVal = nums[0]
		ans = 0
		for i in range(1, n):
			if nums[i] < 0:
				if negFlag:
					tempVal = max(tempVal, nums[i])
				else:
					negFlag = True
					ans += tempVal
					tempVal = nums[i]
			elif not negFlag:
				tempVal = max(tempVal, nums[i])
			else:
				negFlag = False
				ans += tempVal
				tempVal = nums[i]
		ans += tempVal
		print(ans)
	return
main()


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


from sys import stdin, stdout

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

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

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

def op(ans):
	return stdout.write(str(ans) + '\n')
t = ip()
while t:
	t -= 1
	n = ip()
	li = ai()
	i = j = s = 0
	while i < n:
		while i < n and j < n and (li[i] * li[j] > 0):
			j += 1
		s += max(li[i:j])
		i = j
	op(s)


import array
test = int(input())
b = [False for _ in range(200005)]
for t in range(0, test):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	y = 1
	z = 0
	tong = 0
	c = a[0] // abs(a[0]) * -1
	for i in range(0, n):
		if a[i] // abs(a[i]) == c:
			z = max(a[i], z)
		else:
			if y != 1:
				b[x] = z
				x = x + 1
			else:
				y = 0
			c = a[i] // abs(a[i])
			z = a[i]
	b[x] = z
	x = x + 1
	for i in range(0, x):
		tong = tong + b[i]
	print(tong)


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


seqs = []
for i in range(int(input())):
	a = input()
	seqs.append(list(map(int, input().split())))
for t in seqs:
	if t[-1] < 0:
		t.append(1)
	else:
		t.append(-1)
	sub_sums = 0
	start = 0
	a = t[0]
	for i in range(len(t)):
		if a * t[i] < 1:
			sub_sums += max(t[start:i])
			start = i
			a = t[start]
	print(sub_sums)


t = int(input())
for i in range(0, t):
	n = int(input())
	c = 0
	secret = 0
	x = 0
	l = list(map(int, input().split()))
	for j in range(0, len(l)):
		if x == 0:
			if l[j] > 0:
				c += 1
				x = -1
		elif l[j] < 0:
			x = 0
			c += 1
	d = 0
	x = -1
	for j in range(0, len(l)):
		if x == -1:
			if l[j] < 0:
				d += 1
				x = 0
		elif l[j] > 0:
			d += 1
			x = -1
	if c > d:
		secret = 1
	elif d > c:
		secret = -1
	else:
		secret = 0
	sum1 = -11111111111111111111111
	sum2 = -11111111111111111111111
	if secret == 1 or secret == 0:
		x = 0
		sum = 0
		j = 0
		while j < len(l):
			if l[j] > 0 and x == 0:
				m = 0
				for k in range(j, len(l)):
					if l[k] > 0:
						if l[k] > m:
							m = l[k]
					else:
						x = -1
						j = k
						sum = sum + m
						break
				else:
					x = -1
					j = k
					sum = sum + m
			elif l[j] < 0 and x == -1:
				m = -11111111111111
				for k in range(j, len(l)):
					if l[k] < 0:
						if l[k] > m:
							m = l[k]
					else:
						x = 0
						j = k
						sum = sum + m
						break
				else:
					x = 0
					j = k
					sum = sum + m
			else:
				j += 1
		sum1 = sum
	if secret == -1 or secret == 0:
		x = -1
		sum = 0
		j = 0
		while j < len(l):
			if l[j] > 0 and x == 0:
				m = 0
				for k in range(j, len(l)):
					if l[k] > 0:
						if l[k] > m:
							m = l[k]
					else:
						x = -1
						j = k
						sum = sum + m
						break
				else:
					sum = sum + m
					x = -1
					j += 1
			elif l[j] < 0 and x == -1:
				m = -11111111111
				for k in range(j, len(l)):
					if l[k] < 0:
						if l[k] > m:
							m = l[k]
					else:
						x = 0
						j = k
						sum = sum + m
						break
				else:
					x = 0
					j = k
					sum = sum + m
			else:
				j += 1
		sum2 = sum
	print(max(sum1, sum2))


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


from sys import stdin, stdout

class Solve:

	def __init__(self):
		r = stdin.readline
		w = stdout.write
		ans = []
		for _ in range(int(input())):
			n = int(r())
			a = [int(x) for x in r().split()]
			(pos, neg) = (0, 0)
			tmp = []
			cnt = 0
			for num in a:
				if pos == 0 and neg == 0:
					tmp.append(num)
					if num > 0:
						pos = 1
					else:
						neg = 1
				elif num > 0 and pos:
					tmp.append(num)
				elif num < 0 and neg:
					tmp.append(num)
				else:
					pos ^= 1
					neg ^= 1
					cnt += max(tmp)
					tmp = [num]
			cnt += max(tmp)
			ans.append(str(cnt))
		w('\n'.join(ans))

def main():
	s = Solve()
main()


def alternate(a, n):
	ar = [a[0]]
	large = []
	i = 0
	while i < n - 1:
		if a[i] * a[i + 1] < 0:
			large.append(max(ar))
			ar = []
			ar.append(a[i + 1])
		else:
			ar.append(a[i + 1])
		i = i + 1
	large.append(max(ar))
	return sum(large)
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(alternate(a, n))


for i in range(int(input())):
	N = int(input())
	A = list(map(int, input().split())) + [0]
	ans = 0
	b = 0
	for a in A:
		if b == 0:
			b = a
		else:
			if b * a <= 0:
				ans += b
				b = a
			b = max(b, a)
	print(ans)


from collections import *
from bisect import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	r = 0
	flag = 0
	if a[0] > 0:
		flag = 0
		s = -1
	else:
		flag = 1
		s = -100000000000000000
	for i in range(n):
		if flag == 0:
			if a[i] > 0:
				s = max(s, a[i])
				continue
			else:
				flag = 1
				r += s
				s = a[i]
		elif a[i] < 0:
			s = max(s, a[i])
			continue
		else:
			flag = 0
			r += s
			s = a[i]
	r += s
	print(r)


def solve(n, a):
	sign = lambda x: x // abs(x)
	mx = a[0]
	f = a[0] // abs(a[0])
	ans = 0
	for i in range(1, n):
		if sign(a[i]) == f * -1:
			ans += mx
			mx = a[i]
			f *= -1
		mx = max(mx, a[i])
	if sign(a[-1]) == f:
		ans += mx
	return ans

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


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


def change(x, y):
	if x < 0 and y < 0 or (x > 0 and y > 0):
		return False
	return True

def solve():
	n = int(input())
	arr = [int(i) for i in input().split()] + [0]
	ans = 0
	prev = arr[0]
	ar = []
	for x in arr:
		if change(x, prev):
			ans += max(ar)
			ar = [x]
		else:
			ar.append(x)
		prev = x
	print(ans)
for _ in range(int(input())):
	solve()


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


from sys import *
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	mx = -1
	mn = -1e+18
	b = []
	while i < n:
		while i < n and a[i] > 0:
			mx = max(a[i], mx)
			i += 1
		if mx != -1:
			b.append(mx)
			mx = -1
		while i < n and a[i] < 0:
			mn = max(mn, a[i])
			i += 1
		if mn != -1e+18:
			b.append(mn)
			mn = -1e+18
	if mn != -1e+18:
		b.append(mn)
	if mx != -1:
		b.append(mx)
	stdout.write(str(sum(b)) + '\n')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	A = list(map(int, input().strip().split(' ')))
	sumA = 0
	if A[0] > 0:
		current = 'pos'
	else:
		current = 'neg'
	c = 0
	if n == 1:
		print(A[0])
	else:
		for i in range(1, n):
			if current == 'pos':
				if A[i] > 0:
					pass
				else:
					sumA += max(A[c:i])
					c = i
					current = 'neg'
			elif A[i] < 0:
				pass
			else:
				sumA += max(A[c:i])
				c = i
				current = 'pos'
			if current == 'neg' and A[i] < 0 and (i == n - 1):
				sumA += max(A[c:])
			elif current == 'pos' and A[i] > 0 and (i == n - 1):
				sumA += max(A[c:])
		print(sumA)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		if a[i] > 0:
			m = 0
			while i < n and a[i] > 0:
				if m < a[i]:
					m = a[i]
				i += 1
			s += m
		else:
			m = -10 ** 9
			while i < n and a[i] < 0:
				if m < a[i]:
					m = a[i]
				i += 1
			s += m
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	max_sum = 0
	cur_num = float('-inf')
	cur_pos = False
	for i in range(n):
		if i == 0:
			cur_pos = False
			if a[i] > 0:
				cur_pos = True
			cur_num = a[i]
		elif a[i] < 0 and cur_pos == False:
			cur_pos = False
			if a[i] > cur_num:
				cur_num = a[i]
		elif a[i] < 0 and cur_pos == True:
			max_sum += cur_num
			cur_pos = False
			cur_num = a[i]
		elif a[i] > 0 and cur_pos == True:
			if a[i] > cur_num:
				cur_num = a[i]
		elif a[i] > 0 and cur_pos == False:
			cur_pos = True
			max_sum += cur_num
			cur_num = a[i]
	max_sum += cur_num
	print(max_sum)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 0
	B = [A[0]]
	last = A[0] > 0
	for i in range(1, n):
		this = A[i] > 0
		if this != last:
			ans += max(B)
			B = []
		B.append(A[i])
		last = this
	if B:
		ans += max(B)
	print(ans)


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	currlen = 1
	currsum = 0
	curr = a[0]
	i = 0
	while i < n:
		mx = curr
		if curr > 0:
			while i < n and a[i] > 0:
				if a[i] > mx:
					mx = a[i]
				i += 1
			currsum += mx
		else:
			while i < n and a[i] < 0:
				if a[i] > mx:
					mx = a[i]
				i += 1
			currsum += mx
		currlen += 1
		if i < n:
			curr = a[i]
	print(currsum)


import math
import atexit
import io
import sys
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def array_to_string(a):
	s = ''
	for i in a:
		s += str(i)
	return s
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	l = 1
	sign = 1
	if a[0] < 0:
		sign = 0
	temp = []
	initial = 0
	if a[-1] > 0:
		a.append(-999)
	else:
		a.append(999)
	sum = 0
	m = -10 ** 9
	for i in range(n + 1):
		s = 1
		if a[i] < 0:
			s = 0
		if sign != s:
			l += 1
			initial = i
			sign = s
			sum += m
			m = a[i]
		elif a[i] > m:
			m = a[i]
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	l = 0
	r = 1
	output = []
	while l < len(nums):
		while r < n and nums[l] * nums[r] > 0:
			r += 1
		output.append(max(nums[l:r]))
		l = r
		r += 1
	print(sum(output))


for _ in range(int(input())):
	n = int(input())
	ind = s = 0
	lists = list(map(int, input().split()))
	for i in range(1, n):
		if lists[i] * lists[i - 1] < 0:
			s += max(lists[ind:i])
			ind = i
	l = lists[ind:]
	if len(l) > 0:
		print(max(l) + s)
	else:
		print(s)


def signlist(a, n):
	signs = []
	k = [a[0]]
	for i in range(1, n):
		if a[i] * a[i - 1] > 0:
			k.append(a[i])
		else:
			signs.append(k)
			k = [a[i]]
	if a[n - 1] * a[n - 1] > 0:
		signs.append(k)
	return signs

def final(signs):
	final = []
	for i in signs:
		try:
			final.append(max(i))
		except:
			final.append(i)
	return final
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(sum(final(signlist(a, n))))


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


from sys import stdin, stdout
import math
test = int(stdin.readline())
for _ in range(test):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	pos = a[0] > 0
	i = 0
	sum = 0
	out = False
	while i < n:
		if pos:
			elem = -1000000001
			while a[i] > 0:
				elem = max(elem, a[i])
				i += 1
				if i >= n:
					out = True
					break
			sum += elem
			pos = False
		else:
			elem = -1000000001
			while a[i] < 0:
				elem = max(elem, a[i])
				i += 1
				if i >= n:
					out = True
					break
			sum += elem
			pos = True
		if out:
			break
	print(sum)


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()))
		maxlen = 1
		maxnum = arry[0]
		res = 0
		if n == 1:
			yield arry[0]
		else:
			for i in range(1, n):
				curr = arry[i - 1]
				nex1 = arry[i]
				if curr > 0 and nex1 < 0 or (curr < 0 and nex1 > 0):
					maxlen += 1
					res += maxnum
					maxnum = arry[i]
				else:
					maxnum = max(maxnum, arry[i])
				if i == n - 1:
					res += maxnum
			yield res
t = int(input())
ans = gift()
print(*ans, sep='\n')


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, input().split()))
	prev = a[0]
	s = 0
	m = prev
	for i in a[1:]:
		if i * prev > 0:
			m = max(m, i)
		else:
			s += m
			m = i
		prev = i
	s += m
	print(s)


from functools import reduce
t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().split(' ')))
	l = [p[0]]
	for i in range(1, n):
		if p[i] * l[len(l) - 1] < 0:
			l.append(p[i])
		elif p[i] > l[len(l) - 1]:
			l.remove(l[len(l) - 1])
			l.append(p[i])
	x = reduce(lambda x, y: x + y, l)
	print(x)


t = int(input())
mini = -10 ** 9 - 1
while t > 0:
	n = int(input())
	arr = input().split(' ')
	arr = [int(each) for each in arr]
	ans = 0
	j = 1
	maxi = arr[0]
	while j < n:
		if arr[j] * arr[j - 1] < 0:
			ans += maxi
			maxi = mini
		maxi = max(maxi, arr[j])
		j += 1
	if maxi > mini:
		ans += maxi
	t -= 1
	print(ans)


I = input
for t in range(int(I())):
	I()
	nums = I().split()
	nums = list(map(int, nums))
	n = nums.pop(0)
	s = 0
	for i in nums:
		if abs(i) + abs(n) != abs(i + n):
			s += n
			n = i
		if i > n:
			n = i
	s += n
	print(s)


def main():
	t = int(input())
	while t != 0:
		n = int(input())
		arr = [int(x) for x in input().split()]
		start = True
		ptr = 0
		currArr = []
		ans = 0
		smol = 0
		while ptr < n:
			if start:
				smol = arr[ptr]
				currArr.append(smol)
				start = False
			elif arr[ptr] * smol > 0:
				currArr.append(arr[ptr])
			else:
				ans += max(currArr)
				ptr -= 1
				start = True
				currArr = []
			ptr += 1
		ans += max(currArr)
		print(ans)
		t -= 1
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	r = [a[0]]
	c = []
	for i in range(1, n):
		if a[i] < 0 and r[len(r) - 1] < 0:
			r.append(a[i])
		elif a[i] < 0 and r[len(r) - 1] > 0:
			c.append(r)
			r = []
			r.append(a[i])
		elif a[i] > 0 and r[len(r) - 1] > 0:
			r.append(a[i])
		elif a[i] > 0 and r[len(r) - 1] < 0:
			c.append(r)
			r = []
			r.append(a[i])
	c.append(r)
	s = 0
	for i in c:
		s += max(i)
	print(s)


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


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(ans, last_ind) = (0, 0)
	if arr[0] < 0:
		neg_or_pos = 1
		overhead = arr[0]
	else:
		neg_or_pos = 0
		overhead = arr[0]
	for i in range(n):
		if arr[i] > 0 and neg_or_pos == 0:
			overhead = max(overhead, arr[i])
		elif arr[i] < 0 and neg_or_pos == 1:
			overhead = max(overhead, arr[i])
		elif arr[i] < 0 and neg_or_pos == 0:
			ans += overhead
			overhead = arr[i]
			neg_or_pos = 1
		elif arr[i] > 0 and neg_or_pos == 1:
			ans += overhead
			overhead = arr[i]
			neg_or_pos = 0
	print(ans + overhead)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ispos = True
	ans = 0
	curr = 0
	for x in a:
		if (x > 0) != ispos:
			ans += curr
			curr = x
		else:
			curr = max(curr, x)
		ispos = x > 0
	ans += curr
	print(ans)


from collections import defaultdict, deque
import heapq, bisect, math, itertools, string, queue, copy, time
import sys
input = sys.stdin.readline
INF = float('inf')
mod = 10 ** 9 + 7
eps = 10 ** (-7)

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

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

def inpl_str():
	return list(input().split())
t = inp()
for _ in range(t):
	n = inp()
	a = inpl()
	ans = 0
	before_is_negative = a[0] > 0
	tmp = 0
	for i in range(len(a)):
		if before_is_negative:
			if a[i] > 0:
				ans += tmp
				tmp = a[i]
			else:
				tmp = max(tmp, a[i])
		elif a[i] < 0:
			ans += tmp
			tmp = a[i]
		else:
			tmp = max(tmp, a[i])
		before_is_negative = a[i] < 0
	print(ans + tmp)


count = int(input())
groups = []
for i in range(count):
	le = int(input())
	massive = list(map(int, input().split()))
	maxi = massive[0]
	if maxi < 0:
		sign = '-'
	else:
		sign = '+'
	cur = 1
	group = []
	while cur != le:
		if massive[cur] > 0:
			if sign == '+':
				if maxi < massive[cur]:
					maxi = massive[cur]
			elif sign == '-':
				group.append(maxi)
				maxi = massive[cur]
				sign = '+'
		if massive[cur] < 0:
			if sign == '-':
				if maxi < massive[cur]:
					maxi = massive[cur]
			elif sign == '+':
				group.append(maxi)
				maxi = massive[cur]
				sign = '-'
		cur += 1
	group.append(maxi)
	g = 0
	for x in group:
		g += x
	groups.append(g)
for x in groups:
	print(x)


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


def process():
	n = int(input())
	arr = list(map(int, input().split()))
	pool = []
	sign = arr[0] > 0
	maxs = 0
	for i in range(n):
		newsign = arr[i] > 0
		if sign == newsign:
			pool.append(arr[i])
		else:
			maxs += max(pool)
			pool.clear()
			pool.append(arr[i])
		sign = newsign
	maxs += max(pool)
	pool.clear()
	return maxs
for _ in range(int(input())):
	print(process())


inf = int(-1e+18)
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	(maxlen, s, f) = (0, 0, a[0] > 0)
	start = 0
	maxi = inf
	while start < n:
		j = start
		while j < n:
			if not f and a[j] > 0:
				break
			if f and a[j] < 0:
				break
			maxi = max(maxi, a[j])
			j += 1
		s += maxi
		f ^= 1
		start = j
		if maxi != inf:
			maxlen += 1
		maxi = inf
	print(s)


inp = lambda : map(int, input().rstrip().split())
for _ in range(int(input())):
	n = int(input())
	a = list(inp())
	s = 0
	l = [a[0]]
	for j in range(n - 1):
		if a[j] * a[j + 1] > 0:
			l.append(a[j + 1])
		else:
			s += max(l)
			l = [a[j + 1]]
	s += max(l)
	print(s)


t = int(input())
while t > 0:
	n = int(input())
	l = []
	x = input().split()
	for i in x:
		l.append(int(i))
	s = 0
	ma = 0
	mi = -1000000000
	mif = 1
	maf = 1
	for i in l:
		if i > 0:
			maf = 0
			if mif == 0:
				s += mi
				mif = 1
				mi = -1000000000
			if i > ma:
				ma = i
		if i < 0:
			mif = 0
			if maf == 0:
				s += ma
				maf = 1
				ma = 0
			if i > mi:
				mi = i
	if l[n - 1] > 0:
		s += ma
	else:
		s += mi
	print(s)
	t -= 1


t = int(input())
for tt in range(t):
	input()
	a = [int(i) for i in str.split(input(), ' ')]
	cur_min = None
	cur_max = None
	res = 0
	for i in a:
		if i < 0:
			if cur_max is not None:
				res += cur_max
				cur_max = None
			if cur_min is not None:
				cur_min = max(cur_min, i)
			else:
				cur_min = i
		else:
			if cur_min is not None:
				res += cur_min
				cur_min = None
			if cur_max is not None:
				cur_max = max(cur_max, i)
			else:
				cur_max = i
	if cur_max is not None:
		res += cur_max
	if cur_min is not None:
		res += cur_min
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	count = 0
	if l[0] < 0:
		flag = 1
	else:
		flag = 0
	m = 0
	s = 0
	for j in range(n):
		if l[j] < 0:
			if flag == 1:
				s += m
				count = 0
			if count == 0:
				m = l[j]
			elif m < l[j]:
				m = l[j]
			count += 1
			flag = 0
		else:
			if flag == 0:
				count = 0
				s += m
			if count == 0:
				m = l[j]
			elif m < l[j]:
				m = l[j]
			count += 1
			flag = 1
	s += m
	print(s)


for ad in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l[0] > 0:
		t = 1
	else:
		t = -1
	x = [0]
	c = 0
	for i in range(1, n):
		if t == 1:
			if i == n - 1:
				if l[i] > 0:
					c += max(l[x[0]:n])
				else:
					c += l[i]
					c += max(l[x[0]:i])
			elif l[i] < 0:
				x.append(i)
				c += max(l[x[0]:x[1]])
				t = -1
				x = [i]
		elif t == -1:
			if i == n - 1:
				if l[i] < 0:
					c += max(l[x[0]:n])
				else:
					c += l[i]
					c += max(l[x[0]:i])
			elif l[i] > 0:
				x.append(i)
				c += max(l[x[0]:x[1]])
				t = 1
				x = [i]
	if n == 1:
		c = l[0]
	print(c)


for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	max_neg = max_pos = -(1000000000.0 + 1)
	key = arr[0]
	ans = 0
	for i in arr[1:]:
		if i * key > 0:
			key = max(key, i)
		else:
			ans += key
			key = i
	print(ans + key)


t = int(input())
result = []
for ite in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = l[0] > 0
	v = l[0]
	res = []
	for i in range(n):
		sign = l[i] > 0
		if flag == sign:
			v = max(v, l[i])
		else:
			res.append(v)
			flag = not flag
			v = l[i]
		if i == n - 1:
			res.append(v)
	result.append(sum(res))
for el in result:
	print(el)


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


from heapq import heapify, heappop, heappush

def solution(ar):
	(first, second, result) = (0, 0, 0)
	while second < len(ar):
		heap = []
		heapify(heap)
		heappush(heap, -1 * ar[first])
		while second < len(ar) and ar[first] * ar[second] > 0:
			heappush(heap, -1 * ar[second])
			second += 1
		result += -1 * heappop(heap)
		first = second
	return result
test_number = int(input())
ar_input = []
for i in range(test_number):
	size = input()
	ar = [int(x) for x in input().split()]
	ar_input.append(ar)
for ar in ar_input:
	print(solution(ar))


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
from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())
while t:
	t -= 1
	n = int(fastinput())
	a = list(map(int, fastinput().split()))
	stack = [a[0]]
	for i in a[1:]:
		if stack[-1] * i > 0:
			stack[-1] = max(stack[-1], i)
		else:
			stack.append(i)
	print(sum(stack))


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	currentmax = nums[0]
	ans = 0
	if nums[0] > 0:
		pos = 1
	else:
		pos = -1
	for j in range(1, n):
		if nums[j] > 0:
			if pos == 1:
				if nums[j] > currentmax:
					currentmax = nums[j]
			else:
				ans = ans + currentmax
				currentmax = nums[j]
				pos = 1
		elif pos == -1:
			if nums[j] > currentmax:
				currentmax = nums[j]
		else:
			ans = ans + currentmax
			currentmax = nums[j]
			pos = -1
	ans = ans + currentmax
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	b = [a[0]]
	pre = a[0]
	for j in range(n):
		if pre * a[j] > 0:
			pre = max(pre, a[j])
			b[-1] = pre
		elif pre * a[j] < 0:
			pre = a[j]
			b.append(pre)
	print(sum(b))


from itertools import *
for s in [*open(0)][2::2]:
	print(sum((max(g) for (k, g) in groupby(map(int, s.split()), key=lambda x: x > 0))))


for _ in range(int(input())):
	x = int(input())
	(*li,) = map(int, input().split())
	(max1, ans) = (li[0], 0)
	for i in range(1, x):
		if max1 * li[i] < 0:
			ans += max1
			max1 = li[i]
		else:
			max1 = max(max1, li[i])
	ans += max1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	res = []
	res.append(l[0])
	prev = l[0]
	for i in range(1, len(l)):
		if l[i] > 0 and prev > 0 and (l[i] > prev):
			res.remove(prev)
			res.append(l[i])
			prev = l[i]
		elif l[i] < 0 and prev < 0 and (l[i] > prev):
			res.remove(prev)
			res.append(l[i])
			prev = l[i]
		elif l[i] > 0 and prev < 0:
			res.append(l[i])
			prev = l[i]
		if l[i] < 0 and prev > 0:
			res.append(l[i])
			prev = l[i]
	print(sum(res))


from sys import stdin
input = stdin.readline
t = int(input())
for rew in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	wyn = 0
	listka = []
	cur = [l[0]]
	for i in range(1, n):
		if cur and l[i] * cur[-1] < 0:
			listka.append(cur)
			cur = [l[i]]
		else:
			cur.append(l[i])
	listka.append(cur)
	for i in listka:
		wyn += max(i)
	print(wyn)


t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	n = int(input())
	a = [int(i) for i in input().split()]
	c = []
	(lb, rb) = (0, 0)
	temp = []
	while lb < n:
		temp.append(a[lb])
		rb = lb + 1
		while rb < n:
			if a[rb - 1] * a[rb] > 0:
				temp.append(a[rb])
				rb += 1
			else:
				c.append(temp)
				temp = []
				lb = rb
				break
		if temp:
			c.append(temp)
		temp = []
		lb = rb
	result = 0
	for i in c:
		result += max(i)
	print(result)


from itertools import *
for s in [*open(0)][2::2]:
	print(sum((max(map(int, v)) for (_, v) in groupby(s.split(), str.isdigit))))


for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	sum = num[0]
	if num[0] > 0:
		max = num[0]
	else:
		min = num[0]
	for i in range(1, n):
		if num[i] * num[i - 1] > 0:
			if num[i] > 0:
				if num[i] > max:
					sum = sum - max + num[i]
					max = num[i]
			elif num[i] > min:
				if min != -10000000000000:
					sum = sum - min + num[i]
					min = num[i]
				else:
					sum = sum + num[i]
					min = num[i]
		else:
			sum += num[i]
			max = 1
			min = -10000000000000
			if num[i] > 0:
				max = num[i]
			else:
				min = num[i]
	print(sum)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = [0] * n
	flag = 0
	if l[0] < 0:
		flag = -1
	else:
		flag = 1
	mi = -99999999999999999
	ma = -99999999999999999
	s = 0
	for x in range(0, n):
		if flag == 1 and l[x] > 0:
			mi = max(mi, l[x])
		elif flag == -1 and l[x] < 0:
			ma = max(ma, l[x])
		elif flag == 1 and l[x] < 0:
			s += mi
			mi = -9999999999999999
			ma = l[x]
			flag = -1
		elif flag == -1 and l[x] > 0:
			s += ma
			ma = -9999999999999999
			mi = l[x]
			flag = 1
	if flag == -1:
		s += ma
	else:
		s += mi
	print(s)


for i in range(int(input())):
	g = int(input())
	A = list(map(int, input().split()))
	a = A[0]
	su = 0
	for j in range(1, len(A)):
		if a * A[j] > 0:
			if A[j] > a:
				a = A[j]
		else:
			su += a
			a = A[j]
	su += a
	print(su)


t = int(input())
for i in range(t):
	n = int(input())
	m = list(map(int, input().split()))
	a = []
	b = []
	prev = m[0] > 0
	for p in m:
		if (p > 0) == prev:
			b.append(p)
		else:
			a.append(b)
			b = [p]
			prev = p > 0
	if b != []:
		a.append(b)
		b = []
	k = 0
	for t in a:
		k += max(t)
	print(k)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = {0: [0, 0]}
	l1 = [[0, 0, 0, 0]]
	for j in range(n):
		if l[n - 1 - j] < 0:
			v2 = l[n - 1 - j] + l1[j][0]
			v1 = l1[j][1]
			if l1[j][3] > l1[j][2] + 1:
				c1 = v1
				le = l1[j][3]
			elif l1[j][3] < l1[j][2] + 1:
				c1 = v2
				le = l1[j][2] + 1
			else:
				if v1 > v2:
					c1 = v1
				else:
					c1 = v2
				le = l1[j][3]
			l1.append([l1[j][0], c1, l1[j][2], le])
			if le not in d:
				d[le] = ['', '']
			if d[le][1] == '':
				d[le][1] = c1
			else:
				d[le][1] = max(c1, d[le][1])
		else:
			v2 = l[n - 1 - j] + l1[j][1]
			v1 = l1[j][0]
			if l1[j][2] > l1[j][3] + 1:
				c1 = v1
				le = l1[j][2]
			elif l1[j][2] < l1[j][3] + 1:
				c1 = v2
				le = l1[j][3] + 1
			else:
				if v1 > v2:
					c1 = v1
				else:
					c1 = v2
				le = l1[j][2]
			l1.append([c1, l1[j][1], le, l1[j][3]])
			if le not in d:
				d[le] = ['', '']
			if d[le][0] == '':
				d[le][0] = c1
			else:
				d[le][0] = max(c1, d[le][0])
	if d[max(d)][0] == '':
		print(d[max(d)][1])
	elif d[max(d)][1] == '':
		print(d[max(d)][0])
	else:
		print(max(d[max(d)][0], d[max(d)][1]))


def check(num1, num2):
	if num1 > 0 and num2 > 0:
		return 0
	if num1 < 0 and num2 < 0:
		return 0
	return 1
for _ in range(int(input())):
	N = int(input())
	numbers = list(map(int, input().split()))
	answer = [numbers[0]]
	idx = 1
	while idx < N:
		if check(answer[-1], numbers[idx]):
			answer.append(numbers[idx])
		elif answer[-1] < numbers[idx]:
			answer.pop()
			answer.append(numbers[idx])
		idx += 1
	print(sum(answer))


test_cases = int(input())

def handle():
	input()
	values = [int(s) for s in input().split(' ')]
	sign_is_positive = True if values[0] >= 0 else False
	current_sum = 0
	current_max = values[0]
	for value in values:
		if value >= 0 and sign_is_positive:
			current_max = max(current_max, value)
		elif value < 0 and (not sign_is_positive):
			current_max = max(current_max, value)
		else:
			sign_is_positive = not sign_is_positive
			current_sum += current_max
			current_max = value
	return current_sum + current_max
for _ in range(test_cases):
	print(handle())


neg = []
pos = []
assign = 0
for _ in range(int(input())):
	n = int(input())
	a = input().split(' ')
	s = 0
	if int(a[0]) > 0:
		assign = 1
	else:
		assign = 0
	for i in range(n):
		a[i] = int(a[i])
		if assign == 0:
			if a[i] > 0:
				pos.append(a[i])
				s = s + max(neg)
				neg = []
				assign = 1
			else:
				neg.append(a[i])
		elif a[i] < 0:
			neg.append(a[i])
			s = s + max(pos)
			pos = []
			assign = 0
		else:
			pos.append(a[i])
	if assign == 0:
		s = s + max(neg)
		neg = []
	else:
		s = s + max(pos)
		pos = []
	print(s)


a = int(input())
a1 = []
a2 = []
for i in range(a):
	sum = 0
	n = int(input())
	li = list(map(int, input().split()))
	li.append(0)
	for j in li:
		if j > 0:
			a1.append(j)
			if len(a2) > 0:
				sum += max(a2)
				a2.clear()
		elif j < 0:
			a2.append(j)
			if len(a1) > 0:
				sum += max(a1)
				a1.clear()
		elif j == 0:
			if len(a2) > 0:
				sum += max(a2)
				a2.clear()
			if len(a1) > 0:
				sum += max(a1)
				a1.clear()
	print(sum)


t = int(input())
for j in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	g = []
	for i in range(n - 1):
		if l[i + 1] > 0 and l[i] < 0:
			g.append(i)
		elif l[i + 1] < 0 and l[i] > 0:
			g.append(i)
	h = []
	a = 0
	if len(g) != 0:
		for i in range(len(g)):
			b = max(l[a:g[i] + 1])
			h.append(b)
			a = g[i] + 1
		h.append(max(l[a:]))
		print(sum(h))
	else:
		print(max(l))


for _ in range(int(input())):
	length = int(input())
	array = tuple(map(int, input().split()))
	total = array[0]
	cur = array[0]
	if array[0] > 0:
		pos = True
	else:
		pos = False
	for x in range(1, length):
		if pos:
			if array[x] < 0:
				total += array[x]
				cur = array[x]
				pos = False
			elif array[x] > cur:
				total -= cur
				total += array[x]
				cur = array[x]
		elif array[x] > 0:
			total += array[x]
			cur = array[x]
			pos = True
		elif array[x] > cur:
			total -= cur
			total += array[x]
			cur = array[x]
	print(total)


for _ in range(int(input())):
	n = int(input())
	ar = [int(x) for x in input().split()]
	fr = [ar[0]]
	p = 0
	for i in range(1, n):
		if ar[i] < 0 and fr[p] < 0:
			if ar[i] > fr[p]:
				fr[p] = ar[i]
		elif ar[i] > 0 and fr[p] > 0:
			if ar[i] > fr[p]:
				fr[p] = ar[i]
		else:
			p += 1
			fr.append(ar[i])
	sm = 0
	for i in fr:
		sm += i
	print(sm)


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


t = int(input())
for i in range(t):
	n = int(input())
	a = list(input().split())
	a = [int(k) for k in a]
	sum = 0
	last = 0
	max = -1000000001
	for x in a:
		if x > 0:
			if last >= 0:
				if x > max:
					max = x
			else:
				sum += max
				max = x
		elif last <= 0:
			if x > max:
				max = x
		else:
			sum += max
			max = x
		last = x
	sum += max
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	a = input().split()
	lst = []
	s = 0
	for i in range(n):
		a[i] = int(a[i])
		if len(lst) == 0:
			lst.append([a[i]])
		elif a[i] < 0 and lst[-1][0] < 0 or (a[i] > 0 and lst[-1][0] > 0):
			lst[-1].append(a[i])
		else:
			lst.append([a[i]])
	for i in lst:
		s = s + max(i)
	print(s)


for _ in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	now = 1
	temp = []
	if data[0] > 0:
		now = 1
	else:
		now = -1
	result = 0
	for i in data:
		if now == 1 and i > 0:
			temp.append(i)
		if now == 1 and i < 0:
			now = -1
			result += max(temp)
			temp = [i]
		if now == -1 and i < 0:
			temp.append(i)
		if now == -1 and i > 0:
			now = 1
			result += max(temp)
			temp = [i]
	result += max(temp)
	print(result)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	li = []
	pre = -float('inf')
	for i in range(n):
		if pre < 0:
			if ar[i] < 0:
				pre = max(pre, ar[i])
			else:
				if pre != -float('inf'):
					li.append(pre)
				pre = ar[i]
		elif ar[i] > 0:
			pre = max(pre, ar[i])
		else:
			li.append(pre)
			pre = ar[i]
	li.append(pre)
	print(sum(li))


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(p, z) = ([l[0]], [l[0]])
	(i, j) = (0, 0)
	if l[0] < 0:
		for x in range(1, n):
			if l[x] > 0:
				i = x
				p[0] = l[x]
				break
	if l[0] > 0:
		for y in range(1, n):
			if l[y] < 0:
				j = y
				z[0] = l[y]
				break
	for q in range(n):
		e = l[q]
		if q > i:
			if p[-1] > 0 and e > 0 or (p[-1] < 0 and e < 0):
				p[-1] = max(p[-1], e)
			elif p[-1] < 0 and e > 0 or (p[-1] > 0 and e < 0):
				p.append(e)
		if q > j:
			if z[-1] > 0 and e > 0 or (z[-1] < 0 and e < 0):
				z[-1] = max(z[-1], e)
			elif z[-1] < 0 and e > 0 or (z[-1] > 0 and e < 0):
				z.append(e)
	if len(p) == len(z):
		print(max(sum(p), sum(z)))
	elif len(p) > len(z):
		print(sum(p))
	else:
		print(sum(z))


def kadanes(a):
	max_so_far = -float('inf') - 1
	max_ending_here = 0
	start = 0
	end = 0
	s = 0
	for i in range(0, len(a)):
		max_ending_here += a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
			start = s
			end = i
		if max_ending_here < 0:
			max_ending_here = 0
			s = i + 1
	return (max_so_far, start, end)

def sign(a, b):
	if a > 0 and b > 0 or (a < 0 and b < 0):
		return True
	return False
t = int(input())
while t:
	max_len = 0
	sum_ = 0
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	while i < n:
		curr = a[i]
		j = i + 1
		while j < n and sign(a[i], a[j]):
			curr = max(curr, a[j])
			j += 1
		sum_ += curr
		i = j
	print(sum_)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	summ = 0
	j = 0
	prev = 0
	if arr[0] > 0:
		prev = 1
	while j < n:
		val = arr[j]
		if prev == 0 and val < 0:
			k = j + 1
			m = val
			while k < n and arr[k] < 0:
				if arr[k] > m:
					m = arr[k]
				k += 1
			summ += m
			if k == n:
				break
			j = k
		else:
			k = j + 1
			m = val
			while k < n and arr[k] > 0:
				if arr[k] > m:
					m = arr[k]
				k += 1
			summ += m
			if k == n:
				break
			j = k
		if prev == 0:
			prev = 1
		else:
			prev = 0
	print(summ)


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


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


rng = int(input())

def finder(arr):
	lst = [arr[0]]
	for i in range(1, len(arr)):
		if lst[-1] < 0 and arr[i] < 0:
			if arr[i] > lst[-1]:
				lst.pop()
				lst.append(arr[i])
		elif lst[-1] > 0 and arr[i] > 0:
			if arr[i] > lst[-1]:
				lst.pop()
				lst.append(arr[i])
		else:
			lst.append(arr[i])
	add = sum(lst)
	print(add)
for i in range(rng):
	length = int(input())
	arr = list(map(int, input().split()))
	finder(arr)


for tc in range(int(input())):
	n = int(input())
	arr = []
	line = input().split()
	for i in range(n):
		arr.append(int(line[i]))
	if n == 1:
		print(int(line[i]))
		continue
	count = 0
	cur = arr[0]
	summ = 0
	i = 1
	while i < n:
		if i == n - 1:
			if arr[i] > 0 and cur < 0:
				summ += arr[i]
				summ += cur
			elif arr[i] > 0 and cur > 0:
				if arr[i] > cur:
					summ += arr[i]
				else:
					summ += cur
			elif arr[i] < 0 and cur > 0:
				summ += arr[i]
				summ += cur
			elif arr[i] < 0 and cur < 0:
				if arr[i] > cur:
					summ += arr[i]
				else:
					summ += cur
			break
		if arr[i] > 0 and cur > 0:
			if arr[i] > cur:
				cur = arr[i]
		elif arr[i] < 0 and cur < 0:
			if arr[i] > cur:
				cur = arr[i]
		elif arr[i] > 0 and cur < 0:
			summ += cur
			cur = arr[i]
		elif arr[i] < 0 and cur > 0:
			summ += cur
			cur = arr[i]
		i += 1
	print(summ)


t = int(input())

def solving(l):
	is_nat = True if l[0] >= 0 else False
	res_l = []
	loc_l = []
	for i in l:
		if is_nat:
			if i >= 0:
				loc_l.append(i)
			else:
				res_l.append(loc_l)
				loc_l = [i]
				is_nat = False
		elif i < 0:
			loc_l.append(i)
		else:
			res_l.append(loc_l)
			loc_l = [i]
			is_nat = True
	res_l.append(loc_l)
	the_res_l = []
	for i in res_l:
		the_res_l.append(max(i))
	return sum(the_res_l)
for i in range(t):
	input()
	l = list(map(int, input().split()))
	print(solving(l))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n - 1):
		if a[i] > 0 and a[i + 1] < 0 or (a[i] < 0 and a[i + 1] > 0):
			continue
		else:
			a[i + 1] = max(a[i], a[i + 1])
			a[i] = 0
	print(sum(a))


t = int(input())
for _ in range(t):
	n = int(input())
	a = map(int, input().split())
	s = 0
	max_pos_cur = None
	max_neg_cur = None
	prev_sign_pos_flag = None
	for i in a:
		if prev_sign_pos_flag is None:
			if i < 0:
				max_neg_cur = i
				prev_sign_pos_flag = 0
			else:
				max_pos_cur = i
				prev_sign_pos_flag = 1
		elif i < 0:
			if max_neg_cur is None:
				max_neg_cur = i
			if prev_sign_pos_flag == 0:
				max_neg_cur = max(i, max_neg_cur)
			else:
				s += max_pos_cur
				max_pos_cur = None
			prev_sign_pos_flag = 0
		else:
			if max_pos_cur is None:
				max_pos_cur = i
			if prev_sign_pos_flag == 1:
				max_pos_cur = max(i, max_pos_cur)
			else:
				s += max_neg_cur
				max_neg_cur = None
			prev_sign_pos_flag = 1
	if prev_sign_pos_flag == 0:
		s += max_neg_cur
	else:
		s += max_pos_cur
	print(s)


def is_opposite(i, j):
	if i > 0 and j > 0:
		return False
	if i < 0 and j < 0:
		return False
	return True

class Node:

	def __init__(self, node=None):
		if node == None:
			self.sum = 0
			self.len = 0
		else:
			self.sum = node.sum
			self.len = node.len

	def is_better(self, node):
		if self.len > node.len:
			return True
		if self.len == node.len and self.sum > node.sum:
			return True
		return False

	def add(self, n):
		self.sum = self.sum + n
		self.len = self.len + 1

	def print(self):
		print('Sum : {}, len: {}'.format(self.sum, self.len))
T = int(input())
while T > 0:
	T = T - 1
	n = int(input())
	nums = list(map(int, input().split(' ')))
	i = 0
	pos_st = Node()
	neg_st = Node()
	pos_st.add(0)
	neg_st.add(0)
	while i < n:
		if nums[i] > 0:
			temp = Node(neg_st)
			temp.add(nums[i])
			if temp.is_better(pos_st):
				pos_st = temp
		if nums[i] < 0:
			temp = Node(pos_st)
			temp.add(nums[i])
			if temp.is_better(neg_st):
				neg_st = temp
		i = i + 1
	if pos_st.is_better(neg_st):
		print(pos_st.sum)
	else:
		print(neg_st.sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	res_pos = [-1]
	res_neg = [1]
	for x in a:
		if x * res_pos[-1] < 0:
			res_pos.append(x)
		elif x > res_pos[-1]:
			res_pos[-1] = x
		if x * res_neg[-1] < 0:
			res_neg.append(x)
		elif x > res_neg[-1]:
			res_neg[-1] = x
	res = res_pos
	if len(res_pos) < len(res_neg):
		res = res_neg
	res_sum = 0
	for i in range(1, len(res)):
		res_sum += res[i]
	print(res_sum)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(i) for i in input().split()]
	maxi = -10000000000.0
	mini = -10000000000.0
	flag = 2
	summ = 0
	for i in range(n):
		if a[i] < 0:
			if flag == 1:
				summ += maxi
				maxi = -10000000000.0
			flag = 0
			mini = max(a[i], mini)
		if a[i] > 0:
			if flag == 0:
				summ += mini
				mini = -10000000000.0
			flag = 1
			maxi = max(a[i], maxi)
	if flag == 0:
		summ += mini
	else:
		summ += maxi
	print(summ)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = [a[0]]
	for i in range(1, n):
		if l[-1] > 0 and a[i] > 0:
			p = l.pop()
			l.append(max(p, a[i]))
		if l[-1] < 0 and a[i] < 0:
			p = l.pop()
			l.append(max(p, a[i]))
		if l[-1] > 0 and a[i] < 0:
			l.append(a[i])
		if l[-1] < 0 and a[i] > 0:
			l.append(a[i])
	if len(l) == 1:
		print(l[0])
	else:
		s = 0
		i = 0
		if len(l) % 2 == 1:
			p = l.pop()
			s += p
		while i < len(l):
			s += l[i] + l[i + 1]
			i += 2
		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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	sum = 0
	i = 0
	while i < n:
		cur = l[i]
		j = i
		while j < n and (l[i] > 0 and l[j] > 0 or (l[i] < 0 and l[j] < 0)):
			cur = max(l[j], cur)
			j += 1
		sum += cur
		i = j
	print(sum)


for _ in range(int(input())):
	tamanho = int(input())
	valores = map(int, input().split())
	(negativo, positivo) = (False, False)
	menor_maior = -10 ** 9 - 2
	resposta = list()
	for valor in valores:
		if valor < 0:
			if negativo == False:
				negativo = True
			if positivo == True:
				resposta.append(menor_maior)
				menor_maior = -10 ** 9 - 2
				positivo = False
			if negativo:
				if valor > menor_maior:
					menor_maior = valor
		else:
			if positivo == False:
				positivo = True
			if negativo == True:
				resposta.append(menor_maior)
				menor_maior = 0
				negativo = False
			if positivo:
				if valor > menor_maior:
					menor_maior = valor
	resposta.append(menor_maior)
	print(sum(resposta))


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n == 1:
		print(a[0])
		continue
	for i in range(n - 1):
		if a[i] > 0 and a[i + 1] > 0:
			a[i] = max(a[i], a[i + 1])
			a[i + 1] = a[i]
		elif a[i] < 0 and a[i + 1] < 0:
			a[i] = max(a[i], a[i + 1])
			a[i + 1] = a[i]
	if a[0] > 0 and a[1] > 0:
		a[0] = max(a[0], a[1])
	if a[0] < 0 and a[1] < 0:
		a[0] = max(a[0], a[1])
	if a[-1] < 0 and a[-2] < 0:
		a[-1] = max(a[-1], a[-2])
	elif a[-1] < 0 and a[-2] < 0:
		a[-1] = max(a[-1], a[-2])
	for i in range(n - 1, 0, -1):
		if a[i] > 0 and a[i - 1] > 0:
			a[i] = max(a[i], a[i - 1])
			a[i - 1] = a[i]
		elif a[i] < 0 and a[i - 1] < 0:
			a[i] = max(a[i], a[i - 1])
			a[i - 1] = a[i]
	if a[0] > 0 and a[1] > 0:
		a[0] = max(a[0], a[1])
	if a[0] < 0 and a[1] < 0:
		a[0] = max(a[0], a[1])
	if a[-1] < 0 and a[-2] < 0:
		a[-1] = max(a[-1], a[-2])
	elif a[-1] < 0 and a[-2] < 0:
		a[-1] = max(a[-1], a[-2])
	su = a[0]
	if a[0] > 0:
		sin = '+'
	else:
		sin = '-'
	for i in range(1, n):
		if a[i] > 0:
			t = '+'
		else:
			t = '-'
		if sin == t:
			continue
		else:
			su += a[i]
			sin = t
	print(su)


n = int(input())
for x in range(n):
	a = int(input())
	k = list(map(int, input().split()))
	sums = 0
	r = 0
	for xy in range(1, a):
		if k[xy] * k[xy - 1] < 0:
			sums += max(k[r:xy])
			r = xy
	sums += max(k[r:a])
	print(sums)


for ix in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sig = a[0] > 0
	b = [[a[0]]]
	for i in range(1, n):
		if (a[i] > 0) == sig:
			b[-1].append(a[i])
		else:
			b.append([a[i]])
			sig = not sig
	sum = 0
	for i in b:
		sum += max(i)
	print(sum)


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


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ma = 0
	le = 0
	ar = []
	t1 = 0
	t2 = -9999999999
	i = 0
	while True:
		if l[i] > 0:
			while l[i] > 0 and i < n:
				t1 = max(t1, l[i])
				i += 1
				if i == n:
					break
			ar.append(t1)
			t1 = 0
		else:
			while l[i] < 0 and i < n:
				t2 = max(l[i], t2)
				i += 1
				if i == n:
					break
			ar.append(t2)
			t2 = -999999999999999
		if i == n:
			break
	print(sum(ar))


T = int(input())
while T != 0:
	T = T - 1
	n = int(input())
	b = input().split(' ')
	a = [int(x) for x in b]
	v = a[0]
	s = 0
	for i in range(1, n):
		if a[i] > 0:
			if v > 0:
				v = max(v, a[i])
			else:
				s = s + v
				v = a[i]
		elif v < 0:
			v = max(v, a[i])
		else:
			s = s + v
			v = a[i]
	s = s + v
	print(s)


def sign(x):
	return int(x > 0)
t = int(input())
for i in range(t):
	n = int(input())
	a = list((int(num) for num in input().split(' ')))[:n]
	i = 0
	b = []
	while i < n:
		if i == 0:
			b.append(a[i])
			i = i + 1
		elif sign(a[i - 1]) == sign(a[i]):
			maxi = max(b[len(b) - 1], a[i])
			if maxi == a[i]:
				b.remove(b[len(b) - 1])
				b.append(a[i])
				i = i + 1
			else:
				i = i + 1
		else:
			b.append(a[i])
			i = i + 1
	print(sum(b))


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sum = a[0]
	max_digit = a[0]
	positive = True if a[0] > 0 else False
	for i in range(1, n):
		if a[i] > 0:
			if positive == True:
				if a[i] > max_digit:
					sum += a[i] - max_digit
					max_digit = a[i]
			else:
				sum += a[i]
				positive = True
				max_digit = a[i]
		elif positive == False:
			if a[i] > max_digit:
				sum += a[i] - max_digit
				max_digit = a[i]
		else:
			sum += a[i]
			max_digit = a[i]
			positive = False
	print(sum)


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


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = [a[0]]
	ans = 0
	for i in range(1, n):
		if b[0] < 0 and a[i] < 0:
			b.append(a[i])
		if b[0] > 0 and a[i] > 0:
			b.append(a[i])
		if b[0] < 0 and a[i] > 0:
			ans += max(b)
			b = [a[i]]
		if b[0] > 0 and a[i] < 0:
			ans += max(b)
			b = [a[i]]
	if len(b) >= 1:
		ans += max(b)
	print(ans)


def msms(a):
	rmax = a[0]
	ss = rmax
	for i in range(1, len(a)):
		if a[i] * rmax < 0:
			ss += a[i]
			rmax = a[i]
			continue
		if a[i] > rmax:
			ss += a[i] - rmax
			rmax = a[i]
	return ss
rl = []
for i in range(int(input())):
	_ = int(input())
	a = list(map(int, input().split()))
	rl.append(msms(a))
[print(r) for r in rl]


from sys import *
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	mx = 0
	mxn = -9999999999
	fp = 0
	fn = 0
	final = []
	for i in range(n - 1):
		if l[i] > 0 and l[i + 1] > 0:
			mx = max(mx, l[i])
			mx = max(mx, l[i + 1])
		elif l[i] < 0 and l[i + 1] < 0:
			mxn = max(mxn, l[i])
			mxn = max(mxn, l[i + 1])
		elif l[i] > 0:
			final.append(max(l[i], mx))
			mx = 0
		else:
			final.append(max(l[i], mxn))
			mxn = -9999999999
	if l[n - 1] > 0 and l[n - 2] > 0:
		final.append(max(mx, l[n - 1]))
	elif l[n - 1] < 0 and l[n - 2] < 0:
		final.append(max(mxn, l[n - 1]))
	else:
		final.append(l[n - 1])
	print(sum(final))


for i in range(int(input())):
	aL = int(input())
	aL += 1
	a = [int(s) for s in input().split()]
	a.append(0)
	res = 0
	maxEle = a[0]
	lastEle = a[0]
	for i in range(1, aL):
		if a[i] == 0 or (lastEle > 0 and a[i] < 0) or (lastEle < 0 and a[i] > 0):
			res += maxEle
			maxEle = a[i]
		elif maxEle < a[i]:
			maxEle = a[i]
		lastEle = a[i]
	print(res)


n = int(input())
for i in range(n):
	ans = 0
	m = int(input())
	a = list(map(int, input().split()))
	maxi = -10 ** 10
	for j in range(m - 1):
		maxi = max(maxi, a[j])
		if a[j] * a[j + 1] < 0:
			ans += maxi
			maxi = -10 ** 10
	ans += max(maxi, a[-1])
	print(ans)


a = int(input())
for _ in range(a):
	n = int(input())
	y = 0
	vmax = 0
	zmax = -10 ** 9
	summ = 0
	ary = list(map(int, input().split()))
	while y < n:
		if ary[y] > 0:
			while y < n and ary[y] > 0:
				if ary[y] > vmax:
					vmax = ary[y]
				y += 1
			summ += vmax
			vmax = 0
		else:
			while y < n and ary[y] < 0:
				if ary[y] > zmax:
					zmax = ary[y]
				y += 1
			summ += zmax
			zmax = -10 ** 9
	print(summ)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	cur = s[0]
	ans = 0
	if s[0] > 0:
		sign = 1
	else:
		sign = 0
	for i in s:
		if sign and i > 0 or (not sign and i < 0):
			cur = max(i, cur)
		else:
			ans += cur
			cur = i
			sign = (sign + 1) % 2
	ans += cur
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(_) for _ in input().split()]
	ans = 0
	maxNum = a[0]
	if maxNum > 0:
		flag = 1
	else:
		flag = 0
	for i in range(1, n):
		if flag == 1:
			if a[i] > 0:
				pass
			else:
				ans += maxNum
				flag = 0
				maxNum = -(10 ** 9 + 1)
		if flag == 0:
			if a[i] < 0:
				pass
			else:
				ans += maxNum
				flag = 1
				maxNum = -(10 ** 9 + 1)
		maxNum = max(maxNum, a[i])
	ans += maxNum
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(i) for i in input().split()]
	if n == 1:
		print(arr[0])
		continue
	posi = []
	nega = []
	i = 0
	j = 1
	while i < n and j < n:
		if arr[i] > 0 and arr[j] > 0:
			while i < n and j < n and (arr[j] > 0):
				j += 1
			posi.append(arr[i:j])
			i = j
		elif arr[i] > 0 and arr[j] < 0:
			posi.append([arr[i]])
			i = j
		elif arr[i] < 0 and arr[j] < 0:
			while i < n and j < n and (arr[j] < 0):
				j += 1
			nega.append(arr[i:j])
			i = j
		elif arr[i] < 0 and arr[j] > 0:
			nega.append([arr[i]])
			i = j
	ans = sum(list(map(max, posi))) + sum(list(map(max, nega)))
	print(ans)


import sys
input = sys.stdin.readline
for i in range(int(input())):
	n = int(input())
	A = [int(x) for x in input().split()]
	ans = 0
	temp = A[0]
	curr = A[0] // abs(A[0])
	for j in range(1, n):
		if curr * A[j] > 0:
			temp = max(A[j], temp)
		else:
			ans += temp
			temp = A[j]
			curr = -1 * curr
	print(ans + temp)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if True:
		l1 = []
		m = 0
		f = 1
		for i in range(n):
			if l[i] > 0:
				m = max(m, l[i])
				f = 1
			elif f == 1:
				l1.append(m)
				m = 0
		if f == 1:
			l1.append(m)
		f = 1
		p = 0
		for i in range(n):
			if l[i] < 0 and f == 1 and (p == 0):
				m1 = l[i]
				f = 0
				p = 1
			elif f == 0 and l[i] < 0 and (p == 1):
				m1 = max(m1, l[i])
			elif f == 0 and p == 1:
				l1.append(m1)
				f = 1
				p = 0
		if f == 0 and p == 1:
			l1.append(m1)
		print(sum(l1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	best = a[0]
	total = 0
	for i in range(1, n):
		if best < a[i] < 0 or 0 < best < a[i]:
			best = a[i]
		elif best < 0 < a[i] or a[i] < 0 < best:
			total += best
			best = a[i]
	total += best
	print(total)


import os
import sys
from collections import Counter
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 cons(i, lst):
	x = 0
	for z in range(i, len(lst)):
		if lst[z] < 0:
			break
		else:
			x += 1
	if x == len(lst) - i:
		try:
			return (max(lst[i:z + 1]), z + 1)
		except:
			return (lst[z], z + 1)
	else:
		try:
			return (max(lst[i:z]), z)
		except:
			return (lst[z], z)

def negCons(i, lst):
	x = 0
	for z in range(i, len(lst)):
		if lst[z] > 0:
			break
		else:
			x += 1
	if x == len(lst) - i:
		try:
			return (max(lst[i:z + 1]), z + 1)
		except:
			return (lst[z], z + 1)
	else:
		try:
			return (max(lst[i:z]), z)
		except:
			return (lst[z], z)
a = int(input())
for k in range(a):
	a = int(input())
	lst = list(map(int, input().split()))
	(i, ans) = (0, 0)
	while i < a:
		if lst[i] > 0:
			(temp, lol) = cons(i, lst)
			ans += temp
		else:
			(temp, lol) = negCons(i, lst)
			ans += temp
		i = lol
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	max = a[0]
	if a[0] > 0:
		flag = 1
	else:
		flag = 2
	ans = []
	for i in range(1, n):
		if a[i] > 0:
			if flag == 1:
				if max < a[i]:
					max = a[i]
			else:
				ans.append(max)
				max = a[i]
				flag = 1
		elif flag == 2:
			if max < a[i]:
				max = a[i]
		else:
			ans.append(max)
			max = a[i]
			flag = 2
	ans.append(max)
	print(sum(ans))


t = int(input())
for _ in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	cache = []
	res = []
	pos = seq[0] > 0
	for e in seq:
		if pos:
			if e > 0:
				cache.append(e)
			else:
				res.append(max(cache))
				pos = False
				cache = [e]
		elif e < 0:
			cache.append(e)
		else:
			res.append(max(cache))
			pos = True
			cache = [e]
	if len(cache) > 0:
		res.append(max(cache))
	print(sum(res))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	sign = 0
	ans = []
	mx = -1000000000
	for i in range(0, len(l)):
		if l[i] > 0:
			if sign == 0:
				if l[i] > mx:
					mx = l[i]
					sign = 0
			elif sign == 1:
				ans.append(mx)
				mx = l[i]
				sign = 0
		elif l[i] < 0:
			if sign == 1:
				if l[i] > mx:
					mx = l[i]
					sign = 1
			elif sign == 0:
				if i == 0:
					mx = l[i]
					sign = 1
				else:
					ans.append(mx)
					mx = l[i]
					sign = 1
	a = len(ans)
	if len(ans) >= 1:
		if ans[a - 1] > 0 and mx < 0:
			ans.append(mx)
		elif ans[a - 1] < 0 and mx > 0:
			ans.append(mx)
	elif len(ans) == 0:
		ans.append(mx)
	s = sum(ans)
	print(s)


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

def list_input():
	return list(num_input())
for t in range(int(input())):
	n = int(input())
	arr = list_input()
	sum = 0
	i = 0
	while i < n:
		j = i + 1
		max = arr[i]
		sign = False
		if arr[i] < 0:
			sign = True
		while j < n:
			if sign:
				if arr[j] < 0:
					if max < arr[j]:
						max = arr[j]
				else:
					break
			elif arr[j] > 0:
				if max < arr[j]:
					max = arr[j]
			else:
				break
			j += 1
		temp = max
		if sign:
			max = 0
			while j < n and arr[j] > 0:
				if max < arr[j]:
					max = arr[j]
				j += 1
			sum += temp + max
		else:
			min = -1 * 10 ** 10
			while j < n and arr[j] < 0:
				if min < arr[j]:
					min = arr[j]
				j += 1
			if min != -1 * 10 ** 10:
				sum += temp + min
			else:
				sum += temp
		i = j
	print(sum)


def change(sign):
	if sign == -1:
		return 1
	else:
		return -1

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		temp = []
		if a[0] < 0:
			sign = -1
		else:
			sign = 1
		ans = 0
		i = 0
		while i < n:
			if a[i] * sign > 0:
				temp.append(a[i])
				i += 1
			else:
				sign = change(sign)
				ans += max(temp)
				temp = []
				continue
		else:
			ans += max(temp)
		print(ans)
main()


t = int(input())
for p in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	count = 0
	for i in range(n - 2):
		if a[i] * a[i + 1] > 0:
			a[i + 1] = max(a[i], a[i + 1])
		else:
			count += a[i]
	if a[n - 1] * a[n - 2] > 0:
		count += max(a[n - 1], a[n - 2])
	else:
		count += a[n - 1] + a[n - 2]
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	k = 0
	if nums[0] > 0:
		k = 1
	elif nums[0] < 0:
		k = -1
	result = []
	temp = []
	for i in range(n):
		if nums[i] > 0:
			if k == 1:
				temp.append(nums[i])
			elif k == -1:
				result.append(max(temp))
				temp = [nums[i]]
			k = 1
		elif nums[i] < 0:
			if k == 1:
				result.append(max(temp))
				temp = [nums[i]]
			elif k == -1:
				temp.append(nums[i])
			k = -1
	if k == 1 and temp[0] > 0:
		result.append(max(temp))
	elif k == -1 and temp[0] < 0:
		result.append(max(temp))
	elif k == 1 and temp[0] < 0:
		result.append(max(temp))
	elif k == -1 and temp[0] > 0:
		result.append(max(temp))
	print(sum(result))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = []
	a1 = []
	sub_a = []
	max_a = None
	for i in range(len(a)):
		if a[i] > 0:
			b.append(True)
		else:
			b.append(False)
	for i in range(len(a)):
		if b[i] is True:
			a1.append(a[i])
			if i + 1 == n or b[i + 1] is False:
				max_a = max(a1)
				sub_a.append(max_a)
				a1 = []
		if b[i] is False:
			a1.append(a[i])
			if i + 1 == n or b[i + 1] is True:
				max_a = max(a1)
				sub_a.append(max_a)
				a1 = []
	print(sum(sub_a))


def comp(n):
	if n < 0:
		return -1
	return 1
t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	i = 0
	while i < n:
		j = i
		loc = l[i]
		while j < n and comp(l[i]) == comp(l[j]):
			loc = max(loc, l[j])
			j += 1
		sum += loc
		if j == n:
			break
		i = j
	print(sum)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		total = 0
		pos_zone = a[0] > 0
		M = 0 if pos_zone else 10 ** 9 * -1
		for (c, el) in enumerate(a):
			if el > 0:
				if pos_zone:
					M = max(M, el)
				else:
					pos_zone = True
					total += M
					M = el
			elif not pos_zone:
				M = max(M, el)
			else:
				pos_zone = False
				total += M
				M = el
		total += M
		print(total)
main()


for j in range(int(input())):
	n = int(input())
	A = [int(i) for i in input().split()]
	z = -1 if A[0] < 0 else 1
	i = 1
	s = 0
	ti = A[0]
	t = 0
	while i != n:
		t = 0
		if z == -1:
			if A[i] < 0:
				ti = max(ti, A[i])
			else:
				s += ti
				ti = A[i]
				z = 1
				t = 1
		elif A[i] > 0:
			ti = max(ti, A[i])
		else:
			s += ti
			ti = A[i]
			z = -1
			t = 1
		i += 1
	if not t:
		s += ti
	if n > 1:
		if A[-1] // abs(A[-1]) != A[-2] // abs(A[-2]):
			s += A[-1]
	print(s)


def solve():
	n = int(input())
	A = list(map(int, input().split()))
	A.append(-A[-1])
	if n == 1:
		print(A[0])
		return
	if A[0] > 0:
		lp = True
		pos = A[0]
		neg = -10 ** 10
	else:
		lp = False
		neg = A[0]
		pos = 0
	answer = 0
	for a in A[1:]:
		if a > 0:
			if lp:
				pos = max(pos, a)
			else:
				lp = True
				answer += neg
				pos = a
		elif not lp:
			neg = max(neg, a)
		else:
			lp = False
			answer += pos
			neg = a
	print(answer)
t = int(input())
for i in range(t):
	solve()


for tt in range(int(input())):
	n = int(input())
	num = [int(i) for i in input().split()]
	plist = []
	nlist = []
	j = 0
	sum = 0
	while num[j] > 0 or num[j] < 0:
		if j == n - 1:
			if num[j] > 0:
				plist.append(num[j])
				if num[j - 1] > 0:
					sum = sum + max(plist)
				else:
					sum = sum + num[j]
			elif num[j] < 0:
				nlist.append(num[j])
				if num[j - 1] < 0:
					sum = sum + max(nlist)
				else:
					sum = sum + num[j]
			break
		elif num[j] > 0 and num[j + 1] > 0:
			plist.append(num[j])
		elif num[j] > 0 and num[j + 1] < 0:
			plist.append(num[j])
			sum = sum + max(plist)
			plist = []
		elif num[j] < 0 and num[j + 1] > 0:
			nlist.append(num[j])
			sum = sum + max(nlist)
			nlist = []
		elif num[j] < 0 and num[j + 1] < 0:
			nlist.append(num[j])
		j = j + 1
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().strip().split()]
	i = 0
	res = 0
	while i < n:
		if a[i] > 0:
			pi = 0
			while i < n and a[i] > 0:
				pi = max(pi, a[i])
				i += 1
			res += pi
		else:
			ni = -10 ** 10
			while i < n and a[i] < 0:
				ni = max(ni, a[i])
				i += 1
			res += ni
	print(res)


t = int(input())
out = ''
while t > 0:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	if len(a) == 1:
		out += f'{a[0]}\n'
		continue
	b = []
	i = 1
	answer = a[0]
	while i < n:
		if a[i] < 0 and a[i - 1] < 0 or (a[i] > 0 and a[i - 1] > 0):
			answer = max(answer, a[i])
		else:
			b.append(answer)
			answer = a[i]
		i += 1
	b.append(answer)
	out += f'{sum(b)}\n'
print(out)


import sys
import collections
import math
import functools
import itertools
import bisect
import operator
import heapq
import random
true = True
false = False
null = None
tcid = 0
tcmax = 99999999

def compute(val, func):
	return func(val)

def seq(lo, hi, step=1):
	return range(lo, hi + 1, step)

def sround(val, nd):
	return f'{val:.{nd}f}'

def ceil(a, b):
	ans = a // b
	if a % b != 0:
		ans += 1
	return ans

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

def line():
	ln = sys.stdin.readline().strip()
	if ln == '':
		sys.exit()
	return ln

def lines(n):
	return [line() for i in range(n)]

def split(ln=None):
	return (ln or line()).split()

def num(str=None):
	str = str or line()
	return float(str) if '.' in str else int(str)

def nums(o=None):
	if o is not None:
		if isinstance(o, int):
			o = lines(o)
		elif isinstance(o, str):
			o = split(o)
	return list(map(num, o or split()))
tcmax = num()

def mainloop(tcid=1):
	sz = 222222
	inf = 9999999999999999
	n = num()
	a = nums()
	a.append(a[-1] * -1)
	ans = 0
	tmp = a[0]
	for ii in range(1, len(a)):
		curr = a[ii]
		prev = a[ii - 1]
		if curr * prev < 0:
			ans += tmp
			tmp = curr
		else:
			tmp = max(tmp, curr)
	print(ans)
while tcid < tcmax:
	tcid += 1
	mainloop(tcid)


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


import sys
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	n = int(I())
	a = list(map(int, I().split()))
	i = 0
	j = 1
	my_list = []
	while j < n:
		if a[i] < 0 and a[j] > 0 or (a[i] > 0 and a[j] < 0):
			my_list.append(a[i:j])
			i = j
		j += 1
	my_list.append(a[i:j])
	sum_ = 0
	for i in my_list:
		sum_ += max(i)
	ans += str(sum_) + '\n'
print(ans)


for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	pnt1 = 0
	pnt2 = 0
	ans = 0
	while pnt2 < n:
		if nums[pnt1] < 0 and nums[pnt2] < 0 or (nums[pnt1] > 0 and nums[pnt2] > 0):
			pnt2 += 1
		else:
			ans += max(nums[pnt1:pnt2])
			pnt1 = int(pnt2)
	ans += max(nums[pnt1:pnt2]) if nums[pnt1:pnt2] else 0
	print(ans)


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


tc = int(input())
for eachi in range(tc):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	flag = 0
	count = 0
	if a[0] < 0:
		flag = 1
	if flag == 0:
		i = 0
		while i < n:
			c = []
			if count % 2 == 0:
				flg = 0
				for j in range(i, n):
					if a[j] >= 0:
						c.append(a[j])
					else:
						flg = 1
						i = j
						break
				if flg == 0:
					i = j + 1
			else:
				flg = 0
				for j in range(i, n):
					if a[j] >= 0:
						flg = 1
						i = j
						break
					else:
						c.append(a[j])
				if flg == 0:
					i = j + 1
			count = count + 1
			if len(c) != 0:
				b.append(max(c))
			c = []
	else:
		i = 0
		while i < n:
			c = []
			if count % 2 == 1:
				flg = 0
				for j in range(i, n):
					if a[j] >= 0:
						c.append(a[j])
					else:
						flg = 1
						i = j
						break
				if flg == 0:
					i = j + 1
			else:
				flg = 0
				for j in range(i, n):
					if a[j] >= 0:
						flg = 1
						i = j
						break
					else:
						c.append(a[j])
				if flg == 0:
					i = j + 1
			count = count + 1
			if len(c) != 0:
				b.append(max(c))
			c = []
	print(sum(b))


t = int(input())
for i in range(t):
	n = int(input())
	m = list(map(int, input().split()))
	md = 1
	ms = m[0]
	d = 1
	s = m[0]
	l = m[0] // abs(m[0])
	ll = 0
	for j in range(1, n):
		if m[j] // abs(m[j]) != l:
			d += 1
			s += m[j]
			l = -l
			ll = j
		elif m[j] > m[ll]:
			s += m[j]
			s -= m[ll]
			ll = j
	print(s)


from sys import stdin, stdout
from math import *
input = stdin.buffer.readline
t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	b = []
	l = 0
	r = 1
	for i in range(1, n):
		if a[i] > 0 and a[i - 1] < 0 or (a[i] < 0 and a[i - 1] > 0):
			r = i
			b.append(max(a[l:r]))
			l = r
	b.append(max(a[l:n]))
	print(sum(b))
	t -= 1


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
from collections import deque
import collections

def main():
	for _ in range(int(input())):
		n = ri()
		a = ria()
		a.append(a[n - 1] * -1)
		k = 0
		maxp = 1
		maxn = -9999999999999999999
		ts = 0
		pos = 0
		neg = 0
		for i in range(n):
			if a[i] < 0:
				neg += 1
				maxn = max(maxn, a[i])
				if a[i + 1] > 0:
					ts += maxn
					maxn = -9999999999999999999
			if a[i] > 0:
				pos += 1
				maxp = max(maxp, a[i])
				if a[i + 1] < 0:
					ts += maxp
					maxp = 1
		wi(ts)

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()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	i = 0
	while i < n:
		if a[i] > 0:
			m = float('-inf')
			while i < n and a[i] > 0:
				m = max(a[i], m)
				i += 1
			c += m
		elif a[i] < 0:
			m = float('-inf')
			while i < n and a[i] < 0:
				m = max(m, a[i])
				i += 1
			c += m
	print(c)


def sign(x):
	return x > 0
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	res = i = 0
	while i < n:
		maxi = arr[i]
		for j in range(i, n + 1):
			if j == n or sign(arr[j]) != sign(arr[i]):
				break
			maxi = max(maxi, arr[j])
		res += maxi
		i = j
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	qq = list(map(int, input().split()))
	ss = []
	test = False
	nn = 0
	mm = 0
	if qq[0] < 0:
		test = True
		nn = qq[0]
	else:
		mm = qq[0]
	for i in range(0, n):
		if test == False:
			if qq[i] > 0:
				mm = max(mm, qq[i])
				continue
			else:
				ss.append(mm)
				nn = qq[i]
				test = True
		elif qq[i] < 0:
			nn = max(nn, qq[i])
			continue
		else:
			ss.append(nn)
			mm = qq[i]
			test = False
	if test == True:
		ss.append(nn)
	else:
		ss.append(mm)
	print(sum(ss))


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	Q = [a[0]]
	for i in range(1, n):
		if Q[-1] * a[i] > 0:
			Q.append(a[i])
		else:
			ans += max(Q)
			Q.clear()
			Q.append(a[i])
	if Q:
		ans += max(Q)
	print(ans)
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


t = int(input())
lst = []
for i in range(t):
	input()
	lst.append([int(s) for s in input().split(' ')])
for i in range(len(lst)):
	a = [int(s) for s in lst[i]]
	b = [a[0]]
	for k in a[1:]:
		if k * b[-1] > 0:
			b[-1] = max(k, b[-1])
		else:
			b.append(k)
	print(sum(b))


import itertools
for _ in range(int(input())):
	n = input()
	group = itertools.groupby(map(int, input().split()), lambda x: x < 0)
	s = 0
	for (_, v) in group:
		s += max(list(v))
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	b = []
	c = []
	a = list(map(int, input().split()))
	if a[0] < 0:
		f = 0
	else:
		f = 1
	i = 0
	while i != n:
		if f == 0:
			if a[i] < 0:
				c.append(a[i])
				i = i + 1
			else:
				b.append(max(c))
				c = []
				f = 1
				pass
		if f == 1:
			if a[i] > 0:
				c.append(a[i])
				i = i + 1
			else:
				b.append(max(c))
				c = []
				f = 0
				pass
	if len(c) > 0:
		b.append(max(c))
	print(sum(b))


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


import math
for _ in range(int(input())):
	am = int(input())
	arr = list(map(int, input().split()))
	n = []
	fl = True if arr[0] > 0 else False
	l = []
	for i in range(am):
		if fl and arr[i] > 0 or (not fl and arr[i] < 0):
			l.append(arr[i])
		else:
			fl = not fl
			n.append(l)
			l = []
			l.append(arr[i])
	if l:
		n.append(l)
	for i in range(len(n)):
		n[i] = max(n[i])
	s = sum(n)
	print(s)


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	seq = [int(num) for num in input().split()]
	res = 0
	buff = seq[0]
	for j in range(1, n):
		if buff < 0 and seq[j] < 0 or (buff > 0 and seq[j] > 0):
			buff = max(buff, seq[j])
		else:
			res += buff
			buff = seq[j]
	res += buff
	ans.append(res)
print(*ans, sep='\n')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	i = 0
	flag = True
	s = 0
	while i < n:
		if a[i] > 0:
			m = a[i]
			while i < n and a[i] > 0:
				m = max(m, a[i])
				i += 1
			s += m
		else:
			m = a[i]
			while i < n and a[i] < 0:
				m = max(m, a[i])
				i += 1
			s += m
	print(s)


def check(x):
	if x < 0:
		return False
	return True
T = int(input())
while T:
	T -= 1
	N = int(input())
	A = list(map(int, input().split()))
	temp = A[:1]
	ans = []
	flag = check(A[0])
	for i in range(1, N):
		if check(A[i]) == flag:
			temp.append(A[i])
		else:
			ans.append(max(temp))
			temp = [A[i]]
			flag = not flag
	ans.append(max(temp))
	print(sum(ans))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	c = []
	d = []
	for i in range(n):
		if a[i] > 0:
			c.append(a[i])
			if len(d) > 0:
				b.append(d)
				d = []
		if a[i] < 0:
			d.append(a[i])
			if len(c) > 0:
				b.append(c)
				c = []
	if len(c) > 0:
		b.append(c)
		c = []
	if len(d) > 0:
		b.append(d)
		d = []
	s = 0
	for i in range(len(b)):
		s = s + max(b[i])
	print(s)


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	s = 0
	i = 0
	while 1:
		mass = []
		start = seq[i]
		while i < len(seq) and start * seq[i] > 0:
			mass.append(seq[i])
			i += 1
		s += max(mass)
		if i >= len(seq):
			break
	ans.append(s)
for el in ans:
	print(el)


t = int(input())
for i in range(t):
	length = int(input())
	ar = list(map(int, input().split(' ')))
	(p, n, s) = (0, 0, 0)
	arr = []
	for k in range(length):
		if k == 0:
			if ar[k] < 0:
				n = ar[k]
			else:
				p = ar[k]
		elif ar[k] < 0 and n < 0:
			if n > ar[k]:
				continue
			else:
				n = ar[k]
		elif ar[k] > 0 and n < 0:
			arr.append(n)
			n = 0
			p = ar[k]
		elif ar[k] < 0 and p > 0:
			arr.append(p)
			p = 0
			n = ar[k]
		elif p > ar[k]:
			continue
		else:
			p = ar[k]
	if p > 0:
		arr.append(p)
	if n < 0:
		arr.append(n)
	print(sum(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(i) for i in input().split(' ')]
	j = 0
	sum = 0
	while j < len(s):
		i = j
		cur_elem = s[i]
		while j < len(s) and s[i] * s[j] > 0:
			cur_elem = max(s[j], cur_elem)
			j += 1
		sum += cur_elem
	print(sum)


def altsum(L):
	n = len(L)
	s = 0
	i = 0
	j = 0
	while i < n:
		p = L[i]
		while j < n and L[i] * L[j] > 0:
			p = max(p, L[j])
			j += 1
		s += p
		i = j
	return s
t = int(input())
for i in range(0, t):
	n = int(input())
	L = list(map(int, input().split()))
	print(altsum(L))


t = int(input())
for _ in range(t):
	n = int(input())
	l = map(int, input().split())
	out = 0
	curr = 0
	for v in l:
		if (curr >= 0) ^ (v >= 0):
			out += curr
			curr = v
		else:
			curr = max(v, curr)
	print(out + curr)


import io, os
import sys
from atexit import register
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.stdout = io.BytesIO()
register(lambda : os.write(1, sys.stdout.getvalue()))
tokens = []
tokens_next = 0

def next_str():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = input().split()
		tokens_next = 0
	tokens_next += 1
	return tokens[tokens_next - 1]

def nextInt():
	return int(next_str())

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

def f(a, st):
	stNextSeg = next((i for i in range(st, len(a)) if a[i] * a[st] < 0), len(a))
	return (max(a[st:stNextSeg]), stNextSeg)
t = nextInt()
for _ in range(t):
	n = nextInt()
	a = [nextInt() for _ in range(n)]
	sum = 0
	i = 0
	while i < len(a):
		(curSegmentMax, nextInd) = f(a, i)
		sum += curSegmentMax
		i = nextInd
	print(sum)


import math
from functools import lru_cache
import itertools
T = int(input())
for t in range(T):
	N = int(input())
	a = list(map(int, input().split()))
	s = 0
	for (sign, g) in itertools.groupby(a, key=lambda x: x > 0):
		s += max(g)
	print(s)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	neg = 0
	pos = 0
	if a[0] > 0:
		pos = 1
	elif a[0] < 0:
		neg = 1
	ans = []
	remo = a[0]
	i = 0
	while i < n:
		if a[i] > 0 and pos == 1:
			remo = max(a[i], remo)
			pos = 1
			neg = 0
		elif a[i] < 0 and neg == 1:
			remo = max(a[i], remo)
			neg = 1
			pos = 0
		else:
			ans.append(remo)
			neg = 0
			pos = 0
			if a[i] > 0:
				pos = 1
			elif a[i] < 0:
				neg = 1
			remo = a[i]
		i += 1
	ans.append(remo)
	print(sum(ans))


for _ in range(int(input())):
	x = int(input())
	li = list(map(int, input().split()))
	(max1, ans) = (0, 0)
	for i in li:
		if i >= 1:
			max1 = max(max1, i)
		else:
			ans += max1
			max1 = 0
	if li[-1] > 0:
		ans += max1
	max1 = -9 * 10 ** 9
	for i in li:
		if i <= -1:
			max1 = max(max1, i)
		elif max1 != -9 * 10 ** 9:
			ans += max1
			max1 = -9 * 10 ** 9
	if li[-1] < 0 and max1 != -9 * 10 ** 9:
		ans += max1
	print(ans)


import sys, bisect
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	(s, mn, mp, flag1, flag2) = (0, 0, 0, 0, 0)
	for i in range(n):
		if A[i] < 0:
			if flag2 == 1:
				s = s + A[i]
				flag2 = 0
				(mn, mp) = (A[i], 0)
			else:
				flag1 = 1
				if A[i] > mn or mn == 0:
					s = s + A[i] - mn
					mn = A[i]
		elif A[i] > 0:
			if flag1 == 1:
				s = s + A[i]
				flag1 = 0
				(mp, mn) = (A[i], 0)
			else:
				flag2 = 1
				if A[i] > mp or mp == 0:
					s = s + A[i] - mp
					mp = A[i]
	print(s)


t = int(input())
sgn = 0
last = 0
summ = 0
for i in range(t):
	n = int(input())
	mass = list(map(int, input().split()))
	for j in range(n):
		if mass[j] > 0:
			if sgn == '+':
				if mass[j] > last:
					summ += mass[j] - last
					last = mass[j]
			elif sgn == '-':
				summ += mass[j]
				last = mass[j]
			elif sgn == 0:
				summ += mass[j]
				last = mass[j]
			sgn = '+'
		if mass[j] < 0:
			if sgn == '-':
				if mass[j] > last:
					summ += mass[j] - last
					last = mass[j]
			elif sgn == '+':
				summ += mass[j]
				last = mass[j]
			elif sgn == 0:
				summ += mass[j]
				last = mass[j]
			sgn = '-'
	print(summ)
	summ = 0
	sgn = 0
	last = 0


import sys
msg = sys.stdin.readlines()
l = []
for i in msg:
	l.append(list(map(int, i.split())))

def opp(a, b):
	if a > 0 and b < 0:
		return True
	elif a < 0 and b > 0:
		return True
	else:
		return False

def altsub(l):
	ls = []
	a = l[0]
	for i in l[1:]:
		if opp(a, i):
			ls.append(a)
			a = i
		elif a > 0 and i > a:
			a = i
		elif a < 0 and i > a:
			a = i
	ls.append(a)
	ans = sum_l(ls)
	return ans

def sum_l(l):
	a = 0
	for i in l:
		a += i
	return a
for i in range(l[0][0]):
	ls = l[2 * i + 2]
	print(altsub(ls))


def getsign(n):
	return n > 0
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	i = 0
	while i < n:
		j = i
		maxi = a[i]
		while j < n and getsign(a[i]) == getsign(a[j]):
			maxi = max(maxi, a[j])
			j = j + 1
		i = j - 1
		ans = ans + maxi
		i = i + 1
	print(ans)


TEST = int(input())
answers = []
for _ in range(TEST):
	n = int(input())
	arr = [int(x) for x in input().split()]
	max_sum = 0
	length = 1
	prov_max = arr[0]
	for i in range(1, len(arr)):
		if arr[i] * arr[i - 1] < 0:
			length += 1
			max_sum += prov_max
			prov_max = arr[i]
		else:
			prov_max = max(prov_max, arr[i])
	max_sum += prov_max
	answers.append(max_sum)
for ans in answers:
	print(ans)


from sys import stdin, stdout
from math import *
for __ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	ans = [ar[0]]
	for i in range(1, n):
		if ar[i] < 0:
			if ans[-1] < 0 and ar[i] > ans[-1]:
				ans[-1] = ar[i]
			elif ans[-1] > 0:
				ans.append(ar[i])
		elif ans[-1] > 0 and ar[i] > ans[-1]:
			ans[-1] = ar[i]
		elif ans[-1] < 0:
			ans.append(ar[i])
	print(sum(ans))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(lambda x: int(x), input().split()))
	i = 0
	sums = 0
	while i < n:
		(maximum, max1) = (0, 0)
		if i < n and a[i] < 0:
			max1 = a[i]
			while i < n and a[i] < 0:
				if a[i] > max1:
					max1 = a[i]
				i += 1
			sums = sums + max1
		if i < n and a[i] > 0:
			maximum = a[i]
			while i < n and a[i] > 0:
				if a[i] > maximum:
					maximum = a[i]
				i += 1
			sums = sums + maximum
	print(sums)


def main():
	for tin in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		s = a[0]
		if a[0] < 0:
			k = -1
		else:
			k = 1
		b = []
		for _ in range(1, n):
			if k == -1 and a[_] < 0 or (k == 1 and a[_] > 0):
				if s < a[_]:
					s = a[_]
			elif k == -1 and a[_] > 0 or (k == 1 and a[_] < 0):
				b.append(s)
				k *= -1
				s = a[_]
		b.append(s)
		print(sum(b))
main()


def alternate(sequence):
	sum = 0
	a = sequence[0]
	i = 1
	maximum = a
	while i < len(sequence):
		if a * sequence[i] > 0:
			maximum = max(maximum, sequence[i])
			i += 1
		else:
			sum += maximum
			a = sequence[i]
			maximum = a
	sum += maximum
	return sum
t = int(input())
for i in range(t):
	n = int(input())
	sequence = list(map(int, input().split()))
	print(alternate(sequence))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def solve(n, A):
	L = []
	prev = A[0]
	Tmp = []
	for a in A:
		if a * prev > 0:
			Tmp.append(a)
		else:
			L.append(Tmp)
			Tmp = [a]
		prev = a
	L.append(Tmp)
	ans = 0
	for T in L:
		ans += max(T)
	return ans

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = tuple(map(int, input().split()))
		ans = solve(n, A)
		sys.stdout.write(str(ans) + '\n')
main()


def solve(a, k, n):
	ans = 0
	i = 0
	while i < n:
		j = i
		if k == 0:
			max_pos = float('-inf')
			found = False
			while j < n and a[j] > 0:
				max_pos = max(max_pos, a[j])
				found = True
				j += 1
			k = (k + 1) % 2
			if found:
				ans += max_pos
			i = j
		else:
			max_negative = float('-inf')
			found = False
			while j < n and a[j] < 0:
				max_negative = max(max_negative, a[j])
				found = True
				j += 1
			k = (k + 1) % 2
			if found:
				ans += max_negative
			i = j
	return ans
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		ans = solve(a, 0, n)
	else:
		ans = solve(a, 1, n)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	m = 0
	s = 0
	prev = 0
	for i in a:
		if prev * i < 0:
			s += m
			m = 0
		if m == 0:
			m = i
		else:
			m = max(m, i)
		prev = i
	print(s + m)


for xyz in range(0, int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	p = []
	c = 0
	for i in range(0, n - 1):
		if l[i] < 0 and l[i + 1] >= 0:
			p.append(i + 1)
			c += 1
		elif l[i] >= 0 and l[i + 1] < 0:
			p.append(i + 1)
			c += 1
	if c == 0:
		print(max(l))
	else:
		ini = 0
		for i in p:
			s += max(l[ini:i])
			ini = i
		print(s + max(l[i:]))


t = int(input())
while t > 0:
	n = int(input())
	a = [int(ele) for ele in input().split()]
	b = []
	prev_taken = ''
	for i in range(len(a)):
		if i == 0:
			b.append(a[i])
			if a[i] > 0:
				prev_taken = 'pos'
			else:
				prev_taken = 'neg'
		elif a[i] > 0 and prev_taken == 'pos':
			if a[i] > b[-1]:
				b[-1] = a[i]
		elif a[i] < 0 and prev_taken == 'neg':
			if a[i] > b[-1]:
				b[-1] = a[i]
		elif a[i] < 0 and prev_taken == 'pos' or (a[i] > 0 and prev_taken == 'neg'):
			b.append(a[i])
			if a[i] > 0:
				prev_taken = 'pos'
			else:
				prev_taken = 'neg'
	print(sum(b))
	t -= 1


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	if N == 1:
		print(A[0])
	else:
		l = []
		l.append(A[0])
		suma = 0
		for i in range(1, N):
			r = A[i] * l[-1]
			if r > 0:
				l.append(A[i])
			else:
				suma += max(l)
				l = []
				l.append(A[i])
		suma += max(l)
		print(suma)


def solution(A, n):
	curr = [A[0]]
	ans = []
	for i in range(n - 1):
		if A[i + 1] > 0 and A[i] < 0 or (A[i + 1] < 0 and A[i] > 0):
			ans.append(max(curr))
			curr = [A[i + 1]]
		else:
			curr.append(A[i + 1])
	ans.append(max(curr))
	return sum(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split(' ')))
	print(solution(A, n))


def sign(n):
	if n < 0:
		return -1
	else:
		return 1
t = int(input())
for test in range(t):
	n = int(input())
	nums = [int(x) for x in input().split()]
	s = sign(nums[0])
	ans = 0
	cur_max = nums[0]
	for i in range(1, n):
		if sign(nums[i]) == s:
			cur_max = max(cur_max, nums[i])
		else:
			s = -1 * s
			ans += cur_max
			cur_max = max(-1000000000, nums[i])
	ans += cur_max
	print(ans)


from math import inf

def sol():
	n = int(input())
	nums = list(map(int, input().split()))
	ans = []
	prev = nums[0]
	for i in range(n):
		if nums[i] > 0 and prev < 0:
			ans.append(prev)
			prev = nums[i]
		elif nums[i] < 0 and prev > 0:
			ans.append(prev)
			prev = nums[i]
		else:
			prev = max(prev, nums[i])
	if len(ans) > 0:
		if prev > 0 and ans[-1] < 0:
			ans.append(prev)
		elif prev < 0 and ans[-1] > 0:
			ans.append(prev)
		return sum(ans)
	else:
		return prev
for _ in range(int(input())):
	print(sol())


from sys import stdin
input = stdin.readline
for ti in range(int(input().strip())):
	n = int(input().strip())
	a = [int(x) for x in input().strip().split()]
	s = 0
	prev = a[0]
	for ai in a:
		if (ai < 0) == (prev < 0):
			prev = max(prev, ai)
		else:
			s += prev
			prev = ai
	s += prev
	print(s)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	maxi = max(l)
	mini = min(l)
	if maxi < 0 or mini > 0:
		print(maxi)
	else:
		if l[0] > 0:
			f = -1
		else:
			f = 1
		s = 0
		c = 0
		i = 0
		l1 = []
		while i < n:
			if l[i] > 0 and f == -1:
				j = i
				mm = l[i]
				while j < n:
					if l[j] < 0:
						j -= 1
						break
					elif l[j] > mm:
						mm = l[j]
					j += 1
				s += mm
				i = j
				f = 1
				c += 1
			elif l[i] < 0 and f == 1:
				j = i
				mm = l[i]
				while j < n:
					if l[j] > 0:
						j -= 1
						break
					elif l[j] > mm:
						mm = l[j]
					j += 1
				s += mm
				i = j
				f = -1
				c += 1
			l1.append(mm)
			i += 1
		print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = []
	pos = int(a[0] > 0)
	act = a[0]
	for i in range(n):
		if int(a[i] > 0) ^ pos:
			pos = 1 - pos
			ans.append(act)
			act = a[i]
		else:
			act = max(act, a[i])
	ans.append(act)
	print(sum(ans))


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


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')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def abs(x):
	return x if x >= 0 else -x

def sumelem(x, start, end):
	sum = 0
	for i in range(start, end + 1):
		sum += x[i]
	return sum
for t in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	maxi = num[0]
	ans = 0
	li = []
	for i in range(1, n):
		if num[i] * num[i - 1] >= 0:
			maxi = max(maxi, num[i])
		else:
			ans += maxi
			maxi = num[i]
	ans += maxi
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	li = [int(k) for k in input().split()]
	p = 0
	n = 0
	j = 0
	n = len(li)
	sign = [0] * len(li)
	ans = 0
	x = -10 ** 10
	y = 0
	for i in range(len(li)):
		if li[i] > 0:
			sign[i] = 1
		else:
			sign[i] = -1
	while j < n:
		if sign[j] == -1:
			x = -10 ** 10
			while j < n and sign[j] != 1:
				x = max(x, li[j])
				j += 1
			if x < 0 and x != -10 ** 10:
				ans += x
		else:
			y = 0
			while j < n and sign[j] != -1:
				y = max(y, li[j])
				j += 1
			if y > 0 and y != 0:
				ans += y
	print(ans)


for testcase in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	length = 1
	positive = nums[0] > 0
	maxx = nums[0]
	total = 0
	for i in nums[1:]:
		posi = i > 0
		if posi == positive:
			length += 1
			if i > maxx:
				maxx = i
		else:
			positive = posi
			length = 1
			total += maxx
			maxx = i
	total += maxx
	print(total)


def task_1343_c():
	b = int(input())
	array = [int(num) for num in input().split()]
	maxPositive = 0
	minNegative = -10000000000
	res = 0
	for i in range(b):
		if array[i] < 0:
			if i != 0 and array[i - 1] >= 0:
				res += maxPositive
				maxPositive = 0
			minNegative = max(minNegative, array[i])
		elif i != 0 and array[i - 1] < 0:
			res += minNegative
			minNegative = -10000000000
		maxPositive = max(maxPositive, array[i])
	if minNegative == -10000000000:
		res += maxPositive
	else:
		res += maxPositive + minNegative
	print(res)
a = int(input())
for i in range(a):
	task_1343_c()


import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	maximum = -INF
	for j in range(n):
		if j == 0:
			maximum = a[j]
			continue
		if a[j] > 0 and a[j - 1] < 0 or (a[j] < 0 and a[j - 1] > 0):
			ans += maximum
			maximum = a[j]
		else:
			maximum = max(maximum, a[j])
	ans += maximum
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = []
	b = 0
	for i in range(n):
		if i == 0:
			s.append(a[i])
		elif i == n - 1:
			if s[0] > 0:
				if a[i] < 0:
					b += max(s) + a[i]
				elif a[i] > 0:
					s.append(a[i])
					b += max(s)
			if s[0] < 0:
				if a[i] > 0:
					b += max(s) + a[i]
				elif a[i] < 0:
					s.append(a[i])
					b += max(s)
		elif s[0] > 0:
			if a[i] < 0:
				b += max(s)
				s = []
				s.append(a[i])
			elif a[i] > 0:
				s.append(a[i])
		elif s[0] < 0:
			if a[i] < 0:
				s.append(a[i])
			elif a[i] > 0:
				b += max(s)
				s = []
				s.append(a[i])
	if n == 1:
		print(max(s))
	else:
		print(b)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(arr[0])
	else:
		if arr[0] < 0:
			flag = 1
		else:
			flag = 0
		i = 1
		ind = 0
		ls = []
		while i < n:
			if arr[i] < 0 and flag == 0:
				m = max(arr[ind:i])
				ls.append(m)
				flag = 1
				ind = i
				if i == n - 1:
					ls.append(arr[n - 1])
			elif arr[i] > 0 and flag == 1:
				flag = 0
				ls.append(max(arr[ind:i]))
				ind = i
				if i == n - 1:
					ls.append(arr[n - 1])
			elif i == n - 1:
				ls.append(max(arr[ind:]))
			i += 1
		print(sum(ls))


import sys
input = sys.stdin.readline
for test in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	A.append(0)
	num = 0
	maxx = 0
	ans = 0
	for a in A:
		if maxx * a > 0:
			maxx = max(maxx, a)
		else:
			num += 1
			ans += maxx
			maxx = a
	print(ans)


for i in range(int(input())):
	n = int(input())
	l = [int(j) for j in input().split()][:n]
	s = 0
	a = l[0]
	for j in range(1, n):
		if l[j] > a and l[j] < 0 and (a < 0):
			a = l[j]
		elif l[j] > a and l[j] > 0 and (a > 0):
			a = l[j]
		elif l[j] < 0 and a > 0:
			s = s + a
			a = l[j]
		elif l[j] > 0 and a < 0:
			s = s + a
			a = l[j]
	print(s + a)


n = int(input())
while n > 0:
	n -= 1
	t = int(input())
	arr = list(map(int, input().split()))
	stor = []
	l = 0
	while l < t:
		m = 0
		val = 0
		if arr[l] > 0:
			val = arr[l]
			k = l + 1
			l = l + 1
			while k < t and arr[k] > 0:
				if arr[k] > val:
					val = arr[k]
				l += 1
				m += 1
				k += 1
			stor.append(val)
		else:
			val = arr[l]
			k = l + 1
			l = l + 1
			while k < t and arr[k] < 0:
				if arr[k] > val:
					val = arr[k]
				l += 1
				m += 1
				k += 1
			stor.append(val)
	print(sum(stor))


def func(n, l):
	s = i = j = 0
	while i < n:
		j = i
		m1 = 0
		m2 = -1000000000
		while i < n and l[i] < 0:
			m2 = max(m2, l[i])
			i += 1
		if j != i:
			s += m2
		j = i
		while i < n and l[i] > 0:
			m1 = max(m1, l[i])
			i += 1
		if j != i:
			s += m1
	return s
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(func(n, l))


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] < 0:
		z = -1
	else:
		z = 1
	s = 0
	ss = a[0]
	for i in range(n):
		if a[i] < 0 and z == 1:
			s = s + ss
			z = -1
			ss = a[i]
		elif a[i] > 0 and z == -1:
			s = s + ss
			z = 1
			ss = a[i]
		elif ss < a[i]:
			ss = a[i]
			ii = i
	s = s + ss
	print(s)


num_cases = int(input())
for _ in range(num_cases):
	num = int(input())
	arr = [int(num) for num in input().split()]
	count = 0
	sum1 = 0
	sub_arr = []
	sign = arr[0] > 0
	for elt in arr:
		test = elt > 0
		if test == sign:
			sub_arr.append(elt)
			count += 1
		else:
			sum1 += max(sub_arr)
			sub_arr = [elt]
			sign = not sign
	print(sum1 + max(sub_arr))


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	j = 0
	k = 0
	lis = []
	for i in range(n - 1):
		if a[i] * a[i + 1] < 0:
			lis.append(a[j:i + 1])
			k = j
			j = i + 1
	if a[j] * a[n - 1] > 0:
		lis.append(a[j:])
	al = []
	for l in lis:
		al.append(max(l))
	print(sum(al))


def sgn(x):
	if x > 0:
		return 1
	return -1
R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
res = []
for _ in range(R2()):
	l = R2()
	a = R1()
	i = s = j = 0
	while i < l:
		cur = a[i]
		j = i
		while j < l and sgn(a[i]) == sgn(a[j]):
			cur = max(cur, a[j])
			j += 1
		s += cur
		i = j
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	mylist = list(map(int, input().split()))
	i = 0
	mini = mylist[0]
	my_sum = 0
	while i < n:
		mini = mylist[i]
		if mylist[i] < 0:
			while i < n and mylist[i] < 0:
				if mylist[i] > mini:
					mini = mylist[i]
				i += 1
		else:
			while i < n and mylist[i] > 0:
				if mylist[i] > mini:
					mini = mylist[i]
				i += 1
		my_sum += mini
	print(my_sum)


for _ in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	num.append(-num[n - 1])
	mx = num[0]
	summ = 0
	for i in range(1, n + 1):
		if num[i] * num[i - 1] > 0:
			mx = max(num[i], mx)
		else:
			summ += mx
			mx = num[i]
	print(summ)


t = int(input())
for _ in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	s = 0
	(prev_t, poss) = (None, [])
	for i in seq:
		if i < 0:
			t = 'n'
		else:
			t = 'p'
		if prev_t == None or t == prev_t:
			poss.append(i)
		else:
			s += max(poss)
			poss = [i]
		prev_t = t
	s += max(poss)
	print(s)


for t in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	total = 0
	(f, l) = (0, 0)
	prepos = True if li[0] > 0 else False
	for i in range(1, n):
		if li[i] < 0:
			if prepos:
				total = total + max(li[f:l + 1])
				f = i
				l = i
			else:
				l = i
			prepos = False
		else:
			if not prepos:
				total = total + max(li[f:l + 1])
				f = i
				l = i
			else:
				l = i
			prepos = True
	total = total + max(li[f:l + 1])
	print(total)


num_test_cases = int(input())
ns = []
lists = []
for i in range(num_test_cases):
	ns.append(int(input()))
	lists.append([int(s) for s in input().split(' ')])
for (n, a) in zip(ns, lists):
	b = [a[0]]
	for ai in a[1:]:
		if ai * b[-1] > 0:
			b[-1] = max(ai, b[-1])
		else:
			b.append(ai)
	print(sum(b))


for k in range(int(input())):
	n = int(input())
	lista = list(map(int, input().split()))
	flagpos = False
	flagneg = False
	trocafinal = False
	somatorio = 0
	maiorelemento = lista[0]
	if lista[0] > 0:
		flagpos = True
	else:
		flagneg = True
	for i in range(1, len(lista)):
		if lista[i] > 0:
			if flagpos:
				if lista[i] > maiorelemento:
					maiorelemento = lista[i]
			else:
				somatorio += maiorelemento
				maiorelemento = lista[i]
				flagpos = True
				flagneg = False
		elif flagneg:
			if lista[i] > maiorelemento:
				maiorelemento = lista[i]
		else:
			somatorio += maiorelemento
			maiorelemento = lista[i]
			flagpos = False
			flagneg = True
	print(somatorio + maiorelemento)


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 alternating_subsequence(arr):
	subseq = [arr[0]]
	cur_sign = None
	if arr[0] > 0:
		cur_sign = 'POS'
	else:
		cur_sign = 'NEG'
	for i in range(1, len(arr)):
		if arr[i] > 0:
			if cur_sign == 'NEG':
				subseq.append(arr[i])
				cur_sign = 'POS'
			elif arr[i] > subseq[-1]:
				subseq[-1] = arr[i]
		elif arr[i] < 0:
			if cur_sign == 'POS':
				subseq.append(arr[i])
				cur_sign = 'NEG'
			elif arr[i] > subseq[-1]:
				subseq[-1] = arr[i]
	max_sum = 0
	for item in subseq:
		max_sum = max_sum + item
	return max_sum
num_test_cases = inp()
for _ in range(num_test_cases):
	_ = inp()
	arr = inlt()
	result = alternating_subsequence(arr)
	print(result)


for _ in range(int(input())):
	a = int(input())
	b = [int(i) for i in input().split()]
	maxc = 0
	minc = 0
	ans = 0
	for i in range(a):
		if b[i] > 0:
			ans += minc
			minc = 0
			if maxc != 0 and maxc < b[i]:
				maxc = b[i]
			elif maxc == 0:
				maxc = b[i]
		else:
			ans += maxc
			maxc = 0
			if minc != 0 and minc < b[i]:
				minc = b[i]
			elif minc == 0:
				minc = b[i]
	print(ans + maxc + minc)


for _ in range(int(input())):
	int(input())
	a = [int(i) for i in input().split()]
	newa = []
	mxpos = 0
	mxneg = -1000000000.0
	positive_now = True
	for (i, ai) in enumerate(a):
		if ai > 0:
			if not positive_now:
				newa.append(mxneg)
			mxpos = max(mxpos, ai)
			positive_now = True
			mxneg = -1000000000.0
		if ai < 0:
			if positive_now:
				newa.append(mxpos)
			mxneg = max(mxneg, ai)
			positive_now = False
			mxpos = 0
	if positive_now:
		newa.append(mxpos)
	else:
		newa.append(mxneg)
	print(int(sum(newa)))


test = int(input())
for t in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	(last, res) = (a[0], 0)
	for i in range(1, n):
		if last >= 0 and a[i] >= 0 or (last <= 0 and a[i] <= 0):
			last = max(last, a[i])
		else:
			res += last
			last = a[i]
	res += last
	print(res)


import math
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	mx = -math.inf
	sum = 0
	i = 0
	j = 0
	while j < n:
		mx = -math.inf
		while j < n and (a[i] > 0 and a[j] > 0 or (a[i] < 0 and a[j] < 0)):
			mx = max(mx, a[j])
			j += 1
		i = j
		sum += mx
	print(sum)
	t -= 1


num_tests = int(input())
answers = []
for test in range(num_tests):
	length = int(input())
	arr = list(map(int, input().split()))
	answers.append([])
	next_wanted = None
	curr = None
	for num in arr:
		if next_wanted == None:
			curr = num
			next_wanted = not num > 0
		elif next_wanted:
			if num < 0:
				curr = max(curr, num)
			else:
				answers[-1].append(curr)
				curr = num
				next_wanted = not next_wanted
		elif num > 0:
			curr = max(curr, num)
		else:
			answers[-1].append(curr)
			curr = num
			next_wanted = not next_wanted
	answers[-1].append(curr)
for answer in answers:
	print(sum(answer))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	if l[0] >= 0:
		f = 0
		ma = l[0]
		c += 1
	else:
		f = 1
		ma = l[0]
		c += 1
	ans = 0
	siv = 0
	for i in range(1, len(l)):
		if f == 1:
			if l[i] >= 0:
				ans += ma
				ma = l[i]
				if i == len(l) - 1:
					ans += ma
					siv = 1
				f = 0
			else:
				ma = max(ma, l[i])
				c += 1
		elif l[i] >= 0:
			ma = max(ma, l[i])
			c += 1
		else:
			ans += ma
			ma = l[i]
			if i == len(l) - 1:
				siv = 1
				ans += ma
			f = 1
	if c == len(l):
		print(ma)
	elif siv == 1:
		print(ans)
	else:
		print(ans + ma)


import math
test = int(input())
i = 1
while i <= test:
	n = int(input())
	arr = list(map(int, input().split()))
	sn = []
	en = []
	sp = []
	ep = []
	ng = []
	ps = []
	neg = True
	if arr[0] > 0:
		neg = False
	j = 0
	while j < n:
		if neg:
			max = float('-inf')
			sn.append(j)
			while j < n and arr[j] < 0:
				if max < arr[j]:
					max = arr[j]
				j += 1
			en.append(j - 1)
			ng.append(max)
			neg = False
		if not neg and j < n:
			max = 0
			sp.append(j)
			while j < n and arr[j] > 0:
				if max < arr[j]:
					max = arr[j]
				j += 1
			ep.append(j - 1)
			ps.append(max)
			neg = True
	temp = []
	res = min(len(sn), len(sp))
	pos = res
	neg = res
	if len(sp) == len(sn):
		res *= 2
	elif res == len(sp):
		res *= 2
		res += 1
		neg += 1
	else:
		res *= 2
		res += 1
		pos += 1
	count = 0
	count2 = 0
	sum = 0
	if len(sn) == 0:
		print(ps[0])
	elif len(sp) == 0:
		print(ng[0])
	elif sn[0] < sp[0]:
		while count < len(sn) and count2 < len(sp):
			sum += ng[count]
			count += 1
			sum += ps[count2]
			count2 += 1
		if count < len(sn):
			sum += ng[count]
		print(sum)
	else:
		while count < len(sn) and count2 < len(sp):
			sum += ps[count2]
			count2 += 1
			sum += ng[count]
			count += 1
		if count2 < len(ps):
			sum += ps[count2]
		print(sum)
	i += 1


for t in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	goodlst = []
	bolsh = True
	first = True
	for i in lst:
		if first:
			first = False
			goodlst.append(i)
			bolsh = i > 0
		elif bolsh == (i > 0):
			goodlst[-1] = max(goodlst[-1], i)
		else:
			goodlst.append(i)
			bolsh = not bolsh
	print(sum(goodlst))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	cur = arr[0]
	s = 0
	for i in range(1, n):
		if arr[i] < 0 and cur > 0 or (arr[i] > 0 and cur < 0):
			s += cur
			cur = arr[i]
			continue
		cur = max(arr[i], cur)
	s += cur
	print(s)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
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()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	arr = iia()
	res = []
	cur = None
	for i in arr:
		if i > 0:
			if cur and cur < 0:
				res.append(cur)
				cur = None
			if not cur:
				cur = i
			else:
				cur = max(cur, i)
		else:
			if cur and cur > 0:
				res.append(cur)
				cur = None
			if not cur:
				cur = i
			else:
				cur = max(cur, i)
	if cur:
		res.append(cur)
	print(sum(res))


te = int(input())
while te:
	n = int(input())
	l = list(map(int, input().split()))
	t = []
	(start, end) = (0, 0)
	while end < n:
		if l[start] * l[end] > 0:
			end += 1
		else:
			t.append(max(l[start:end]))
			start = end
	t.append(max(l[start:end]))
	print(sum(t))
	te -= 1


for _ in range(int(input())):
	n = int(input())
	x = [int(w) for w in input().split()]
	y = []
	prev = 0
	for i in x:
		if prev < 0 and i > 0 or (prev > 0 and i < 0) or prev == 0:
			y.append([i])
		else:
			y[-1].append(i)
		prev = i
	ans = 0
	for i in y:
		ans += max(i)
	print(ans)


t = int(input())
for loop in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		state = 0
	else:
		state = 1
	nmax = float('-inf')
	ans = 0
	for i in range(n):
		if state == 0:
			if a[i] > 0:
				nmax = max(nmax, a[i])
			else:
				ans += nmax
				state = 1
				nmax = a[i]
		elif a[i] < 0:
			nmax = max(nmax, a[i])
		else:
			ans += nmax
			state = 0
			nmax = a[i]
	ans += nmax
	print(ans)


numTest = int(input())

def test():
	n = int(input())
	data = list(map(int, input().split(' ')))
	sum = 0
	start = 0
	nxt = 0
	maxVal = data[start]
	while nxt < n:
		if data[nxt] * data[start] < 0:
			start = nxt
			sum += maxVal
			maxVal = data[start]
		elif data[nxt] >= maxVal:
			maxVal = data[nxt]
		nxt += 1
	sum += maxVal
	print(sum)
for _ in range(0, numTest):
	test()


t = int(input())
for tt in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	mx = arr[0]
	sm = 0
	for j in range(n):
		if arr[j] * mx < 0:
			sm += mx
			mx = arr[j]
		else:
			mx = max(mx, arr[j])
	print(sm + mx)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	IND = []
	for j in range(1, len(l)):
		if abs(l[j] + l[j - 1]) < abs(l[j]) + abs(l[j - 1]):
			IND.append(j)
	if len(IND) == 0:
		print(max(l))
		continue
	BABA = []
	BABA.append(l[:IND[0]])
	for j in range(1, len(IND)):
		BABA.append(l[IND[j - 1]:IND[j]])
	BABA.append(l[IND[-1]:])
	JAX = []
	for j in range(len(BABA)):
		JAX.append(max(BABA[j]))
	print(sum(JAX))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	buf = [a[0]]
	for (ai_, ai) in zip(a, a[1:]):
		if ai_ // abs(ai_) == ai // abs(ai):
			buf.append(ai)
			continue
		s += max(buf)
		buf = [ai]
	s += max(buf)
	print(s)


t = int(input())
for i in range(t):
	N = int(input())
	row = [int(g) for g in input().split(' ')]
	if row[0] > 0:
		p_n = 1
		current = 0
	else:
		p_n = 0
		current = -float('inf')
	total = 0
	for i in range(N):
		if (row[i] > 0) == p_n:
			current = max(current, row[i])
		else:
			total += current
			p_n = (p_n + 1) % 2
			current = row[i]
	total += current
	print(total)


def allpositve(arr):
	for i in range(len(arr)):
		if arr[i] < 0:
			return 0
	return 1

def allnegative(arr):
	for i in range(len(arr)):
		if arr[i] > 0:
			return 0
	return 1
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	l = [0 for i in range(len(arr))]
	if allpositve(arr):
		print(max(arr))
	elif allnegative(arr):
		print(max(arr))
	else:
		positive = 0
		negative = 0
		prev = 0
		s = 0
		val = 0
		if arr[0] > 0:
			prev = 1
			positive = arr[0]
			p = 1
		else:
			prev = 0
			negative = arr[0]
			p = 0
		for i in range(1, len(arr)):
			if arr[i] > 0 and prev == 0:
				positive = negative + arr[i]
				prev = 1
			if arr[i] > 0 and prev == 1:
				positive = max(positive, negative + arr[i])
			if arr[i] < 0 and prev == 0:
				negative = max(negative, positive + arr[i])
			if arr[i] < 0 and prev == 1:
				negative = positive + arr[i]
				prev = 0
		if arr[n - 1] > 0:
			print(positive)
		else:
			print(negative)


inp = int(input())
for k in range(inp):
	s = int(input())
	array = list(map(int, input().strip(' ').split(' ')))
	i = 0
	sum1 = 0
	while i < s:
		d = 0
		i = i
		while i < s and array[i] > 0:
			d = max(array[i], d)
			i += 1
		sum1 += d
		if i < s:
			d = array[i]
		else:
			break
		while i < s and array[i] < 0:
			d = max(array[i], d)
			i += 1
		sum1 += d
	print(sum1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	answer = 0
	i = 0
	while i < n:
		mpos = 0
		mneg = -1000000001
		inpos = False
		inneg = False
		while i < n and arr[i] > 0:
			if arr[i] > mpos:
				mpos = arr[i]
				inpos = True
			i += 1
		if inpos:
			answer += mpos
		while i < n and arr[i] < 0:
			if arr[i] > mneg:
				mneg = arr[i]
				inneg = True
			i += 1
		if inneg:
			answer += mneg
	print(answer)


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
while t:
	t -= 1
	n = int(inp().strip())
	a = [int(k) for k in inp().strip().split()]
	lastvalue = a[0]
	maxSum = 0
	for num in a:
		if lastvalue > 0:
			if num > 0:
				if num > lastvalue:
					lastvalue = num
			else:
				maxSum += lastvalue
				lastvalue = num
		elif num > 0:
			maxSum += lastvalue
			lastvalue = num
		elif num > lastvalue:
			lastvalue = num
	maxSum += lastvalue
	print(maxSum)


import sys
input = sys.stdin.readline
power = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	alt = [arr[0]]
	for j in range(1, len(arr)):
		if arr[j] > 0 and alt[-1] < 0 or (arr[j] < 0 and alt[-1] > 0):
			alt.append(arr[j])
		elif arr[j] > 0 and alt[-1] > 0:
			alt[-1] = max(alt[-1], arr[j])
		elif arr[j] < 0 and alt[-1] < 0:
			alt[-1] = max(alt[-1], arr[j])
	print(sum(alt))


t = int(input())
for _ in range(t):
	n = int(input())
	v = list(map(int, input().split(' ')))
	old = 0
	p_sum = 0
	for k in range(n):
		if old == 0:
			old = v[k]
			if k == n - 1:
				p_sum += old
		elif old * v[k] < 0:
			p_sum += old
			old = v[k]
			if k == n - 1:
				p_sum += old
		else:
			if v[k] > old:
				old = v[k]
			if k == n - 1:
				p_sum += old
	print(p_sum)


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


for _ in range(int(input())):
	c = 0
	p1 = 0
	p2 = -10 ** 9
	c1 = 0
	c2 = 0
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(len(l)):
		if l[i] > 0:
			c1 += 1
			if l[i] > p1:
				p1 = l[i]
			if c2 > 0:
				c += p2
				p2 = -10 ** 9
			c2 = 0
		elif l[i] < 0:
			c2 += 1
			if l[i] > p2:
				p2 = l[i]
			if c1 > 0:
				c += p1
				p1 = 0
			c1 = 0
	if c1 != 0:
		c += p1
	if c2 != 0:
		c += p2
	print(c)


tests = int(input())
while tests > 0:
	tests -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	maxAmt = [0] * 2
	maxSum = [0] * 2
	for i in range(n - 1, -1, -1):
		nAmt = [0] * 2
		nSum = [0] * 2
		for j in range(0, 2):
			nAmt[j] = maxAmt[j]
			nSum[j] = maxSum[j]
		my = 0
		if arr[i] < 0:
			my = 1
		if 1 + maxAmt[my ^ 1] > nAmt[my]:
			nAmt[my] = 1 + maxAmt[my ^ 1]
			nSum[my] = arr[i] + maxSum[my ^ 1]
		elif 1 + maxAmt[my ^ 1] == nAmt[my] and arr[i] + maxSum[my ^ 1] > nSum[my]:
			nSum[my] = arr[i] + maxSum[my ^ 1]
		maxAmt = nAmt
		maxSum = nSum
	ans = maxSum[0]
	if maxAmt[1] > maxAmt[0]:
		ans = maxSum[1]
	elif maxAmt[1] == maxAmt[0]:
		ans = max(ans, maxSum[1])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lst = [int(i) for i in input().split()]
	nd = 0
	if lst[0] < 0:
		nd = 1
	else:
		nd = -1
	lst2 = []
	sum1 = 0
	for i in range(0, n):
		if nd == 1:
			if lst[i] > 0:
				lst2.append(lst[i])
			else:
				nn = len(lst2)
				if nn != 0:
					sum1 = sum1 + max(lst2)
				lst2 = []
				lst2.append(lst[i])
				nd = 0
		elif lst[i] < 0:
			lst2.append(lst[i])
		else:
			nn = len(lst2)
			if nn != 0:
				sum1 = sum1 + max(lst2)
			lst2 = []
			lst2.append(lst[i])
			nd = 1
	n = len(lst2)
	if n == 0:
		print(sum1)
	elif lst[0] > 0:
		print(sum1 + max(lst2))
	else:
		print(sum1 + max(lst2))


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	prev = arr[0]
	ans = 0
	for elem in arr[1:]:
		if prev ^ elem < 0:
			ans += prev
			prev = elem
		elif elem > prev:
			prev = elem
	ans += prev
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		pos = a[0]
		flag = True
		ans_sum = a[0]
	elif a[0] < 0:
		neg = a[0]
		flag = False
		ans_sum = a[0]
	for j in range(1, n):
		if a[j] > 0:
			if flag == True:
				if a[j] > pos:
					ans_sum = ans_sum - pos
					pos = a[j]
					ans_sum = ans_sum + a[j]
			else:
				pos = a[j]
				ans_sum = ans_sum + a[j]
				flag = True
		elif flag == False:
			if a[j] > neg:
				ans_sum = ans_sum - neg
				neg = a[j]
				ans_sum = ans_sum + a[j]
		else:
			neg = a[j]
			ans_sum = ans_sum + a[j]
			flag = False
	print(ans_sum)


def f(arr):
	s = 0
	i = 0
	while i < n:
		j = i + 1
		m = arr[i]
		while j < n and arr[j] * arr[i] > 0:
			m = max(m, arr[j])
			j += 1
		s += m
		i = j
	return s
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().strip().split()))
	print(f(l))


t = int(input())
Ans = ''
for p in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	last = a[0]
	best = last
	for num in a:
		if last > 0:
			if num < 0:
				best += num
				last = num
			elif num > last:
				best = best - last + num
				last = num
		if last < 0:
			if num > 0:
				best += num
				last = num
			elif num > last:
				best = best - last + num
				last = num
	Ans += str(best) + '\n'
print(Ans)


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


for _ in range(int(input())):
	n = int(input())
	T = [*map(int, input().split())]
	s = 0
	i = 0
	while i < n:
		if T[i] < 0:
			m = T[i]
			while i + 1 < n and T[i + 1] < 0:
				i += 1
				m = max(m, T[i])
		elif T[i] > 0:
			m = T[i]
			while i + 1 < n and T[i + 1] > 0:
				i += 1
				m = max(m, T[i])
		s += m
		i += 1
	print(s)


import os
import sys
from math import *
from io import BytesIO, IOBase

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

def array():
	return [int(i) for i in input().split()]
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())
	a = array()
	i = 0
	ans = 0
	ans = []
	while i < n:
		if a[i] > 0:
			ma = a[i]
			while i < n and a[i] > 0:
				ma = max(ma, a[i])
				i += 1
		elif a[i] < 0:
			ma = a[i]
			while i < n and a[i] < 0:
				ma = max(ma, a[i])
				i += 1
		ans.append(ma)
	print(sum(ans))


def main():
	t = int(input())
	for c in range(t):
		n = int(input())
		arr = [int(s) for s in input().split(' ')]
		print(solve(arr, n))

def sgn(n):
	return n > 0

def solve(arr, n):
	s = []
	i = 0
	while i < n - 1:
		if sgn(arr[i]) != sgn(arr[i + 1]):
			s.append(i)
		i += 1
	if len(s) == 0:
		return max(arr)
	max_sum = 0
	max_sum += max(arr[:s[0] + 1])
	max_sum += max(arr[s[len(s) - 1] + 1:])
	if len(s) > 1:
		for i in range(len(s) - 1):
			max_sum += max(arr[s[i] + 1:s[i + 1] + 1])
	return max_sum
main()


import sys
input = sys.stdin.readline
m = 1000000007

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

def main():
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		c = -1000000001
		l1 = []
		l2 = []
		m2 = 0
		m1 = c
		f = 0
		for i in range(n):
			if l[i] < 0:
				if l[i] >= m1:
					m1 = l[i]
				if m2 != 0:
					l2.append(m2)
				m2 = 0
				f = 1
			else:
				if l[i] > m2:
					m2 = l[i]
				if m1 != c:
					l1.append(m1)
				m1 = c
				f = 2
		if f == 2:
			l2.append(m2)
		if f == 1:
			l1.append(m1)
		print(sum(l1) + sum(l2))
main()


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


for _ in [0] * int(input()):
	(p, q) = (0, 0)
	n = int(input())
	ll = list(map(int, input().split()))
	res = []
	for q in range(1, n + 1):
		if q < n and (ll[p] >= 0 and ll[q] >= 0 or (ll[p] <= 0 and ll[q] <= 0)):
			if ll[p] < ll[q]:
				p = q
		else:
			res.append(ll[p])
			p = q
	print(sum(res))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	res = [l[0]]
	total = 0
	for i in l[1:]:
		if i > 0 and res[-1] < 0 or (i < 0 and res[-1] > 0):
			total += max(res)
			res.clear()
			res.append(i)
		else:
			res.append(i)
	print(total + max(res))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	aux = a[0]
	sum = 0
	for num in a:
		if num * aux > 0:
			aux = max(num, aux)
		else:
			sum += aux
			aux = num
	sum += aux
	print(sum)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	new_seq = []
	i = 0
	j = i
	if arr[j] < 0:
		temp = []
		while j < n and arr[j] < 0:
			temp.append(arr[j])
			j += 1
		new_seq.append(max(temp))
	else:
		temp = []
		while j < n and arr[j] > 0:
			temp.append(arr[j])
			j += 1
		new_seq.append(max(temp))
	i = j
	while True:
		if new_seq[-1] < 0:
			temp = []
			while j < n and arr[j] > 0:
				temp.append(arr[j])
				j += 1
			if temp == []:
				break
			new_seq.append(max(temp))
		else:
			temp = []
			while j < n and arr[j] < 0:
				temp.append(arr[j])
				j += 1
			if temp == []:
				break
			new_seq.append(max(temp))
	print(sum(new_seq))


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

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

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

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		arr = L()
		(sm, v) = (0, arr[0])
		flag = 0
		if arr[0] < 0:
			flag = 0
		else:
			flag = 1
		for i in range(1, n):
			if flag and arr[i] < 0:
				sm += v
				flag = 0
				v = arr[i]
			elif flag and arr[i] > 0:
				v = max(v, arr[i])
			elif not flag and arr[i] < 0:
				v = max(v, arr[i])
			else:
				sm += v
				flag = 1
				v = arr[i]
		sm += v
		print(sm)
main()


for _ in range(int(input())):
	n = int(input())
	List = list(map(int, input().split()))
	ans = []
	ans.append(List[0])
	for x in List:
		if ans[-1] < 0 and x < 0:
			cum = ans[-1]
			ans.pop()
			ans.append(max(cum, x))
		elif ans[-1] < 0 and x > 0:
			ans.append(x)
		elif ans[-1] > 0 and x < 0:
			ans.append(x)
		elif ans[-1] > 0 and x > 0:
			cum = ans[-1]
			ans.pop()
			ans.append(max(cum, x))
	print(sum(ans))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	po = []
	ne = []
	ans = []
	for i in range(n):
		if i == n - 1:
			if l[i] > 0:
				po.append(l[i])
				ans.append(max(po))
			else:
				ne.append(l[i])
				ans.append(max(ne))
			break
		if l[i] > 0 and l[i + 1] > 0:
			po.append(l[i])
		elif l[i] > 0 and l[i + 1] < 0:
			po.append(l[i])
			ans.append(max(po))
			po = []
		elif l[i] < 0 and l[i + 1] < 0:
			ne.append(l[i])
		else:
			ne.append(l[i])
			ans.append(max(ne))
			ne = []
	print(sum(ans))


from sys import stdin as inp
from sys import stdout as out
t = int(inp.readline())
for i in range(t):
	n = int(inp.readline())
	a = list(map(int, inp.readline().split()))
	f = None
	if a[0] < 0:
		f = False
	else:
		f = True
	m = a[0]
	s = 0
	for i in a[1:]:
		if f == False and i < 0:
			m = max(m, i)
		elif f == True and i > 0:
			m = max(i, m)
		else:
			s += m
			m = i
			f = f ^ True
	s += m
	out.write(str(s) + '\n')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = -10 ** 10
	c = 0
	for i in range(0, n):
		if not i:
			if a[i] > 0:
				s = 1
				m = max(m, a[i])
			else:
				s = -1
				m = max(m, a[i])
		elif s == 1 and a[i] < 0:
			c += m
			s = -1
			m = a[i]
		elif s == -1 and a[i] > 0:
			c += m
			s = 1
			m = a[i]
		else:
			m = max(a[i], m)
	c += m
	print(c)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	temp = []
	i = 0
	while i < n:
		if s[i] < 0:
			mx = s[i]
			while i < n and s[i] < 0:
				if abs(s[i]) < abs(mx):
					mx = s[i]
				i += 1
			temp.append(mx)
		else:
			mx = s[i]
			while i < n and s[i] > 0:
				if s[i] > mx:
					mx = s[i]
				i += 1
			temp.append(mx)
	print(sum(temp))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		(c, j) = (l[i], i)
		while j < n and l[i] / abs(l[i]) == l[j] / abs(l[j]):
			c = max(c, l[j])
			j += 1
		s += c
		i = j - 1
		i += 1
	print(s)


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(arr[0])
		continue
	if arr[0] < 0:
		curr = 0
	else:
		curr = 1
	k = 0
	segments = [[arr[0]]]
	for i in range(1, n):
		if curr == 0 and arr[i] < 0 or (curr == 1 and arr[i] > 0):
			segments[k].append(arr[i])
		else:
			segments.append([arr[i]])
			k += 1
			if arr[i] < 0:
				curr = 0
			else:
				curr = 1
	maxSum = 0
	for j in segments:
		maxSum += max(j)
	print(maxSum)


def getMax(s):
	summ = 0
	i = 0
	while i < len(s):
		(temp, r1, r2) = (s[i], False, False)
		while i < len(s) and s[i] < 0:
			r1 = True
			if s[i] > temp:
				temp = s[i]
			i += 1
		if r1:
			summ += temp
		while i < len(s) and s[i] > 0:
			r2 = True
			if s[i] > temp:
				temp = s[i]
			i += 1
		if r2:
			summ += temp
	return summ
n = int(input(''))
for arr in range(0, n):
	_ = input('')
	array = input('').split()
	print(getMax([int(i) for i in array]))


t = int(input())
for _ in range(t):
	a = int(input())
	b = list(map(int, input().split(' ')))
	maxm = b[0]
	ans = 0
	for i in range(1, a):
		if b[i] * b[i - 1] < 0:
			ans += maxm
			maxm = float('-inf')
		maxm = max(maxm, b[i])
	ans += maxm
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	sm = 0
	previous = arr[0]
	for i in range(1, len(arr)):
		v = arr[i]
		if v > 0 and previous > 0 or (v < 0 and previous < 0):
			previous = max(v, previous)
		else:
			sm += previous
			previous = v
	print(sm + previous)


test_cases = int(input())
data = []
while test_cases > 0:
	length = input()
	data.append(list(map(int, input().split())))
	test_cases -= 1
for i in range(0, len(data)):
	data[i].append(0)
	maxLength = 1
	for j in range(0, len(data[i]) - 1):
		if data[i][j] < 0 and data[i][j + 1] > 0 or (data[i][j] > 0 and data[i][j + 1] < 0):
			maxLength += 1
	travel = 0
	maxAdd = data[i][0]
	maxSum = 0
	while maxLength > 0:
		if data[i][travel] > 0 and data[i][travel + 1] > 0 or (data[i][travel] < 0 and data[i][travel + 1] < 0):
			maxAdd = max(maxAdd, data[i][travel + 1])
			travel += 1
		else:
			maxSum += maxAdd
			maxLength -= 1
			travel += 1
			maxAdd = data[i][travel]
	print(maxSum)


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	firsts = [0]
	mem = round(a[0] / abs(a[0]))
	for i in range(n):
		if mem * a[i] < 0:
			mem *= -1
			firsts.append(i)
	breaks = []
	for i in range(len(firsts) - 1):
		breaks.append(a[firsts[i]:firsts[i + 1]])
	breaks.append(a[firsts[-1]:len(a)])
	q = 0
	for x in breaks:
		q += max(x)
	print(q)


m = 10 ** 9 + 7
t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	i = 0
	while i < n:
		lmax = 0
		if i < n and l[i] > 0:
			while i < n and l[i] > 0:
				if l[i] > lmax:
					lmax = l[i]
				i += 1
			ans += lmax
		if i < n and l[i] < 0:
			lmax = -10 ** 9
			while i < n and l[i] < 0:
				if l[i] > lmax:
					lmax = l[i]
				i += 1
			ans += lmax
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = 0
	start = 0
	end = 0
	l = []
	if a[0] < 0:
		last = -1
	else:
		last = 1
	for i in range(1, n):
		if a[i] * last > 0:
			continue
		elif last == 1:
			l.append(max(a[start:i]))
			start = i
			last *= -1
		else:
			l.append(max(a[start:i]))
			start = i
			last *= -1
	if last == 1:
		l.append(max(a[start:n]))
	else:
		l.append(max(a[start:n]))
	print(sum(l))


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

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

def fi():
	return float(stdin.readline())

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

def fmi():
	return map(float, stdin.readline().split())

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

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()
res = ['YES', 'NO']
t_c = ii()
for _ in range(t_c):
	n = ii()
	a = li()
	s = 0
	f = 1
	if a[0] < 0:
		f = 0
	x = [a[0]]
	for i in a[1:]:
		if f and i > 0 or (not f and i < 0):
			x.append(i)
		else:
			s += max(x)
			x = [i]
			if f:
				f = 0
			else:
				f = 1
	if x:
		s += max(x)
	print(s)


t = int(input())
for _ in range(t):
	input()
	a = list(map(int, input().split()))
	pos = a[0] > 0
	result = 0
	cur_max = a[0]
	for aa in a:
		if pos == (aa > 0):
			if aa > cur_max:
				cur_max = aa
		else:
			pos = not pos
			result += cur_max
			cur_max = aa
	result += cur_max
	print(result)


def solve(n, arr):
	li = []
	i = 0
	while i < n:
		a = [arr[i]]
		j = i + 1
		while j < n:
			if arr[i] * arr[j] > 0:
				a.append(arr[j])
				j += 1
			else:
				li.append(a)
				break
		else:
			li.append(a)
		i = j
	summ = 0
	for i in range(len(li)):
		summ += max(li[i])
	return summ
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


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


import sys
from collections import defaultdict as dd
import math

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(data())
	a = l()
	(k, d, c) = (0, 0, a[0])
	if a[0] < 0:
		k = 1
	for i in range(1, n):
		if a[i] > 0:
			if k == 0:
				c = max(a[i], a[i - 1], c)
			else:
				d += c
				k = 0
				c = a[i]
		elif k == 1:
			c = max(a[i], a[i - 1], c)
		else:
			d += c
			k = 1
			c = a[i]
	print(d + c)


for x in range(int(input())):
	n = int(input())
	lista = list(map(int, input().split()))
	ant = lista[0]
	cont = 1
	sol = [lista[0]]
	for i in lista:
		if i * ant < 0:
			cont = cont + 1
			sol.append(i)
		elif sol[-1] < i:
			sol[-1] = i
		ant = i
	sum = 0
	for i in sol:
		sum = sum + i
	print(sum)


from itertools import takewhile
t = int(input())
for i in range(t):
	element_quantity = int(input())
	a_arr = [int(x) for x in input().split()]
	left_idx = 0
	max_sum = 0
	while left_idx < len(a_arr):
		is_neg = True if a_arr[left_idx] < 0 else False
		right_idx = left_idx
		if is_neg:
			while right_idx < len(a_arr) and a_arr[right_idx] < 0:
				right_idx += 1
		else:
			while right_idx < len(a_arr) and a_arr[right_idx] >= 0:
				right_idx += 1
		max_sum += max(a_arr[left_idx:right_idx])
		left_idx = right_idx
	print(max_sum)


t = int(input())
for tt in range(t):
	n = int(input())
	lis = input().split()
	for i in range(n):
		lis[i] = int(lis[i])
	final_answer = []
	best_yet = 0
	for i in range(n):
		if best_yet == 0:
			best_yet = lis[i]
		elif best_yet > 0:
			if lis[i] > 0:
				best_yet = max(best_yet, lis[i])
			else:
				final_answer.append(best_yet)
				best_yet = lis[i]
		elif lis[i] < 0:
			best_yet = max(best_yet, lis[i])
		else:
			final_answer.append(best_yet)
			best_yet = lis[i]
	final_answer.append(best_yet)
	print(sum(final_answer))


from math import *
from copy import *
from heapq import *
from string import *
from random import *
from bisect import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
from collections import defaultdict

def solve(l):
	n = len(l)
	maxxK = 1
	prev = -1
	ans = 0
	(minn, maxx) = (-maxsize, -maxsize)
	(pos, neg) = (0, 0)
	if l[0] > 0:
		prev = 1
		pos += 1
		maxx = l[0]
	else:
		neg += 1
		minn = l[0]
	f = 0
	for i in range(1, n):
		f = 1
		if l[i] > 0:
			pos += 1
			if prev == -1:
				f = 0
				maxxK += 1
				prev = 1
				ans += minn
				maxx = l[i]
				minn = -maxsize
			else:
				maxx = max(maxx, l[i])
		else:
			neg += 1
			if prev == 1:
				f = 0
				maxxK += 1
				prev = -1
				ans += maxx
				minn = l[i]
				maxx = -maxsize
			else:
				minn = max(minn, l[i])
	if pos == 0:
		return max(l)
	elif neg == 0:
		return max(l)
	if prev == 1:
		ans += maxx
	else:
		ans += minn
	return ans
T = int(input())
for _ in range(T):
	N = int(stdin.readline())
	l = list(map(int, stdin.readline().rstrip().split()))
	print(solve(l))


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	arr = [-float('inf')] * (n + 5)
	arr[0] = max(arr[0], l[0])
	count = [0] * (n + 5)
	count[0] = 1
	if l[0] > 0:
		last = 1
	else:
		last = -1
	for i in range(1, n):
		if l[i] > 0 and last < 0 or (l[i] < 0 and last > 0):
			count[i] = count[i - 1] + 1
			last *= -1
		else:
			count[i] = count[i - 1]
		arr[count[i] - 1] = max(arr[count[i] - 1], l[i])
	x = count[n - 1]
	print(sum(arr[:x]))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	if a[0] < 0:
		p = -1
	else:
		p = 1
	mi = -10000000000
	ma = -1
	re = 0
	for i in range(n):
		if a[i] < 0:
			if p == -1:
				if a[i] > mi:
					mi = a[i]
			else:
				re += ma
				p = -1
				ma = -1
				if a[i] > mi:
					mi = a[i]
		if a[i] > 0:
			if p == -1:
				re += mi
				mi = -10000000000
				p = 1
				if a[i] > ma:
					ma = a[i]
			elif a[i] > ma:
				ma = a[i]
	if p < 0:
		re += mi
	else:
		re += ma
	print(re)


a = int(input())
for x in range(a):
	a = int(input())
	b = [int(x) for x in input().split()]
	x = 0
	answer = 0
	while x < len(b):
		extreme = 0
		while b[x] < 0:
			if extreme == 0 or extreme < b[x]:
				extreme = b[x]
			x += 1
			if x == len(b):
				break
		answer += extreme
		extreme = 0
		if x < len(b):
			while b[x] > 0:
				if extreme == 0 or extreme < b[x]:
					extreme = b[x]
				x += 1
				if x == len(b):
					break
		answer += extreme
	print(answer)


for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input().split())]
	ans = 0
	i = 0
	while i < n:
		if a[i] < 0:
			tmp = a[i]
			while i < n and a[i] < 0:
				tmp = max(tmp, a[i])
				i += 1
		else:
			tmp = a[i]
			while i < n and a[i] > 0:
				tmp = max(tmp, a[i])
				i += 1
		ans += tmp
	print(ans)


itr = int(input())

def m_sum(l, arr):
	s = 0
	if arr[0] > 0:
		c = 'P'
		max_i = 0
	else:
		c = 'N'
		max_i = -10 ** 9
	l = len(arr)
	for i in range(len(arr)):
		val = arr[i]
		if c == 'P':
			if val > max_i:
				max_i = val
			if i <= l - 2 and arr[i + 1] < 0:
				s += max_i
				max_i = -10 ** 9
				c = 'N'
			elif i == l - 1:
				s += max_i
		elif c == 'N':
			if val > max_i:
				max_i = val
			if i <= l - 2 and arr[i + 1] > 0:
				s += max_i
				max_i = 0
				c = 'P'
			elif i == l - 1:
				s += max_i
	return s
for _ in range(itr):
	l = int(input())
	arr = list(map(int, input().split()))
	ans = m_sum(l, arr)
	print(ans)


test = int(input())
for _ in range(test):
	n = int(input())
	arr = input().split()
	m = int(arr[0])
	out = 0
	if int(arr[0]) > 0:
		flag = 1
	else:
		flag = -1
	i = 0
	while i < n:
		if int(arr[i]) > 0 and flag == 1:
			if int(arr[i]) > m:
				m = int(arr[i])
			i += 1
		elif int(arr[i]) < 0 and flag == -1:
			if int(arr[i]) > m:
				m = int(arr[i])
			i += 1
		elif int(arr[i]) > 0 and flag == -1:
			out += m
			m = int(arr[i])
			flag = 1
		elif int(arr[i]) < 0 and flag == 1:
			out += m
			m = int(arr[i])
			flag = -1
	out += m
	print(out)


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


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = [[]]
	b = True if a[0] > 0 else False
	ans = 0
	for i in a:
		if b:
			if i > 0:
				c[-1].append(i)
			else:
				c.append([i])
				b = False
		elif i < 0:
			c[-1].append(i)
		else:
			c.append([i])
			b = True
	for i in c:
		ans += max(i)
	print(ans)


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


def max_sum(num):
	max_list = []
	curr_num = int(num[0])
	positive = None
	if curr_num > 0:
		positive = 1
	else:
		positive = 0
	for i in num:
		if positive and int(i) > 0:
			if int(i) > curr_num:
				curr_num = int(i)
		elif positive and int(i) < 0:
			positive = 0
			max_list.append(curr_num)
			curr_num = int(i)
		elif int(i) < 0 and int(i) > curr_num:
			curr_num = int(i)
		elif int(i) > 0:
			positive = 1
			max_list.append(curr_num)
			curr_num = int(i)
	max_list.append(curr_num)
	return sum(max_list)
for i in range(int(input())):
	r = input()
	print(max_sum(input().split()))


import sys, bisect
from math import gcd
input = sys.stdin.readline
ninf = -10 ** 9 - 7

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

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

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

def invr():
	return map(int, input().rstrip().split())
times = 60
t = inp()
for _ in range(t):
	n = inp()
	nums = inlt()
	max_len = 0
	prev = 1
	small = ninf
	big = 0
	s1 = 0
	if n == 1:
		print(nums[0])
	else:
		for i in range(n):
			if prev > 0:
				if nums[i] < 0:
					s1 += big
					big = 0
					prev = -1
					small = nums[i]
				elif nums[i] > big:
					big = nums[i]
			elif nums[i] > 0:
				s1 += small
				small = ninf
				prev = 1
				big = nums[i]
			elif nums[i] > small:
				small = nums[i]
		if prev > 0 and nums[-1] > 0:
			s1 += big
		elif prev < 0 and nums[-1] < 0:
			s1 += small
		prev = -1
		small = ninf
		big = 0
		s2 = 0
		for i in range(n):
			if prev > 0:
				if nums[i] < 0:
					s2 += big
					big = 0
					prev = -1
					small = nums[i]
				elif nums[i] > big:
					big = nums[i]
			elif nums[i] > 0:
				s2 += small
				small = ninf
				prev = 1
				big = nums[i]
			elif nums[i] > small:
				small = nums[i]
		if prev > 0 and nums[-1] > 0:
			s2 += big
		elif prev < 0 and nums[-1] < 0:
			s2 += small
		print(max(s1, s2))


def positif(n):
	if n > 0:
		return True
	else:
		return False
nb_cases = int(input())
while nb_cases > 0:
	nb_cases -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	dernier_max = a[0]
	liste = []
	for x in range(1, len(a)):
		if positif(a[x]) == positif(dernier_max):
			if a[x] > dernier_max:
				dernier_max = a[x]
		else:
			liste.append(dernier_max)
			dernier_max = a[x]
	liste.append(dernier_max)
	print(sum(liste))


for _ in range(int(input())):
	Ans = []
	N = int(input())
	X = list(map(int, input().split()))
	(i, Current) = (0, X[0])
	while i < N:
		if X[i] > 0 and Current > 0:
			Current = max(Current, X[i])
		elif X[i] < 0 and Current < 0:
			Current = max(Current, X[i])
		else:
			Ans.append(Current)
			Current = X[i]
		i += 1
	print(sum(Ans) + Current)


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = []
	pos = 0
	while pos < len(a):
		mn = a[pos]
		while pos < len(a) and a[pos] * mn > 0:
			mn = max(a[pos], mn)
			pos += 1
		ans.append(mn)
	print(sum(ans))


def fun(n):
	a = [int(x) for x in input().split()]
	ans = 0
	m_p = 0
	m_n = -float('inf')
	for i in range(n):
		if i == n - 1:
			if a[i - 1] * a[i] > 0:
				if a[i] > 0:
					if a[i] > m_p:
						m_p = a[i]
					ans += m_p
				else:
					if a[i] > m_n:
						m_n = a[i]
					ans += m_n
			else:
				ans += a[i]
			break
		if a[i] * a[i + 1] > 0:
			if a[i] > 0:
				if a[i] > m_p:
					m_p = a[i]
			elif a[i] > m_n:
				m_n = a[i]
		elif a[i] > 0:
			if a[i] > m_p:
				m_p = a[i]
			ans += m_p
			m_p = 0
		else:
			if a[i] > m_n:
				m_n = a[i]
			ans += m_n
			m_n = -float('inf')
	print(int(ans))
t = int(input())
for i in range(t):
	n = int(input())
	fun(n)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	if arr[0] > 0:
		loc = 0
	else:
		loc = -10000000000
	glo = 0
	while i < n:
		while arr[i] > 0:
			if arr[i] > loc:
				loc = arr[i]
			i += 1
			if i >= n:
				break
		if i != 0:
			glo += loc
		if i >= n:
			break
		loc = -10000000000
		while arr[i] < 0:
			if arr[i] > loc:
				loc = arr[i]
			i += 1
			if i >= n:
				break
		glo += loc
	print(glo)


for _ in range(int(input())):
	n = int(input())
	arr = [int(s) for s in input().split()]
	flag = True
	sum = 0
	if arr[0] < 0:
		flag = False
	maxi = arr[0]
	for i in range(1, len(arr)):
		if flag and arr[i] < 0:
			flag = False
			sum += maxi
			maxi = arr[i]
		elif not flag and arr[i] > 0:
			flag = True
			sum += maxi
			maxi = arr[i]
		else:
			maxi = max(arr[i], maxi)
	if arr[n - 2] * arr[n - 1] < 0:
		sum += arr[n - 1]
	else:
		sum += maxi
	print(sum)


for _ in range(int(input())):
	n = int(input())
	a = (*map(int, input().split()),)
	ii = 0
	b = [[] for _ in range(n)]
	b[0].append(a[0])
	for (i, j) in zip(a, a[1:]):
		if i * j < 0:
			ii += 1
		b[ii].append(j)
	ans = sum((max(i) for i in b if i))
	print(ans)


testcases = int(input())
for i in range(testcases):
	n = int(input())
	l = list(map(int, input().strip().split(' ')))
	pOn = []
	p = 0
	for j in range(n):
		if l[j] > 0 and len(pOn) == 0:
			pOn.append(l[j])
			p += 1
		elif p != 0:
			if l[j] > 0:
				if p % 2 == 0:
					pOn.append(l[j])
					p += 1
				elif p % 2 != 0:
					if l[j] > pOn[-1]:
						pOn[-1] = l[j]
					else:
						pass
			elif l[j] < 0:
				if p % 2 != 0:
					pOn.append(l[j])
					p += 1
				elif p % 2 == 0:
					if l[j] > pOn[-1]:
						pOn[-1] = l[j]
					else:
						pass
	nOp = []
	m = 0
	for k in range(n):
		if l[k] < 0 and len(nOp) == 0:
			nOp.append(l[k])
			m += 1
		elif m != 0:
			if l[k] > 0:
				if m % 2 != 0:
					nOp.append(l[k])
					m += 1
				elif l[k] > nOp[-1]:
					nOp[-1] = l[k]
				else:
					pass
			elif l[k] < 0:
				if m % 2 == 0:
					nOp.append(l[k])
					m += 1
				elif l[k] > nOp[-1]:
					nOp[-1] = l[k]
				else:
					pass
	if len(pOn) > len(nOp):
		print(sum(pOn))
	else:
		print(sum(nOp))


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict
import math

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

def stringListIn():
	return [x for x in stdin.readline().split()]

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

def stringIn():
	return stdin.readline().strip()
t = intIn()
for _ in range(t):
	n = intIn()
	arr = listIn()
	ans = 0
	li = []
	if n == 1:
		print(arr[0])
	else:
		for i in range(n):
			if arr[i] < 0:
				li.append(0)
			else:
				li.append(1)
		m = arr[0]
		for i in range(n):
			if i < n - 1 and li[i] == li[i + 1]:
				m = max(m, arr[i], arr[i + 1])
			elif i > 0 and i < n - 1 and (li[i] != li[i - 1] and li[i] != li[i + 1]):
				ans += arr[i]
			elif i == n - 1:
				if li[i] == li[i - 1]:
					ans += m
					m = float('-inf')
				else:
					ans += arr[i]
			else:
				ans += m
				m = float('-inf')
		print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	st = []
	for i in range(n):
		if st:
			if st[-1] < 0:
				if a[i] < 0:
					if a[i] > st[-1]:
						st.pop()
						st.append(a[i])
				else:
					st.append(a[i])
			if st[-1] > 0:
				if a[i] > 0:
					if a[i] > st[-1]:
						st.pop()
						st.append(a[i])
				else:
					st.append(a[i])
		else:
			st.append(a[i])
	print(sum(st))


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	s = 0
	cur_max = a[0]
	for i in range(1, len(a)):
		if a[i] * a[i - 1] < 0:
			s += cur_max
			cur_max = a[i]
		else:
			cur_max = max(cur_max, a[i])
	print(s + cur_max)


from collections import Counter
import math
mod = 10 ** 9 + 7
for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	i = 0
	ans = 0
	while i < n:
		mp = 0
		mn = -10 ** 10
		while i < n and arr[i] > 0:
			mp = max(mp, arr[i])
			i += 1
		ans += mp
		while i < n and arr[i] < 0:
			mn = max(mn, arr[i])
			i += 1
		if mn != -10 ** 10:
			ans += mn
	print(ans)


def alternating():
	cases = int(input())
	length = []
	array = []
	for i in range(cases):
		length.append(int(input()))
		array.append(list(map(int, input().split())))
	for i in range(len(array)):
		maximum = array[i][0]
		output = 0
		if array[i][0] < 0:
			positive = False
		else:
			positive = True
		for j in range(len(array[i])):
			if positive:
				if array[i][j] < 0:
					positive = False
					output += maximum
					maximum = array[i][j]
				elif array[i][j] > maximum:
					maximum = array[i][j]
			elif array[i][j] > 0:
				positive = True
				output += maximum
				maximum = array[i][j]
			elif array[i][j] > maximum:
				maximum = array[i][j]
		output += maximum
		print(output)
alternating()


def positive(num):
	if num > 0:
		return 1
	else:
		return 0

def yo():
	for i in range(int(input().strip())):
		n = int(input().strip())
		components = []
		nums = list(map(int, input().split()))
		if positive(nums[0]):
			sign = 1
		else:
			sign = 0
		components.append([nums[0]])
		current = 0
		for num in nums[1:]:
			new_sign = positive(num)
			if sign == new_sign:
				components[current].append(num)
			else:
				components.append([num])
				sign = new_sign
				current += 1
		sum = 0
		for component in components:
			sum += max(component)
		print(sum)
yo()


def checker():
	testcases = int(input())
	for _ in range(testcases):
		n = int(input())
		arr = list(map(int, input().split(' ')))
		if len(arr) < 2:
			print(sum(arr))
			continue
		chosen = [arr[0]]
		idx = 1
		while idx < n:
			if chosen[-1] < 0:
				if arr[idx] < 0 and arr[idx] > chosen[-1]:
					chosen[-1] = arr[idx]
				elif arr[idx] > 0:
					chosen.append(arr[idx])
			elif arr[idx] > chosen[-1]:
				chosen[-1] = arr[idx]
			elif arr[idx] < 0:
				chosen.append(arr[idx])
			idx += 1
		print(sum(chosen))
checker()


num = int(input())
for _ in range(num):
	n = int(input())
	list_ = [int(i) for i in input().split()]
	tmp = None
	count = 0
	for i in list_:
		if not tmp:
			tmp = 1 if i > 0 else -1
			count += 1
		if i ^ tmp < 0:
			count += 1
			tmp = 1 if tmp == -1 else -1
			flag = True
	tmp = list_[0]
	sum_ = 0
	count_2 = 0
	for i in list_:
		if i == tmp:
			continue
		if tmp ^ i < 0:
			sum_ += tmp
			tmp = i
			count_2 += 1
			continue
		if i > 0 and i > tmp:
			tmp = i
		elif i < 0 and i > tmp:
			tmp = i
	if count_2 == count - 1:
		sum_ += tmp
	print(sum_)


t = int(input())
for i in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	maxC = L[0]
	sum1 = 0
	for i in range(1, n):
		if L[i] * L[i - 1] > 0:
			maxC = max(maxC, L[i])
		else:
			sum1 += maxC
			maxC = L[i]
	sum1 += maxC
	print(sum1)


t = int(input())
for i in range(t):
	n = int(input())
	seq_a = list(map(int, input().split()))
	stack = list()
	stack.append(seq_a[0])
	for k in range(1, n):
		if seq_a[k] > 0:
			if stack[-1] > 0:
				if stack[-1] < seq_a[k]:
					stack.pop()
					stack.append(seq_a[k])
			else:
				stack.append(seq_a[k])
		elif stack[-1] > 0:
			stack.append(seq_a[k])
		elif stack[-1] < seq_a[k]:
			stack.pop()
			stack.append(seq_a[k])
	print(sum(stack))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	m = arr[0]
	sign = 0 if arr[0] < 0 else 1
	s = 0
	if len(arr) == 1:
		print(arr[0])
	else:
		for i in range(1, len(arr)):
			if arr[i] < 0 and sign == 0:
				m = max(m, arr[i])
				sign = 0
				if i == len(arr) - 1:
					s += m
			elif arr[i] > 0 and sign == 1:
				m = max(m, arr[i])
				sign = 1
				if i == len(arr) - 1:
					s += m
			elif arr[i] >= 0 and sign == 0:
				s += m
				m = arr[i]
				sign = 1
				if i == len(arr) - 1:
					s += m
			elif arr[i] < 0 and sign == 1:
				s += m
				m = arr[i]
				sign = 0
				if i == len(arr) - 1:
					s += m
		print(s)


from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline
sqrt = math.sqrt

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

def LF():
	return list(map(float, input().split()))

def LI_():
	return list(map(lambda x: int(x) - 1, input().split()))

def II():
	return int(input())

def IF():
	return float(input())

def S():
	return input().rstrip()

def LS():
	return S().split()

def IR(n):
	return [II() for _ in range(n)]

def LIR(n):
	return [LI() for _ in range(n)]

def FR(n):
	return [IF() for _ in range(n)]

def LFR(n):
	return [LI() for _ in range(n)]

def LIR_(n):
	return [LI_() for _ in range(n)]

def SR(n):
	return [S() for _ in range(n)]

def LSR(n):
	return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

def solve(x):
	a = LI()
	ans = 0
	tern = a[0] > 0
	tmp = [a[0]]
	for i in range(1, x):
		if tern:
			if a[i] > 0:
				tmp.append(a[i])
			else:
				ans += max(tmp)
				tmp = [a[i]]
				tern = 0
		elif a[i] < 0:
			tmp.append(a[i])
		else:
			ans += max(tmp)
			tmp = [a[i]]
			tern = 1
	print(ans + max(tmp))
	return
for _ in range(II()):
	solve(II())


def sign(n):
	if n > 0:
		return 1
	else:
		return -1
t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	count = 1
	current = sign(array[0])
	max1 = -10 ** 9 - 1
	sum1 = 0
	for i in range(n):
		if sign(array[i]) != current:
			sum1 += max1
			max1 = -10 ** 9 - 1
			max1 = max(max1, array[i])
			current = sign(array[i])
		else:
			max1 = max(max1, array[i])
	sum1 += max1
	print(sum1)


t = int(input())
for hh in range(0, t):
	n = int(input())
	a = [int(x) for x in input().split()]
	if a[-1] > 0:
		a.append(-1)
	else:
		a.append(1)
	n += 1
	prev_pos = -2
	prev_neg = -2
	mx_pos = -1000000001
	mx_neg = -1000000001
	max_pos_set = 0
	max_neg_set = 0
	ans = 0
	for i in range(0, n):
		if a[i] > 0:
			max_pos_set = 1
			if prev_pos == i - 1:
				mx_pos = max(mx_pos, a[i])
			else:
				if max_neg_set > 0:
					ans += mx_neg
				mx_pos = a[i]
			prev_pos = i
		else:
			max_neg_set = 1
			if prev_neg == i - 1:
				mx_neg = max(mx_neg, a[i])
			else:
				if max_pos_set > 0:
					ans += mx_pos
				mx_neg = a[i]
			prev_neg = i
	print(ans)


def solve(ar, n):
	if ar[0] > 0:
		positive = True
	else:
		positive = False
	sum1 = ar[0]
	index = 0
	for i in range(1, n):
		if positive and ar[i] > 0:
			if sum1 - ar[index] + ar[i] > sum1:
				sum1 = sum1 - ar[index] + ar[i]
				index = i
		elif positive and ar[i] < 0:
			sum1 = sum1 + ar[i]
			index = i
			positive = False
		elif positive != True and ar[i] < 0:
			if sum1 - ar[index] + ar[i] > sum1:
				sum1 = sum1 - ar[index] + ar[i]
				index = i
		else:
			sum1 = sum1 + ar[i]
			index = i
			positive = True
	return sum1
for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(solve(l, n))


t = int(input())
import math
for _ in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	prev = seq[0]
	totsum = 0
	for (i, curr) in enumerate(seq):
		if curr * prev > 0:
			prev = max(curr, prev)
		if curr * prev < 0:
			totsum += prev
			prev = curr
	totsum += prev
	print(totsum)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	temp = a[0]
	ans = 0
	if a[0] > 0:
		now = 1
	else:
		now = -1
	for x in a[1:]:
		if now == 1:
			if x > 0:
				temp = max(temp, x)
			else:
				ans += temp
				temp = x
				now = -1
		elif x < 0:
			temp = max(temp, x)
		else:
			ans += temp
			temp = x
			now = 1
	ans += temp
	print(ans)


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for t in range(test):
		n = int(input()) // 2
		l = [int(i) for i in input().split(' ')]
		pos = False
		if l[0] > 0:
			pos = True
		maxi = l[0]
		sum = 0
		for i in range(len(l)):
			if pos:
				if l[i] > 0:
					maxi = max(maxi, l[i])
				else:
					sum += maxi
					maxi = l[i]
					pos = False
			elif l[i] < 0:
				maxi = max(maxi, l[i])
			else:
				sum += maxi
				maxi = l[i]
				pos = True
		sum += maxi
		print(sum)
main()


for i in range(int(input()) * 2):
	z = input().split()
	z = [int(x) for x in z]
	if i % 2 == 0:
		help = [[] for _ in range(z[0] + 1)]
	if i % 2 == 1:
		counter = 0
		for j in range(len(z)):
			if j > 0 and (z[j] > 0 and z[j - 1] < 0) or (z[j] < 0 and z[j - 1] > 0):
				counter += 1
			help[counter].append(z[j])
		total = 0
		for j in range(len(help)):
			if len(help[j]) > 0:
				total += max(help[j])
		print(total)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sign = True if a[0] > 0 else False
	j = pow(10, 9)
	pmax = -j
	nmax = -j
	tsum = 0
	for i in a:
		if sign and i > 0:
			pmax = max(pmax, i)
		elif sign and i < 0:
			sign = False
			tsum += pmax
			pmax = -j
			nmax = i
		elif not sign and i < 0:
			nmax = max(nmax, i)
		elif not sign and i > 0:
			sign = True
			tsum += nmax
			pmax = i
			nmax = -j
	if a[-1] > 0:
		tsum += pmax
	else:
		tsum += nmax
	print(tsum)


nbtest = int(input())
for i in range(nbtest):
	nbelement = int(input())
	liste = input().split(' ')
	for i in range(nbelement):
		liste[i] = int(liste[i])
	somme = liste[0]
	instant = liste[0]
	for i in range(1, nbelement):
		if instant * liste[i] > 0:
			somme = somme + max(instant, liste[i]) - instant
			instant = max(instant, liste[i])
		else:
			somme += liste[i]
			instant = liste[i]
	print(somme)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pre = True
	i = 0
	s = 0
	while i < n:
		t = -1000000000
		if a[i] < 0:
			while i < n and a[i] < 0:
				t = max(t, a[i])
				i += 1
			s += t
		else:
			while i < n and a[i] > 0:
				t = max(t, a[i])
				i += 1
			s += t
	print(s)


t = int(input())
for t_itr in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	maxlen = 0
	i = 0
	while i < n:
		curmax = arr[i]
		j = i + 1
		while j < n and arr[i] ^ arr[j] >= 0:
			if arr[j] > curmax:
				curmax = arr[j]
			j += 1
		maxlen += curmax
		i = j
	print(maxlen)


def fun(n, l):
	prev = l[0]
	sum = []
	for i in range(1, n):
		if prev > 0 and l[i] < 0:
			sum.append(prev)
			prev = l[i]
		elif prev < 0 and l[i] > 0:
			sum.append(prev)
			prev = l[i]
		elif prev < l[i]:
			prev = l[i]
		if i == n - 1:
			sum.append(prev)
	suma = 0
	for i in sum:
		suma += i
	print(suma)
for T in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n > 1:
		fun(n, l)
	else:
		print(l[0])


t = int(input())

def check(a, b):
	if a < 0 and b > 0:
		return True
	if a > 0 and b < 0:
		return True
	return False
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	res = [a[0]]
	for i in a:
		if check(i, res[-1]):
			res.append(i)
		elif i > res[-1]:
			res[-1] = i
	for i in res:
		s += i
	print(s)


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	poslen = 0
	neglen = 0
	possum = 0
	negsum = 0
	ind = 0
	pflag = 'p'
	nflag = 'n'
	ps = 0
	ns = 0
	nlocal = -float('inf')
	plocal = 0
	for ind in range(n):
		if l[ind] < 0:
			if pflag == 'p' and ps:
				poslen += 1
				possum += plocal
				plocal = l[ind]
				pflag = 'n'
			elif pflag == 'n' and ps:
				plocal = max(plocal, l[ind])
			if nflag == 'n':
				ns = 1
				nlocal = max(l[ind], nlocal)
			else:
				neglen += 1
				negsum += nlocal
				nlocal = l[ind]
				nflag = 'n'
		else:
			if pflag == 'n':
				poslen += 1
				possum += plocal
				plocal = l[ind]
				pflag = 'p'
			else:
				ps = 1
				plocal = max(plocal, l[ind])
			if nflag == 'n' and ns:
				neglen += 1
				negsum += nlocal
				nlocal = l[ind]
				nflag = 'p'
			elif nflag == 'p' and ns:
				nlocal = max(nlocal, l[ind])
	if ps == 1:
		poslen += 1
	if ns == 1:
		neglen += 1
	possum += plocal
	negsum += nlocal
	if poslen > neglen:
		print(possum)
	elif neglen > poslen:
		print(negsum)
	else:
		print(max(possum, negsum))


t = int(input())
for test in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	segments = []
	if a[0] > 0:
		positive = True
	else:
		positive = False
	current = []
	for e in a:
		if e > 0 and positive:
			current.append(e)
		elif e < 0 and (not positive):
			current.append(e)
		elif e > 0 and (not positive):
			segments.append(current)
			current = [e]
			positive = True
		else:
			segments.append(current)
			current = [e]
			positive = False
	segments.append(current)
	total = 0
	for segment in segments:
		total = total + max(segment)
	print(total)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l[0] > 0:
		p = 0
	else:
		p = 1
	ans = []
	k = 0
	ma = -(10 ** 9 + 1)
	while k < n:
		if p == 0:
			if l[k] > 0:
				ma = max(l[k], ma)
				k += 1
			else:
				ans.append(ma)
				ma = -(10 ** 9 + 1)
				p = 1
		elif l[k] < 0:
			ma = max(ma, l[k])
			k += 1
		else:
			p = 0
			ans.append(ma)
			ma = -(10 ** 9 + 1)
	ans.append(ma)
	print(sum(ans))


def sign(x):
	return 1 if x > 0 else -1

def solve(n, a):
	sig = sign(a[0])
	prev = float('-inf')
	sol = 0
	for x in a:
		if sign(x) != sig:
			sol += prev
			prev = x
			sig = sign(x)
		else:
			prev = max(prev, x)
	return sol + prev
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))


def solve(n, a):
	ans = 0
	last = a[0]
	for i in range(1, n + 1):
		if i < n and (last > 0) == (a[i] > 0):
			last = max(last, a[i])
		else:
			ans += last
			if i < n:
				last = a[i]
	print(ans)

def main():
	t = int(input())
	inputs = [[]] * t
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		inputs[i] = [n, a]
	for (n, a) in inputs:
		solve(n, a)
main()


sign = lambda a: (a > 0) - (a < 0)

def checker(n, lst):
	(s, i, j) = (0, 0, 0)
	while i <= n and j <= n:
		cur = lst[i]
		while j < n and sign(lst[i]) == sign(lst[j]):
			cur = max(cur, lst[j])
			j += 1
		i = j
		s += cur
		if i >= n:
			break
	return s
for _ in range(int(input())):
	m = int(input())
	a = [int(i) for i in input().split()]
	print(checker(m, a))


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


t = int(input())
for _ in range(t):
	n = int(input())
	seq = [int(i) for i in input().split()]
	resp = []
	if seq[0] > 0:
		pos = True
	else:
		pos = False
	lastVal = seq[0]
	i = 1
	while i < n:
		now = seq[i] > 0
		if pos != now:
			resp.append(lastVal)
			pos = now
			lastVal = seq[i]
		elif seq[i] > lastVal:
			lastVal = seq[i]
		i += 1
	resp.append(lastVal)
	print(sum(resp))


T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	f = True
	s1 = 0
	inf = float('inf')
	M1 = -inf
	for i in a:
		if f and i > 0:
			M1 = max(M1, i)
		elif f and i < 0:
			s1 += M1
			M1 = i
			f = False
		elif f == False and i < 0:
			M1 = max(M1, i)
		elif f == False and i > 0:
			s1 += M1
			M1 = i
			f = True
	s1 += M1
	f = False
	s2 = 0
	inf = float('inf')
	M2 = -inf
	for i in a:
		if f and i > 0:
			M2 = max(M2, i)
		elif f and i < 0:
			s2 += M2
			M2 = i
			f = False
		elif f == False and i < 0:
			M2 = max(M2, i)
		elif f == False and i > 0:
			s2 += M2
			M2 = i
			f = True
	s2 += M2
	print(max(s1, s2))


import sys
from random import choice, randint
inp = sys.stdin.readline
out = sys.stdout.write
flsh = sys.stdout.flush
sys.setrecursionlimit(10 ** 9)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
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 MI():
	return map(int, inp().strip().split())

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

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines().strip()]

def LI_():
	return [int(x) - 1 for x in inp().strip().split()]

def LF():
	return [float(x) for x in inp().strip().split()]

def LS():
	return inp().strip().split()

def I():
	return int(inp().strip())

def F():
	return float(inp().strip())

def S():
	return inp().strip()

def pf(s):
	return out(s + '\n')

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 main():
	t = I()
	l = []
	for _ in range(t):
		n = I()
		a = LI()
		lp = 0
		ln = 0
		(i, j) = (0, 1)
		sm = 0
		while i < n:
			if a[i] > 0:
				lp = a[i]
				k = i + 1
				while k < n and a[k] > 0:
					if a[k] > lp:
						lp = a[k]
					k += 1
				sm += lp
				i = k
			elif a[i] < 0:
				ln = a[i]
				k = i + 1
				while k < n and a[k] < 0:
					if a[k] > ln:
						ln = a[k]
					k += 1
				sm += ln
				i = k
		print(sm)
main()


import sys
input = sys.stdin.buffer.readline
T = int(input())
for _ in range(1, T + 1):
	n = int(input())
	arr = [int(x) for x in input().split()]
	signPlus = True if arr[0] >= 0 else False
	answer = 0
	sum = -1000000000.0
	for idx in range(len(arr)):
		i = arr[idx]
		if signPlus and i >= 0:
			sum = max(sum, i)
		elif signPlus and i < 0:
			answer += sum
			signPlus = False
			sum = i
		elif not signPlus and i < 0:
			sum = max(sum, i)
		elif not signPlus and i >= 0:
			answer += sum
			sum = i
			signPlus = True
	answer += sum
	print(int(answer))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	e = l[0]
	k = 1
	s = 0
	for j in range(1, n):
		if l[j] * e < 0:
			s += e
			e = l[j]
			k += 1
		elif l[j] > e:
			e = l[j]
	s += e
	print(s)


k = list(map(int, input().split()))
t = k[0]
for i in range(t):
	s = list(map(int, input().split()))
	n = list(map(int, input().split()))
	maxminus = -1000000000000
	maxplus = 0
	flag = 0
	summa = 0
	for i in range(len(n)):
		if i == 0:
			if n[i] > 0:
				flag = 1
				maxplus = max(maxplus, n[i])
			else:
				flag = -1
				maxminus = max(maxminus, n[i])
		elif n[i] > 0:
			if flag == 1:
				maxplus = max(maxplus, n[i])
			else:
				summa = summa + maxminus
				flag = 1
				maxplus = n[i]
		elif flag == -1:
			maxminus = max(maxminus, n[i])
		else:
			summa = summa + maxplus
			flag = -1
			maxminus = n[i]
	if flag == 1:
		summa = summa + maxplus
	else:
		summa = summa + maxminus
	print(summa)


for z in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 0 or n == 1:
		print(sum(l))
	else:
		s = 0
		p = 0
		ne = -1000000001
		fp = 0
		fn = 0
		i = 0
		while i < n:
			if l[i] < 0:
				if fn == 1:
					ne = max(ne, l[i])
				else:
					ne = l[i]
				fn = 1
				if fp == 1:
					s += p
				fp = 0
			else:
				if fp == 1:
					p = max(p, l[i])
				else:
					p = l[i]
				fp = 1
				if fn == 1:
					s += ne
				fn = 0
			i += 1
		if l[n - 1] < 0:
			s += ne
		else:
			s += p
		print(s)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = -10 ** 18
	som = 0
	if l[0] > 0:
		f = 0
	else:
		f = 1
	j = 0
	while j < len(l):
		i = l[j]
		if f == 0:
			if i > 0:
				if i > m:
					m = i
				j += 1
			else:
				som = som + m
				m = -10 ** 18
				f = 1
		elif i < 0:
			if i > m:
				m = i
			j += 1
		else:
			som = som + m
			m = -10 ** 18
			f = 0
	som = som + m
	print(som)


for _ in range(int(input())):
	N = int(input())
	ar = list(map(int, input().split()))
	arr = []
	curr = []
	sign = 1 if ar[0] >= 0 else -1
	for item in ar:
		if item // abs(item) == sign:
			curr.append(item)
		else:
			arr.append(curr)
			sign *= -1
			curr = [item]
	arr.append(curr)
	ans = 0
	a = sum([max(l) for l in arr])
	print(a)


from itertools import groupby
t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dst = [sum((1 for e in it)) for (_, it) in groupby(a, key=lambda x: x > 0)]
	sumb = max(a[0:dst[0]])
	tmp = dst[0]
	for i in range(1, len(dst)):
		if tmp + dst[i] == len(a):
			sumb += max(a[tmp:])
		else:
			sumb += max(a[tmp:tmp + dst[i]])
			tmp += dst[i]
	ans.append(sumb)
for x in ans:
	print(x)


import sys
import math
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	p = -1
	q = -1000000000000000
	s = 0
	for j in range(n):
		if b[j] > 0:
			p = max(p, b[j])
			if q != -1000000000000000:
				s += q
			q = -1000000000000000
		else:
			q = max(q, b[j])
			if p != -1:
				s += p
			p = -1
	if p != -1:
		s += p
	if q != -1000000000000000:
		s += q
	print(s)


from itertools import *
for _ in range(int(input())):
	n = int(input())
	print(sum((max(map(int, v)) for (_, v) in groupby(input().split(), str.isdigit))))


import sys
import math
from collections import defaultdict

def solve(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = 0
	rec_min = sys.maxsize
	rec_max = -1
	for i in range(n):
		if a[i] < 0:
			if rec_max > 0:
				ans += rec_max
				rec_max = -1
			rec_min = -min(abs(rec_min), abs(a[i]))
		else:
			if rec_min < 0:
				ans += rec_min
				rec_min = sys.maxsize
			rec_max = max(rec_max, a[i])
	if rec_max > 0:
		ans += rec_max
	if rec_min < 0:
		ans += rec_min
	print(ans)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


from math import *
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = [l[0]]
	n = len(l)
	for i in range(1, n):
		if l[i] > 0 and m[-1] > 0 and (l[i] > m[-1]):
			m.pop()
			m.append(l[i])
		elif l[i] < 0 and m[-1] > 0:
			m.append(l[i])
		elif l[i] < 0 and m[-1] < 0 and (l[i] > m[-1]):
			m.pop()
			m.append(l[i])
		elif l[i] > 0 and m[-1] < 0:
			m.append(l[i])
	print(sum(m))


def pos(i, sum):
	m = a[i]
	while i < n and a[i] > 0:
		m = max(m, a[i])
		i += 1
	sum += m
	return (i, sum)

def neg(i, sum):
	m = a[i]
	while i < n and a[i] < 0:
		m = max(m, a[i])
		i += 1
	sum += m
	return (i, sum)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	sum = 0
	while i < n:
		if a[i] > 0:
			(i, sum) = pos(i, sum)
		elif a[i] < 0:
			(i, sum) = neg(i, sum)
	print(sum)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	new = [arr[0]]
	p = 1 if arr[0] > 0 else 0
	for i in range(1, n):
		if p ^ (arr[i] > 0):
			p ^= 1
			new.append(arr[i])
		else:
			new[-1] = max(new[-1], arr[i])
	print(sum(new))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	pos = True if a[0] > 0 else False
	mini = a[0]
	for i in range(n):
		if a[i] > 0 and pos or (a[i] < 0 and (not pos)):
			mini = max(mini, a[i])
		else:
			ans += mini
			mini = a[i]
			pos = not pos
	ans += mini
	print(ans)


def signof(i):
	if i < 0:
		return -1
	if i > 0:
		return 1
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	maxsum = 0
	i = 0
	while i < n:
		current = a[i]
		j = i
		while j < n and signof(a[i]) == signof(a[j]):
			current = max(current, a[j])
			j += 1
		maxsum += current
		i = j
	print(maxsum)


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


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


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	f = 0
	v = []
	i = 0
	while i < n:
		if i < n and l[i] < 0:
			k = []
			while i < n and l[i] < 0:
				k.append(l[i])
				i += 1
			v.append(k)
		if i < n and l[i] > 0:
			k = []
			while i < n and l[i] > 0:
				k.append(l[i])
				i += 1
			v.append(k)
	for t in v:
		s += max(t)
	print(s)


from sys import stdin

def solve(n, a):
	s = 1 if a[0] > 0 else -1
	sm = 0
	M = 10 ** 9 + 1
	pos = -M
	for i in range(n):
		if a[i] > 0 and s == 1:
			pos = max(pos, a[i])
		elif a[i] < 0 and s == -1:
			pos = max(pos, a[i])
		else:
			s = -s
			sm += pos
			pos = a[i]
	if a[-1] < 0 and s == -1 or (a[-1] > 0 and s == 1):
		sm += pos
	print(sm)
	return

def main():
	inp = stdin.readline
	for _ in range(int(inp())):
		n = int(inp())
		a = list(map(int, inp().split()))
		solve(n, a)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	S = 0
	m = a[0]
	a.append(-a[-1])
	for x in a[1:]:
		if x * m > 0:
			m = max(x, m)
		else:
			S += m
			m = x
	print(S)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	case1 = []
	case2 = []
	check = False
	for i in range(n):
		if a[i] > 0:
			start = i
			now = a[i]
			check = True
			break
	if check:
		for i in range(start + 1, n):
			if a[i] * now >= 0:
				now = max(now, a[i])
			else:
				case1.append(now)
				now = a[i]
		if len(case1) == 0 or now != case1[-1]:
			case1.append(now)
	check = False
	for i in range(n):
		if a[i] < 0:
			start = i
			now = a[i]
			check = True
			break
	if check:
		for i in range(start + 1, n):
			if a[i] * now >= 0:
				now = max(now, a[i])
			else:
				case2.append(now)
				now = a[i]
		if len(case2) == 0 or now != case2[-1]:
			case2.append(now)
	if len(case1) > len(case2):
		result = case1
	elif len(case1) < len(case2):
		result = case2
	else:
		result = case1 if sum(case1) > sum(case2) else case2
	print(sum(result))


t = int(input())
for k in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	temp = arr[0]
	sign = 0
	s = 0
	last = arr[0] / abs(arr[0])
	for i in range(1, n):
		if arr[i] < 0 and arr[i - 1] > 0 or (arr[i] > 0 and arr[i - 1] < 0):
			s += temp
		if arr[i] < 0:
			if last > 0:
				temp = arr[i]
			else:
				temp = max(temp, arr[i])
			last = -1
		elif arr[i] > 0:
			if last < 0:
				temp = arr[i]
			else:
				temp = max(temp, arr[i])
			last = 1
	print(s + temp)


T = int(input())

def f(x):
	if x > 0:
		return 1
	else:
		return 0
while T:
	n = int(input())
	A = list(map(int, input().split()))
	S = A[0]
	P = A[0]
	F = f(A[0])
	i = 1
	while i < n:
		if f(A[i]) != F:
			F = f(A[i])
			S = S + A[i]
			P = A[i]
		else:
			S = S - P
			P = max(P, A[i])
			S = S + P
		i = i + 1
	print(S)
	T = T - 1


t = int(input())
for i in range(t):
	n = input()
	nums = input()
	nums = list(map(int, nums.split()))
	sums = 0
	flag = 0
	last = 0
	for now in nums:
		if now > 0:
			if flag > 0:
				if now > last:
					last = now
			else:
				flag = 1
				sums += last
				last = now
		elif flag < 0:
			if now > last:
				last = now
		else:
			flag = -1
			sums += last
			last = now
	sums += last
	print(sums)


import math
t = int(input())
for q in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(0, n):
		b.append(a[i] > 0)
	ans = 0
	ma = a[0]
	for i in range(1, n):
		if b[i] != b[i - 1]:
			ans += ma
			ma = a[i]
		else:
			ma = max(ma, a[i])
	ans += ma
	print(ans)


for f in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	ma = a[0]
	po = 1
	if ma < 0:
		po = 0
	for i in range(n):
		if po == 1:
			if a[i] > 0:
				ma = max(ma, a[i])
			else:
				s += ma
				ma = a[i]
				po = 1 - po
		elif a[i] < 0:
			ma = max(ma, a[i])
		else:
			s += ma
			ma = a[i]
			po = 1 - po
	s += ma
	print(s)


t = int(input())
for _ in range(t):
	n = map(int, input())
	nums = map(int, input().split())
	maxLenNeg = 0
	maxLenPos = 0
	maxSumPos = 0
	maxSumNeg = 0
	for x in nums:
		if x < 0:
			if maxLenPos + 1 > maxLenNeg:
				maxLenNeg = maxLenPos + 1
				maxSumNeg = maxSumPos + x
			else:
				maxSumNeg = max(maxSumNeg, maxSumPos + x)
		elif maxLenNeg + 1 > maxLenPos:
			maxLenPos = maxLenNeg + 1
			maxSumPos = maxSumNeg + x
		else:
			maxSumPos = max(maxSumPos, maxSumNeg + x)
	m = -1e+18
	if maxLenPos >= maxLenNeg:
		m = max(m, maxSumPos)
	if maxLenNeg >= maxLenPos:
		m = max(m, maxSumNeg)
	print(m)


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


for t in range(0, int(input())):
	Sm = p = lst = 0
	l = 1
	input()
	for i in map(int, input().split()):
		if p > 0 and i > 0 or (p < 0 and i < 0):
			if i > lst:
				Sm += i - lst
				lst = i
		else:
			l += 1
			Sm += i
			lst = i
		p = i
	print(Sm)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	j = 0
	maxe = 0
	maxe2 = 0
	sume = 0
	while j < n:
		if arr[j] > 0:
			maxe = arr[j]
			while j < n:
				if j + 1 < n:
					if arr[j + 1] > 0:
						if arr[j + 1] > maxe:
							maxe = arr[j + 1]
						j += 1
					else:
						sume += maxe
						maxe = 0
						j += 1
						break
				else:
					sume += maxe
					maxe = 0
					j += 1
			maxe = 0
		elif arr[j] < 0:
			maxe2 = arr[j]
			while j < n:
				if j + 1 < n:
					if arr[j + 1] < 0:
						if arr[j + 1] > maxe2:
							maxe2 = arr[j + 1]
						j += 1
					else:
						sume += maxe2
						maxe2 = 0
						j += 1
						break
				else:
					sume += maxe2
					maxe2 = 0
					j += 1
			maxe = 0
	print(sume)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = su = cr = 0
	k = -(2 * 10 ** 5 + 1)
	maxi = k
	flag = True
	while i < n:
		cur = arr[i]
		j = i
		while j < n and cur * arr[j] > 0:
			cur = max(cur, arr[j])
			j += 1
		su += cur
		i = j
	print(su)


from sys import stdin

def input():
	return stdin.readline()
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 0 or n == 1:
		print(sum(a))
		continue
	else:
		su = 0
		pos = 0
		neg = -1000000000
		fp = 0
		fn = 0
		for i in range(n):
			if a[i] < 0:
				if fn == 1:
					neg = max(neg, a[i])
				else:
					neg = a[i]
				fn = 1
				if fp == 1:
					su += pos
				fp = 0
			elif a[i] > 0:
				if fp == 1:
					pos = max(pos, a[i])
				else:
					pos = a[i]
				fp = 1
				if fn == 1:
					su += neg
				fn = 0
		if a[n - 1] < 0:
			su += neg
		else:
			su += pos
		print(su)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	start = [0] * n
	ind = 0
	for i in range(n):
		if i == 0:
			start[ind] = arr[i]
		elif arr[i] > 0 and arr[i - 1] > 0:
			start[ind] = max(start[ind], arr[i])
		elif arr[i] < 0 and arr[i - 1] < 0:
			start[ind] = max(start[ind], arr[i])
		elif arr[i] > 0 and arr[i - 1] < 0:
			ind += 1
			start[ind] = arr[i]
		elif arr[i] < 0 and arr[i - 1] > 0:
			ind += 1
			start[ind] = arr[i]
	print(sum(start))


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input()
	A = list(map(int, list(a.split())))
	if min(A) > 0:
		print(max(A))
	elif max(A) < 0:
		print(max(A))
	else:
		i = 0
		l = []
		while i < n:
			if A[i] < 0:
				j = i + 1
				m = A[i]
				while j < n:
					if A[j] > 0:
						break
					else:
						m = max(m, A[j])
					j = j + 1
				i = j
				l.append(m)
			else:
				j = i + 1
				m = A[i]
				while j < n:
					if A[j] < 0:
						break
					else:
						m = max(m, A[j])
					j = j + 1
				i = j
				l.append(m)
		print(sum(l))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = l[0]
	sol = 0
	for i in range(1, n):
		if l[i - 1] < 0 and l[i] < 0 or (l[i - 1] > 0 and l[i] > 0):
			x = max(l[i], x)
		else:
			sol += x
			x = l[i]
	sol += x
	print(sol)


from itertools import *
for s in range(int(input())):
	a = int(input())
	b = [x for x in input().split()]
	g = 0
	for (_, v) in groupby(b, str.isdigit):
		g += max(map(int, v))
	print(g)


t = int(input())
for case in range(t):
	n = int(input())
	nos = list(map(int, input().split()))
	state = False
	if nos[0] > 0:
		state = True
	length = 1
	maxVal = nos[0]
	maxSum = 0
	for no in nos:
		if no < 0 and state or (no > 0 and (not state)):
			state = not state
			maxSum += maxVal
			maxVal = no
		if no > maxVal:
			maxVal = no
	maxSum += maxVal
	print(maxSum)


import sys
input = sys.stdin.readline
ans = []
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = []
	if a[0] > 0:
		tmp = a[0]
		switch = 1
	else:
		tmp = a[0]
		switch = 0
	for i in a[1:]:
		if switch and i > 0:
			tmp = max(tmp, i)
		elif switch and i < 0:
			ans.append(tmp)
			tmp = i
			switch = 0
		elif not switch and i < 0:
			tmp = max(tmp, i)
		else:
			ans.append(tmp)
			tmp = i
			switch = 1
	ans.append(tmp)
	print(sum(ans))


def sol(l):
	dp = [0] * (len(l) + 1)
	(i, j) = (0, 0)
	count = 0
	while j <= len(l) - 1:
		count += 1
		dp[count] = l[i]
		j = i + 1
		while j <= len(l) - 1:
			if l[i] * l[j] < 0:
				i = j
				break
			else:
				dp[count] = max(l[j], dp[count])
				j += 1
	return print(sum(dp))
t = int(input())
for _ in range(t):
	i = int(input())
	l = list(map(int, input().split()))
	sol(l)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dp = [arr[0]]
	for i in range(1, n):
		if arr[i] < 0 and arr[i - 1] > 0:
			dp += [arr[i]]
		else:
			dp += [max(dp[-1], arr[i])]
	for i in range(n - 2, -1, -1):
		if dp[i] > 0 and dp[i + 1] > 0 or (dp[i] < 0 and dp[i + 1] < 0):
			dp[i] = dp[i + 1]
	s = dp[0]
	for i in range(1, n):
		if dp[i] != dp[i - 1]:
			s += dp[i]
	print(s)


import sys
from math import log2, floor
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

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 ** 18
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	n = int(ri())
	a = Ri()
	minn = INF
	maxx = -INF
	ans = []
	minn = maxx = a[0]
	for i in range(1, len(a)):
		prevminn = minn
		prevmaxx = maxx
		minn = min(minn, a[i])
		maxx = max(maxx, a[i])
		if prevmaxx < 0 and maxx > 0:
			ans.append(prevmaxx)
			minn = maxx = maxx
		if prevminn > 0 and minn < 0:
			ans.append(prevmaxx)
			minn = maxx = minn
	ans.append(maxx)
	print(sum(ans))


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	NOW = []
	SUM = 0
	for i in range(n):
		if NOW == []:
			NOW.append(A[i])
		elif NOW[0] * A[i] < 0:
			SUM += max(NOW)
			NOW = [A[i]]
		else:
			NOW.append(A[i])
	if NOW != []:
		SUM += max(NOW)
	print(SUM)


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	sum = 0
	i = 0
	while i < len(l):
		curr = l[i]
		j = i
		while j < n and l[i] * l[j] > 0:
			curr = max(curr, l[j])
			j += 1
		sum += curr
		i = j - 1
		i += 1
	print(sum)


for r in range(int(input())):
	n = int(input())
	(u, r, x, z) = (1, 1, 0, 0)
	p = []
	(y, k, t) = (0, 0, 0)
	l = list(map(int, input().split()))
	for i in range(len(l)):
		if l[i] > 0:
			x = 1
			t = t + y
			y = 0
			r = 1
			if u == 1:
				u = 0
				k = l[i]
				if i == len(l) - 1:
					p.append(k)
			else:
				if l[i] > k:
					k = l[i]
					u = 0
				if i == len(l) - 1:
					p.append(k)
		else:
			z = 1
			u = 1
			t = t + k
			k = 0
			if r == 1:
				y = l[i]
				r = 0
				if i == len(l) - 1:
					p.append(y)
			else:
				if l[i] > y:
					y = l[i]
					r = 0
				if i == len(l) - 1:
					p.append(y)
	print(t + p[0])


for _ in range(1, int(input()) + 1):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	tot = 0
	while i < n:
		sign = a[i] > 0
		j = i + 1
		ans = a[i]
		while j < n and (a[j] > 0) == sign:
			ans = max(ans, a[j])
			j += 1
		tot += ans
		i = j
	print(tot)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	lp = 0
	ln = 0
	sm = list()
	le = a[0]
	for i in range(1, n):
		if a[i] > 0 and le > 0 or (a[i] < 0 and le < 0):
			le = max(le, a[i])
		else:
			sm.append(le)
			le = a[i]
	sm.append(le)
	print(sum(sm))


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	sum = 0
	i = 0
	while i != len(li):
		if li[i] < 0:
			minneg = li[i]
			while i < len(li) and li[i] < 0:
				minneg = max(minneg, li[i])
				i = i + 1
			sum = sum + minneg
		else:
			minneg = li[i]
			while i < len(li) and li[i] > 0:
				minneg = max(minneg, li[i])
				i = i + 1
			sum = sum + minneg
	print(sum)


from collections import defaultdict as dc
from heapq import *
import math
import bisect
import sys
from collections import deque as dq
from heapq import heapify, heappush, heappop
mod = 10 ** 12

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

def line():
	p = list(map(int, input().split()))
	return p

def read_mat():
	n = inp()
	a = []
	for i in range(n):
		a.append(line())
	return a

def digit(n):
	s = str(n)
	p = 0
	for i in s:
		p += int(i) ** 2
	return p

def check(a, b):
	for i in range(26):
		for j in range(len(a) + 1):
			q = a[:j] + chr(97 + i) + a[j:]
			if q == b:
				return 1
	return 0

def solve(a, n):
	ans = 0
	l = max(a)
	r = min(a)
	if l < 0:
		return l
	if r > 0:
		return l
	i = 0
	while i < n:
		if a[i] < 0:
			l = float('-inf')
			while i < n and a[i] < 0:
				l = max(l, a[i])
				i += 1
			ans += l
		else:
			r = float('-inf')
			while i < n and a[i] > 0:
				r = max(r, a[i])
				i += 1
			ans += r
	return ans
for _ in range(inp()):
	n = inp()
	a = line()
	l = solve(a, n)
	print(l)


def solve(arr, n):
	s = 0
	mi = arr[0]
	ma = arr[0]
	c = -1
	for i in range(n - 1):
		if arr[i] * arr[i + 1] < 0:
			s = s + max(arr[c + 1:i + 1])
			c = i
	if c < n - 1:
		s = s + max(arr[c + 1:])
	return s
t = int(input())
while t:
	t = t - 1
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr, n))


import math

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

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

def li():
	return list(mi())
for _ in range(ii()):
	n = int(input())
	a = li()
	j = 0
	i = 0
	maxx = -1000000007
	c = []
	while j < n:
		if j < n and a[j] > 0:
			i = 0
			while j + i < n and a[j + i] > 0:
				if a[j + i] > maxx:
					maxx = a[j + i]
				i += 1
			j += i
			c.append(maxx)
			maxx = -1000000007
		if j < n and a[j] < 0:
			i = 0
			while j + i < n and a[j + i] < 0:
				if a[j + i] > maxx:
					maxx = a[j + i]
				i += 1
			j += i
			c.append(maxx)
			maxx = -1000000007
	print(sum(c))


from collections import Counter, defaultdict
from heapq import heappush, heappop, heapify
import math
from functools import reduce
import sys

def all_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 = int(input())
for _ in range(t):
	n = int(input())
	ls = list(map(int, input().strip().split()))
	search = True
	choose = ls[0]
	if ls[0] < 0:
		search = False
	ans = 0
	for val in ls[1:]:
		if search:
			if val > 0:
				choose = max(choose, val)
			else:
				ans += choose
				choose = val
				search = not search
		elif val < 0:
			choose = max(choose, val)
		else:
			ans += choose
			choose = val
			search = not search
	ans += choose
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	k = []
	flag1 = 0
	flag2 = 0
	h = []
	g = []
	for i in range(n):
		if l[i] < 0:
			if len(h) > 0:
				k.append(max(h))
				h = []
			g.append(l[i])
		elif l[i] > 0:
			if len(g) > 0:
				k.append(max(g))
				g = []
			h.append(l[i])
	if len(h) > 0:
		k.append(max(h))
	if len(g) > 0:
		k.append(max(g))
	print(sum(k))


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	max = x[0]
	sum = 0
	maxsign = x[0] / abs(x[0])
	for j in range(n):
		if maxsign == x[j] / abs(x[j]):
			if max < x[j]:
				max = x[j]
		else:
			maxsign = x[j] / abs(x[j])
			sum += max
			max = x[j]
	sum += max
	print(sum)


inf = 10 ** 9 + 7
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	if a[0] > 0:
		neg_f = False
	else:
		neg_f = True
	tmp = a[0]
	for num in a:
		if neg_f and num > 0 or (not neg_f and num < 0):
			ans += tmp
			neg_f = not neg_f
			tmp = -inf
		tmp = max(num, tmp)
	ans += tmp
	print(ans)


from sys import stdin
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	sign = 1 if a[0] > 0 else -1
	biggest = a[0]
	res = 0
	for (idx, value) in enumerate(a):
		if value * sign < 0:
			res += biggest
			biggest = value
			sign = 1 if value > 0 else -1
		elif value > biggest:
			biggest = value
	res += biggest
	print(res)


t = int(input())
for q in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	neg = a[0] < 0
	cur = a[0]
	ans = 0
	for i in range(n):
		if neg and a[i] > 0 or (not neg and a[i] < 0):
			ans += cur
			neg = not neg
			cur = a[i]
		else:
			cur = max(cur, a[i])
	ans += cur
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	if l[0] > 0:
		k = 1
	else:
		k = -1
	s = 0
	x = []
	for i in range(n):
		if k == 1:
			if l[i] > 0:
				x.append(l[i])
			else:
				s = s + max(x)
				x = []
				x.append(l[i])
				k = -1
		elif k == -1:
			if l[i] < 0:
				x.append(l[i])
			else:
				s = s + max(x)
				x = []
				x.append(l[i])
				k = 1
	if x != []:
		s = s + max(x)
		print(s)
	else:
		print(s)


t = int(input())
for x in range(0, t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	sum = 0
	x = a[0]
	z = 0
	while z < n:
		if a[z] > 0:
			while z < n and a[z] > 0:
				x = max(a[z], x)
				z += 1
			sum += x
		else:
			x = a[z]
			while z < n and a[z] < 0:
				x = max(a[z], x)
				z += 1
			sum += x
	print(sum)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ind = 0
	f = 0
	if a[0] < 0:
		ind = -1
	else:
		ind = 1
	for i in range(1, n):
		if ind == -1 and a[i] > 0:
			f = 1
			break
		elif ind == 1 and a[i] < 0:
			f = 1
			break
	if f == 0:
		print(max(a))
	else:
		sign = [0]
		for i in range(1, n):
			if a[i] > 0 and a[i - 1] < 0:
				sign.append(i)
			elif a[i] < 0 and a[i - 1] > 0:
				sign.append(i)
		s = 0
		for i in range(1, len(sign)):
			s += max(a[sign[i - 1]:sign[i]])
		s += max(a[sign[len(sign) - 1]:])
		print(s)


from math import ceil, sqrt, floor, sqrt
import collections
from typing import List
from fractions import gcd
from collections import deque
from collections import defaultdict, Counter
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	i = 0
	res = 0
	while i < n:
		j = i
		while j < n and (A[i] > 0 and A[j] > 0 or (A[i] < 0 and A[j] < 0)):
			j += 1
		res += max(A[i:j])
		i = j
	print(res)


def main(n, a):
	curr_sum = [-9999999999999] * n
	max_sum = [-9999999999999] * n
	curr_sum[0] = a[0]
	max_sum[0] = a[0]
	for i in range(1, n):
		if a[i] * a[i - 1] > 0:
			variable = curr_sum[i - 1] - a[i - 1]
			currsum = variable + a[i]
			curr_sum[i] = currsum
			max_sum[i] = max(currsum, max_sum[i - 1])
		else:
			curr_sum[i] = a[i] + max_sum[i - 1]
			max_sum[i] = curr_sum[i]
	return max_sum[-1]
k = int(input())
for _ in range(k):
	n = int(input())
	a = list(map(int, input().split()))
	print(main(n, a))


n = int(input())
for i in range(n):
	N = int(input())
	numbers = list(map(int, input().split()))
	eligible = [numbers[0]]
	for j in range(1, len(numbers)):
		current = numbers[j]
		if current * eligible[-1] < 0:
			eligible.append(current)
		elif current > eligible[-1]:
			eligible[-1] = current
	print(sum(eligible))


t = int(input())
for you in range(t):
	n = int(input())
	l = input().split()
	li = [int(i) for i in l]
	i = 0
	curr = li[0]
	maxa = li[0]
	sumi = 0
	while i < n:
		if li[i] * curr > 0:
			maxa = li[i]
			while li[i] * curr > 0:
				maxa = max(li[i], maxa)
				i += 1
				if i == n:
					break
			sumi += maxa
			if i == n:
				break
			else:
				curr = li[i]
		else:
			sumi += li[i]
			curr = li[i]
			maxa = li[i]
	print(sumi)


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	lst = [list(j) for (i, j) in groupby(arr, key=lambda x: x > 0)]
	print(sum((max(i) for i in lst)))


def main():
	for _ in inputt():
		(n,) = inputi()
		A = inputl()
		B = [A[0]]
		for a in A:
			if a * B[-1] < 0:
				B.append(a)
			elif a > B[-1]:
				B[-1] = a
		print(sum(B))
from math import *
from heapq import *
from itertools import *
from functools import reduce, lru_cache, partial
from collections import Counter, defaultdict
import re, copy, operator, cmath
import sys, io, os, builtins
sys.setrecursionlimit(1000)
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.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(io.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):
	sys.stdout.write(kwargs.pop('split', ' ').join(map(str, args)))
	sys.stdout.write(kwargs.pop('end', '\n'))
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip()
inputt = lambda t=0: range(t) if t else range(int(input()))
inputs = lambda : input().split()
inputi = lambda : map(int, inputs())
inputl = lambda t=0, k=inputi: map(list, (k() for _ in range(t))) if t else list(k())

def len(a):
	if isinstance(a, range):
		return -((a.start - a.stop) // a.step)
	return builtins.len(a)

def bisect_left(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if key(a[mid]) < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def bisect_right(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if x < key(a[mid]):
			hi = mid
		else:
			lo = mid + 1
	return lo

def insort_left(a, x, key=None, lo=0, hi=None):
	lo = bisect_left(a, x, key, lo, hi)
	a.insert(lo, x)

def insort_right(a, x, key=None, lo=0, hi=None):
	lo = bisect_right(a, x, key, lo, hi)
	a.insert(lo, x)
do_nothing = lambda x: x
bisect = bisect_right
insort = insort_right

def index(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi and key(a[i]) == x:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_lt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_le(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_gt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_ge(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError
main()


n = int(input())
for i in range(n):
	llen = int(input())
	ar = []
	ss = input().split()
	for num in ss:
		ar.append(int(num))
	sign = ar[0] / abs(ar[0])
	max_pos = 0
	min_neg = 0
	if sign > 0:
		max_pos = ar[0]
	else:
		min_neg = ar[0]
	ans = []
	for i in range(1, len(ar)):
		if ar[i] / abs(ar[i]) != sign:
			if sign > 0:
				ans.append(max_pos)
				max_pos = 0
				min_neg = ar[i]
			elif sign < 0:
				ans.append(min_neg)
				min_neg = -99999999999999999999999999999999
				max_pos = ar[i]
			sign = ar[i] / abs(ar[i])
		elif sign > 0:
			if ar[i] >= max_pos:
				max_pos = ar[i]
		elif ar[i] >= min_neg:
			min_neg = ar[i]
	if sign > 0:
		ans.append(max_pos)
	elif sign < 0:
		ans.append(min_neg)
	print(sum(ans))


T = int(input())
for _ in range(0, T):
	n = int(input())
	s = [int(x) for x in input().split()]
	ans = 0
	mx = s[0]
	for i in range(1, len(s)):
		if s[i] < 0 and s[i - 1] < 0:
			mx = max(mx, s[i])
		elif s[i] > 0 and s[i - 1] > 0:
			mx = max(mx, s[i])
		else:
			ans += mx
			mx = s[i]
	ans += mx
	print(ans)


case = int(input())
for n in range(case):
	n = int(input())
	a = list(map(lambda x: int(x), input().split()))
	goal = []
	s = 0
	goal.append(a[0])
	for i in range(1, n):
		if a[i] * a[i - 1] < 0:
			s = s + max(goal)
			goal = []
		goal.append(a[i])
	s = s + max(goal)
	print(s)


def same_sign(a, b):
	if a * b < 0:
		return False
	else:
		return True

def main():
	t = int(input())
	while t:
		t -= 1
		n = int(input())
		a = list(map(int, input().split()))
		max_sum = 0
		if n == 1:
			print(a[0])
		else:
			(i, j) = (0, 1)
			while n > i:
				while n > j and same_sign(a[i], a[j]):
					j += 1
				max_sum += max(a[i:j])
				i = j
				j += 1
			print(max_sum)
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	s = 0
	(i, j) = (0, 0)
	while j != n - 1:
		if (a[j] > 0) == (a[j + 1] > 0):
			j += 1
		else:
			s += max(a[i:j + 1])
			j += 1
			i = j
	s += max(a[i:j + 1])
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = a[0] > 0
	ans = 0
	i = 0
	for j in range(n):
		if flag:
			if a[j] < 0:
				ans += a[i]
				flag = not flag
				i = j
			elif a[j] > a[i]:
				i = j
		elif a[j] > 0:
			ans += a[i]
			flag = not flag
			i = j
		elif a[j] > a[i]:
			i = j
	ans += a[i]
	print(ans)


def Function():
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
		return
	s = 0
	index = 0
	negOrPos = 0
	start = 0
	if a[0] > a[-1]:
		start = a[index]
		s += start
		if start < 0:
			negOrPos = -1
		else:
			negOrPos = 1
		while index < n - 1:
			index += 1
			if negOrPos == 1:
				if a[index] < 0:
					start = a[index]
					s += start
					negOrPos = -1
				elif a[index] > start:
					s -= start
					start = a[index]
					s += start
			elif a[index] > 0:
				start = a[index]
				s += start
				negOrPos = 1
			elif a[index] > start:
				s -= start
				start = a[index]
				s += start
	else:
		index = n - 1
		start = a[index]
		s += start
		if start < 0:
			negOrPos = -1
		else:
			negOrPos = 1
		while index > 0:
			index -= 1
			if negOrPos == 1:
				if a[index] < 0:
					start = a[index]
					s += start
					negOrPos = -1
				elif a[index] > start:
					s -= start
					start = a[index]
					s += start
			elif a[index] > 0:
				start = a[index]
				s += start
				negOrPos = 1
			elif a[index] > start:
				s -= start
				start = a[index]
				s += start
	print(s)
for i in range(int(input())):
	Function()


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(neg, pos) = (0, 0)
	sum1 = 0
	mini = -math.inf
	maxi = 0
	for i in range(len(a)):
		if a[i] < 0:
			mini = max(mini, a[i])
			sum1 += maxi
			maxi = 0
		if a[i] > 0:
			maxi = max(maxi, a[i])
			if mini != -math.inf:
				sum1 += mini
			mini = -math.inf
	if a[len(a) - 1] < 0 and a[len(a) - 2] < 0:
		sum1 += mini
		print(sum1)
		continue
	if a[len(a) - 1] > 0 and a[len(a) - 2] > 0:
		sum1 += maxi
		print(sum1)
		continue
	else:
		sum1 += a[len(a) - 1]
	print(sum1)


z = input
from math import *
for _ in range(int(z())):
	n = int(z())
	l = list(map(int, z().split()))
	k = 0
	lp = []
	ln = []
	la = []
	k = 0
	m = 0
	for i in range(n):
		if l[i] > 0:
			lp.append(l[i])
			try:
				la.append(max(ln))
				ln = []
			except:
				pass
		if l[i] < 0:
			ln.append(l[i])
			try:
				la.append(max(lp))
				lp = []
			except:
				pass
	ans = 0
	try:
		ans += max(lp)
	except:
		pass
	try:
		ans += max(ln)
	except:
		pass
	print(sum(la) + ans)


T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = -1
	last_positive = 0
	minn = min(arr)
	last_negative = minn
	lis = []
	for i in arr:
		if i > last_positive:
			if flag == 0:
				lis.append(last_negative)
				last_negative = minn
			last_positive = i
			flag = 1
		elif 0 > i >= last_negative:
			if flag == 1:
				lis.append(last_positive)
				last_positive = 0
			last_negative = i
			flag = 0
	if flag == 0:
		lis.append(last_negative)
	if flag == 1:
		lis.append(last_positive)
	print(sum(lis))


for _ in range(int(input())):
	n = int(input())
	a = tuple(map(int, input().split()))
	INF = 10 ** 10
	if len(a) == 0:
		print(0)
		continue
	if a[0] > 0:
		prev_sign = 1
	else:
		prev_sign = -1
	ret = []
	a_max = -INF
	for ai in a:
		if prev_sign * ai < 0:
			if a_max != -INF:
				ret.append(a_max)
				a_max = -INF
			prev_sign *= -1
		if ai > a_max:
			a_max = ai
	if a_max != -INF:
		ret.append(a_max)
	print(sum(ret))


T = int(input())
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	a = []
	last = -1
	maxx = int(-1000000000000.0)
	minn = int(1000000000000.0)
	for i in l:
		if i > 0:
			if last == -1:
				last = 0
				maxx = i
			elif last == 0:
				maxx = max(i, maxx)
			else:
				a.append(minn)
				minn = int(1000000000000.0)
				maxx = i
				last = 0
		elif last == -1:
			last = 1
			minn = i
		elif last == 1:
			minn = max(i, minn)
		else:
			a.append(maxx)
			maxx = int(-1000000000000.0)
			minn = i
			last = 1
	if last == 0:
		a.append(maxx)
	else:
		a.append(minn)
	print(sum(a))


def sol(l, n):
	m = 0
	cm = l[0]
	prev = 0 if cm < 0 else 1
	for i in range(1, n):
		if l[i] < 0:
			if not prev:
				cm = max(l[i], cm)
			else:
				m += cm
				cm = l[i]
				prev = 0
		elif prev:
			cm = max(cm, l[i])
		else:
			m += cm
			cm = l[i]
			prev = 1
	return m + cm
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(sol(l, n))


N = int(input())
for i in range(N):
	n = int(input())
	s = list(map(int, input().split()))
	a = []
	t = 0
	if s[0] > 0:
		t = s[0]
		for i in range(1, n):
			if s[i] > 0 and s[i - 1] > 0:
				t = max(t, s[i])
			if s[i] < 0 and s[i - 1] > 0:
				a.append(t)
				t = min(t, s[i])
			if s[i] < 0 and s[i - 1] < 0:
				t = max(t, s[i])
			if s[i] > 0 and s[i - 1] < 0:
				a.append(t)
				t = max(t, s[i])
			if i == n - 1:
				if len(a) > 0:
					if a[len(a) - 1] > 0 and t > 0:
						a[len(a) - 1] = max(t, a[len(a) - 1])
					if a[len(a) - 1] < 0 and t < 0:
						a[len(a) - 1] = max(t, a[len(a) - 1])
					else:
						a.append(t)
				else:
					a.append(t)
	if s[0] < 0:
		t = s[0]
		for i in range(1, n):
			if s[i] > 0 and s[i - 1] > 0:
				t = max(t, s[i])
			if s[i] < 0 and s[i - 1] > 0:
				a.append(t)
				t = min(t, s[i])
			if s[i] < 0 and s[i - 1] < 0:
				t = max(t, s[i])
			if s[i] > 0 and s[i - 1] < 0:
				a.append(t)
				t = max(t, s[i])
			if i == n - 1:
				if len(a) > 0:
					if a[len(a) - 1] > 0 and t > 0:
						a[len(a) - 1] = max(t, a[len(a) - 1])
					if a[len(a) - 1] < 0 and t < 0:
						a[len(a) - 1] = max(t, a[len(a) - 1])
					else:
						a.append(t)
				else:
					a.append(t)
	if n == 1:
		print(s[0])
		continue
	print(sum(a))


import math
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	m = [l[0]]
	s = 0
	for i in range(n - 1):
		if l[i] * l[i + 1] > 0:
			m.append(l[i + 1])
		else:
			s += max(m)
			m = [l[i + 1]]
	s += max(m)
	print(s)


t = int(input())
from math import inf
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	res = 0
	i = 0
	while i < n:
		pos = 0
		while i < n and arr[i] > 0:
			pos = max(pos, arr[i])
			i += 1
		res += pos
		neg = -inf
		while i < n and arr[i] < 0:
			neg = max(neg, arr[i])
			i += 1
		if neg != -inf:
			res += neg
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	main = []
	li = []
	i = 0
	s = 0
	while i < n:
		if arr[i] < 0:
			while i < n:
				if arr[i] > 0:
					break
				li.append(arr[i])
				i += 1
			main.append(li)
			li = []
		else:
			while i < n:
				if arr[i] < 0:
					break
				li.append(arr[i])
				i += 1
			main.append(li)
			li = []
	for i in main:
		s += max(i)
	print(s)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	idx = 0
	res = 0
	b = []
	if n == 1:
		print(a[0])
		continue
	for i in range(1, len(a)):
		if a[i] * a[i - 1] < 0:
			b.append(a[idx:i])
			idx = i
		if i == n - 1:
			b.append(a[idx:i + 1])
	for i in range(len(b)):
		res += max(b[i])
	print(res)


for j in [*open(0)][2::2]:
	l = [*map(int, j.split())]
	(q, w, r) = (l[0], [], [])
	for i in l:
		if q * i > 0:
			r += [i]
		else:
			q *= -1
			w += [max(r)]
			r = [i]
	print(sum(w) + max(r))


import math
N = int(input())
for i in range(N):
	n = int(input())
	a = list(map(int, input().strip().split(' ')))
	sum = 0
	prev = None
	for x in a:
		if prev is None:
			prev = x
			continue
		elif prev < 0 and x > 0:
			sum += prev
			prev = x
		elif prev > 0 and x < 0:
			sum += prev
			prev = x
		elif x > prev:
			prev = x
	print(sum + prev)


from sys import stdin, stdout
t = int(stdin.readline())
NINF = -10 ** 12
while t > 0:
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	ans = 0
	i = 0
	while i < n:
		if a[i] > 0:
			m = NINF
			while i < n and a[i] > 0:
				m = max(m, a[i])
				i += 1
			ans += m
		else:
			m = NINF
			while i < n and a[i] < 0:
				m = max(m, a[i])
				i += 1
			ans += m
	stdout.write('{}\n'.format(ans))
	t -= 1


t = int(input())
for p in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	k = 1
	chck = a[0]
	for i in range(1, n):
		if a[i] * chck < 0:
			k += 1
			chck = a[i]
	if k == 1:
		print(max(a))
	else:
		sum = 0
		i = 0
		while i < n and k > 0:
			if i == n - 1:
				sum += a[n - 1]
				k -= 1
			else:
				j = i + 1
				temp = [a[i]]
				while j < n and a[j] * a[i] > 0:
					temp.append(a[j])
					j += 1
				sum += max(temp)
				i = j
				k -= 1
		print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = l[0]
	p = []
	c = 0
	p.append(x)
	for i in range(n):
		if p[-1] > 0:
			if l[i] <= 0:
				p.append(l[i])
			elif p[-1] < l[i]:
				p[-1] = l[i]
		if p[-1] < 0:
			if l[i] >= 0:
				p.append(l[i])
			elif p[-1] < l[i]:
				p[-1] = l[i]
	print(sum(p))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	c = 0
	su = 0
	while c < n:
		mp = 0
		mn = -9999999999
		while l[c] > 0:
			if l[c] > mp:
				mp = l[c]
			c = c + 1
			if c >= n:
				break
		su = su + mp
		if c >= n:
			break
		while l[c] < 0:
			if l[c] > mn:
				mn = l[c]
			c = c + 1
			if c >= n:
				break
		su = su + mn
	print(su)


import sys
INT_MAX = sys.maxsize
INT_MIN = -sys.maxsize
sys.setrecursionlimit(10 ** 7)
mod = 1000000007

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

def sublist(somelist):
	result = []
	a = []
	for i in somelist:
		if i >= 0:
			a.append(i)
		else:
			if a:
				result.append(a)
			a = []
	if a:
		result.append(a)
	return result

def subnlist(somelist):
	result = []
	a = []
	for i in somelist:
		if i < 0:
			a.append(i)
		else:
			if a:
				result.append(a)
			a = []
	if a:
		result.append(a)
	return result

def main():
	t = int(sys.stdin.readline())
	for _ in range(t):
		nn = int(sys.stdin.readline())
		a = list(map(int, sys.stdin.readline().split()))
		p = sublist(a)
		n = subnlist(a)
		ans = 0
		for i in p:
			ans += max(i)
		for i in n:
			ans += max(i)
		print(ans)
try:
	IOE()
except:
	pass
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	max1 = -10 ** 100
	sum1 = 0
	for i in range(0, n):
		if l[i] > 0:
			f1 = 1
		else:
			f1 = -1
		if abs(f - f1) == 2:
			sum1 = sum1 + max1
			max1 = -10 ** 100
		max1 = max(max1, l[i])
		f = f1
	print(sum1 + max1)


for _ in range(int(input())):
	(n,) = map(int, input().split())
	r = 0
	c = 0
	for v in map(int, input().split()):
		if c * v > 0:
			c = max(c, v)
		else:
			r += c
			c = v
	print(r + c)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	pmax = 0
	nmax = -10 ** 9
	seq = a[0] / abs(a[0])
	sum1 = 0
	for i in a:
		if i > 0 and seq == -1:
			sum1 += nmax
			nmax = -10 ** 9
			seq = 1
		elif i < 0 and seq == 1:
			sum1 += pmax
			pmax = 0
			seq = -1
		if i > 0:
			pmax = max(pmax, i)
		else:
			nmax = max(nmax, i)
	if seq == -1:
		sum1 += nmax + pmax
	else:
		sum1 += pmax
	print(sum1)


import sys
import math
from collections import deque
import random

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

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

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

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
t = read_int()
for i in range(t):
	n = read_int()
	a = read_int_line()
	if a[0] > 0:
		sign = 1
	else:
		sign = 0
	m = a[0]
	s = 0
	ele = []
	for i in range(1, n):
		if a[i] > 0 and sign == 1:
			m = max(m, a[i])
		elif a[i] > 0 and sign == 0:
			s += m
			ele.append(m)
			m = a[i]
			sign = 1
		if a[i] < 0 and sign == 0:
			m = max(m, a[i])
		elif a[i] < 0 and sign == 1:
			s += m
			ele.append(m)
			m = a[i]
			sign = 0
	ele.append(m)
	s = sum(ele)
	print(s)


tes = int(input())
for t in range(tes):
	n = int(input())
	l = list(map(int, input().split(' ')))
	a = []
	for i in range(n):
		a.append(0)
	sc = 0
	cr_sgn = ''
	if len(l) > 1:
		if l[0] > 0:
			if l[1] > 0:
				a[0] = 1
				sc = 1
			else:
				a[0] = 0
		elif l[1] < 0:
			a[0] = -1
			sc = 1
		else:
			a[0] = 0
	for i in range(1, n - 1):
		pr = l[i - 1]
		cr = l[i]
		nx = l[i + 1]
		if cr > 0:
			if pr < 0:
				sc = 0
			if nx > 0 or pr > 0:
				sc = sc + 1
				a[i] = sc
			else:
				sc = 0
		else:
			if pr > 0:
				sc = 0
			if nx < 0 or pr < 0:
				sc = sc + 1
				a[i] = -1 * sc
			else:
				sc = 0
	if len(l) > 1:
		if l[n - 1] > 0:
			if l[n - 2] > 0:
				a[n - 1] = abs(a[n - 2]) + 1
			else:
				a[n - 1] = 0
		elif l[n - 2] < 0:
			a[n - 1] = -1 * (abs(a[n - 2]) + 1)
			sc = 1
		else:
			a[n - 1] = 0
	else:
		c = max(l)
	i = 0
	st = '*'
	z = []
	c = 0
	while i < n:
		if a[i] == 0:
			if st == '*':
				c = c + l[i]
			elif st != '*':
				c = c + max(z)
				c = c + l[i]
				st = '*'
			z = []
			i = i + 1
		elif a[i] > 0:
			if st == '*' or st == '+':
				st = '+'
				z.append(l[i])
				i = i + 1
			elif st == '-':
				c = c + max(z)
				z = []
				z.append(l[i])
				i = i + 1
				st = '+'
		elif a[i] < 0:
			if st == '*' or st == '-':
				st = '-'
				z.append(l[i])
				i = i + 1
			elif st == '+':
				c = c + max(z)
				z = []
				z.append(l[i])
				i = i + 1
				st = '-'
	if st != '*':
		if len(z) > 0 and z[0] > 0:
			c = c + max(z)
		elif len(z) > 0 and z[0] < 0:
			c = c + max(z)
	print(c)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	j = 0
	suma = 0
	while j < n:
		if l[j] < 0:
			mini = l[j]
			while l[min(j, n - 1)] < 0 and j < n:
				if mini < l[j]:
					mini = l[j]
				j += 1
			suma += mini
		else:
			maxi = l[j]
			while l[min(j, n - 1)] > 0 and j < n:
				if maxi < l[j]:
					maxi = l[j]
				j += 1
			suma += maxi
	print(suma)


import sys
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sub_seq = [[a[0]]]
	sign = ['-', '+'][a[0] > 0]
	(min_neg, max_pos) = (0, 0)
	for num in a[1:]:
		if num > 0:
			if sign == '+':
				sub_seq[-1].append(num)
			else:
				sub_seq.append([num])
				sign = '+'
		elif sign == '-':
			sub_seq[-1].append(num)
		else:
			sub_seq.append([num])
			sign = '-'
	res = [max(x) for x in sub_seq]
	total = sum(res)
	print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	sign = []
	temp = [arr[0]]
	for i in range(n - 1):
		if arr[i] * arr[i + 1] < 0:
			temp.sort()
			sign += [temp[:]]
			temp = [arr[i + 1]]
		else:
			temp += [arr[i + 1]]
	temp.sort()
	sign += [temp[:]]
	ans = 0
	for x in sign:
		ans += x[-1]
	print(ans)


for i in range(int(input())):
	lenght = int(input())
	a = [int(i1) for i1 in input().split()]
	err = True
	max = 0
	min1 = min(a)
	sum = 0
	if a[0] < 0:
		err = False
		max = min1
	for c in range(lenght - 1):
		if a[c] > max:
			max = a[c]
		if err == True:
			if a[c + 1] < 0:
				sum += max
				max = min1
				err = False
		elif a[c + 1] > 0:
			sum += max
			max = 0
			err = True
	if err == True and a[lenght - 1] < 0:
		sum += a[lenght - 1]
	elif err == True and a[lenght - 1] > 0:
		if a[lenght - 1] > max:
			sum += a[lenght - 1]
		else:
			sum += max
	elif err == False and a[lenght - 1] > 0:
		sum += a[lenght - 1]
	elif err == False and a[lenght - 1] < 0:
		if a[lenght - 1] > max:
			sum += a[lenght - 1]
		else:
			sum += max
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	x = 0
	while x < len(a) - 1:
		if a[x] * a[x + 1] < 0:
			b.append(x + 1)
			x += 1
		else:
			x += 1
	res = [a[i:j] for (i, j) in zip([0] + b, b + [None])]
	sumn = 0
	for q in res:
		sumn += max(q)
	print(sumn)


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


from sys import setrecursionlimit
from collections import deque
import heapq
import functools
setrecursionlimit(10 ** 6)

def IntInput():
	return int(input())

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

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

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

def modInverse(p, a, m):
	return p % m * pow(a % m, m - 2, m) % m

def compare(item1, item2):
	return item2[0] - item1[0] if item1[0] != item2[0] else item1[1] - item2[1]

def DFS(s, n, paths, answer):
	visited = [False] * (n + 1)
	stack = []
	stack.append(s)
	visited[s] = True
	DFSArray = []
	while stack:
		s = stack.pop()
		DFSArray.append(s)
		flag = 0
		for i in paths[s]:
			if visited[i] == False:
				flag = 1
				stack.append(i)
				visited[i] = True
		if not flag:
			answer[s - 1][1] = 0
	while DFSArray != []:
		s = DFSArray.pop()
		if answer[s - 1][1] < 0:
			answer[s - 1][1] = 0
			for i in paths[s]:
				if answer[i - 1][1] >= 0:
					answer[s - 1][1] += answer[i - 1][1] + 1
	return answer

def BFS(s, n, paths, answer):
	visited = [False] * (n + 1)
	answer[s - 1][0] = 0
	queue = deque()
	queue.append(s)
	visited[s] = True
	while queue:
		s = deque.popleft(queue)
		for i in paths[s]:
			if visited[i] == False:
				queue.append(i)
				answer[i - 1][0] = answer[s - 1][0] + 1
				visited[i] = True
	return answer
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for _ in range(int(input())):
	n = int(input())
	array = IntArrayInput()
	ansArray = [array[0]]
	for i in array:
		if i * ansArray[-1] >= 0:
			if i >= ansArray[-1]:
				ansArray[len(ansArray) - 1] = i
		else:
			ansArray.append(i)
	print(sum(ansArray))


def sign(a):
	if a < 0:
		return -1
	elif a > 0:
		return 1
	else:
		return 0
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	i = 0
	while i < len(a):
		max = a[i]
		i += 1
		while i < len(a) and sign(a[i]) == sign(max):
			if a[i] > max:
				max = a[i]
			i += 1
		s += max
	print(s)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l = list(map(int, input().split()))
	s = []
	i = 0
	while i < n:
		l1 = []
		if l[i] < 0:
			while i < n and l[i] < 0:
				l1.append(l[i])
				i += 1
		else:
			while i < n and l[i] >= 0:
				l1.append(l[i])
				i += 1
		s.append(l1)
	ans = 0
	for item in s:
		ans += max(item)
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	B = []
	if A[0] > 0:
		pl = '+'
	else:
		pl = '-'
	val = A[0]
	if N == 1:
		print(A[0])
	else:
		for i in range(1, len(A)):
			if A[i] < 0 and pl == '+':
				B.append(val)
				pl = '-'
				val = A[i]
				if i == len(A) - 1:
					B.append(val)
			elif A[i] > 0 and pl == '+':
				val = max(val, A[i])
				if i == len(A) - 1:
					B.append(val)
			elif A[i] < 0 and pl == '-':
				val = max(A[i], val)
				if i == len(A) - 1:
					B.append(val)
			elif A[i] > 0 and pl == '-':
				B.append(val)
				pl = '+'
				val = A[i]
				if i == len(A) - 1:
					B.append(val)
		print(sum(B))


import math

def main():
	for q in range(int(input())):
		N = int(input())
		l = list(map(int, input().split()))
		lb = -10 ** 12
		ans = 0
		curr = lb
		for i in range(len(l)):
			curr = max(curr, l[i])
			if i < N - 1 and l[i] * l[i + 1] < 0:
				ans += curr
				curr = lb
		ans += curr
		print(ans)
main()


def AlternatingSubsequence(n, A):
	vis = [0] * n
	for i in range(n):
		if A[i] > 0:
			vis[i] = 1
	i = 0
	sum = 0
	while i < n:
		M = A[i]
		j = i + 1
		while j < n and vis[i] == vis[j]:
			M = max(M, A[j])
			j += 1
		sum += M
		i = j
	return sum
L = []
t = int(input())
for i in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	L.append(AlternatingSubsequence(n, A))
for l in L:
	print(l)


import math
LI = lambda : list(map(int, input().split()))
MI = lambda : map(int, input().split())
yes = lambda : print('YES')
no = lambda : print('NO')
I = lambda : list(input())
J = lambda x: ''.join(x)
II = lambda : int(input())
SI = lambda : input()
t = II()
mi = -10 ** 9
for q in range(t):
	n = II()
	a = LI()
	c1 = 0
	c2 = 0
	x = False
	y = False
	tt = -1
	su = 0
	if a[0] > 0:
		tt = 1
	else:
		tt = -1
	for i in range(n):
		if a[i] > 0:
			if x == False:
				x = True
				c2 = a[i]
			elif y == True and tt == 1:
				su += c1 + c2
				c1 = 0
				c2 = a[i]
				x = True
				y = False
			elif a[i] > c2:
				c2 = a[i]
		elif y == False:
			y = True
			c1 = a[i]
		elif x == True and tt == -1:
			su += c1 + c2
			c1 = a[i]
			c2 = 0
			x = False
			y = True
		elif a[i] > c1:
			c1 = a[i]
	su += c1 + c2
	print(su)


for _ in range(int(input())):
	n = int(input())
	ll = list(map(int, input().split()))
	i = 1
	st = [ll[0]]
	f = []
	while i < n:
		if st[-1] > 0 and ll[i] > 0:
			st.append(ll[i])
		elif st[-1] < 0 and ll[i] < 0:
			st.append(ll[i])
		else:
			f.append(max(st))
			st.clear()
			st.append(ll[i])
		i += 1
	if len(st) > 0:
		f.append(max(st))
	print(sum(f))


rw = int(input())
for weqeq in range(rw):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	bs = set()
	d = a[0] > 0
	for i in range(n):
		if (a[i] > 0) != d:
			b.append(max(bs))
			bs = {a[i]}
		bs.add(a[i])
		d = a[i] > 0
	b.append(max(bs))
	print(sum(b))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = 0
	m = 0
	b = []
	c = []
	s = 0
	for i in range(len(a)):
		if a[i] > 0:
			if len(c) > 0:
				s += max(c)
				c = []
			b.append(a[i])
		else:
			if len(b) > 0:
				s += max(b)
				b = []
			c.append(a[i])
	if len(b) > 0:
		s += max(b)
	if len(c) > 0:
		s += max(c)
	print(s)


import sys
input = sys.stdin.readline
for tc in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	temp = [l[0]]
	p = -1 if l[0] < 0 else 1
	for i in range(1, n):
		pp = -1 if l[i] < 0 else 1
		if p == pp:
			temp.append(l[i])
		else:
			p = pp
			ans.append(temp)
			temp = [l[i]]
	ans.append(temp)
	an = 0
	for i in ans:
		an += max(i)
	print(an)


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


t = int(input())

def sign(val):
	if val < 0:
		return '-'
	return '+'
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	S = sign(arr[0])
	mx = arr[0]
	ans = []
	for x in range(1, n):
		if sign(arr[x]) == S:
			mx = max(arr[x], mx)
		else:
			ans.append(mx)
			mx = arr[x]
			S = sign(arr[x])
	ans.append(mx)
	print(sum(ans))


for t in range(int(input())):
	n = int(input())
	L = [int(X) for X in input().split()]
	list = [L[0]]
	for i in range(1, n):
		if list[-1] > 0:
			if L[i] > 0 and L[i] > list[-1]:
				list[-1] = L[i]
			elif L[i] < 0:
				list.append(L[i])
		elif L[i] < 0 and L[i] > list[-1]:
			list[-1] = L[i]
		elif L[i] > 0:
			list.append(L[i])
	print(sum(list))


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


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	pos = a[0] > 0
	best = a[0]
	s = 0
	for i in range(1, n):
		ai = a[i]
		if pos and ai > 0 or (not pos and ai < 0):
			best = max(best, ai)
		else:
			s += best
			pos = ai > 0
			best = ai
	s += best
	print(s)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


inf1 = 100000000000
inf2 = -100000000000
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	pre = 0
	ans = 0
	for i in range(n):
		if flag == 0:
			if l[i] < 0:
				flag = 1
			else:
				flag = 2
			pre = l[i]
		elif flag == 1:
			if l[i] < 0:
				if l[i] > pre:
					pre = l[i]
			else:
				ans += pre
				pre = l[i]
				flag = 2
		elif flag == 2:
			if l[i] > 0:
				if l[i] > pre:
					pre = l[i]
			else:
				ans += pre
				pre = l[i]
				flag = 1
	print(ans + pre)
	t -= 1


import math

def getdyfirst(lis, a):
	res = None
	for i in range(a, len(l)):
		if l[i] > 0:
			res = i
			break
	if res == None:
		return len(lis)
	else:
		return res

def getxyfirst(lis, a):
	res = None
	for i in range(a, len(lis)):
		if l[i] < 0:
			res = i
			break
	if res == None:
		return len(lis)
	else:
		return res
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	t = []
	k = 0
	s = 0
	while k <= len(l) - 1:
		if l[k] < 0:
			d = getdyfirst(l, k)
		else:
			d = getxyfirst(l, k)
		s += max(l[k:d])
		k = d
	print(s)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = []
	lis = []
	prevsign = -1
	if l[0] > 0:
		prevsign = 1
	else:
		prevsign = 0
	i = 0
	for i in l:
		if i > 0 and prevsign == 1 or (i < 0 and prevsign == 0):
			lis.append(i)
		elif prevsign == 1:
			a.append(max(lis))
			lis = []
			lis.append(i)
			prevsign = 0
		else:
			a.append(max(lis))
			lis = []
			lis.append(i)
			prevsign = 1
	if prevsign == 1:
		a.append(max(lis))
		lis = []
		lis.append(i)
		prevsign = 0
	else:
		a.append(max(lis))
		lis = []
		lis.append(i)
		prevsign = 1
	print(sum(a))


t = int(input())
array = []
for i in range(t):
	n = int(input())
	array.append(n)
	arr = list(map(int, input().split()))
	array.append(arr)

def alt_subs(n, array):
	arr = []
	count = 0
	for i in range(1, n):
		if array[i - 1] > 0 and array[i] < 0 or (array[i - 1] < 0 and array[i] > 0):
			if count == 0 or array[i - 1] > arr[count - 1]:
				arr.append(array[i - 1])
				count += 1
			arr.append(array[i])
			count += 1
		elif count == 0:
			if array[i - 1] > array[i]:
				arr.append(array[i - 1])
				count += 1
			else:
				arr.append(array[i])
				count += 1
		elif array[i] > arr[count - 1]:
			arr[count - 1] = array[i]
	if len(arr) == 0:
		arr.append(max(array))
	return sum(arr)
for i in range(0, t * 2, 2):
	n = array[i]
	arr = array[i + 1]
	print(alt_subs(n, arr))


def getPol(n):
	if n < 0:
		pol = -1
	else:
		pol = 1
	return pol
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	s = 0
	m = -1000000001
	while i < n:
		pol = getPol(a[i])
		j = i
		m = -1000000001
		while j < n:
			if getPol(a[j]) != pol:
				break
			else:
				m = max(a[j], m)
			j += 1
		i = j
		s += m
	print(s)
	t -= 1


from collections import defaultdict
from collections import Counter
from string import ascii_lowercase
import math
from sys import setrecursionlimit as srl
srl(2 * 10 ** 6)

def solve(n, a):
	ans = 0
	i = 0
	while i < n:
		j = i
		mx = a[i]
		while j < n and abs(a[i]) + abs(a[j]) == abs(a[i] + a[j]):
			mx = max(mx, a[j])
			j += 1
		else:
			i = j - 1
		i += 1
		ans += mx
	return ans
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = 0
	r = 1
	b = list()
	c = list()
	b.append(a[l])
	if n == 1:
		print(a[0])
	else:
		while l < n and r < n:
			while r < n and a[r] * a[l] > 0:
				b.append(a[r])
				r += 1
			c.append(max(b))
			l = r
			if l < n:
				b = [a[l]]
			r += 1
		if l < n and a[l] * a[l - 1] < 0:
			c.append(a[l])
		print(sum(c))


t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	curr_sum = 0
	if nums[0] < 0:
		curr_sign = 1
	cur_num = nums[0]
	done = False
	for i1 in range(n):
		i = nums[i1]
		if i / cur_num > 0:
			if i > cur_num:
				cur_num = i
		else:
			curr_sum += cur_num
			cur_num = i
			if i1 == n - 1:
				curr_sum += i
				done = True
	if not done:
		curr_sum += cur_num
	print(curr_sum)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	arr = [a[0]]
	if a[0] < 0:
		flag = -1
	else:
		flag = 1
	for i in range(1, n):
		if flag == 1 and a[i] >= 0:
			if a[i] > arr[-1]:
				arr[-1] = a[i]
		elif flag == -1 and a[i] < 0:
			if a[i] > arr[-1]:
				arr[-1] = a[i]
		elif flag == 1 and a[i] < 0:
			arr.append(a[i])
			flag = -1
		elif flag == -1 and a[i] >= 0:
			arr.append(a[i])
			flag = 1
	print(sum(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	sign = 2
	ma = -1
	mi = 1
	if l[0] > 0:
		sign = 0
	else:
		sign = 1
	for j in range(n):
		if sign == 0:
			if l[j] > ma:
				ma = l[j]
		elif sign == 1:
			if mi == 1:
				if l[j] < mi:
					mi = l[j]
			elif l[j] > mi:
				mi = l[j]
		if j < n - 1 and (l[j] < 0 and l[j + 1] > 0 or (l[j] > 0 and l[j + 1] < 0)):
			if sign == 0:
				ans += ma
				ma = -1
				sign = 1
			else:
				ans += mi
				sign = 0
				mi = 1
		if j == n - 1:
			if l[j] > 0 and l[j - 1] > 0 or (l[j] < 0 or l[j - 1] < 0):
				if sign == 0:
					ans += ma
					ma = -1
					break
				else:
					ans += mi
					mi = 1
					break
			elif l[j] > 0:
				ans += ma
				break
			else:
				ans += mi
				break
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for r in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	i = 0
	toggle = 0
	if l[i] > 0:
		toggle = 1
	maxineg = -99999999999999
	maxipos = -99999999999999
	while i < n:
		if l[i] < 0 and toggle == 0:
			if l[i] > maxineg:
				maxineg = l[i]
			i += 1
		elif l[i] > 0 and toggle == 0:
			ans += maxineg
			maxineg = -99999999999999
			toggle = 1
		elif l[i] > 0 and toggle == 1:
			if l[i] > maxipos:
				maxipos = l[i]
			i += 1
		elif l[i] < 0 and toggle == 1:
			ans += maxipos
			maxipos = -99999999999999
			toggle = 0
	if toggle == 1:
		ans += maxipos
	else:
		ans += maxineg
	print(ans)


for t in range(int(input())):
	n = int(input())
	l = 0
	s = 0
	maxV = 0
	for a in list(map(int, input().split())):
		if l == 0 or maxV * a < 0:
			s += maxV
			l += 1
			maxV = a
		elif maxV * a > 0:
			maxV = max(maxV, a)
	s += maxV
	print(s)


testCases = int(input())

def solve(arr):
	max_val = -100000000000000
	ans = 0
	for i in range(0, len(arr), 1):
		if int(arr[i]) < 0:
			max_val = max(max_val, int(arr[i]))
		if (int(arr[i]) > 0 or (i == len(arr) - 1 and int(arr[i] < 0))) and max_val != -100000000000000:
			ans += max_val
			max_val = -100000000000000
		if int(arr[i]) > 0 and max_val == -100000000000000:
			continue
	for i in range(0, len(arr), 1):
		if int(arr[i]) > 0:
			max_val = max(max_val, int(arr[i]))
		if (int(arr[i]) < 0 or (i == len(arr) - 1 and int(arr[i] > 0))) and max_val != -100000000000000:
			ans += max_val
			max_val = -100000000000000
		if int(arr[i]) < 0 and max_val == -100000000000000:
			continue
	return ans
while testCases:
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))
	testCases -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr += [arr[-1]]
	tempans = []
	ansmat = []
	for i in range(n):
		tempans.append(arr[i])
		if arr[i] * arr[i + 1] < 0:
			ansmat.append(max(tempans))
			tempans = []
	ansmat.append(max(tempans))
	print(sum(ansmat))


t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	curr_min = -1
	curr_max = 1
	ans = 0
	if a[0] > 0:
		curr_max = a[0]
		curr = 1
	else:
		curr_min = a[0]
		curr = 0
	for i in range(1, n):
		if a[i] > 0 and curr == 1:
			curr_max = max(curr_max, a[i])
		elif a[i] > 0 and curr == 0:
			ans += curr_min
			curr_max = a[i]
			curr = 1
		elif a[i] < 0 and curr == 1:
			ans += curr_max
			curr_min = a[i]
			curr = 0
		else:
			curr_min = max(curr_min, a[i])
	if curr == 1:
		ans += curr_max
	else:
		ans += curr_min
	print(ans)


for _ in range(int(input())):
	N = int(input())
	arr = list(map(int, input().split()))
	i = 0
	res = 0
	if arr[0] >= 0:
		m = arr[0]
		i = 0
		while i < N and arr[i] >= 0:
			if arr[i] > m:
				m = arr[i]
			i += 1
		res += m
	while i < N:
		m = arr[i]
		while i < N and arr[i] < 0:
			if arr[i] > m:
				m = arr[i]
			i += 1
		res += m
		if i >= N:
			break
		m = arr[i]
		while i < N and arr[i] >= 0:
			if arr[i] > m:
				m = arr[i]
			i += 1
		res += m
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	sumOfSubs = 0
	maxVal = arr[0]
	sign = '-'
	if arr[0] > 0:
		sign = '+'
	for num in arr[1:]:
		if num < 0 and sign == '-':
			if num > maxVal:
				maxVal = num
		elif num > 0 and sign == '+':
			if num > maxVal:
				maxVal = num
		elif sign == '-':
			sumOfSubs += maxVal
			sign = '+'
			maxVal = num
		else:
			sumOfSubs += maxVal
			sign = '-'
			maxVal = num
	sumOfSubs += maxVal
	print(sumOfSubs)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	summ = 0
	keep = l[0]
	for i in range(n - 1):
		if l[i] * l[i + 1] < 0:
			summ += keep
			keep = l[i + 1]
		elif l[i + 1] > keep:
			keep = l[i + 1]
	print(summ + keep)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.append(-a[-1])
	pre_index = 0
	sum = 0
	for j in range(n):
		if a[j] * a[j + 1] < 0:
			sum += max(a[pre_index:j + 1])
			pre_index = j + 1
	print(sum)


import math
import sys
from collections import defaultdict
from collections import Counter
from collections import deque
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

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)]
for _ in range(int(input())):
	N = int(input())
	A = alele()
	i = 0
	B = []
	temp = []
	pos = -1
	while i < len(A):
		if len(temp) == 0:
			if A[i] < 0:
				pos = 0
				temp.append(A[i])
			else:
				pos = 1
				temp.append(A[i])
		elif pos == 1:
			if A[i] > 0:
				temp.append(A[i])
			else:
				pos = 0
				B.append(max(temp))
				temp = []
				temp.append(A[i])
		elif A[i] < 0:
			temp.append(A[i])
		else:
			pos = 1
			B.append(max(temp))
			temp = []
			temp.append(A[i])
		i += 1
	if len(temp) != 0:
		if temp[0] < 0:
			B.append(max(temp))
		else:
			B.append(max(temp))
	print(sum(B))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	su = 0
	z = a[0]
	if a[0] > 0:
		tr = True
	else:
		tr = False
	for i in range(n):
		if tr == (a[i] > 0):
			z = max(z, a[i])
		else:
			su += z
			tr = not tr
			z = a[i]
	su += z
	print(su)


def sign(x):
	if x > 0:
		return 1
	return -1
T = int(input())
while T:
	n = int(input())
	arr = list(map(int, input().split()))
	max_ = float('-inf')
	res = 0
	i = 0
	while i < n:
		max_ = arr[i]
		j = i + 1
		while j < n and sign(arr[i]) == sign(arr[j]):
			max_ = max(max_, arr[j])
			j += 1
		res += max_
		i = j
	print(res)
	T -= 1


from sys import stdin, stdout
from math import ceil
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007

def solve():
	n = inp()
	l = li()
	x = []
	ans = 0
	for i in range(n):
		if x == []:
			x.append(l[i])
		elif l[i] > 0:
			if x[-1] > 0:
				x.append(l[i])
			else:
				ans += max(x)
				x = [l[i]]
		elif x[-1] < 0:
			x.append(l[i])
		else:
			ans += max(x)
			x = [l[i]]
	if x:
		if x[-1] < 0:
			ans += max(x)
		else:
			ans += max(x)
	pr(ans)
for _ in range(inp()):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	pos = True
	length = 1
	if array[0] > 0:
		pos = True
		least = array[0]
	else:
		pos = False
		maxi = array[0]
	result = 0
	for i in range(1, len(array)):
		if pos:
			if array[i] < 0:
				maxi = array[i]
				result += least
				length += 1
				pos = not pos
				if i == n - 1:
					result += maxi
			else:
				least = max(least, array[i])
				if i == n - 1:
					result += least
		elif array[i] > 0:
			least = array[i]
			result += maxi
			length += 1
			pos = not pos
			if i == n - 1:
				result += least
		else:
			maxi = max(maxi, array[i])
			if i == n - 1:
				result += maxi
	if length == n:
		print(sum(array))
	elif length == 1:
		print(max(array))
	else:
		print(result)


import math
t = int(input())
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	positive = False
	negative = False
	sum = 0
	maxe = 0
	mine = -10 ** 9
	for i in range(0, n):
		if a[i] > 0:
			if not positive:
				sum = sum + a[i]
				maxe = a[i]
				positive = True
				negative = False
			elif a[i] > maxe:
				sum = sum + a[i] - maxe
				maxe = a[i]
		elif not negative:
			sum = sum + a[i]
			mine = a[i]
			positive = False
			negative = True
		elif a[i] > mine:
			sum = sum + a[i] - mine
			mine = a[i]
	print(int(sum))


import math
import sys
input = sys.stdin.readline

def solve(n, arr):
	seg = -math.inf
	neg = arr[0] < 0
	total = 0
	for a in arr:
		if a < 0 and neg:
			seg = max(seg, a)
		elif a > 0 and (not neg):
			seg = max(seg, a)
		elif a < 0 and (not neg) or (a > 0 and neg):
			total += seg
			seg = a
		neg = a < 0
	return total + seg
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


def f(s):
	res = 0
	sm = 0
	for n in s:
		if n > 0:
			sm = max(sm, n)
		else:
			res += sm
			sm = 0
	res += sm
	return res

def a(x):
	return -x

def g(s):
	res = 0
	sm = 3000000000.0
	for n in s:
		if n > 0:
			sm = min(sm, n)
		else:
			if sm != 3000000000.0:
				res += sm
			sm = 3000000000.0
	if sm != 3000000000.0:
		res += sm
	return int(res)

def sol():
	x = int(input())
	s = list(map(int, input().split()))
	res = f(s) - g(list(map(a, s)))
	print(res)
for n in range(int(input())):
	sol()


from sys import stdin

def solve():
	n = int(input())
	k = [int(i) for i in stdin.readline().split()]
	rs = 0
	mx = k[0]
	prev = k[0]
	for i in range(1, len(k)):
		if prev > 0 and k[i] > 0 or (prev < 0 and k[i] < 0):
			mx = max(mx, k[i])
		else:
			rs += mx
			mx = k[i]
		prev = k[i]
	rs += mx
	print(rs)
t = int(input())
for i in range(t):
	solve()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ind = 0
	ans = 0
	while ind < n:
		cur = -10000000001
		if a[ind] < 0:
			while ind < n and a[ind] < 0:
				cur = max(a[ind], cur)
				ind += 1
		else:
			while ind < n and a[ind] > 0:
				cur = max(a[ind], cur)
				ind += 1
		ans += cur
	print(ans)


t = int(input())

def max_sum(lista, n):
	i = 0
	result = 0
	while i < n:
		maximum1 = 0
		maximum2 = 0
		if lista[i] >= 0:
			maximum1 = lista[i]
			while i < n and lista[i] >= 0:
				if lista[i] > maximum1:
					maximum1 = lista[i]
				i += 1
		else:
			maximum2 = lista[i]
			while i < n and lista[i] < 0:
				if lista[i] > maximum2:
					maximum2 = lista[i]
				i += 1
		result += maximum1 + maximum2
	return result
for i in range(t):
	n = int(input())
	lista = input().split()
	for j in range(n):
		lista[j] = int(lista[j])
	print(max_sum(lista, n))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	suma = 0
	el = 0
	el1 = 0
	i = 0
	el = a[0]
	for i in range(1, n):
		if a[i] > 0 and a[i - 1] < 0:
			suma += el
			el = -10000000000000000
		if a[i] < 0 and a[i - 1] > 0:
			suma += el
			el = -10000000000000000
		el = max(el, a[i])
	suma += el
	print(suma)


def xxx(x):
	if x > 0:
		y = 0
	else:
		y = 1
	return y
for i in range(int(input())):
	x = int(input())
	y = [int(i) for i in input().split()]
	z = 0
	a = y[0]
	for i in range(1, x):
		if xxx(y[i]) == xxx(y[i - 1]):
			if y[i] > a:
				a = y[i]
		else:
			z += a
			a = y[i]
	print(z + a)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		sequence = list(map(int, input().split()))
		max_sum = sequence[0]
		previous_elem = sequence[0]
		for elem in sequence[1:]:
			if previous_elem > 0 and elem > 0 or (previous_elem < 0 and elem < 0):
				if elem > previous_elem:
					max_sum = max_sum - previous_elem + elem
					previous_elem = elem
			elif previous_elem > 0 and elem < 0 or (previous_elem < 0 and elem > 0):
				max_sum += elem
				previous_elem = elem
		print(max_sum)
main()


t = int(input())
for i in range(0, t):
	input()
	d = [int(f) for f in input().split()]
	ans = [[d[0]]]
	startPos = 0
	m = 0
	cnt = 0
	for i in range(1, len(d)):
		if d[i] * d[i - 1] > 0:
			ans[-1].append(d[i])
		else:
			ans.append([d[i]])
	s = 0
	for i in ans:
		s += max(i)
	print(s)


T = int(input())
out = []
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	out1 = []
	out1.append([l[0]])
	count = 0
	for j in range(1, n):
		if l[j] * l[j - 1] < 0:
			count = count + 1
			out1.append([l[j]])
		else:
			out1[count].append(l[j])
	out2 = []
	for j in range(len(out1)):
		if out1[j][0] < 0:
			out2.append(max(out1[j]))
		else:
			out2.append(max(out1[j]))
	out.append(sum(out2))
for i in range(T):
	print(out[i])


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = a[0]
	if k > 0:
		p = 0
		b = []
		i = 0
		while i < len(a):
			while i != len(a) and a[i] > 0:
				b.append(a[i])
				i += 1
			if len(b) != 0:
				p += max(b)
				b.clear()
			if i == len(a):
				break
			while i != len(a) and a[i] < 0:
				b.append(a[i])
				i += 1
			if len(b) != 0:
				p += max(b)
				b.clear()
	else:
		p = 0
		b = []
		i = 0
		while i < len(a):
			while i != len(a) and a[i] < 0:
				b.append(a[i])
				i += 1
			if len(b) != 0:
				p += max(b)
				b.clear()
			if i == len(a):
				break
			while i != len(a) and a[i] > 0:
				b.append(a[i])
				i += 1
			if len(b) != 0:
				p += max(b)
				b.clear()
	print(p)


res = []
for _ in range(int(input())):
	input()
	x = []
	y = []
	for a in map(int, input().split()):
		(n, m) = (len(x), len(y))
		if a < 0:
			if n % 2:
				x[-1] = max(x[-1], a)
			else:
				x += (a,)
			if m % 2:
				y += (a,)
			elif m:
				y[-1] = max(y[-1], a)
		else:
			if m % 2:
				y[-1] = max(y[-1], a)
			else:
				y += (a,)
			if n % 2:
				x += (a,)
			elif n:
				x[-1] = max(x[-1], a)
	(n, m) = (len(x), len(y))
	if n > m:
		res += (sum(x),)
	elif m > n:
		res += (sum(y),)
	else:
		res += (max(sum(x), sum(y)),)
print('\n'.join(map(str, res)))


t = int(input())
for tt in range(t):
	n = int(input())
	a = [int(c) for c in input().split()]
	i = 1
	ans = 0
	blockmax = a[0]
	while i <= n - 1:
		while i <= n - 1 and a[i] * a[i - 1] > 0:
			if a[i] > blockmax:
				blockmax = a[i]
			i += 1
		ans += blockmax
		if i <= n - 1:
			blockmax = a[i]
		i += 1
	if n == 1 or a[n - 1] * a[n - 2] < 0:
		ans += a[n - 1]
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	res = []
	res.append(a[0])
	for i in range(1, n):
		if a[i] > 0 and a[i - 1] < 0:
			res.append(a[i])
		elif a[i] > 0 and a[i - 1] > 0:
			res[-1] = max(res[-1], a[i])
		elif a[i] < 0 and a[i - 1] > 0:
			res.append(a[i])
		elif a[i] < 0 and a[i - 1] < 0:
			res[-1] = max(res[-1], a[i])
	print(sum(res))


ins = int(input())
entries = []
lens = []
for i in range(0, ins):
	length = int(input())
	instr = input()
	instr = instr.split(' ')
	entries.append(instr)
	lens.append(length)
for i in range(0, ins):
	curr = entries[i]
	sign = ''
	sum = 0
	if int(curr[0]) < 0:
		sign = 'neg'
	else:
		sign = 'pos'
	last = int(curr[0])
	alt_list = [last]
	for j in range(1, len(curr)):
		temp = int(curr[j])
		if temp < 0:
			tsign = 'neg'
		else:
			tsign = 'pos'
		if sign == tsign and temp > alt_list[-1]:
			alt_list[-1] = temp
		if sign != tsign:
			sign = tsign
			sum = sum + alt_list[-1]
			alt_list.append(temp)
	sum = sum + alt_list[-1]
	print(sum)


T = int(input())
for _ in range(T):
	n = int(input())
	ar = list(map(int, input().split()))
	k = ar[0]
	ans = 0
	for i in range(1, n):
		x = k * ar[i]
		if x > 0:
			k = max(k, ar[i])
		else:
			ans += k
			k = ar[i]
	ans += k
	print(ans)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a = 0
	b = 0
	summ = 0
	while a < n:
		while a < n and b < n and (arr[a] * arr[b] > 0):
			b += 1
		summ += max(arr[a:b])
		a = b
	print(summ)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	s = 0
	prev = 0
	for i in A:
		if i < 0 and prev < 0:
			prev = max(prev, i)
		elif i > 0 and prev > 0:
			prev = max(prev, i)
		else:
			temp = prev
			s += prev
			prev = i
	if temp * prev <= 0:
		s += prev
	print(s)


t = int(input())
for t in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	sign = []
	for i in range(n):
		if a[i] > 0:
			sign.append(1)
		else:
			sign.append(-1)
	i = 0
	result = []
	while i < n:
		j = i
		temp = a[i]
		while j < n and sign[i] == sign[j]:
			temp = max(temp, a[j])
			j += 1
		result.append(temp)
		i = j - 1
		i += 1
	print(sum(result))


def solve():
	n = int(input())
	lis = input().split()
	lis = [int(i) for i in lis]
	sum = 0
	curr = ''
	recent = 0
	for i in range(n):
		if lis[i] > 0:
			if curr == '' or curr == 'neg':
				recent = lis[i]
				sum += recent
				curr = 'pos'
			elif lis[i] > recent:
				sum -= recent
				recent = lis[i]
				sum += recent
		elif curr == '' or curr == 'pos':
			recent = lis[i]
			sum += recent
			curr = 'neg'
		elif lis[i] > recent:
			sum -= recent
			recent = lis[i]
			sum += recent
	print(sum)
	return

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


def single():
	return int(input())

def multiple():
	return list(map(int, input().split()))
t = single()

def sign(num):
	if num > 0:
		return 1
	return -1
for i in range(t):
	n = single()
	arr = multiple()
	summ = 0
	ln = 0
	i = 0
	while i < n:
		curr = arr[i]
		j = i
		while j < n and sign(arr[i]) == sign(arr[j]):
			curr = max(curr, arr[j])
			j += 1
		summ += curr
		i = j - 1
		i += 1
	print(summ)


T = int(input())
for t in range(T):
	N = int(input())
	L = [int(i) for i in input().split()]
	is_positive = True
	current_v = 0
	answer = 0
	for i in range(len(L)):
		if is_positive:
			if L[i] > 0:
				current_v = max(current_v, L[i])
			else:
				is_positive = False
				answer += current_v
				current_v = L[i]
		elif L[i] < 0:
			current_v = max(current_v, L[i])
		else:
			is_positive = True
			answer += current_v
			current_v = L[i]
	answer += current_v
	print(answer)


test = int(input())
for i in range(test):
	n = int(input())
	l = list(map(int, input().split()))
	(c, s2) = (0, 0)
	s = []
	s1 = []
	for i in range(len(l)):
		if l[i] > 0:
			s.append(l[i])
			if s1 != []:
				q = max(s1)
				s2 = s2 + q
				s1 = []
		if l[i] < 0:
			s1.append(l[i])
			if s != []:
				q = max(s)
				s2 = s2 + q
				s = []
	if s != []:
		s2 = s2 + max(s)
	if s1 != []:
		s2 = s2 + max(s1)
	print(s2)


t = int(input())
for _ in range(t):
	n = int(input())
	k = []
	a = list(map(int, input().split()))
	k.append(a[0])
	for i in range(1, n):
		if k[-1] < 0 and a[i] > 0 or (k[-1] > 0 and a[i] < 0):
			k.append(a[i])
		elif k[-1] < a[i]:
			k[-1] = a[i]
	print(sum(k))


for t in range(int(input())):
	x = int(input())
	a = list(map(int, input().split()))
	n = p = 0
	for i in range(x):
		if a[i] > 0:
			p += 1
		else:
			n += 1
	if n == x or p == x:
		print(max(a))
	else:
		cur = 0
		if a[0] > 0:
			cur = 'p'
		else:
			cur = 'n'
		m = a[0]
		s = 0
		for i in range(1, x):
			if a[i] > 0 and cur == 'p':
				if m < a[i]:
					m = a[i]
			elif a[i] < 0 and cur == 'p':
				s += m
				m = a[i]
				cur = 'n'
			elif a[i] < 0 and cur == 'n':
				if m < a[i]:
					m = a[i]
			elif a[i] > 0 and cur == 'n':
				s += m
				m = a[i]
				cur = 'p'
		s += m
		print(s)


from sys import stdin, stdout
input = stdin.buffer.readline

def allneg(a):
	cc = 0
	for x in a:
		if x < 0:
			cc += 1
	if cc == len(a):
		return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if allneg(a):
		print(max(a))
		continue
	res = 0
	ini = 0
	if a[0] > 0:
		ini = 1
	else:
		ini = -1
	tt = []
	idx = -1
	for i in range(n):
		if ini == 1 and a[i] > 0 or (ini == -1 and a[i] < 0):
			tt.append(a[i])
		else:
			idx = i
			break
	res = max(tt)
	fl = 1
	if a[0] < 0:
		fl = -1
	while 1:
		temp = []
		while i < n:
			if fl == -1 and a[i] > 0:
				temp.append(a[i])
			elif fl == 1 and a[i] < 0:
				temp.append(a[i])
			else:
				break
			i += 1
		if temp == []:
			break
		res += max(temp)
		fl = -1 * fl
	print(res)


for _ in range(int(input())):
	N = int(input())
	arr = [int(x) for x in input().split()]
	ans = arr.pop(0)
	last = ans
	is_positive = last > 0
	for n in arr:
		if (n > 0) != is_positive:
			is_positive = not is_positive
			ans += n
		elif n > last:
			ans += n - last
		else:
			continue
		last = n
	print(ans)


def same_sgn(x, y):
	if x > 0:
		return y > 0
	else:
		return y < 0
T = int(input())
for _ in range(T):
	n = int(input())
	nums = list(map(int, input().split()))
	s = [nums[0]]
	for i in range(len(nums)):
		if same_sgn(s[-1], nums[i]):
			if nums[i] > s[-1]:
				s[-1] = nums[i]
		else:
			s.append(nums[i])
	print(sum(s))


class Pair:

	def __init__(self, a, b):
		self.a = a
		self.b = b

	def __lt__(self, o):
		return self.b < o.b if self.a == o.a else self.a < o.a

	def add(self, c):
		return Pair(self.a + 1, self.b + c)
cas = int(input())
for _ in range(cas):
	n = int(input())
	r1 = Pair(0, 0)
	r2 = Pair(0, 0)
	x = [int(i) for i in input().split()]
	for xx in x:
		if xx > 0:
			r = r2.add(xx)
			if r1 < r:
				r1 = r
		else:
			r = r1.add(xx)
			if r2 < r:
				r2 = r
	print(r1.b if r2 < r1 else r2.b)


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	ans = 0
	c = float('-inf')
	(T1, T2) = (False, False)
	for i in range(n):
		if a[i] > 0:
			T1 = True
			if T2:
				ans += c
				T2 = False
				c = a[i]
			c = max(c, a[i])
		else:
			T2 = True
			if T1:
				ans += c
				T1 = False
				c = a[i]
			c = max(c, a[i])
	ans += c
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	m = a[0]
	k = a[0] / abs(a[0])
	for j in range(1, n):
		if k == a[j] / abs(a[j]):
			m = max(m, a[j])
			if j == n - 1:
				s += m
		else:
			s += m
			m = a[j]
			k = a[j] / abs(a[j])
			if j == n - 1:
				s += a[j]
	if n == 1:
		s = m
	print(s)


number = int(input())
for k in range(number):
	length = int(input())
	text_sequence = input()
	sequence = text_sequence.split()
	for i in range(len(sequence)):
		sequence[i] = int(sequence[i])
	if len(sequence) == 1:
		print(sequence[0])
	else:
		subsequence_length = 1
		toggle = 0
		toggled = 0
		total = 0
		currentmax = 0
		if sequence[0] > 0:
			toggle = 1
		else:
			toggle = -1
		for i in range(len(sequence)):
			if sequence[i] > 0 and toggle == -1:
				subsequence_length += 1
				toggle = 1
				toggled = 1
			elif sequence[i] < 0 and toggle == 1:
				subsequence_length += 1
				toggle = -1
				toggled = 1
			if toggled == 1:
				total = total + currentmax
				currentmax = 0
				toggled = 0
				currentmax = sequence[i]
			if sequence[i] > currentmax or currentmax == 0:
				currentmax = sequence[i]
			if i == len(sequence) - 1:
				total = total + currentmax
		print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	a = tuple(map(int, input().split()))
	sgn = lambda x: x > 0
	flag = sgn(a[0])
	(total, zone) = (0, a[0])
	for elem in a:
		if sgn(elem) == flag:
			zone = max(zone, elem)
		else:
			flag = not flag
			total += zone
			zone = elem
	print(total + zone)


def get_max_sum_subseq(sequence):
	max_subseq = [sequence[0]]
	for element in sequence:
		if is_different_sign(max_subseq[-1], element):
			max_subseq.append(element)
		else:
			max_subseq[-1] = max(max_subseq[-1], element)
	return sum(max_subseq)

def is_different_sign(element1, element2):
	return element2 < 0 < element1 or element1 < 0 < element2
sequences = []
for i in range(int(input())):
	_ = input()
	sequences.append([int(s) for s in input().split()])
max_subseqs = [str(get_max_sum_subseq(s)) for s in sequences]
print('\n'.join(max_subseqs), sep='')


t = int(input())
s = []
for dfhh in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	g = 0
	q = -1e+100
	if n == 1:
		g = x[0]
	for i in range(0, n - 1):
		q = max(q, x[i])
		if x[i] * x[i + 1] < 0:
			g += q
			q = -1e+100
	if n > 1 and x[n - 1] * x[n - 2] > 0:
		g += max(q, x[n - 1])
	elif n > 1 and x[n - 1] * x[n - 2] < 0:
		g += x[n - 1]
	s.append(g)
for i in s:
	print(i)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	sm = 0
	i = 0
	while i < n:
		mx = -(10 ** 9 + 1)
		f1 = 1
		while i < n and a[i] > 0:
			if a[i] > mx:
				f1 = 0
				mx = a[i]
			i += 1
		if f1 == 0:
			sm += mx
		mx = -(10 ** 9 + 1)
		f2 = 1
		while i < n and a[i] < 0:
			if a[i] > mx:
				mx = a[i]
				f2 = 0
			i += 1
		if f2 == 0:
			sm += mx
	print(sm)


def sign(x):
	if x > 0:
		return 1
	else:
		return -1
t = int(input())
a = []
x = []
for i in range(t):
	a.append(int(input()))
	x.append(list(map(int, input().rstrip().split())))
for i in range(t):
	sum = x[i][0]
	temp = sign(x[i][0])
	max = x[i][0]
	for j in range(1, a[i]):
		if sign(x[i][j]) == temp:
			if x[i][j] >= max:
				sum += x[i][j] - max
				max = x[i][j]
		else:
			max = -9999999999
			temp = sign(x[i][j])
			if x[i][j] >= max:
				sum += x[i][j]
				max = x[i][j]
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	array_of_same_signs = [[arr[0]]]
	for j in range(1, n):
		if arr[j] * arr[j - 1] > 0:
			array_of_same_signs[-1].append(arr[j])
		else:
			array_of_same_signs.append([arr[j]])
	sums = 0
	for element in array_of_same_signs:
		sums += max(element)
	print(sums)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	b = []
	while i < n:
		if a[i] < 0:
			c = []
			j = i
			while j < n:
				if a[j] < 0:
					c.append(a[j])
					j += 1
				else:
					break
			b.append(c)
			i = j
			continue
		if a[i] > 0:
			c = []
			j = i
			while j < n:
				if a[j] > 0:
					c.append(a[j])
					j += 1
				else:
					break
			i = j
			b.append(c)
	l = 0
	for i in b:
		if i[0] > 0:
			l += max(i)
		if i[0] < 0:
			l += max(i)
	print(l)


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 case in range(itg()):
	n = itg()
	arr = list(mpint())
	max_ = arr.pop(0)
	last_sign = max_ > 0
	ans = 0
	for a in arr:
		if last_sign:
			if a > 0:
				max_ = max(max_, a)
			else:
				ans += max_
				max_ = a
				last_sign = False
		elif a > 0:
			ans += max_
			max_ = a
			last_sign = True
		else:
			max_ = max(max_, a)
	ans += max_
	print(ans)


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

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

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

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

def invr():
	return map(int, input().split())
test_count = inp()
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	n = inp()
	a = inlt()
	ans = 0
	last = a[0]
	for i in range(n - 1):
		curr = a[i + 1]
		if last > 0 and curr < 0 or (last < 0 and curr > 0):
			ans += last
			last = curr
		else:
			last = max(curr, last)
	ans += last
	print(ans)


def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	sign = 0
	if a[0] > 0:
		sign = 1
	mx = a[0]
	sum = 0
	for i in a:
		if i > 0:
			if sign == 1:
				mx = max(mx, i)
			else:
				sum += mx
				mx = i
				sign = 1
		elif sign == 1:
			sum += mx
			mx = i
			sign = 0
		else:
			mx = max(mx, i)
	sum += mx
	print(sum)
t = int(input())
for i in range(t):
	solve()


def solve(a):
	res = 0
	current = None
	for v in a:
		if current is None:
			current = v
		elif (current > 0) == (v > 0):
			current = max(current, v)
		else:
			res += current
			current = v
	if current is not None:
		res += current
	return res

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


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = []
	i = 0
	while i < n:
		if l[i] < 0:
			m = -999999999999
			while l[i] < 0:
				m = max(m, l[i])
				i += 1
				if i == n:
					break
			k.append(m)
		else:
			m = 0
			while l[i] > 0:
				m = max(m, l[i])
				i += 1
				if i == n:
					break
			k.append(m)
	print(sum(k))


n = int(input())
for i in range(n):
	k = int(input())
	l = [int(i) for i in input().split()]
	win = 0
	k = []
	m = []
	for b in l:
		if win == 0 and b < 0:
			m.append(b)
			win = 1
		elif win == 1 and b < 0:
			m.append(b)
		elif win == 1 and b > 0:
			k.append(max(m))
			m = []
			win = -1
			m.append(b)
		elif win == 0 and b > 0:
			m.append(b)
			win = -1
		elif win == -1 and b > 0:
			m.append(b)
		elif win == -1 and b < 0:
			k.append(max(m))
			m = []
			win = 1
			m.append(b)
	k.append(max(m))
	print(sum(k))


t = int(input())
for k in range(t):
	l = int(input())
	a = input().split()
	a = [int(x) for x in a]
	out = []
	i = 0
	while i < len(a):
		if a[i] > 0:
			m = a[i]
			i += 1
			while i < len(a) and a[i] > 0:
				if a[i] > m:
					m = a[i]
				i += 1
			out.append(m)
		else:
			m = a[i]
			i += 1
			while i < len(a) and a[i] < 0:
				if a[i] > m:
					m = a[i]
				i += 1
			out.append(m)
	print(sum(out))


def get_sign(v):
	return 1 if v > 0 else -1

def solve(arr):
	compressed = []
	smallest = min(arr)
	cur = smallest
	for i in range(0, len(arr)):
		if i != 0 and cur * arr[i] < 0:
			compressed.append(cur)
			cur = smallest
		cur = max(cur, arr[i])
	compressed.append(cur)
	return sum(compressed)
for _ in range(int(input())):
	n = int(input())
	(*arr,) = map(int, input().split())
	print(solve(arr))


import sys
input = sys.stdin.readline
from math import *
import bisect as bs
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	f1 = 0
	f2 = 0
	i = 0
	c = 0
	while i < n:
		p = a[i]
		maxi = a[i]
		i += 1
		if i == n:
			c += maxi
			break
		if maxi > 0:
			while i < n and a[i] > 0:
				maxi = max(maxi, a[i])
				i += 1
		elif maxi < 0:
			while i < n and a[i] < 0:
				maxi = max(maxi, a[i])
				i += 1
		c += maxi
	print(c)


import math
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(s, f, v) = (0, 2, 0)
	for i in a:
		if i > 0:
			if f == 2:
				f = 1
				s += i
				v = i
			elif f == 1:
				if i > v:
					s += i - v
					v = i
			elif f == -1:
				s += i
				v = i
			f = 1
		else:
			if f == 2:
				f = -1
				s += i
				v = i
			elif f == -1:
				if i > v:
					s += i - v
					v = i
			elif f == 1:
				s += i
				v = i
			f = -1
	print(s)


def r(t=int):
	return list(map(t, input().split()))

def cd(it):
	ret_val = dict()
	for v in it:
		ret_val[v] = ret_val.get(v, 0) + 1
	return ret_val
for _ in range(r()[0]):
	r()
	a = r()
	sign = a[0] < 0
	g = set()
	s = 0
	for i in a:
		if sign == (i > 0):
			g.add(i)
		else:
			sign = i > 0
			if g:
				s += max(g)
			g = set()
			g.add(i)
	s += max(g)
	print(s)


x = int(input())
for i in range(x):
	n = int(input())
	a = list(map(int, input().split()))
	t = 0
	l = []
	for i in range(n - 1):
		if a[i] > 0:
			l.append(a[i])
			if a[i + 1] < 0:
				t += max(l)
				l = []
		else:
			l.append(a[i])
			if a[i + 1] > 0:
				t += max(l)
				l = []
	if n >= 2:
		if a[-1] * a[-2] > 0:
			l.append(a[-1])
			t += max(l)
		else:
			t += a[-1]
		print(t)
	else:
		print(a[0])


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


import math
from collections import Counter
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
I = lambda : int(input())
IN = lambda : input()
mod = 10 ** 9 + 7

def s(a):
	print(' '.join(list(map(str, a))))
for i in range(I()):
	n = I()
	a = L()
	s = 0
	if a[0] > 0:
		x = 1
	else:
		x = -1
	index = 0
	while n > index:
		if x == 1:
			i = index
			val = a[index]
			while i < n and a[i] > 0:
				val = max(val, a[i])
				i += 1
			s += val
			x = -1
		else:
			i = index
			val = a[index]
			while i < n and a[i] < 0:
				val = max(val, a[i])
				i += 1
			s += val
			x = 1
		index = i
	print(s)


def sign(x):
	return x > 0
t = int(input())
while t:
	n = int(input())
	arr = [int(x) for x in input().split()]
	sum = 0
	maiorAtual = arr[0]
	ultimo = arr[0]
	for i in arr[1:]:
		if sign(i) == sign(ultimo):
			maiorAtual = max(maiorAtual, i)
		else:
			sum += maiorAtual
			maiorAtual = i
		ultimo = i
	sum += maiorAtual
	print(sum)
	t -= 1


for k in range(int(input())):
	a = int(input())
	t = list(map(int, input().split()))
	start = t[0]
	u = 0
	for j in range(a):
		if t[j] < 0:
			if start < 0:
				if t[j] > start:
					start = t[j]
			elif start > 0:
				u += start
				start = t[j]
		elif t[j] > 0:
			if start > 0:
				if t[j] > start:
					start = t[j]
				else:
					pass
			elif start < 0:
				u += start
				start = t[j]
	u += start
	print(u)


t = input()
for _ in range(int(t)):
	n = int(input())
	a = input().split()
	cpos = 0
	cneg = 0
	mxpos = -1
	mxneg = -1000000000000000
	for i in range(n):
		a[i] = int(a[i])
		if a[i] > 0:
			cpos += 1
			mxpos = max(a[i], mxpos)
		else:
			cneg += 1
			mxneg = max(a[i], mxneg)
	if cpos == 0:
		ans = mxneg
	elif cneg == 0:
		ans = mxpos
	else:
		temp = -10000000000000000
		ans = 0
		for i in range(n):
			if a[i] > 0:
				temp = max(a[i], temp)
				if i == n - 1:
					ans += temp
				elif a[i + 1] < 0:
					ans += temp
					temp = -10000000000000000
			else:
				temp = max(a[i], temp)
				if i == n - 1:
					ans += temp
				elif a[i + 1] > 0:
					ans += temp
					temp = -10000000000000000
	print(ans)


for i in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	pos = []
	neg = []
	new_array = []
	for j in range(len(l1)):
		if l1[j] > 0:
			pos.append(l1[j])
			if j != n - 1:
				if l1[j + 1] < 0:
					new_array.append(max(pos))
					pos = []
			else:
				new_array.append(max(pos))
				pos = []
		else:
			neg.append(l1[j])
			if j != n - 1:
				if l1[j + 1] > 0:
					new_array.append(max(neg))
					neg = []
			else:
				new_array.append(max(neg))
				neg = []
	print(sum(new_array))


n = int(input())
for i in range(n):
	m = int(input())
	ll = [int(i) for i in input().split(' ')]
	is_pos = True
	temp = ll[0]
	is_pos = True if temp > 0 else False
	res = []
	for x in ll:
		if is_pos and x > 0:
			temp = max(x, temp)
		elif is_pos and x < 0:
			res.append(temp)
			is_pos = False
			temp = x
		elif not is_pos and x < 0:
			temp = max(x, temp)
		elif not is_pos and x > 0:
			is_pos = True
			res.append(temp)
			temp = x
	res.append(temp)
	print(sum(res))


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


t = int(input())

def sign(a):
	return a > 0
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n == 1:
		print(a[0])
		continue
	curmax = a[0]
	ans = 0
	for i in range(len(a) - 1):
		curmax = max(curmax, a[i])
		if sign(a[i + 1]) != sign(a[i]):
			ans += curmax
			curmax = a[i + 1]
	curmax = max(curmax, a[n - 1])
	ans += curmax
	print(ans)


t = int(input())
for v in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = [a[0]]
	b = []
	ans = 0
	for i in range(1, n):
		if a[i] > 0 and a[i - 1] < 0 or (a[i] < 0 and a[i - 1] > 0):
			b.append(l)
			l = [a[i]]
		else:
			l.append(a[i])
	b.append(l)
	for i in b:
		ans += max(i)
	print(ans)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	sumVal = 0
	maxVal = -sys.maxsize
	for i in range(n):
		maxVal = max(maxVal, arr[i])
		if i == n - 1:
			sumVal += maxVal
		elif arr[i] * arr[i + 1] < 0:
			sumVal += maxVal
			maxVal = arr[i + 1]
	print(sumVal)


t = input()
t = int(t)
for i in range(t):
	n = input()
	n = int(n)
	a = [int(x) for x in input().split()]
	mp = 0
	mn = -10000000000.0
	ans = 0
	for j in range(n):
		if a[j] < 0:
			mn = max(mn, a[j])
			ans = ans + mp
			mp = 0
		else:
			mp = max(mp, a[j])
			if mn != -10000000000.0:
				ans = ans + mn
			mn = -10000000000.0
	ans = ans + mp
	if mn != -10000000000.0:
		ans = ans + mn
	print(ans)


import math

def solve(n, ar):
	best = ar[0]
	track = []
	if n == 1:
		track.append(ar[0])
	for i in range(1, n):
		if math.copysign(1, best) == math.copysign(1, ar[i]):
			if ar[i] > 0:
				best = max(ar[i], best)
			else:
				best = max(ar[i], best)
			if i == n - 1:
				track.append(best)
		else:
			track.append(best)
			best = ar[i]
			if i == n - 1:
				track.append(best)
	print(sum(track))
	return
t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	solve(n, ar)


t = int(input())
for ts in range(t):
	l = int(input())
	a = [int(i) for i in input().split()]
	mode = a[0] / abs(a[0])
	su = 0
	m = a[0]
	for i in a:
		if i * mode < 0:
			su += m
			m = i
			mode *= -1
		else:
			m = max(m, i)
	su += m
	print(su)


import sys

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

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
m_min = -10 ** 9 - 1
for __ in range(int(input())):
	n = int(input())
	arr = I()
	i = 0
	s = 0
	while i < n:
		if arr[i] < 0:
			s_min = m_min
			while i < n and arr[i] < 0:
				s_min = max(arr[i], s_min)
				i += 1
			s += s_min
		else:
			s_max = -1
			while i < n and arr[i] > 0:
				s_max = max(arr[i], s_max)
				i += 1
			s += s_max
	print(s)


for _ in range(int(input())):
	n = int(input())
	m = -1000000000
	ls = [int(x) for x in input().split()]
	s = 0
	i = 0
	while i < n:
		if ls[i] > 0:
			while i < n:
				if ls[i] > 0:
					m = max(m, ls[i])
					i += 1
				else:
					break
			s += m
			m = -1000000000
		else:
			while i < n:
				if ls[i] < 0:
					m = max(m, ls[i])
					i += 1
				else:
					break
			s += m
			m = -1000000000
	print(s)


try:
	n = int(input())
	for i in range(n):
		k = int(input())
		A = list(map(int, input().split()))
		maxi = 0
		g = 0
		for j in range(1, k):
			if A[j] * A[j - 1] < 0:
				maxi += max(A[g:j])
				g = j
		maxi += max(A[g:])
		print(maxi)
except:
	pass


from sys import stdin
import math

def inp():
	return stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	ar = [int(x) for x in inp().split()]
	ans1 = []
	pos = 0
	neg = float('-inf')
	posFlag = False
	negFlag = False
	for i in range(n):
		if ar[i] > 0:
			posFlag = True
			if negFlag:
				ans1.append(neg)
				neg = float('-inf')
				negFlag = False
			pos = max(pos, ar[i])
		elif ar[i] < 0:
			negFlag = True
			if posFlag:
				ans1.append(pos)
				pos = 0
				posFlag = False
			neg = max(neg, ar[i])
	if neg != float('-inf'):
		ans1.append(neg)
		neg = float('-inf')
	elif pos != 0:
		ans1.append(pos)
		pos = 0
	print(sum(ans1))


import sys
import math

def main():
	T = int(input())
	for _ in range(T):
		n = int(input())
		nums = list(map(int, input().split()))
		neg = nums[0] if nums[0] < 0 else 0
		pos = nums[0] if nums[0] > 0 else 0
		neg_cnt = 1 if nums[0] < 0 else 0
		pos_cnt = 1 if nums[0] > 0 else 0
		for i in range(1, n):
			(new_pos_cnt, new_neg_cnt) = (pos_cnt, neg_cnt)
			(new_pos, new_neg) = (pos, neg)
			num = nums[i]
			if num > 0:
				if neg_cnt + 1 > pos_cnt:
					new_pos_cnt = neg_cnt + 1
					new_pos = neg + num
				elif neg_cnt + 1 == pos_cnt and pos < neg + num:
					new_pos = neg + num
			elif pos_cnt + 1 > neg_cnt:
				new_neg_cnt = pos_cnt + 1
				new_neg = pos + num
			elif pos_cnt + 1 == neg_cnt and neg < pos + num:
				new_neg = pos + num
			(pos, neg) = (new_pos, new_neg)
			(pos_cnt, neg_cnt) = (new_pos_cnt, new_neg_cnt)
		if pos_cnt > neg_cnt:
			print(pos)
		elif pos_cnt < neg_cnt:
			print(neg)
		else:
			print(max(pos, neg))
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	ans1 = 0
	ans2 = 0
	i = 0
	while i < n:
		if l[i] > 0:
			max1 = 0
			while i < n and l[i] > 0:
				max1 = max(max1, l[i])
				i += 1
			ans1 += max1
		elif l[i] < 0:
			max1 = -1000000001
			while i < n and l[i] < 0:
				max1 = max(l[i], max1)
				i += 1
			ans1 += max1
	i = 0
	while i < n:
		if l[i] < 0:
			max1 = -1000000001
			while i < n and l[i] < 0:
				max1 = max(l[i], max1)
				i += 1
			ans2 += max1
		elif l[i] > 0:
			max1 = 0
			while i < n and l[i] > 0:
				max1 = max(max1, l[i])
				i += 1
			ans2 += max1
	print(max(ans1, ans2))


def solve(n, arr):
	inf = float('inf')
	sign = 1 if arr[0] > 0 else 0
	segmax = arr[0]
	ans = 0
	for i in range(1, n):
		if sign == 1 and arr[i] > 0:
			segmax = max(segmax, arr[i])
		elif sign == 1 and arr[i] < 0:
			sign = sign ^ 1
			ans = ans + segmax
			segmax = arr[i]
		elif sign == 0 and arr[i] < 0:
			segmax = max(segmax, arr[i])
		elif sign == 0 and arr[i] > 0:
			sign = sign ^ 1
			ans = ans + segmax
			segmax = arr[i]
	ans = ans + segmax
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(n, arr))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	dp = [0 for _ in range(n + 1)]
	length_sums = defaultdict(lambda : None)
	max_length = -1
	last_positive = -1
	last_negative = -1
	for i in range(n - 1, -1, -1):
		element = arr[i]
		sign = element > 0
		if i == n - 1:
			prev_idx = n
		elif element > 0 and last_negative == -1 or (element < 0 and last_positive == -1):
			prev_idx = None
		else:
			prev_idx = last_positive if element < 0 else last_negative
		if prev_idx is not None and prev_idx != n:
			length = dp[prev_idx] + 1
			new_sum = length_sums[dp[prev_idx]] + element
		else:
			length = 1
			new_sum = element
		dp[i] = length
		if prev_idx == n:
			assert length == 1
			length_sums[length] = element
		elif length_sums[length] is not None:
			length_sums[length] = max(length_sums[length], new_sum)
		else:
			length_sums[length] = new_sum
		max_length = max(max_length, length)
		if element > 0:
			last_positive = i
		else:
			last_negative = i
	print(length_sums[max_length])


from itertools import groupby
for _ in range(int(input())):
	l = int(input())
	s = [int(x) for x in input().split()]
	xg = groupby(s, key=lambda x: x > 0)
	sums = 0
	for (_, g) in xg:
		sums += max(list(g))
	print(sums)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		pos = 0
		while i < n and a[i] >= 0:
			pos = max(pos, a[i])
			i += 1
		s += pos
		neg = -10 ** 9 - 1
		while i < n and a[i] < 0:
			neg = max(neg, a[i])
			i += 1
		if neg != -10 ** 9 - 1:
			s += neg
	if pos == -1 or neg == 1:
		print(-1)
	else:
		print(s)


dp = [[999999999999] * 100 for i in range(100)]

def sign(x):
	return 1 if x > 0 else -1
t = int(input())
for j in range(1, t + 1):
	n = int(input())
	nums = list(map(int, input().split()))
	i = 0
	ans = 0
	while i < n:
		cur = nums[i]
		j = i + 1
		while j < n and sign(nums[j]) == sign(nums[i]):
			cur = max(cur, nums[j])
			j += 1
		ans += cur
		i = j
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	x = 0
	z = 0
	for i in range(n):
		if i == 0:
			x = l[i]
			if l[i] < 0:
				f = 2
			else:
				f = 4
		elif f == 4:
			t = l[i]
			if l[i] < 0:
				z = z + x
				x = l[i]
				f = 2
			elif x < t:
				x = t
		else:
			t = l[i]
			if l[i] > 0:
				z = z + x
				x = l[i]
				f = 4
			elif x < t:
				x = t
	print(z + x)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	last = a[0]
	lastind = 0
	for i in range(n):
		if a[i] < 0 and last > 0 or (a[i] > 0 and last < 0):
			b += [max(a[lastind:i])]
			lastind = i
			last = a[i]
	b += [max(a[lastind:n])]
	print(sum(b))


for _ in range(int(input())):
	b = int(input())
	l = list(map(int, input().split()))
	mxs = l[0]
	c = 0
	for i in range(1, b):
		if l[i] * mxs < 0:
			c += mxs
			mxs = l[i]
		else:
			mxs = max(mxs, l[i])
	print(c + mxs)


T = int(input())
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	bi = []
	for i in range(n):
		if l[i] < 0:
			bi.append(0)
		else:
			bi.append(1)
	zero = []
	one = []
	for i in range(n):
		if bi[i] == 0:
			zero.append(i)
			break
	for i in range(n):
		if bi[i] == 1:
			one.append(i)
			break
	if len(zero) == 0 or len(one) == 0:
		print(max(l))
		continue
	negpre = l[0]
	su0 = negpre
	if negpre < 0:
		flag0 = 1
	else:
		flag0 = 0
	for i in range(1, n):
		if bi[i] == 0:
			if flag0 == 1:
				if l[i] > negpre:
					su0 -= negpre
					su0 += l[i]
					negpre = l[i]
			else:
				su0 += l[i]
				negpre = l[i]
				flag0 = 1
		elif bi[i] == 1:
			if flag0 == 0:
				if l[i] > negpre:
					su0 -= negpre
					su0 += l[i]
					negpre = l[i]
			else:
				su0 += l[i]
				negpre = l[i]
				flag0 = 0
	print(su0)


from sys import stdin
t = int(stdin.readline().rstrip())
for _ in range(t):
	z = int(stdin.readline().rstrip())
	l = list(map(int, stdin.readline().rstrip().split(' ')))
	n = []
	last = -1
	ma = l[0]
	if z == 1:
		print(l[0])
	else:
		for i in range(z - 1):
			if l[i] * l[i + 1] > 0:
				ma = max(ma, l[i])
			else:
				ma = max(ma, l[i])
				n.append(ma)
				ma = l[i + 1]
		if l[-1] * l[-2] > 0:
			ma = max(ma, l[-1])
			n.append(ma)
			ma = l[-1]
		else:
			n.append(ma)
			ma = l[-1]
		if len(n) == 0:
			n.append(ma)
		print(sum(n))


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	k = a[0]
	l = [[k]]
	len(l)
	t -= 1
	m = 0
	for i in range(1, len(a)):
		if a[i] * a[i - 1] > 0:
			try:
				l[m].append(a[i])
			except IndexError:
				print(m)
				print(i)
				print('s')
		else:
			try:
				l.append([a[i]])
			except IndexError:
				print(m)
				print(i)
				print('t')
			m += 1
	ans = 0
	for i in l:
		ans += max(i)
	print(ans)


cnt = 213
for u in range(200000):
	cnt -= 99
	cnt += 2
tt = int(input())
for _ in range(tt):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	z = -10 ** 18
	anss = 0
	while i < n:
		if l[i] > 0:
			maxi = 0
			while l[i] > 0:
				maxi = max(maxi, l[i])
				i += 1
				if i == n:
					break
			anss += maxi
		if i >= n:
			break
		if l[i] < 0:
			maxi = z
			while l[i] < 0:
				maxi = max(l[i], maxi)
				i += 1
				if i == n:
					break
			anss += maxi
	print(anss)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = []
	p = []
	np = []
	s = 0
	for i in range(len(a)):
		if a[i] < 0:
			if p != []:
				s += max(p)
				p = []
			np.append(a[i])
		elif a[i] > 0:
			if np != []:
				s += max(np)
				np = []
			p.append(a[i])
	if np != []:
		s += max(np)
	else:
		s += max(p)
	print(s)


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


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math

def bin1(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = k ** mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin1(mid + 1, r, k, t, b, val, ans)
		else:
			return bin1(l, mid - 1, k, t, b, val, ans)

def bin2(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = t * k ** mid + b * mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin2(l, mid - 1, k, t, b, val, ans)
		else:
			return bin2(mid + 1, r, k, t, b, val, ans)

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 * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def bin(l, r, ll, val):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if val >= ll[mid][0] and val <= ll[mid][1]:
			return mid
		elif val < ll[mid][0]:
			return bin(l, mid - 1, ll, val)
		else:
			return bin(mid + 1, r, ll, val)

def deci(n):
	s = ''
	while n != 0:
		if n % 2 == 0:
			n = n // 2
			s = '0' + s
		else:
			n = n // 2
			s = '1' + s
	return s

def diff(s1, s2):
	if len(s1) < len(s2):
		v = len(s1)
		while v != len(s2):
			s1 = '0' + s1
			v = v + 1
	else:
		v = len(s2)
		while v != len(s1):
			s2 = '0' + s2
			v = v + 1
	c = 0
	for i in range(len(s1)):
		if s1[i:i + 1] != s2[i:i + 1]:
			c = c + 1
	return c
from sys import stdin, stdout

def fac(a, b):
	v = a
	while a != b:
		v *= a - 1
		a = a - 1
	return v

def bino(l, r, n):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		val1 = math.log(n / mid + 1, 2)
		val2 = int(val1)
		if val1 == val2:
			return val1
		elif val1 < 1.0:
			return bino(l, mid - 1, n)
		else:
			return bino(mid + 1, r, n)
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	max1 = 0
	max2 = -1000000001
	c = 0
	count1 = 0
	count2 = 0
	for i in range(n):
		if c == 0:
			if l[i] > 0:
				c = 1
				if l[i] > max1:
					max1 = l[i]
			else:
				c = -1
				if l[i] > max2:
					max2 = l[i]
		elif c == -1:
			if l[i] > 0:
				c = 1
				count2 += max2
				max2 = -1000000001
				if l[i] > max1:
					max1 = l[i]
			elif l[i] > max2:
				max2 = l[i]
		elif l[i] > 0:
			if l[i] > max1:
				max1 = l[i]
		else:
			c = -1
			count1 += max1
			max1 = 0
			if l[i] > max2:
				max2 = l[i]
	if max1 != 0:
		count1 += max1
	if max2 != -1000000001:
		count2 += max2
	print(count1 + count2)


m = int(input())
for i in range(m):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	max = a[0]
	for x in a:
		if x * max > 0 and x > max:
			max = x
		elif x * max < 0:
			s += max
			max = x
	s += max
	print(s)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	maxArr = []
	pos = 0
	neg = 0
	flag = True if arr[0] > 0 else False
	res = 0
	for i in arr:
		if i > 0:
			if flag:
				pos = max(pos, i)
			else:
				res += neg
				neg = 0
				pos = i
				flag = True
		elif flag:
			res += pos
			pos = 0
			neg = i
			flag = False
		else:
			neg = max(neg, i)
			if neg == 0:
				neg = i
	if flag:
		res += pos
	else:
		res += neg
	print(res)


from math import *

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = [int(x) for x in input().split()]
		ind = 0
		ans = 0
		while ind < n:
			if arr[ind] < 0:
				loc = arr[ind]
				while arr[ind] < 0:
					if ind == n - 1:
						loc = max(loc, arr[ind])
						break
					loc = max(loc, arr[ind])
					ind += 1
				ans += loc
			else:
				loc = arr[ind]
				while arr[ind] > 0:
					if ind == n - 1:
						loc = max(loc, arr[ind])
						break
					loc = max(loc, arr[ind])
					ind += 1
				ans += loc
			if ind == n - 1:
				if arr[n - 1] > 0 and arr[n - 2] < 0:
					ans += arr[n - 1]
				elif arr[n - 1] < 0 and arr[n - 2] > 0:
					ans += arr[n - 1]
				print(ans)
				break
main()


def main():
	n = int(input())
	arr = []
	for _ in range(n):
		temp = input()
		arr.append(list(map(int, input().split())))
	for i in range(n):
		calcSum(arr[i])

def calcSum(arr):
	lenarr = len(arr)
	total = 0
	maxn = 0
	flag = True
	for i in range(lenarr):
		if arr[i] > 0 and flag:
			maxn = max(maxn, arr[i])
		elif arr[i] < 0 and (not flag):
			maxn = max(maxn, arr[i])
		else:
			total += maxn
			if arr[i] > 0:
				flag = True
				maxn = arr[i]
			else:
				flag = False
				maxn = arr[i]
	total += maxn
	print(total)
main()


t = int(input())
ans = []
for k in range(t):
	n = int(input())
	seq = list(map(int, input().split()))
	result = 0
	high = float('-inf')
	if seq[0] > 0:
		dir = -1
	else:
		dir = 1
	for i in range(n):
		if seq[i] < 0 and dir < 0:
			dir = 1
			result += high
			high = seq[i]
		elif seq[i] > 0 and dir > 0:
			dir = -1
			result += high
			high = seq[i]
		elif seq[i] > high:
			high = seq[i]
	ans.append(result + high)
for i in ans:
	print(i)


for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	(i, xsum) = (0, 0)
	while i < N:
		if a[i] > 0:
			pmax = a[i]
			while i < N and a[i] > 0:
				pmax = max(pmax, a[i])
				i += 1
			xsum += pmax
		else:
			nmax = a[i]
			while i < N and a[i] < 0:
				nmax = max(nmax, a[i])
				i += 1
			xsum += nmax
	print(xsum)


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


t = int(input())

def sign(a):
	if a > 0:
		return 1
	else:
		return -1
for i in range(t):
	n = int(input())
	a = [int(el) for el in input().split()]
	curr_sign = sign(a[0])
	max = a[0]
	sum = 0
	ind = 0
	while ind < n:
		while ind < n and sign(a[ind]) == curr_sign:
			if a[ind] > max:
				max = a[ind]
			ind += 1
		if ind == n:
			sum += max
		else:
			curr_sign = sign(a[ind])
			sum += max
			max = a[ind]
	print(sum)


def solve(n, l):
	k = list(map(int, l))
	m = []
	i = 0
	parity = None
	while i < n:
		if k[i] > 0:
			parity = True
		else:
			parity = False
		f = False
		for j in range(i, n):
			if not (k[j] > 0) == parity:
				f = True
				m.append(max(k[i:j]))
				break
		if j == n - 1 and f == False:
			m.append(max(k[i:]))
			break
		i = j
	print(sum(m))
	return None
no = int(input())
for i in range(no):
	n = int(input())
	l = input().strip().split()
	solve(n, l)


from itertools import accumulate
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = []
	neg = False
	if a[0] < 0:
		neg = True
	temp = []
	i = 0
	while i < n:
		if neg:
			if a[i] > 0:
				s.append(temp)
				temp = [a[i]]
				neg = False
			else:
				temp.append(a[i])
		elif a[i] > 0:
			temp.append(a[i])
		else:
			s.append(temp)
			temp = [a[i]]
			neg = True
		i += 1
	s.append(temp)
	summ = 0
	for i in s:
		summ += max(i)
	print(summ)


from sys import stdin, stdout
from collections import Counter
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007

def solve():
	n = inp()
	l = li()
	i = 0
	ans = []
	while i < n:
		j = i + 1
		x = [l[i]]
		if l[i] < 0:
			while j < n and l[j] < 0:
				x.append(l[j])
				j += 1
		else:
			while j < n and l[j] > 0:
				x.append(l[j])
				j += 1
		ans.append(x)
		i = j
	val = 0
	for i in ans:
		val += max(i)
	print(val)
for _ in range(inp()):
	solve()


n = int(input())
ans1 = []
while n != 0:
	t = int(input())
	a = [int(x) for x in input().split()]
	if min(a) > 0 or max(a) < 0:
		ans1.append(max(a))
	else:
		j = 0
		ans = []
		while j < t:
			if a[j] < 0:
				i = j + 1
				l = a[j]
				while i < t:
					if a[i] > 0:
						break
					else:
						l = max(l, a[i])
					i += 1
				ans.append(l)
				j = i
			else:
				i = j + 1
				l = a[j]
				while i < t:
					if a[i] < 0:
						break
					else:
						l = max(l, a[i])
					i += 1
				ans.append(l)
				j = i
		ans1.append(sum(ans))
	n -= 1
print(*ans1, sep='\n')


for i in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	for j in range(n):
		if j == 0:
			if data[j] > 0:
				flag = 0
			else:
				flag = 1
			temp = data[j]
			sum = 0
		if flag == 0:
			if data[j] > temp:
				temp = data[j]
			if j < n - 1:
				if data[j + 1] < 0:
					flag = 1
					sum = sum + temp
					temp = data[j + 1]
			else:
				sum = sum + temp
		else:
			if data[j] > temp:
				temp = data[j]
			if j < n - 1:
				if data[j + 1] > 0:
					flag = 0
					sum = sum + temp
					temp = data[j + 1]
			else:
				sum = sum + temp
	print(sum)


n = int(input())
for ii in range(n):
	q = int(input())
	t = list(map(int, input().split()))
	x = t[0]
	s = 0
	for j in range(1, q):
		if t[j] * x > 0 and t[j] > x:
			x = t[j]
		if t[j] * x < 0:
			s += x
			x = t[j]
	s += x
	print(s)


from sys import stdin
input = stdin.readline
n = int(input())
for _ in range(n):
	m = int(input())
	case = list(map(int, input().split()))
	total = 0
	remember = [case[0]]
	for i in range(1, m):
		if case[i - 1] > 0 and case[i] < 0 or (case[i - 1] < 0 and case[i] > 0):
			total += max(remember)
			remember = [case[i]]
		else:
			remember.append(case[i])
	total += max(remember)
	print(total)


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


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	lst = list(map(int, stdin.readline().split()))
	temp = lst[0]
	end = 0
	k = 1
	sum = lst[0]
	length = 1
	for i in range(1, n):
		if lst[i] * lst[end] < 0:
			end = i
			sum += lst[i]
		elif lst[i] > lst[end]:
			sum += lst[i] - lst[end]
			end = i
	stdout.write(str(sum) + '\n')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	su = 0
	sign = 0
	j = 0
	while j < n:
		if l[j] > 0:
			ma = l[j]
			for k in range(j + 1, n):
				if l[k] > ma and l[k] > 0:
					ma = l[k]
				elif l[k] < 0:
					j = k - 1
					break
				if k == n - 1:
					j = k
			su += ma
			j = j + 1
		elif l[j] < 0:
			mi = l[j]
			for k in range(j + 1, n):
				if l[k] > mi and l[k] < 0:
					mi = l[k]
				elif l[k] > 0:
					j = k - 1
					break
				if k == n - 1:
					j = k
			su += mi
			j += 1
	print(su)


for _ in range(int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	pos = 0
	maxy = 0
	miny = 0
	sum = 0
	for i in range(x):
		if l[i] > 0:
			if pos == 0:
				sum += miny
			miny = -99999999999
			pos = 1
			maxy = max(l[i], maxy)
		else:
			if i == 0:
				miny = -99999999999
			if pos == 1:
				sum += maxy
			pos = 0
			maxy = 0
			miny = max(l[i], miny)
	sum += maxy
	if miny != -99999999999:
		sum += miny
	print(sum)


def search_max(mas):
	maxx = mas[0]
	for i in mas:
		if maxx < i:
			maxx = i
	return maxx
t = int(input())
for q in range(t):
	n = int(input())
	a = [int(i) for i in input().strip().split(' ')]
	mass = []
	lol_xd = [a[0]]
	i = 1
	while i < n:
		if (a[i] < 0) == (a[i - 1] < 0):
			lol_xd.append(a[i])
		else:
			mass.append(lol_xd)
			lol_xd = [a[i]]
		i += 1
	mass.append(lol_xd)
	summ = 0
	for i in mass:
		summ += search_max(i)
	print(summ)


for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	sign = arr[0]
	(m, s) = (arr[0], 0)
	na = []
	for i in range(n):
		if sign * arr[i] > 0:
			m = max(m, arr[i])
		else:
			s += m
			na.append(m)
			(m, sign) = (arr[i], arr[i])
	print(s + m)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	i = c = ans = 0
	while i < n:
		if c % 2 == 0 and lis[i] > 0:
			mx = 0
			while i < n and lis[i] > 0:
				mx = max(lis[i], mx)
				i += 1
			ans += mx
			c += 1
			i -= 1
		elif c % 2 != 0 and lis[i] < 0:
			mx = -100000000000
			while i < n and lis[i] < 0:
				mx = max(lis[i], mx)
				i += 1
			ans += mx
			c += 1
			i -= 1
		i += 1
	c1 = ans1 = i = 0
	while i < n:
		if c1 % 2 != 0 and lis[i] > 0:
			mx = 0
			while i < n and lis[i] > 0:
				mx = max(lis[i], mx)
				i += 1
			ans1 += mx
			c1 += 1
			i -= 1
		elif c1 % 2 == 0 and lis[i] < 0:
			mx = -100000000000
			while i < n and lis[i] < 0:
				mx = max(lis[i], mx)
				i += 1
			ans1 += mx
			c1 += 1
			i -= 1
		i += 1
	if c1 > c:
		print(ans1)
	elif c > c1:
		print(ans)
	else:
		print(max(ans, ans1))


a = int(input())
for i in range(a):
	s = int(input())
	z = list(map(int, input().split()))
	ans = []
	count = 1
	for i in range(1, len(z)):
		if z[i] > 0 and z[i - 1] > 0:
			count += 1
		elif z[i] < 0 and z[i - 1] < 0:
			count += 1
		else:
			ans.append(count)
			count = 1
	ans.append(count)
	total = 0
	start = 0
	if z[0] > 0:
		start = 0
		for i in range(len(ans)):
			if i % 2 == 0:
				total += max(z[start:start + ans[i]])
				start = start + ans[i]
			else:
				total += max(z[start:start + ans[i]])
				start += ans[i]
	else:
		for i in range(len(ans)):
			if i % 2 == 0:
				total += max(z[start:start + ans[i]])
				start = start + ans[i]
			else:
				total += max(z[start:start + ans[i]])
				start += ans[i]
	print(total)


import random, math
from copy import deepcopy as dc
from bisect import bisect_left, bisect_right

def solution(li, n):
	pos = 0
	neg = 0
	prev = -1
	cur_e = float('-inf')
	flag = False
	for i in range(n):
		if prev < 0 and li[i] > 0:
			flag = True
			cur_e = max(cur_e, li[i])
		elif prev < 0 and li[i] < 0:
			pos += cur_e
			prev = 1
			cur_e = li[i]
		elif prev > 0 and li[i] < 0:
			cur_e = max(cur_e, li[i])
		elif prev > 0 and li[i] > 0:
			pos += cur_e
			prev = -1
			cur_e = li[i]
	pos += cur_e
	if not flag:
		pos = float('-inf')
	prev = 1
	cur_e = float('-inf')
	flag = False
	for i in range(n):
		if prev < 0 and li[i] > 0:
			cur_e = max(cur_e, li[i])
		elif prev < 0 and li[i] < 0:
			neg += cur_e
			prev = 1
			cur_e = li[i]
		elif prev > 0 and li[i] < 0:
			flag = True
			cur_e = max(cur_e, li[i])
		elif prev > 0 and li[i] > 0:
			neg += cur_e
			prev = -1
			cur_e = li[i]
	neg += cur_e
	if not flag:
		neg = float('-inf')
	return max(pos, neg)

def input_test():
	for _ in range(int(input())):
		n = int(input())
		li = list(map(int, input().strip().split(' ')))
		out = solution(li, n)
		print(out)

def test():
	pass
input_test()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = a[0]
	l = a[0]
	for i in range(1, n):
		if a[i] * l < 0:
			l = a[i]
			s += l
		elif l < a[i]:
			s += a[i] - l
			l = a[i]
	print(s)


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


import sys
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	msum = 0
	j = 0
	while j <= n - 1:
		mx = -sys.maxsize - 1
		if a[j] > 0:
			while j <= n - 1 and a[j] > 0:
				mx = max(mx, a[j])
				j += 1
		else:
			while j <= n - 1 and a[j] < 0:
				mx = max(mx, a[j])
				j += 1
		msum += mx
	print(msum)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	buffer = [arr[0]]
	sign = -1 if arr[0] < 0 else 1
	total = 0
	for i in range(1, n):
		current_sign = -1 if arr[i] < 0 else 1
		if sign != current_sign:
			sign = current_sign
			total += max(buffer)
			buffer = []
		buffer.append(arr[i])
	total += max(buffer)
	print(total)


for ii in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	temp = []
	f = 0
	ans = 0
	for i in range(n):
		if not temp:
			if arr[i] < 0:
				f = 0
			else:
				f = 1
			temp.append(arr[i])
		else:
			if arr[i] > 0 and f:
				temp.append(arr[i])
			if arr[i] < 0 and (not f):
				temp.append(arr[i])
			if arr[i] > 0 and (not f):
				ans += max(temp)
				temp = []
				temp.append(arr[i])
				f = not f
			if arr[i] < 0 and f:
				ans += max(temp)
				temp = []
				temp.append(arr[i])
				f = not f
	ans += max(temp)
	print(ans)


import sys
import math
from collections import defaultdict
mod = 1000000007
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	a = 0
	x = l[0]
	for i in range(1, n):
		if l[i - 1] > 0 and l[i] > 0:
			if l[i] > x:
				x = l[i]
		elif l[i - 1] < 0 and l[i] < 0:
			if l[i] > x:
				x = l[i]
		else:
			a += x
			x = l[i]
	print(a + x)


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	N = int(input())
	A = list(map(int, input().split()))
	Query.append((N, A))
for (N, A) in Query:
	T = []
	sig = A[0]
	ans = 0
	for a in A:
		if a > 0:
			if sig > 0:
				T.append(a)
			else:
				ans += max(T)
				T = [a]
				sig = 1
		elif sig > 0:
			ans += max(T)
			T = [a]
			sig = -1
		else:
			T.append(a)
	if T:
		ans += max(T)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(dl, su, masu, madl) = (1, 0, 0, 0)
	nn = a[0]
	for i in range(1, n):
		if a[i] > 0 and a[i - 1] > 0 or (a[i] < 0 and a[i - 1] < 0):
			if nn < 0 and a[i] < 0:
				nn = max(a[i], a[i - 1], nn)
			elif nn > 0 and a[i] > 0:
				nn = max(a[i], a[i - 1], nn)
			else:
				nn = max(a[i], a[i - 1])
		elif a[i] > 0 and a[i - 1] < 0 or (a[i] < 0 and a[i - 1] > 0):
			dl += 1
			su += nn
			nn = a[i]
		elif madl < dl:
			madl = dl
			masu = su
			dl = 1
			su = a[i]
		elif dl == madl and su > masu:
			masu = su
			dl = 1
			su = a[i]
	su += nn
	if madl < dl:
		madl = dl
		masu = su
	elif dl == madl and su > masu:
		masu = su
	print(masu)


import sys
import collections
import threading

def main():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	previsNeg = a[0] < 0
	previsPos = a[0] > 0
	acc = a[0]
	for c in a:
		if c > 0:
			if previsPos:
				acc = max(acc, c)
			else:
				ans += acc
				previsNeg = False
				previsPos = True
				acc = c
			continue
		elif previsNeg:
			acc = max(acc, c)
		else:
			ans += acc
			previsNeg = True
			previsPos = False
			acc = c
	ans += acc
	print(ans)
input = sys.stdin.readline
tnum = int(input())
for _ in range(tnum):
	main()


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	m = []
	maxi = -1000000000000000000000000000000000000
	if l[0] > 0:
		c = 1
	else:
		c = 0
	for i in range(n):
		if c == 1 and l[i] > 0:
			maxi = max(maxi, l[i])
			c = 1
		elif c == 1 and l[i] < 0:
			m.append(maxi)
			maxi = l[i]
			c = 0
		elif c == 0 and l[i] < 0:
			maxi = max(maxi, l[i])
			c = 0
		else:
			m.append(maxi)
			maxi = l[i]
			c = 1
	if len(m) != 0:
		if max(l[-1], m[-1]) > 0 and min(l[-1], m[-1]) < 0:
			m.append(maxi)
		print(sum(m))
	else:
		print(max(l))


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


for test in range(int(input())):
	n = int(input())
	lt = list(map(int, input().split()))
	for i in range(n - 1):
		if lt[i] > 0 and lt[i + 1] > 0 or (lt[i] < 0 and lt[i + 1] < 0):
			lt[i + 1] = max(lt[i], lt[i + 1])
			lt[i] = 0
	s = sum(lt)
	print(s)


for _ in range(int(input())):
	l = int(input())
	arr = list(map(int, input().split()))
	pos = 0
	nev = -9999999999
	finalsum = 0
	for i in range(l):
		if arr[i] > 0:
			if i < l - 1 and arr[i + 1] > 0:
				if arr[i] > pos:
					pos = arr[i]
			elif i < l - 1 and arr[i + 1] < 0:
				if arr[i] > pos:
					pos = arr[i]
					finalsum = pos + finalsum
					pos = 0
				else:
					finalsum = pos + finalsum
					pos = 0
			elif arr[i] > pos:
				pos = arr[i]
				finalsum = pos + finalsum
				pos = 0
			else:
				finalsum = pos + finalsum
		elif i < l - 1 and arr[i + 1] < 0:
			if arr[i] > nev:
				nev = arr[i]
		elif i < l - 1 and arr[i + 1] > 0:
			if arr[i] > nev:
				nev = arr[i]
				finalsum = nev + finalsum
				nev = -99999999999
			else:
				finalsum = nev + finalsum
				nev = -99999999999
		elif arr[i] > nev:
			nev = arr[i]
			finalsum = nev + finalsum
		else:
			finalsum = nev + finalsum
	print(finalsum)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = []
	cur = arr[0]
	flag = 0
	if cur < 0:
		flag = 0
	else:
		flag = 1
	ans.append(cur)
	for i in range(1, n):
		if flag == 0 and arr[i] < 0 or (flag == 1 and arr[i] > 0):
			ans[-1] = max(arr[i], ans[-1])
		elif flag == 0 and arr[i] > 0 or (flag == 1 and arr[i] < 0):
			ans.append(arr[i])
			if arr[i] > 0:
				flag = 1
			else:
				flag = 0
	print(sum(ans))


T = int(input())
for p in range(T):
	n = int(input())
	A = [int(c) for c in input().split()]
	i = 0
	l = []
	if A[0] > 0:
		flag = 'pos'
	else:
		flag = 'neg'
	while i < len(A):
		if i >= len(A):
			break
		maxi = A[i]
		while flag == 'pos' and i < len(A):
			if A[i] < 0:
				flag = 'neg'
				l.append(maxi)
				break
			else:
				if A[i] > maxi:
					maxi = A[i]
				if i == len(A) - 1:
					l.append(maxi)
				i += 1
		if i >= len(A):
			break
		mini = A[i]
		while flag == 'neg' and i < len(A):
			if A[i] > 0:
				flag = 'pos'
				l.append(mini)
				break
			else:
				if mini < A[i]:
					mini = A[i]
				if i == len(A) - 1:
					l.append(mini)
				i += 1
	print(sum(l))


import io, os
R = lambda : map(int, input().split())

def che(x):
	if x > 0:
		return 1
	if x < 0:
		return -1
for _ in range(int(input())):
	n = int(input())
	a = list(R())
	(dp, pre, siz) = (0, 0, 0)
	now = che(a[0])
	for i in a:
		if che(i) == now:
			dp += i
			siz += 1
			pre = i
			now = -now
		elif i > pre:
			dp -= pre
			pre = i
			dp += pre
	print(dp)


for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	if a[0] > 0:
		ch = 1
		ma = a[0]
	else:
		ch = -1
		mi = a[0]
	sy = 0
	for j in range(1, n):
		if a[j] > 0:
			if ch == 1:
				ma = max(a[j], ma)
			else:
				sy += mi
				ch = 1
				ma = a[j]
		elif ch == -1:
			mi = max(a[j], mi)
		else:
			sy += ma
			ch = -1
			mi = a[j]
	if a[-1] > 0:
		print(sy + ma)
	else:
		print(sy + mi)


t = int(input())
for z in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 0 or n == 1:
		print(sum(a))
	else:
		c = 0
		p = 0
		ne = -1000000000
		fp = 0
		fn = 0
		for i in range(n):
			if a[i] < 0:
				if fn == 1:
					ne = max(ne, a[i])
				else:
					ne = a[i]
				fn = 1
				if fp == 1:
					c += p
				fp = 0
			else:
				if fp == 1:
					p = max(p, a[i])
				else:
					p = a[i]
				fp = 1
				if fn == 1:
					c += ne
				fn = 0
		if a[n - 1] < 0:
			c += ne
		else:
			c += p
		print(c)


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


import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

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 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()
		s = 0
		x = 0
		for i in range(n):
			if x == 0:
				x = l[i]
			elif x > 0:
				if l[i] > 0:
					x = max(x, l[i])
				else:
					s += x
					x = l[i]
			elif l[i] < 0:
				x = max(x, l[i])
			else:
				s += x
				x = l[i]
		print(s + x)
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()


import itertools
for _ in range(int(input())):
	n = input()
	group = itertools.groupby(map(int, input().split()), lambda x: x < 0)
	l = []
	for (_, v) in group:
		l.append(max(list(v)))
	print(sum(l))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count = 1
	index = []
	sums = 0
	if arr[0] > 0:
		sign = 1
	else:
		sign = -1
	for i in range(n):
		if arr[i] < 0 and sign == 1:
			count += 1
			sign = -1
			index += [i]
		elif arr[i] > 0 and sign == -1:
			count += 1
			sign = 1
			index += [i]
	if len(index) == 0:
		print(max(arr))
	else:
		sums += max(arr[0:index[0]])
		for i in range(len(index)):
			if i != len(index) - 1:
				sums += max(arr[index[i]:index[i + 1]])
			else:
				sums += max(arr[index[i]:])
		print(sums)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(pos, neg) = (False, False)
	if l[0] > 0:
		pos = True
	else:
		neg = True
	li = [l[0]]
	for i in range(1, n):
		if l[i] > 0:
			if pos:
				li[-1] = max(li[-1], l[i])
			else:
				neg = False
				pos = True
				li.append(l[i])
		elif neg:
			li[-1] = max(li[-1], l[i])
		else:
			neg = True
			pos = False
			li.append(l[i])
	print(sum(li))


def sign(n):
	return n > 0
for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	sm = 0
	i = 0
	while i < n:
		cs = sign(p[i])
		j = i + 1
		while j < n:
			if cs != sign(p[j]):
				break
			j += 1
		sm += max(p[i:j])
		i = j
	print(sm)


for i in range(int(input())):
	b = int(input())
	a = list(map(int, input().split()))
	r = 0
	z = a[0]
	for j in range(1, len(a)):
		if a[j] > 0 and a[j - 1] < 0 or (a[j] < 0 and a[j - 1] > 0):
			r += z
			z = a[j]
		elif a[j] > z:
			z = a[j]
	r += z
	print(r)


import sys, random, bisect
from collections import deque, defaultdict
from heapq import heapify, heappop, heappush
mod = int(1000000000.0 + 7)
inf = int(1e+20)
input = lambda : sys.stdin.readline().rstrip()
mi = lambda : map(int, input().split())
li = lambda : list(mi())
ii = lambda : int(input())
py = lambda : print('YES')
pn = lambda : print('NO')
t = ii()
for _ in range(t):
	n = ii()
	arr = li()
	res = arr[0]
	pre = -arr[0]
	for a in arr:
		if a * pre > 0:
			pre = max(pre, a)
		else:
			res += pre
			pre = a
	print(res + pre)


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


k = int(input())
for x in range(k):
	m = int(input())
	l = list(map(int, input().split()))
	l2 = []
	i = 0
	while i < m:
		j = i
		if l[i] > 0:
			while l[i] > 0:
				i = i + 1
				if i == m:
					break
			l2.append(max(l[j:i]))
		elif l[i] < 0:
			while l[i] < 0:
				i = i + 1
				if i == m:
					break
			l2.append(max(l[j:i]))
	print(sum(l2))


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = []
	ma = -1000000000
	flag = a[0] < 0
	for i in range(n):
		if a[i] >= 0:
			if flag == 1:
				d.append(ma)
				flag = 0
			ma = max(a[i], ma)
		else:
			if flag == 0:
				d.append(ma)
				ma = a[i]
				flag = 1
			ma = max(ma, a[i])
	d.append(ma)
	print(sum(d))


def solve(n, d):
	last = 1
	if d[0] < 0:
		last = -1
	sums = 0
	lastadded = None
	for i in range(n):
		c = d[i]
		if c * last > 0:
			sums += c
			last *= -1
			lastadded = c
		elif lastadded != None:
			newSum = max(sums, sums + c - lastadded)
			if newSum > sums:
				sums = newSum
				lastadded = c
	return sums

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


tc = int(input())
for i in range(tc):
	n = int(input())
	x = list(map(int, input().split()))
	(sm, mx, mn) = (0, 0, 0)
	if x[0] > 0:
		mx = x[0]
	else:
		mn = x[0]
	for j in x[1:]:
		if mx != 0:
			if j > 0:
				if j > mx:
					mx = j
			else:
				sm += mx
				mx = 0
				mn = j
		elif j < 0:
			if j > mn:
				mn = j
		else:
			sm += mn
			mn = 0
			mx = j
	if mx:
		sm += mx
	elif mn:
		sm += mn
	print(sm)


T = int(input())
for t in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	ans = i = 0
	while i < n:
		if a[i] > 0:
			maxv = -10 ** 9
			while i < n and a[i] > 0:
				maxv = max(maxv, a[i])
				i += 1
			ans += maxv
		else:
			maxv = -10 ** 9
			while i < n and a[i] < 0:
				maxv = max(maxv, a[i])
				i += 1
			ans += maxv
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	sign = -1
	ans = []
	if L[0] > 0:
		sign = 2
	else:
		sign = 1
	ans.append(L[0])
	for i in L[1:]:
		if i > 0:
			if sign == 2:
				if ans[-1] < i:
					ans[-1] = i
			else:
				sign = 2
				ans.append(i)
		elif sign == 1:
			if abs(ans[-1]) > abs(i):
				ans[-1] = i
		else:
			ans.append(i)
			sign = 1
	print(sum(ans))


t = int(input())
for p in range(t):
	n = int(input())
	a = map(int, input().split())
	choice = 0
	sumup = 0
	for i in a:
		if i > 0 > choice or choice > 0 > i:
			sumup += choice
			choice = i
		if choice == 0 or i > choice > 0 or 0 > i > choice:
			choice = i
	sumup += choice
	print(sumup)


def arr_from(minus, a):
	pos1 = []
	i = 0
	minim = None
	maxim = None
	while i < len(a):
		if minus == True:
			if a[i] < 0:
				if minim == None:
					minim = a[i]
				else:
					minim = max(minim, a[i])
				i += 1
			elif minim != None:
				minus = False
				pos1.append(minim)
				minim = None
			else:
				i += 1
		elif a[i] > 0:
			if maxim == None:
				maxim = a[i]
			else:
				maxim = max(maxim, a[i])
			i += 1
		elif maxim != None:
			minus = True
			pos1.append(maxim)
			maxim = None
		else:
			i += 1
	if minim != None:
		pos1.append(minim)
	if maxim != None:
		pos1.append(maxim)
	return pos1

def main():
	t = int(input())
	for _ in range(t):
		_ = input()
		a = [int(x) for x in input().split()]
		pos1 = arr_from(True, a)
		pos2 = arr_from(False, a)
		if len(pos1) > len(pos2):
			print(sum(pos1))
		else:
			print(sum(pos2))
main()


t = int(input())
buf = []
NINF = -10 ** 10
for _ in range(t):
	n = int(input())
	aaa = list(map(int, input().split()))
	ans = 0
	prev_is_positive = aaa[0] > 0
	tmp = NINF
	for a in aaa:
		curr_is_positive = a > 0
		if prev_is_positive != curr_is_positive:
			ans += tmp
			tmp = a
		else:
			tmp = max(tmp, a)
		prev_is_positive = curr_is_positive
	ans += tmp
	buf.append(ans)
print('\n'.join(map(str, buf)))


test = int(input())
for t in range(test):
	n = int(input())
	l = list(map(int, input().split(' ')))
	l1 = []
	lapp = [l[0]]
	it = l[0]
	for i in range(1, len(l)):
		if it * l[i] > 0:
			lapp.append(l[i])
			it = l[i]
		else:
			l1.append(lapp)
			lapp = [l[i]]
			it = l[i]
	l1.append(lapp)
	s = 0
	for i in range(len(l1)):
		s += max(l1[i])
	print(s)


t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().split()))
	indices = []
	for i in range(n - 1):
		if arr[i] > 0 and arr[i + 1] < 0 or (arr[i] < 0 and arr[i + 1] > 0):
			indices.append(i + 1)
	start = 0
	Sum = 0
	if indices == []:
		print(max(arr))
	else:
		for i in indices:
			Sum += max(arr[start:i])
			start = i
		print(Sum + max(arr[indices[-1]:n]))
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	(i, j) = (0, 0)
	Sum = 0
	while i < n and j < n:
		if A[i] < 0:
			i += 1
		elif A[j] > 0:
			j += 1
		elif i > j:
			Max = A[j]
			for k in range(j, i):
				Max = max(Max, A[k])
			Sum += Max
			j = i + 1
		else:
			Max = A[i]
			for k in range(i, j):
				Max = max(Max, A[k])
			Sum += Max
			i = j + 1
	if i >= n:
		Max = A[j]
		for k in range(j, n):
			Max = max(Max, A[k])
		Sum += Max
	else:
		Max = A[i]
		for k in range(i, n):
			Max = max(Max, A[k])
		Sum += Max
	print(Sum)


t = int(input())
for i in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	ar = []
	ar.append(li[0])
	last = []
	for j in range(0, n - 1):
		if li[j] > 0 and li[j + 1] > 0:
			ar.append(li[j + 1])
		elif li[j] > 0 and li[j + 1] < 0:
			last.append(max(ar))
			ar = []
			ar.append(li[j + 1])
		elif li[j] < 0 and li[j + 1] > 0:
			last.append(max(ar))
			ar = []
			ar.append(li[j + 1])
		else:
			ar.append(li[j + 1])
	last.append(max(ar))
	print(sum(last))


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = [a[0]]
	sign = []
	(p, nn) = (0, 10 ** 18)
	if ans[0] >= 0:
		p = a[0]
	else:
		nn = a[0]
	for i in range(1, n):
		if a[i] >= 0 and a[i - 1] >= 0:
			ans[-1] = max(ans[-1], a[i])
		elif a[i] >= 0 and a[i - 1] < 0:
			ans.append(a[i])
			p = a[i]
		elif a[i] < 0 and a[i - 1] < 0:
			ans[-1] = max(ans[-1], a[i])
		else:
			ans.append(a[i])
			nn = a[i]
	print(sum(ans))


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


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(input().split())
	curr = int(li[0])
	sum = 0
	x = 0
	for i in range(1, n):
		x = curr * int(li[i])
		if x > 0:
			curr = max(curr, int(li[i]))
		else:
			sum = sum + curr
			curr = int(li[i])
	sum = sum + curr
	print(sum)


inputs = int(input())
for j in range(inputs):
	n = int(input())
	arr = list(map(int, input().split()))
	(max_sum, curr) = (0, arr[0])
	for i in range(1, len(arr)):
		if arr[i] > 0 and curr > 0 or (arr[i] < 0 and curr < 0):
			curr = max(arr[i], curr)
		else:
			max_sum += curr
			curr = arr[i]
	print(max_sum + curr)


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


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

	def sign(x):
		return 1 if 0 < x else -1
	while i < n:
		cur = a[i]
		j = i
		while j < n and sign(a[i]) == sign(a[j]):
			cur = max(cur, a[j])
			j += 1
		ans += cur
		i = j
	print(ans)


sign = lambda x: (1, -1)[x < 0]
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	k = 1
	sum = 0
	x = a[0]
	maxi = x
	psign = sign(x)
	for i in range(1, n):
		x = a[i]
		if sign(x) != psign:
			k += 1
			sum += maxi
			maxi = x
			psign = sign(x)
		else:
			maxi = max(maxi, x)
	sum += maxi
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ind = [0]
	j = 1
	while j < n:
		if a[j] * a[j - 1] < 0:
			ind.append(j)
		j += 1
	s = 0
	for k in range(len(ind) - 1):
		r = max(a[ind[k]:ind[k + 1]])
		s += r
	b = len(ind) - 1
	c = max(a[ind[b]:n])
	s += c
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [*map(int, input().split())]
	res = 0
	positive = True
	sub = [0]
	for i in a:
		if positive and i < 0 or (not positive and i > 0):
			res += max(sub)
			positive = not positive
			sub = []
		sub.append(i)
	res += max(sub)
	print(res)


T = int(input())
for i in range(0, T):
	N = int(input())
	A = list(map(int, input().split()))
	j = 1
	if A[0] > 0:
		flag = 1
	else:
		flag = -1
	stick = A[0]
	sume = 0
	while j < N:
		if A[j] > 0:
			if flag == 1:
				if A[j] > stick:
					stick = A[j]
			else:
				sume += stick
				flag = 1
				stick = A[j]
		elif flag == 1:
			sume += stick
			flag = -1
			stick = A[j]
		elif A[j] > stick:
			stick = A[j]
		j += 1
	sume += stick
	print(sume)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sarr = []
	i = 0
	while i < n:
		if arr[i] < 0:
			tarr = []
			while i < n and arr[i] < 0:
				tarr.append(arr[i])
				i += 1
			sarr.append(tarr)
		else:
			tarr = []
			while i < n and arr[i] > 0:
				tarr.append(arr[i])
				i += 1
			sarr.append(tarr)
	if len(sarr) == 1:
		print(max(sarr[0]))
	else:
		c = 0
		for i in range(len(sarr)):
			c = c + max(sarr[i])
		print(c)


t = int(input())
ans = [0 for _ in range(t)]
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	pos = 0
	neg = 0
	for i in range(0, n):
		if a[i] > 0:
			pos += 1
		else:
			neg += 1
	if pos == 0 or neg == 0:
		a.sort()
		ans[_] = a[n - 1]
	else:
		i = 0
		while i < n:
			a1 = []
			a2 = []
			while i < n and a[i] > 0:
				a1.append(a[i])
				i += 1
			while i < n and a[i] < 0:
				a2.append(a[i])
				i += 1
			if len(a1) > 0:
				a1.sort(reverse=True)
				ans[_] += a1[0]
			if len(a2) > 0:
				a2.sort(reverse=True)
				ans[_] += a2[0]
for i in range(0, len(ans)):
	print(ans[i])


import sys
ip = lambda : sys.stdin.readline().rstrip()
for _ in range(int(ip())):
	n = int(ip())
	a = list(map(int, ip().split()))
	n = len(a)
	ans = 0
	(psign, pvalue) = (-1 if a[0] < 0 else 1, a[0])
	for i in range(1, n):
		sign = -1 if a[i] < 0 else 1
		if sign == psign:
			pvalue = max(pvalue, a[i])
		else:
			ans += pvalue
			pvalue = a[i]
			psign = sign
	ans += pvalue
	print(ans)


def solve(d):
	sol = []
	for a in d:
		if not sol:
			sol.append(a)
		else:
			last = sol[-1]
			if last > 0 and a > 0 and (a > last):
				sol[-1] = a
			elif last > 0 and a < 0:
				sol.append(a)
			elif last < 0 and a < 0 and (a > last):
				sol[-1] = a
			elif last < 0 and a > 0:
				sol.append(a)
	return sum(sol)
test_cases = int(input())
for i in range(test_cases):
	input()
	d = [int(a) for a in input().split(' ')]
	print(str(solve(d)))


from sys import stdin, gettrace
MIN = int(-10000000000.0)
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		n = int(input())
		aa = [int(a) for a in input().split()]
		sum = 0
		mxp = 0
		mxn = MIN
		for a in aa:
			if a > 0:
				mxp = max(mxp, a)
				if mxn != MIN:
					sum += mxn
				mxn = MIN
			else:
				mxn = max(mxn, a)
				sum += mxp
				mxp = 0
		sum += mxp + (0 if mxn == MIN else mxn)
		print(sum)
	q = int(input())
	for _ in range(q):
		solve()
main()


a = int(input())
for i in range(0, a):
	b = input()
	arr = list(map(int, input().split()))
	count = 0
	sign = arr[0]
	maxx = -float('inf')
	minn = maxx
	summ = 0
	for i in range(0, len(arr)):
		if arr[i] > 0 and sign < 0:
			count = count + 1
			sign = 1
			summ = summ + minn
			maxx = arr[i]
			minn = float('inf')
		if arr[i] < 0 and sign > 0:
			count = count + 1
			sign = -1
			summ = summ + maxx
			minn = arr[i]
			maxx = float('inf')
		if arr[i] > 0 and sign > 0:
			if arr[i] > maxx:
				maxx = arr[i]
			if i == len(arr) - 1:
				summ = summ + maxx
		if arr[i] < 0 and sign < 0:
			if arr[i] > minn:
				minn = arr[i]
			if i == len(arr) - 1:
				summ = summ + minn
	print(summ)


import math
c = int(input())
for _ in range(c):
	n = int(input())
	str = input().split()
	arr = []
	if int(str[0]) > 0:
		now = 1
	else:
		now = 0
	max_now = int(str[0])
	sum = 0
	for i in range(1, n):
		if int(str[i]) > 0:
			tmp = 1
		else:
			tmp = 0
		if tmp != now:
			sum += max_now
			now = tmp
			max_now = -math.inf
		if int(str[i]) > max_now:
			max_now = int(str[i])
	print(sum + max_now)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	sum1 = 0
	i = 0
	while i < n:
		temp = []
		j = i
		if l[i] < 0:
			while j < n and l[j] < 0:
				temp.append(l[j])
				j += 1
		if l[i] > 0:
			while j < n and l[j] > 0:
				temp.append(l[j])
				j += 1
		ans.append(temp)
		i = j
	for i in ans:
		sum1 += max(i)
	print(sum1)


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd, deque

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

def mdata():
	return map(int, data().split())
out = sys.stdout.write
for i in range(int(data())):
	n = int(data())
	A = list(mdata())
	d1 = 0
	s1 = 0
	d2 = 0
	s2 = 0
	ind = 0
	for i in range(n):
		if A[i] > 0:
			ind = i
			break
	s2 += A[ind]
	t = A[ind]
	d2 += 1
	for i in range(ind + 1, n):
		if A[i] > 0 and A[i - 1] > 0 or (A[i] < 0 and A[i - 1] < 0):
			s2 -= t
			t = max(t, A[i])
			s2 += t
		else:
			d2 += 1
			s2 += A[i]
			t = A[i]
	ind = 0
	for i in range(n):
		if A[i] < 0:
			ind = i
			break
	s1 += A[ind]
	t = A[ind]
	d1 += 1
	for i in range(ind + 1, n):
		if A[i] > 0 and A[i - 1] > 0 or (A[i] < 0 and A[i - 1] < 0):
			s1 -= t
			t = max(t, A[i])
			s1 += t
		else:
			d1 += 1
			s1 += A[i]
			t = A[i]
	if d1 == d2:
		print(max(s1, s2))
	elif d1 > d2:
		print(s1)
	else:
		print(s2)


for a0 in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	z = []
	z.append([l[0]])
	current = l[0] > 0
	for i in range(1, n):
		if l[i] > 0 and current:
			z[-1].append(l[i])
		elif l[i] < 0 and (not current):
			z[-1].append(l[i])
		else:
			z.append([l[i]])
			if current == True:
				current = False
			else:
				current = True
	sum = 0
	for i in z:
		sum += max(i)
	print(sum)


for _ in range(int(input())):
	input()
	a = list(map(int, input().split()))
	(x, r) = (a[0], 0)
	for i in a[1:]:
		if i * x < 0:
			r += x
			x = i
		if i < 0 and i > x:
			x = i
		if i > 0 and i > x:
			x = i
	print(r + x)


def solve(n, seq):
	nextState = None
	tempSum = 0
	maxSum = 0
	for x in seq:
		if nextState == None:
			if x < 0:
				nextState = 'P'
			else:
				nextState = 'N'
			tempSum = x
		elif x > 0:
			if nextState == 'P':
				maxSum += tempSum
				tempSum = x
				nextState = 'N'
			elif x > tempSum:
				tempSum = x
		elif x < 0:
			if nextState == 'N':
				maxSum += tempSum
				tempSum = x
				nextState = 'P'
			elif x > tempSum:
				tempSum = x
	maxSum += tempSum
	return maxSum
n = int(input())
for x in range(n):
	m = int(input())
	seq2 = list(map(int, input().split()))
	print(solve(m, seq2))


def max_num(mas):
	res = 0
	if len(mas) == 0:
		return 0
	tmp = mas[0]
	for i in range(len(mas)):
		if tmp > 0 and mas[i] < 0:
			res += tmp
			tmp = mas[i]
		elif tmp < 0 and mas[i] > 0:
			res += tmp
			tmp = mas[i]
		elif mas[i] > tmp:
			tmp = mas[i]
	res += tmp
	return res
t = int(input())
for i in range(t):
	n = int(input())
	print(max_num(list(map(int, input().split()))))


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ka = 0
	m = -1000000007
	i = 0
	while i < n:
		m = l[i]
		if l[i] < 0:
			while i < n and l[i] < 0:
				if l[i] > m:
					m = l[i]
				i = i + 1
			ka = ka + m
			m = -1000000007
		else:
			while i < n and l[i] > 0:
				if l[i] > m:
					m = l[i]
				i = i + 1
			ka = ka + m
			m = -1000000007
	print(ka)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	i = 0
	j = 0
	sum = 0
	while i < n:
		while i < n and j < n and (lst[i] * lst[j] > 0):
			j += 1
		sum += max(lst[i:j])
		i = j
	print(sum)


from math import *
test = int(input())
for test_case in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		f = 1
	else:
		f = 0
	i = 0
	ans = 0
	while i < n:
		m = a[i]
		if f == 1:
			while i < n and a[i] > 0:
				m = max(m, a[i])
				i += 1
		else:
			while i < n and a[i] < 0:
				m = max(m, a[i])
				i += 1
		f = f ^ 1
		ans += m
	print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	values = list(map(int, input().split()))
	value = 0
	i = 0
	while i < n - 1:
		j = i
		ans = values[i]
		while values[i] // abs(values[i]) * (values[j] // abs(values[j])) > 0:
			ans = max(ans, values[j])
			j += 1
			if j >= n:
				break
		value += ans
		i = j
	if n == 1:
		print(values[0])
	elif values[n - 1] * values[n - 2] < 0:
		value += values[n - 1]
		print(value)
	else:
		print(value)


t = int(input())
for i in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	sm = 0
	i = 0
	while i < n:
		if li[i] < 0:
			l = []
			l.append(li[i])
			if i == n - 1:
				sm += max(l)
			for j in range(i + 1, n):
				if li[j] > 0:
					sm += max(l)
					i = j - 1
					break
				else:
					l.append(li[j])
					if j == n - 1:
						sm += max(l)
						i = j
						break
		else:
			l1 = []
			l1.append(li[i])
			if i == n - 1:
				sm += max(l1)
			for j in range(i + 1, n):
				if li[j] < 0:
					sm += max(l1)
					i = j - 1
					break
				else:
					l1.append(li[j])
					if j == n - 1:
						sm += max(l1)
						i = j
						break
		i += 1
	print(sm)


from sys import stdin

def help():
	stri = stdin.readline().strip('\n')
	n = int(stri)
	arr = list(map(int, stdin.readline().split(' ')))
	if arr[0] < 0:
		sign = 0
	else:
		sign = 1
	sumi = 0
	currmax = arr[0]
	for i in range(1, n):
		if sign == 0 and arr[i] < 0:
			currmax = max(arr[i], currmax)
		elif sign == 1 and arr[i] > 0:
			currmax = max(arr[i], currmax)
		else:
			sumi += currmax
			if arr[i] < 0:
				sign = 0
			else:
				sign = 1
			currmax = arr[i]
	sumi += currmax
	print(sumi)
for i in range(int(stdin.readline())):
	help()


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	l = a[0]
	su = 0
	for i in range(n - 1):
		if a[i] * a[i + 1] < 0:
			su += l
			l = a[i + 1]
		elif a[i + 1] > l:
			l = a[i + 1]
	print(su + l)


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


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	sum1 = 0
	i = 0
	while i < n:
		curr = arr[i]
		j = i + 1
		while j < n:
			if arr[i] > 0 and arr[j] > 0 or (arr[i] < 0 and arr[j] < 0):
				curr = max(curr, arr[j])
				j += 1
			else:
				break
		i = j - 1
		sum1 += curr
		i += 1
	print(sum1)


cases = int(input().strip())
for case in range(cases):
	n = int(input().strip())
	accepted_numbers = []
	numbers = [int(x) for x in input().strip().split(' ')]
	same_sign = []
	for number in numbers:
		if len(same_sign) == 0:
			same_sign += [number]
		elif same_sign[0] * number < 0:
			accepted_numbers += [max(same_sign)]
			same_sign = [number]
		else:
			same_sign += [number]
	accepted_numbers += [max(same_sign)]
	print(sum(accepted_numbers))


from collections import deque

def solve():
	n = int(input())
	li = [int(x) for x in input().split()]
	(count_p, count_n, bool_p, bool_n) = (0, 0, True, True)
	(note_p, note_n) = (0, 0)
	final = deque()
	for i in range(n):
		if li[i] > 0:
			if bool_p == True:
				if note_n != 0:
					final.append(note_n)
				if i == n - 1:
					final.append(li[i])
				note_p = li[i]
				count_p += 1
				bool_p = False
				bool_n = True
			else:
				if li[i] > note_p:
					note_p = li[i]
				if i == n - 1:
					final.append(note_p)
		elif li[i] < 0:
			if bool_n == True:
				if note_p != 0:
					final.append(note_p)
				if i == n - 1:
					final.append(li[i])
				note_n = li[i]
				count_n += 1
				bool_n = False
				bool_p = True
			else:
				if li[i] > note_n:
					note_n = li[i]
				if i == n - 1:
					final.append(note_n)
	print(sum(final))
t = int(input())
for i in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	current = a[0]
	for i in a:
		if current > 0 and i > 0 and (i > current):
			current = i
		elif current > 0 and i < 0:
			l.append(current)
			current = i
		elif current < 0 and i < 0 and (i > current):
			current = i
		elif current < 0 and i > 0:
			l.append(current)
			current = i
	l.append(current)
	print(sum(l))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(pos, neg) = ([], [])
	if a[0] > 0:
		prev = 1
	else:
		prev = 0
	ar = [a[0]]
	for i in range(1, len(a)):
		if prev == 0:
			if a[i] > 0:
				ar.append(a[i])
				prev = 1
			elif a[i] > ar[-1]:
				ar.pop()
				ar.append(a[i])
		elif a[i] < 0:
			ar.append(a[i])
			prev = 0
		elif a[i] > ar[-1]:
			ar.pop()
			ar.append(a[i])
	print(sum(ar))


import math
for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	l = []
	if lis[0] < 0:
		f = 0
	else:
		f = 1
	x = []
	for i in range(n):
		if f == 0 and lis[i] < 0:
			x.append(lis[i])
		elif f == 1 and lis[i] > 0:
			x.append(lis[i])
		elif f == 0 and lis[i] > 0:
			l.append(x)
			x = [lis[i]]
			f = 1
		else:
			l.append(x)
			x = [lis[i]]
			f = 0
	l.append(x)
	s = 0
	for i in range(len(l)):
		s += max(l[i])
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	boo = True if a[0] > 0 else False
	a += [0]
	ans = 0
	c = -100000000000
	for i in a:
		if boo and i <= 0 or (not boo and i >= 0):
			ans += c
			c = i
			boo ^= True
		else:
			c = max(c, i)
	print(ans)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(maxi, s) = (a[0], 0)
	for i in range(len(a)):
		if a[i] * maxi < 0:
			s += maxi
			maxi = a[i]
		else:
			maxi = max(maxi, a[i])
	print(maxi + s)


def solve(n, tab):
	out = 0
	pos = 1 if tab[0] > 0 else 0
	prev = 0
	for k in tab:
		if k < 0 and pos == 1:
			out += prev
			pos = 0
			prev = k
		elif k > 0 and pos == 0:
			out += prev
			pos = 1
			prev = k
		elif pos == 1:
			prev = max(prev, k)
		else:
			prev = max(prev, k) if prev != 0 else k
	out += prev
	return out
T = int(input())
for line in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	y = solve(n, a)
	print(y)


T = int(input())
for xx in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	l = [a[0]]
	sim = 0
	if a[0] > 0:
		flag = 0
	else:
		flag = 1
	for i in range(1, n):
		if a[i] > 0:
			if flag == 1:
				sim += max(l)
				l = []
				flag = 0
			l.append(a[i])
		elif a[i] < 0:
			if flag == 0:
				sim += max(l)
				l = []
				flag = 1
			l.append(a[i])
	sim += max(l)
	print(sim)


t = int(input())
ans = []
for s in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	if n == 1:
		ans.append(arr[0])
	else:
		sump = 0
		start = arr[0]
		k = 0
		i = 1
		while i < n:
			if arr[i] * arr[k] < 0:
				sump += max(arr[k:i])
				k = i
			elif i == n - 1:
				sump += max(arr[k:])
			if k == n - 1:
				sump += arr[k]
			i += 1
		ans.append(sump)
for it in ans:
	print(it)


for t in range(int(input())):
	n = int(input())
	l = [int(j) for j in input().split()]
	mx = -1 * 100000000000000.0
	mn = -1 * 100000000000000.0
	ans = []
	for i in range(n):
		if l[i] > 0:
			if mn != -1 * 100000000000000.0:
				ans.append(mn)
				mn = -1 * 100000000000000.0
			mx = max(mx, l[i])
		else:
			if mx != -1 * 100000000000000.0:
				ans.append(mx)
				mx = -1 * 100000000000000.0
			mn = max(mn, l[i])
	if l[n - 1] > 0:
		ans.append(mx)
	elif l[n - 1] < 0:
		ans.append(mn)
	print(sum(ans))


def hello():
	n = int(input())
	pos = list(map(int, input().split()))
	mas_s = 1
	maxim = pos[0]
	v = []
	s = 0
	for i in range(1, n):
		if pos[i] * pos[i - 1] < 0:
			s += maxim
			maxim = pos[i]
		else:
			maxim = max(maxim, pos[i])
	s += maxim
	print(s)
	for i in range(n):
		pass
a = int(input())
for i in range(a):
	hello()


import itertools
t = int(input())
cnt = 0
for cnt in range(t):
	n = int(input())
	a = list()
	pos_cnt = 0
	neg_cnt = 0
	counter = 0
	a = list(map(int, input().split()))
	pos = list()
	neg = list()
	final = 0
	max_pos = 0
	min_neg = 0
	if a[0] > 0:
		counter = 1
		max_pos = a[0]
	else:
		counter = -1
		min_neg = a[0]
	for i in a:
		if i > 0:
			if counter == 1:
				if max_pos <= i:
					max_pos = i
			else:
				final += min_neg
				counter = 1
				max_pos = i
		elif counter == -1:
			if min_neg <= i:
				min_neg = i
		else:
			final += max_pos
			counter = -1
			min_neg = i
	if counter == 1:
		final += max_pos
	else:
		final += min_neg
	print(final)
	pos.clear()
	neg.clear()


def sign(k):
	return 1 if k > 0 else -1
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	last_index = 0
	for i in range(1, n):
		if a[i] * a[i - 1] < 0:
			ans += max(a[last_index:i])
			last_index = i
	print(ans + max(a[last_index:n]))


for __ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	curr_max = a[0]
	curr_sign = a[0] > 0
	for i in range(1, len(a)):
		sign = a[i] > 0
		if sign != curr_sign:
			ans += curr_max
			curr_max = a[i]
			curr_sign = sign
		else:
			curr_max = max(curr_max, a[i])
	ans += curr_max
	print(ans)


cases = int(input())
for _ in range(cases):
	n = int(input())
	c = list(map(int, input().split()))
	dp = []
	pos = []
	neg = []
	flag = 0
	if c[0] > 0:
		flag = 0
		pos.append(c[0])
	else:
		flag = 1
		neg.append(c[0])
	for i in range(1, len(c)):
		if flag == 0:
			if c[i] > 0:
				pos.append(c[i])
			else:
				dp.append(pos)
				pos = []
				neg.append(c[i])
				flag = 1
		elif c[i] < 0:
			neg.append(c[i])
		else:
			dp.append(neg)
			neg = []
			pos.append(c[i])
			flag = 0
	dp.append(pos)
	dp.append(neg)
	tot = 0
	cnt = 0
	for i in range(len(dp)):
		if len(dp[i]) > 0:
			cnt += 1
			tot += max(dp[i])
	print(tot)


T = int(input())
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	lf = []
	j = 0
	su = 0
	while j < n:
		c = 0
		if l[j] < 0:
			s = -10000000000
			k = j
			while k < n and l[k] < 0:
				if l[k] > s:
					s = l[k]
				k = k + 1
				c = c + 1
			j = j + c
		else:
			s = -10000000000
			k = j
			while k < n and l[k] >= 0:
				if l[k] > s:
					s = l[k]
				k = k + 1
				c = c + 1
			j = j + c
		su = su + s
	print(su)


def main():
	t = int(input())
	for _ in range(1, t + 1):
		n = int(input())
		a = [int(i) for i in input().split(' ')]
		sol = 0
		i = 0
		while i < n:
			if a[i] > 0:
				max_i = 0
				while i < n and a[i] > 0:
					max_i = max(a[i], max_i)
					i += 1
			else:
				max_i = -10 ** 9 - 1
				while i < n and a[i] < 0:
					max_i = max(a[i], max_i)
					i += 1
			sol += max_i
		print(sol)
main()


def solve(n, a):
	result = []
	maxnr = a[0]
	for i in range(1, n):
		if a[i] > 0 and maxnr < 0:
			result.append(maxnr)
			maxnr = a[i]
		elif a[i] < 0 and maxnr > 0:
			result.append(maxnr)
			maxnr = a[i]
		elif a[i] > maxnr:
			maxnr = a[i]
	result.append(maxnr)
	print(sum(result))
t = int(input().strip())
a = []
for _ in range(t):
	n = int(input().strip())
	a = list(map(int, input().split()))
	solve(n, a)


t = int(input(''))
arr = []
for m in range(t):
	int(input(''))
	arr.append(list(map(int, input().split())))
for i in arr:
	n = len(i)
	sol = 0
	j = 0
	while j < n:
		mn = 10000000000000000
		mx = -mn
		k = j
		while k < n and (i[j] > 0) == (i[k] > 0):
			mx = max(mx, i[k])
			k += 1
		sol += mx
		j = k
	print(sol)


from math import *
from collections import *
from itertools import *
from bisect import *
from heapq import *
from operator import *

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

def op():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	l = op()
	fg = 0
	t = 0
	ans = []
	for i in range(len(l)):
		if l[i] >= 0:
			if fg == 0 and i != 0:
				ans.append(t)
			fg = 1
			t = max(t, l[i])
		if i == 0 and l[i] < 0 or (fg == 1 and l[i] < 0):
			if i != 0:
				ans.append(t)
			fg = 0
			t = l[i]
		if l[i] < 0:
			t = max(t, l[i])
	ans.append(t)
	print(sum(ans))


t = int(input())
count = 0
while count < t:
	count += 1
	n = int(input())
	s = input()
	s = s.split(' ')
	m = int(s[0])
	prev = int(s[0])
	Sum = 0
	for i in s[1:]:
		if 1.0 * int(i) / prev < 0:
			Sum += m
			prev = int(i)
			m = int(i)
		else:
			m = max(m, int(i))
			prev = int(i)
	Sum += m
	print(Sum)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	bestPos = (0, float('-inf'))
	bestNeg = (0, float('-inf'))
	for x in A:
		if x < 0:
			bestNeg = max(bestNeg, (bestPos[0] + 1, bestPos[1] + x))
			bestNeg = max(bestNeg, (1, x))
		else:
			assert x > 0
			bestPos = max(bestPos, (bestNeg[0] + 1, bestNeg[1] + x))
			bestPos = max(bestPos, (1, x))
	return max(bestPos, bestNeg)[1]
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


n = int(input())
for i in range(0, n):
	a = int(input())
	l = list(map(int, input().split()))
	i = 0
	j = 0
	c = 0
	while j < a and i < a:
		if i < a and j < a and (l[i] > 0):
			while j < a and l[j] > 0:
				j = j + 1
			d = max(l[i:j])
			c = c + d
			i = j
		elif l[i] < 0:
			while j < len(l) and i < len(l) and (l[j] < 0):
				j = j + 1
			d = max(l[i:j])
			c = c + d
			i = j
	print(c)


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

def sign(n):
	return -1 if n < 0 else 1

def solve(n, a):
	total = 0
	m = a[0]
	s = sign(a[0])
	for i in range(1, n):
		si = sign(a[i])
		if si == s:
			m = max(a[i], m)
		else:
			total += m
			s = si
			m = a[i]
	total += m
	return total
for i in range(int(input())):
	result = solve(*read())
	print(result)


import math
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = []
	mm = arr[0]
	if mm > 0:
		k = 1
	else:
		k = -1
	for i in range(1, len(arr)):
		if k == 1:
			if arr[i] > 0:
				mm = max(mm, arr[i])
			else:
				ans.append(mm)
				k *= -1
				mm = arr[i]
		elif k == -1:
			if arr[i] < 0:
				mm = max(mm, arr[i])
			else:
				ans.append(mm)
				k *= -1
				mm = arr[i]
	ans.append(mm)
	print(sum(ans))


for _ in range(int(input())):
	n = int(input())
	nl = list(map(int, input().split()))
	mnl = [nl[0]]
	for i in range(1, n):
		if (mnl[-1] < 0) == (nl[i] < 0):
			mnl[-1] = max(mnl[-1], nl[i])
		else:
			mnl.append(nl[i])
	print(sum(mnl))


test = int(input())
while test != 0:
	n = int(input())
	arr = [int(x) for x in input().split()]
	segment = []
	mark = [1] * n
	for i in range(0, n):
		if arr[i] < 0:
			mark[i] = -1
	i = 0
	while i < n:
		x = []
		x.append(arr[i])
		y = mark[i]
		i += 1
		while i < n and mark[i] == y:
			x.append(arr[i])
			i += 1
		segment.append(x)
	maxx = -float('inf')
	curr = 0
	for i in range(len(segment)):
		curr += max(segment[i])
	print(curr)
	test -= 1


n_cases = int(input())
for case_num in range(n_cases):
	n = int(input())
	arr = [int(x) for x in input().split()]
	run_positive = arr[0] > 0
	ans = 0
	run_best = arr[0]
	for x in arr:
		nparity = x > 0
		if run_positive != nparity:
			run_positive = nparity
			ans += run_best
			run_best = x
		elif x > run_best:
			run_best = x
	ans += run_best
	print(ans)


from sys import stdin, stdout
for testcases in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	total = 0
	local = arr[0]
	for i in range(1, n):
		if arr[i] * arr[i - 1] > 0:
			local = max(arr[i], local)
		else:
			total += local
			local = arr[i]
	if arr[n - 1] * arr[n - 2] > 0:
		total += local
	else:
		total += arr[n - 1]
	stdout.write(str(total) + '\n')


from __future__ import division, print_function
import os
import sys
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

def main():
	t = int(input())
	for _ in range(t):
		solve()

def solve():
	_ = inp()
	seq = inlt()
	cur = seq[0]
	cur_sum = 0
	for i in range(1, len(seq)):
		nxt = seq[i]
		if alternate(cur, nxt):
			cur_sum += cur
			cur = nxt
		elif cur < nxt:
			cur = nxt
	cur_sum += cur
	print(cur_sum)

def alternate(cur, nxt):
	return (cur > 0) ^ (nxt > 0)
BUFSIZE = 8192

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())

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()


t = int(input())
for _ in range(t):
	n = int(input())
	pnt = 0
	A = list(map(int, input().split()))
	sign = 0
	X = []
	X.append(A[0])
	if A[0] > 0:
		sign = 1
	else:
		sign = -1
	for i in range(1, n):
		if A[i] * X[pnt] > 0:
			if X[pnt] < A[i]:
				X[pnt] = A[i]
		else:
			X.append(A[i])
			pnt += 1
	print(sum(X))


import sys
import collections, itertools, bisect, heapq, math, string

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

def main():
	t = int(input())
	num = []
	for i in range(t):
		n = int(input())
		temp = list(map(int, input().split()))
		num.append(temp)
	index = 0
	while index < t:
		mx = 0
		a = num[index]
		temp = [a[0]]
		for i in range(1, len(a)):
			if a[i - 1] > 0 and a[i] < 0 or (a[i - 1] < 0 and a[i] > 0):
				temp.append(a[i])
			elif temp[-1] < a[i]:
				temp[-1] = a[i]
		print(sum(temp))
		index += 1
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	prev = a[0]
	current_max = a[0]
	for i in range(1, n):
		if a[i] * prev > 0:
			current_max = max(current_max, a[i])
		else:
			ans += current_max
			current_max = a[i]
		prev = a[i]
	ans += current_max
	print(ans)


def sign(l):
	if l < 0:
		return -1
	else:
		return 1

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	(ans, i) = (0, 0)
	while i < n:
		cur = arr[i]
		j = i
		while j < n and sign(arr[i]) == sign(arr[j]):
			cur = max(cur, arr[j])
			j += 1
		ans += cur
		i = j
	print(ans)
for _ in range(int(input())):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr[0] < 0:
		flag = -1
	else:
		flag = 1
	brr = []
	i = 0
	while i < n:
		check = []
		if flag == -1:
			while i < n and arr[i] < 0:
				check.append(arr[i])
				i += 1
			brr.append(max(check))
			flag = 1
		else:
			while i < n and arr[i] >= 0:
				check.append(arr[i])
				i += 1
			brr.append(max(check))
			flag = -1
	print(sum(brr))


import sys
import math
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pos = True
	if a[0] < 0:
		pos = False
	i = 0
	mVal = int(-1000000000.0)
	ar = 0
	while i < n:
		if pos:
			if a[i] > 0:
				mVal = max(mVal, a[i])
			else:
				ar += mVal
				mVal = a[i]
				pos = False
		elif a[i] < 0:
			mVal = max(mVal, a[i])
		else:
			ar += mVal
			mVal = a[i]
			pos = True
		i += 1
	ar += mVal
	print(ar)


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	(c, curmax) = (0, ar[0])
	for i in range(n):
		if curmax > 0 and ar[i] < 0 or (curmax < 0 and ar[i] > 0):
			c += curmax
			curmax = ar[i]
		else:
			curmax = max(curmax, ar[i])
	print(c + curmax)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = []
	c = 0
	d = 0
	for j in range(len(l)):
		if l[j] > 0:
			d = 0
			if c > 0:
				if x[-1] <= l[j]:
					x[-1] = l[j]
			else:
				x.append(l[j])
			c = c + 1
		else:
			c = 0
			if d > 0:
				if x[-1] <= l[j]:
					x[-1] = l[j]
			else:
				x.append(l[j])
			d = d + 1
	print(sum(x))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] < 0:
		check = False
	else:
		check = True
	ans = 0
	maxx = a[0]
	for i in a:
		if not check and i > 0:
			ans += maxx
			check = True
			maxx = i
		elif not check:
			maxx = max(maxx, i)
		elif check and i < 0:
			ans += maxx
			check = False
			maxx = i
		else:
			maxx = max(maxx, i)
	ans += maxx
	print(ans)


cases = int(input())
s = True

def sign(n):
	if n > 0:
		return True
	else:
		return False
for p in range(cases):
	le = int(input())
	case = [int(i) for i in input().split()]
	arr = []
	tot = 0
	for f in range(le - 1):
		if sign(case[f]) == sign(case[f + 1]):
			arr.append(case[f])
		else:
			arr.append(case[f])
			tot += max(arr)
			arr = []
	arr.append(case[le - 1])
	tot += max(arr)
	print(tot)


from sys import stdin
from collections import Counter, deque
input = stdin.buffer.readline
T = int(input())
for _ in range(T):
	n = int(input())
	ls = list(map(int, input().split()))
	(sm, tl) = (0, [])
	for (i, u) in enumerate(ls):
		tl.append(u)
		if i == n - 1 or u * ls[i + 1] < 0:
			sm += max(tl)
			tl.clear()
	print(sm)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	tmp = a[0]
	ma = a[0]
	start = 1
	res = a[0]
	for i in a[1:]:
		if ma * i < 0:
			res += i
			ma = i
		else:
			res -= ma
			ma = max(ma, i)
			res += ma
	print(res)


def sgn(x):
	if x > 0:
		return True
	else:
		return False

def solve():
	_ = int(input())
	data = [int(x) for x in input().split(' ')]
	s = 0
	(i, n) = (0, len(data))
	while i < n:
		cur = data[i]
		j = i
		while j < n and sgn(data[i]) == sgn(data[j]):
			cur = max(cur, data[j])
			j += 1
		s += cur
		i = j
	print(s)
nums = int(input())
for _ in range(1, nums + 1):
	solve()


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):
	if a[0] > 0:
		sign = 1
	else:
		sign = -1
	sol = []
	choices = [a[0]]
	for elem in a[1:]:
		if elem / sign < 0:
			sol.append(max(choices))
			choices = [elem]
			sign = -1 if sign == 1 else 1
		else:
			choices.append(elem)
	sol.append(max(choices))
	return sum(sol)
t = read_int()
for case in range(t):
	n = read_int()
	a = read_ints()
	sol = solve(n, a)
	print(sol)


t = int(input())
for i in range(t):
	n = int(input())
	num1 = list(map(int, input().split()))
	num2 = [num1[0]]
	a = max(num1)
	c = min(num1)
	b = 0
	if a < 0:
		print(a)
		b += 1
	if c > 0:
		print(a)
		b += 1
	for i in range(1, n):
		if num1[i] < 0 and num2[-1] > 0:
			num2.append(num1[i])
		if num1[i] > 0 and num2[-1] < 0:
			num2.append(num1[i])
		if num2[-1] > 0 and num1[i] > 0 and (num1[i] > num2[-1]):
			num2.pop()
			num2.append(num1[i])
		if num2[-1] < 0 and num1[i] < 0 and (num1[i] > num2[-1]):
			num2.pop()
			num2.append(num1[i])
		else:
			continue
	if b == 0:
		print(sum(num2))


t = int(input())
while t > 0:
	n = int(input())
	arr = [int(i) for i in input().split()]
	l = 0
	r = 0
	r = 1
	if arr[l] > 0:
		flag = 1
	else:
		flag = 0
	out = []
	while r < n:
		if arr[r] > 0 and flag == 1:
			r += 1
			continue
		elif arr[r] < 0 and flag == 0:
			r += 1
			continue
		elif flag == 1:
			out.append(max(arr[l:r]))
			l = r
			flag = 0
		elif flag == 0:
			out.append(max(arr[l:r]))
			l = r
			flag = 1
		r += 1
	if flag == 1:
		out.append(max(arr[l:r]))
		l = r
		flag = 0
	else:
		out.append(max(arr[l:r]))
		l = r
		flag = 1
	print(sum(out))
	t -= 1


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

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

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	n = int(input())
	a = list(mapi())
	sm = 0
	i = 0
	while i < n:
		tmp = a[i]
		sign = -1 if a[i] < 0 else 1
		while i < n and sign * a[i] > 0:
			if a[i] > tmp:
				tmp = a[i]
			i += 1
		sm += tmp
		sign = -sign
		if i >= n:
			break
	print(sm)


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve(n, arr):
	tot = 0
	if arr[0] > 0:
		fg = 0
		mx = arr[0]
	else:
		fg = 1
		mi = arr[0]
	for i in range(1, n):
		if fg == 1:
			if arr[i] > 0:
				fg = 0
				tot += mi
				mx = arr[i]
			elif arr[i] > mi:
				mi = arr[i]
		elif arr[i] < 0:
			fg = 1
			tot += mx
			mi = arr[i]
		elif arr[i] > mx:
			mx = arr[i]
	if arr[-1] < 0:
		tot += mi
	else:
		tot += mx
	print(tot)
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	solve(n, arr)


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	pos = []
	nev = []
	ans = []
	for i in a:
		if i > 0:
			if nev != []:
				ans.append(max(nev))
				nev = []
			pos.append(i)
		elif i < 0:
			if pos != []:
				ans.append(max(pos))
				pos = []
			nev.append(i)
	if nev != []:
		ans.append(max(nev))
		nev = []
	if pos != []:
		ans.append(max(pos))
		pos = []
	print(sum(ans))


from collections import deque
t = int(input())
while t != 0:
	n = int(input())
	l = list(map(int, input().strip().split()))
	d = deque()
	d.append(0)
	for i in l:
		if d[len(d) - 1] == 0:
			d.append(i)
		elif d[len(d) - 1] < 0:
			if i < 0:
				x = d[len(d) - 1]
				d.pop()
				d.append(max(x, i))
			else:
				d.append(i)
		elif i > 0:
			x = d[len(d) - 1]
			d.pop()
			d.append(max(i, x))
		else:
			d.append(i)
	print(sum(d))
	t -= 1


def solution():
	n = int(input())
	for _ in range(n):
		m = int(input())
		nums = list(map(int, input().split()))
		current_max = nums[0]
		i = 1
		negative = False
		if current_max < 0:
			negative = True
		result_sum = 0
		while i < m:
			if negative:
				if nums[i] > 0:
					result_sum += current_max
					current_max = nums[i]
					negative = False
				elif nums[i] > current_max:
					current_max = nums[i]
			elif nums[i] < 0:
				result_sum += current_max
				current_max = nums[i]
				negative = True
			elif nums[i] > current_max:
				current_max = nums[i]
			i += 1
		result_sum += current_max
		print(result_sum)
solution()


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	ls = []
	temp = [a[0]]
	for i in range(1, n):
		if a[i - 1] > 0 and a[i] > 0:
			temp.append(a[i])
		elif a[i - 1] < 0 and a[i] < 0:
			temp.append(a[i])
		else:
			ls.append(temp)
			temp = [a[i]]
	if len(temp):
		ls.append(temp)
	for i in range(len(ls)):
		ls[i] = max(ls[i])
	print(sum(ls))
	t -= 1


import math
T = int(input())
for _ in range(T):
	n = int(input())
	a = [int(x) for x in input().split()]
	runningsign = 0
	summaximisingnum = 0
	maxsum = 0
	sumUpdateleft = False

	def getSign(x):
		return int(x / abs(x))
	for i in range(len(a)):
		if getSign(a[i]) != runningsign:
			runningsign = getSign(a[i])
			maxsum += summaximisingnum
			summaximisingnum = a[i]
		else:
			summaximisingnum = max(summaximisingnum, a[i])
	maxsum += summaximisingnum
	print(maxsum)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
from collections import *
ORDA = 97

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

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

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

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

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def function(arr, size, dp):
	if size == 1 or size == 0:
		return 1
	key = ''.join(map(str, arr[:size]))
	if dp[key]:
		return dp[key]
	output = function(arr, size - 1, dp)
	key = ''.join(map(str, arr[:size - 1]))
	dp[key] = output
	if arr[size - 2] * 10 + arr[size - 1] <= 26:
		output += function(arr, size - 2, dp)
		key = ''.join(map(str, arr[:size - 2]))
		dp[key] = output
	return output

def main():
	for _ in range(ii()):
		n = ii()
		arr = li()
		i = 0
		s = 0
		while i < n:
			if arr[i] > 0:
				j = i + 1
				while j < n:
					if arr[j] < 0:
						break
					j += 1
				s += max(arr[i:j])
			else:
				j = i + 1
				while j < n:
					if arr[j] > 0:
						break
					j += 1
				s += max(arr[i:j])
			i = j
		print(s)
main()


import sys, math, cmath, time
start_time = time.time()

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

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

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

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

def solve():
	n = inp()
	a = inlt()
	i = 0
	qw = 0
	qq = 0
	ww = 0
	while i < len(a):
		q = -9999999999999999
		if qw == 0:
			while i < n and a[i] > 0:
				q = max(a[i], q)
				i = i + 1
			qw = 1
			qq = qq + q
		else:
			q = -9999999999999999
			while i < n and a[i] < 0:
				q = max(a[i], q)
				i = i + 1
			qw = 0
			qq = qq + q
	i = 0
	q = 1
	qw = 1
	while i < len(a):
		q = -9999999999999999
		if qw == 0:
			while i < n and a[i] > 0:
				q = max(a[i], q)
				i = i + 1
			qw = 1
			ww = ww + q
		else:
			q = -9999999999999999
			while i < n and a[i] < 0:
				q = max(a[i], q)
				i = i + 1
			qw = 0
			ww = ww + q
	print(max(qq, ww))
ONLINE_JUDGE = __debug__
if not ONLINE_JUDGE:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
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()


import sys
input = sys.stdin.readline
tc = int(input())
INF = 1000000005
for tx in range(tc):
	n = int(input())
	a = list(map(int, input().split()))
	ma = -INF
	b = 0
	for i in range(0, len(a)):
		if i and a[i - 1] * a[i] < 0:
			b = b + ma
			ma = -INF
		ma = max(ma, a[i])
	b = b + ma
	print(b)


def dp(arr, n, i, status):
	i = 0
	ans = 0
	while i < n:
		max1 = float('-inf')
		max2 = float('-inf')
		if arr[i] < 0:
			while i < n and arr[i] < 0:
				max1 = max(arr[i], max1)
				i += 1
			while i < n and arr[i] > 0:
				max2 = max(arr[i], max2)
				i += 1
		else:
			while i < n and arr[i] > 0:
				max2 = max(arr[i], max2)
				i += 1
			while i < n and arr[i] < 0:
				max1 = max(arr[i], max1)
				i += 1
		if max1 == float('-inf'):
			max1 = 0
		if max2 == float('-inf'):
			max2 = 0
		ans += max1 + max2
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(dp(arr, n, 0, -1))


tc = int(input())
while tc > 0:
	tc -= 1
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	i = 0
	mx = 0
	while i < n:
		if l[i] < 0:
			cnt = l[i]
			while i < n and l[i] < 0:
				if l[i] > cnt:
					cnt = l[i]
				i += 1
		else:
			cnt = l[i]
			while i < n and l[i] > 0:
				if l[i] > cnt:
					cnt = l[i]
				i += 1
		mx += cnt
	print(mx)


import sys
input = sys.stdin.readline
inf = int(1e+18)
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	mx = [(0, 0), (0, 0)]
	tans = (0, 0)
	for i in range(n):
		pp = arr[i] > 0
		ans = (mx[pp ^ 1][0] + 1, mx[pp ^ 1][1] + arr[i])
		tans = max(tans, ans)
		mx[pp] = max(mx[pp], ans)
	print(tans[1])


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		c = a[i]
		while i < n and a[i] * c > 0:
			c = max(a[i], c)
			i += 1
		s += c
	print(s)


T = int(input())
for t in range(T):
	N = int(input())
	a = list(map(int, input().split()))
	cur = a[0]
	ans = 0
	for i in range(1, N):
		if a[i] > 0 and a[i - 1] < 0 or (a[i] < 0 and a[i - 1] > 0):
			ans += cur
			cur = a[i]
		else:
			cur = max(cur, a[i])
	ans += cur
	print(ans)


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


t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	tmp = -10 ** 9 - 1
	for i in a:
		if i * tmp > 0:
			tmp = max(i, tmp)
		else:
			if tmp != -10 ** 9 - 1:
				ans += tmp
			tmp = i
	ans += tmp
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	total = 0
	i = 0
	if l[0] > 0:
		max = l[0]
		min = -(10 ** 9 + 1)
		while i < n:
			if i < n and l[i] > 0:
				while i < n and l[i] > 0:
					if l[i] > max:
						max = l[i]
					i += 1
				total += max
				max = 0
			if i < n and l[i] < 0:
				while i < n and l[i] < 0:
					if l[i] > min:
						min = l[i]
					i += 1
				total += min
				min = -(10 ** 9 + 1)
		print(total)
	else:
		min = l[0]
		max = 0
		while i < n:
			if i < n and l[i] < 0:
				while i < n and l[i] < 0:
					if l[i] > min:
						min = l[i]
					i += 1
				total += min
				min = -(10 ** 9 + 1)
			if i < n and l[i] > 0:
				while i < n and l[i] > 0:
					if l[i] > max:
						max = l[i]
					i += 1
				total += max
				max = 0
		print(total)


t = int(input())
for i in range(t):
	n = int(input())
	summ = 0
	final = -10 ** 10
	prev = 0
	for elem in map(int, input().split()):
		if prev > 0 and elem < 0 or (prev < 0 and elem > 0):
			summ += final
			prev = 0
			final = -10 ** 10
		if elem > final:
			final = elem
		prev = elem
	print(summ + final)


T = int(input())
for _ in range(T):
	N = input()
	N = int(N)
	a = list(map(int, input().split()))
	ans = 0
	if a[0] < 0:
		flag = False
	else:
		flag = True
	x = 0
	prev = a[0]
	while x < N:
		if flag:
			if a[x] > 0:
				prev = max(prev, a[x])
			else:
				ans = ans + prev
				prev = a[x]
				flag = False
		elif a[x] < 0:
			prev = max(prev, a[x])
		else:
			ans = ans + prev
			prev = a[x]
			flag = True
		x += 1
	print(ans + prev)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	negmx = -1 * 1000000000.0
	posmx = -1 * 1000000000.0
	for i in range(0, n):
		if i != 0:
			if l[i] >= 0 and l[i - 1] < 0:
				ans += negmx
				negmx = -1000000000.0
				posmx = -1000000000.0
			elif l[i] < 0 and l[i - 1] >= 0:
				ans += posmx
				negmx = -1000000000.0
				posmx = -1000000000.0
		if l[i] < 0:
			negmx = max(negmx, l[i])
		else:
			posmx = max(posmx, l[i])
	if l[n - 1] < 0:
		ans += negmx
	else:
		ans += posmx
	print(int(ans))


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		maxVal = [a[0]]
		for j in range(1, n):
			if a[j] * a[j - 1] > 0:
				maxVal.append(max(a[j], maxVal[j - 1]))
			else:
				maxVal.append(a[j])
		ans = 0
		for j in range(n - 1):
			if a[j] * a[j + 1] < 0:
				ans += maxVal[j]
		print(ans + maxVal[-1])
main()


t = int(input())
answer = []
nums = []
for i in range(t):
	line = input()
	line = input()
	ar = line.split(' ')
	stack = [int(ar[0])]
	for el in ar[1:]:
		el = int(el)
		if el > stack[-1]:
			if stack[-1] / abs(stack[-1]) == el / abs(el):
				stack[-1] = el
			else:
				stack.append(el)
		elif stack[-1] / abs(stack[-1]) != el / abs(el):
			stack.append(el)
	answer.append(sum(stack))
for a in answer:
	print(a)


num_t = int(input())
for t in range(num_t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	sum = 0
	best = 0
	for (i, item) in enumerate(arr):
		if item < 0 and best > 0 or (item > 0 and best < 0):
			sum += best
			best = 0
		if best == 0 or item > best:
			best = item
	sum += best
	print(sum)


def sign(x):
	return 1 if x > 0 else -1
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	sgn = sign(a[0])
	rmax = a[0]
	ans = 0
	for i in range(1, n):
		if sign(a[i]) != sgn:
			ans += rmax
			sgn = sign(a[i])
			rmax = a[i]
		else:
			rmax = max(rmax, a[i])
	ans += rmax
	print(ans)


import math as m

def f1():
	T = int(input())

def f3():
	T = int(input())
	while T != 0:
		T = T - 1
		N = int(input())
		Arr = [int(i) for i in input().split(' ')]
		ans = 0
		mx = -100000000000.0
		flag = False
		for i in range(0, len(Arr)):
			if i > 0:
				if Arr[i - 1] < 0 and Arr[i] > 0:
					ans = ans + mx
					mx = -100000000000.0
					flag = True
				elif Arr[i - 1] > 0 and Arr[i] < 0:
					ans = ans + mx
					mx = -100000000000.0
					flag = True
			if mx < Arr[i]:
				mx = Arr[i]
			if i == len(Arr) - 1:
				ans = ans + mx
		print(ans)

def f2():
	T = int(input())
	while T != 0:
		T = T - 1
		n = int(input())
		if n // 2 % 2 != 0:
			print('NO')
		else:
			E = [2 * i for i in range(1, n // 2 + 1)]
			O = [i for i in range(1, n - 1, 2)]
			se = sum(E)
			so = sum(O)
			O.append(se - so)
			print('YES')
			for i in E:
				print(i, end=' ')
			for i in O:
				print(i, end=' ')
			print()
f3()


n = int(input())
for i in range(n):
	length = int(input())
	myList = input().split()
	ans = 0
	maxNum = int(myList[0])
	for i in range(len(myList) - 1):
		if int(myList[i + 1]) > 0 and int(myList[i]) < 0:
			ans += maxNum
			maxNum = int(myList[i + 1])
		elif int(myList[i + 1]) < 0 and int(myList[i]) > 0:
			ans += maxNum
			maxNum = int(myList[i + 1])
		elif int(myList[i + 1]) > maxNum:
			maxNum = int(myList[i + 1])
	ans += maxNum
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	s = 0
	m = -9999999999
	while i < n:
		if i < n and l[i] > 0:
			while i < n and l[i] > 0:
				m = max(m, l[i])
				i += 1
			s += m
			m = -9999999999
		elif i < n and l[i] < 0:
			while i < n and l[i] < 0:
				m = max(m, l[i])
				i += 1
			s += m
			m = -9999999999
		else:
			i += 1
	print(s)


t = int(input())
for i in range(t):
	n = int(input())
	arr = input().split()
	val = 0
	tot = 0
	while val < n:
		curr = int(arr[val])
		if curr > 0:
			maxP = curr
			while val < n and int(arr[val]) > 0:
				maxP = max(maxP, int(arr[val]))
				val += 1
			tot += maxP
		else:
			maxN = curr
			while val < n and int(arr[val]) < 0:
				maxN = max(maxN, int(arr[val]))
				val += 1
			tot += maxN
	print(tot)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(j) for j in input().split()]
	if a[0] < 0:
		curr_neg = a[0]
		curr_pos = 0
		neg = True
	elif a[0] > 0:
		curr_pos = a[0]
		curr_neg = -float('inf')
		neg = False
	j = 0
	ans = 0
	while j < len(a):
		if a[j] < 0:
			if neg == False:
				ans += curr_pos
				curr_pos = 0
				neg = True
			if a[j] > curr_neg:
				curr_neg = a[j]
		if a[j] > 0:
			if neg == True:
				ans += curr_neg
				curr_neg = -float('inf')
				neg = False
			if a[j] > curr_pos:
				curr_pos = a[j]
		j += 1
	if curr_pos != 0:
		ans += curr_pos
	else:
		ans += curr_neg
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	convert = [0] * n
	for i in range(n):
		if a[i] > 0:
			convert[i] = 1
	if convert[0] == 1:
		one = 1
		i = 0
		ans1 = 0
		maxx1 = a[0]
		while i < n and convert[i] == 1:
			maxx1 = max(maxx1, a[i])
			i += 1
			if i < n and convert[i] == 0:
				ans1 += maxx1
				one += 1
				minn1 = a[i]
				while i < n and convert[i] == 0:
					minn1 = max(minn1, a[i])
					i += 1
				ans1 += minn1
				if i < n:
					maxx1 = a[i]
					one += 1
		if convert[n - 1] == 1:
			ans1 += maxx1
		print(ans1)
	elif convert[0] == 0:
		zero = 1
		i = 0
		ans2 = 0
		maxx2 = a[0]
		while i < n and convert[i] == 0:
			maxx2 = max(maxx2, a[i])
			i += 1
			if i < n and convert[i] == 1:
				ans2 += maxx2
				zero += 1
				minn2 = a[i]
				while i < n and convert[i] == 1:
					minn2 = max(minn2, a[i])
					i += 1
				ans2 += minn2
				if i < n:
					maxx2 = a[i]
					zero += 1
		if convert[n - 1] == 0:
			ans2 += maxx2
		print(ans2)


def main():
	cc = int(input())
	for ccc in range(cc):
		c = int(input())
		data = [int(v) for v in input().split()]
		s = [[data[0]]]
		for i in range(1, len(data)):
			if s[-1][0] > 0 and data[i] < 0 or (s[-1][0] < 0 and data[i] > 0):
				s.append([data[i]])
			else:
				s[-1].append(data[i])
		ss = 0
		for (i, v) in enumerate(s):
			ss += max(v)
		print(ss)
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	j = 0
	while j < n:
		limit = 0
		while j < n and a[j] < 0:
			if a[j] > limit or limit == 0:
				limit = a[j]
			j += 1
		ans += limit
		limit = 0
		while j < n and a[j] > 0:
			if a[j] > limit:
				limit = a[j]
			j += 1
		ans += limit
	print(ans)


def signOf(elem):
	return elem > 0

def maxSum(arr):
	sum = 0
	maximum = arr[0]
	for i in range(1, n):
		if signOf(arr[i]) == signOf(maximum):
			maximum = max(arr[i], maximum)
		if i == n - 1:
			if signOf(arr[i]) != signOf(maximum):
				sum += arr[i]
			sum += maximum
		elif signOf(arr[i]) != signOf(maximum):
			sum += maximum
			maximum = arr[i]
	return sum
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print(arr[0])
	else:
		print(maxSum(arr))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	le = 0
	r = -10 ** 9 - 1
	ans = 0
	a = False
	b = False
	f = l[0]
	if 1 > 0:
		for i in range(n):
			if l[i] > 0:
				le = max(l[i], le)
				if a:
					ans += r
					r = -10 ** 9 - 1
					a = False
				b = True
			if l[i] < 0:
				if b:
					ans += le
					le = 0
					b = False
				a = True
				r = max(r, l[i])
		if r != -10 ** 9 - 1:
			ans = ans + r
		ans = ans + le
		print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	current = arr[0]
	i = 1
	max_sum = 0
	while i < len(arr):
		if current * arr[i] >= 0:
			current = max(current, arr[i])
		else:
			max_sum += current
			current = arr[i]
		i += 1
	max_sum += current
	print(max_sum)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] < 0:
		mode = 0
	else:
		mode = 1
	b = a[0]
	s = 0
	for i in range(len(a)):
		j = i
		i = a[i]
		if mode == 0:
			if i < 0:
				if i > b:
					b = i
			else:
				mode = 1
				s += b
				b = a[j]
		if mode == 1:
			if i > 0:
				if i > b:
					b = i
			else:
				mode = 0
				s += b
				b = a[j]
	print(s + b)


from collections import deque
from collections import OrderedDict
import math
for i in range(int(input())):
	value = int(input())
	array = input().split()
	answer = []
	isPlus = False
	maxValue = -1
	minValue = +1
	for j in range(len(array)):
		array[j] = int(array[j])
		if j == 0:
			if array[j] > 0:
				isPlus = True
				maxValue = array[j]
			else:
				isPlus = False
				minValue = array[j]
			continue
		if array[j] > 0 and isPlus == True:
			maxValue = max(maxValue, array[j])
		elif array[j] < 0 and isPlus == False:
			minValue = max(minValue, array[j])
		elif array[j] > 0 and isPlus == False:
			answer.append(minValue)
			maxValue = array[j]
			isPlus = True
		elif array[j] < 0 and isPlus == True:
			answer.append(maxValue)
			minValue = array[j]
			isPlus = False
	if array[value - 1] > 0:
		answer.append(maxValue)
	elif array[value - 1] < 0:
		answer.append(minValue)
	sumAns = 0
	for j in range(len(answer)):
		sumAns += answer[j]
	print(sumAns)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	x = a[0]
	z = 0
	j = 0
	while j < n:
		if a[j] * x < 0:
			z = z + x
			x = a[j]
		else:
			x = max(x, a[j])
		j += 1
	print(z + x)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	li = [a[0]]
	for i in range(1, n):
		if a[i] * a[i - 1] > 0:
			li[-1] = max(li[-1], a[i])
		else:
			li.append(a[i])
	print(sum(li))


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

def sign(num):
	return 1 if num > 0 else -1

def split_patches(nums):
	cur_patch = []
	for num in nums:
		if len(cur_patch) == 0 or sign(cur_patch[-1]) == sign(num):
			cur_patch.append(num)
		else:
			yield cur_patch
			cur_patch = [num]
	if len(cur_patch) > 0:
		yield cur_patch

def main():
	for _ in range(int(input())):
		_ = read_nums()
		nums = read_nums()
		print(sum(map(max, split_patches(nums))))
main()


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	maxni = -9832798030928
	maxn = maxni
	maxp = 0
	j = 0
	s = 0
	while j < n:
		if a[j] < 0:
			maxn = max(a[j], maxn)
			while j < n and a[j] < 0:
				maxn = max(a[j], maxn)
				j += 1
			s += maxn
			maxn = maxni
		else:
			maxp = max(a[j], maxp)
			while j < n and a[j] > 0:
				maxp = max(a[j], maxp)
				j += 1
			s += maxp
			maxp = 0
	print(s)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = 0
	pos_neg = -1
	pos_pos = -1
	dp = [0] * n
	dp[0] = arr[0]
	if arr[0] > 0:
		pos_pos = 0
		pos = 1
	else:
		pos_neg = 0
		pos = 0
	for i in range(1, n):
		if pos == 0:
			if arr[i] > 0:
				dp[i] = dp[pos_neg] + arr[i]
				pos_pos = i
				pos = 1
			elif arr[i] > arr[pos_neg]:
				dp[i] = dp[pos_neg] - arr[pos_neg] + arr[i]
				pos_neg = i
				pos = 0
			else:
				dp[i] = dp[pos_neg]
		elif arr[i] < 0:
			dp[i] = dp[pos_pos] + arr[i]
			pos_neg = i
			pos = 0
		elif arr[i] > arr[pos_pos]:
			dp[i] = dp[pos_pos] - arr[pos_pos] + arr[i]
			pos_pos = i
			pos = 1
		else:
			dp[i] = dp[pos_pos]
	print(dp[n - 1])


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	alt = []
	ct = 0
	while ct < n:
		a = []
		while ct < n:
			a.append(ar[ct])
			ct += 1
			if ct != 0 and ct < n and (ar[ct] * ar[ct - 1] < 0):
				break
		alt.append(max(a))
	print(sum(alt))


def A():
	t = int(input())
	for i in range(t):
		a = []
		n = int(input())
		s = input()
		for num in [int(ss) for ss in s.split(' ')]:
			if len(a) == 0:
				a.append(num)
			elif num * a[len(a) - 1] < 0:
				a.append(num)
			elif num > a[len(a) - 1]:
				a[len(a) - 1] = num
		print(sum(a))
A()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
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
mod = pow(10, 9) + 7
mod2 = 998244353

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

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

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

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()
	parity = 0
	if arr[0] > 0:
		parity = 1
	m = arr[0]
	answer = 0
	for i in range(1, n):
		if arr[i] < 0:
			if parity:
				answer += m
				m = arr[i]
				parity = 0
			else:
				m = max(arr[i], m)
		elif parity:
			m = max(arr[i], m)
		else:
			answer += m
			m = arr[i]
			parity = 1
	outln(answer + m)


for i in range(int(input())):
	n = int(input())
	lis = [int(x) for x in input().split()]
	lis.append(0)
	p = lis[0]
	ans = 0
	for j in range(1, len(lis)):
		if lis[j] >= 0 and p < 0 or (lis[j] <= 0 and p > 0):
			ans += p
			p = lis[j]
		elif lis[j] > p:
			p = lis[j]
	print(ans)


from sys import stdin
input = stdin.readline

def A():
	li = []
	for i in range(2, 30):
		li.append(2 ** i - 1)
	for _ in range(int(input())):
		n = int(input())
		j = 0
		while j < len(li):
			if n % li[j] == 0:
				print(n // li[j])
				break
			j += 1
	return

def B():
	for _ in range(int(input())):
		n = int(input())
		if n // 2 % 2 == 1:
			print('NO')
		else:
			print('YES')
			arr = [0] * n
			for i in range(1, n // 2 + 1):
				arr[i - 1] = 2 * i
			for i in range(n // 2, n - 1):
				arr[i] = 2 * (i - n // 2) + 1
			arr[n - 1] = sum(arr[:n // 2]) - sum(arr[n // 2:])
			print(*arr)
	return

def C():
	from collections import deque
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		pos = False
		if a[0] > 0:
			pos = True
		curr = a[0]
		tot = 0
		for i in range(n):
			if a[i] > 0:
				if not pos:
					tot += curr
					pos = True
					curr = a[i]
				else:
					curr = max(a[i], curr)
			elif pos:
				tot += curr
				pos = False
				curr = a[i]
			else:
				curr = max(a[i], curr)
		tot += curr
		print(tot)
	return

def D():
	return

def E():
	return
C()


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	l = 1
	k = 0
	for j in range(1, n):
		if arr[k] * arr[j] < 0:
			l += 1
			k = j
	na = []
	a = 0
	k = 0
	while k < n:
		t = k + 1
		while t < n:
			if arr[k] * arr[t] < 0:
				break
			t += 1
		na.append(arr[k:t])
		k = t
	for k in na:
		a += max(k)
	ans.append(a)
print(*ans, sep='\n')


t = int(input())
for j in range(t):
	n = input()
	l = list(map(int, input().split()))
	s = l[0] > 0
	m = l[0]
	summ = 0
	for i in range(len(l)):
		if s == (l[i] > 0) and m < l[i]:
			m = l[i]
		elif s != (l[i] > 0):
			summ += m
			m = l[i]
			s = l[i] > 0
	summ += m
	print(summ)


for x in range(int(input())):
	n = int(input())
	(*l,) = map(int, input().split())
	r = 0
	s = l[0]
	for i in range(1, n):
		if l[i] * s < 0:
			r += s
			s = l[i]
		s = max(s, l[i])
	print(r + s)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	id = 0
	ans = 0
	while id < n:
		maxx = 0
		while id < n and a[id] > 0:
			maxx = max(maxx, a[id])
			id += 1
		ans += maxx
		minn = -100000000000
		while id < n and a[id] == 0:
			id += 1
		while id < n and a[id] < 0:
			minn = max(minn, a[id])
			id += 1
		if minn != -100000000000:
			ans += minn
	print(ans)


t = int(input())
while t:
	n = int(input())
	seq = list(map(int, input().split()))
	res2 = []
	for x in seq:
		if x < 0:
			if len(res2) == 0:
				res2.append(x)
			elif res2[-1] > 0:
				res2.append(x)
			elif res2[-1] < 0:
				if res2[-1] < x:
					res2[-1] = x
		elif x > 0:
			if len(res2) == 0:
				res2.append(x)
			elif res2[-1] < 0:
				res2.append(x)
			elif res2[-1] > 0:
				if res2[-1] < x:
					res2[-1] = x
	print(sum(res2))
	t -= 1


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


def solve(n, arr):
	res1 = []
	res2 = []
	for i in range(0, n):
		if len(res1) == 0 and arr[i] > 0 or (len(res1) > 0 and arr[i] * arr[res1[-1]]) < 0:
			res1.append(i)
		if len(res2) == 0 and arr[i] < 0 or (len(res2) > 0 and arr[i] * arr[res2[-1]]) < 0:
			res2.append(i)
	for i in range(0, len(res1)):
		start = res1[i]
		end = n
		if i < len(res1) - 1:
			end = res1[i + 1]
		best = start
		for j in range(start, end):
			if arr[j] > arr[best]:
				best = j
		res1[i] = best
	for i in range(0, len(res2)):
		start = res2[i]
		end = n
		if i < len(res2) - 1:
			end = res2[i + 1]
		best = start
		for j in range(start, end):
			if arr[j] > arr[best]:
				best = j
		res2[i] = best
	(sum1, sum2) = (0, 0)
	for i in res1:
		sum1 += arr[i]
	for i in res2:
		sum2 += arr[i]
	if len(res1) > len(res2):
		return sum1
	elif len(res1) < len(res2):
		return sum2
	else:
		return max(sum1, sum2)
t = int(input())
for i in range(0, t):
	n = int(input())
	arr = list(map(int, input().split()))
	res = solve(n, arr)
	print(res)


for uhuipio in range(int(input())):
	input()
	l = list(map(int, input().split()))
	max = l[0]
	sum = 0
	sig = (True, False)[max < 0]
	for j in range(1, len(l)):
		if (True, False)[l[j] < 0] != sig:
			sig = (True, False)[l[j] < 0]
			sum += max
			max = l[j]
		elif l[j] > max:
			max = l[j]
	print(sum + max)


t = int(input())
for test in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	sum1 = 0
	a = lst[0]
	p = 0
	if a > 0:
		p = 1
	lst1 = []
	lst1.append(a)
	for i in range(1, n):
		if lst[i] > 0 and p == 1:
			lst1.append(lst[i])
		elif lst[i] < 0 and p == 0:
			lst1.append(lst[i])
		else:
			m = max(lst1)
			sum1 += m
			lst1 = []
			if i <= n - 1:
				a = lst[i]
				p = 0
				if a > 0:
					p = 1
				lst1.append(a)
	l = len(lst1)
	if l > 0:
		m = max(lst1)
		sum1 += m
	print(sum1)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	input()
	a = list(map(int, stdin.readline().split()))
	s = 0
	m = a[0]
	for v in a:
		if m * v < 0:
			s += m
			m = v
		m = max(m, v)
	s += m
	stdout.write(str(s) + '\n')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = 1
	val = None
	ans = 0
	for i in arr:
		if i < 0 and flag < 0:
			val = i if val is None else max(i, val)
		elif i < 0 and flag > 0:
			flag = -1
			ans = ans + val if val is not None else ans
			val = i
		elif i > 0 and flag > 0:
			val = i if val is None else max(i, val)
		elif i > 0 and flag < 0:
			flag = 1
			ans = ans + val if val is not None else ans
			val = i
	ans += val
	print(ans)


import sys
t = int(input())
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	ans = []
	val = arr[0]
	if val > 0:
		flag = 1
	else:
		flag = 0
	for i in arr:
		if flag:
			if i > val:
				val = i
			elif i < 0:
				ans.append(val)
				val = i
				flag = 0
		elif i > 0:
			ans.append(val)
			val = i
			flag = 1
		elif i > val:
			val = i
	if len(ans):
		if ans[-1] != val:
			ans.append(val)
	if len(ans):
		print(sum(ans))
	else:
		print(max(arr))


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


T = int(input())
for i in range(T):
	N = int(input())
	ar = list(map(int, input().split()))
	r = 0
	sm = 0
	prevN = float('inf')
	prevP = float('-inf')
	while r < len(ar):
		if ar[r] < 0:
			if prevN != float('inf'):
				curN = max(curN, ar[r])
			else:
				curN = ar[r]
				prevN = ar[r]
			if prevP != float('-inf'):
				sm += curP
			prevP = float('-inf')
			curP = float('-inf')
		else:
			if prevP == float('-inf'):
				prevP = ar[r]
				curP = ar[r]
			else:
				curP = max(ar[r], curP)
			if prevN != float('inf'):
				sm += curN
			prevN = float('inf')
			curN = float('inf')
		r += 1
	if curN != float('inf'):
		sm += curN
	if curP != float('-inf'):
		sm += curP
	print(sm)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = l[0] // abs(l[0])
	s = 0
	maxi = l[0]
	for i in l:
		if i // abs(i) == x:
			maxi = max(maxi, i)
		else:
			s += maxi
			x = i // abs(i)
			maxi = i
	s += maxi
	print(s)


import sys
import math

def func(d):
	return math.ceil(math.log2(d + 1))

def process(A):
	signePositive = A[0] > 0
	lists = []
	tmp = []
	for i in range(len(A)):
		a = A[i]
		if len(tmp) == 0:
			tmp.append(a)
		elif (a > 0) == (tmp[0] > 0):
			tmp.append(a)
		else:
			lists.append(tmp)
			tmp = [a]
		if i == len(A) - 1:
			lists.append(tmp)
	results = []
	for l in lists:
		results.append(max(l))
	print(sum(results))
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 f(a, n):
	return True

def is_permutation(arr):
	for e in range(1, len(arr) + 1):
		if e not in arr:
			return False
	return True
t = inp()
for i in range(t):
	n = inp()
	A = inlt()
	process(A)


x = int(input())
for a in range(x):
	y = int(input())
	z = list(map(int, input().split()))
	s = 0
	num = []
	start = z[0]
	f = True
	for b in range(y - 1):
		if abs(z[b]) + abs(z[b + 1]) != abs(z[b] + z[b + 1]):
			num.append(start)
			s += start
			start = z[b + 1]
			if b == y - 2:
				f = False
		elif z[b + 1] > start:
			start = z[b + 1]
	num.append(start)
	s += start
	print(s)


t = int(input())
for _ in range(t):
	input()
	b = []
	s = []
	for a in map(int, input().split()):
		if a > 0:
			if s and s[-1] < 0:
				b.append(max(s))
				s.clear()
			s.append(a)
		else:
			if s and s[-1] > 0:
				b.append(max(s))
				s.clear()
			s.append(a)
	if s:
		b.append(max(s))
	print(sum(b))


for t in range(int(input().strip())):
	n = int(input().strip())
	arr = tuple(map(int, input().strip().split()))
	pv = arr[0]
	res = 0
	for i in range(1, n):
		if pv * arr[i] < 0:
			res += pv
			pv = arr[i]
		else:
			pv = max(pv, arr[i])
	res += pv
	print(res)


import math
t = int(input())
for j in range(t):
	k = int(input())
	l = list(map(int, input().split()))
	(n, p, c) = ([], [], 0)
	for i in range(len(l)):
		if l[i] > 0:
			p.append(l[i])
			if i > 0 and l[i - 1] < 0:
				c = c + max(n)
				n = []
			if i == k - 1:
				c = c + max(p)
		else:
			n.append(l[i])
			if i > 0 and l[i - 1] > 0:
				c = c + max(p)
				p = []
			if i == k - 1:
				c = c + max(n)
	print(c)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = 0
	m = 0
	f = []
	for i in a:
		if i < 0:
			if m == 0:
				m = i
			m = max(m, i)
			f.append(p)
			p = 0
		if i > 0:
			p = max(p, i)
			f.append(m)
			m = 0
	f.append(p)
	f.append(m)
	print(sum(f))


def solve(n, a):
	(i, j, s) = (0, 0, 0)
	while i < n:
		m = -float('INF')
		while j < n and a[i] * a[j] > 0:
			m = max(m, a[j])
			j += 1
		s += m
		i = j
	return s

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


from sys import stdin, stdout
from collections import Counter
import math

def main2():
	n = input()
	finaloutput = []
	for i in range(int(n)):
		asdf = input()
		array = input()
		arr2 = [int(x) for x in array.split()]
		output = []
		num = arr2[0]
		for i in range(1, len(arr2)):
			if math.copysign(1, arr2[i]) != math.copysign(1, num):
				output.append(num)
				num = arr2[i]
			elif arr2[i] > num:
				num = arr2[i]
		output.append(num)
		finaloutput.append(str(sum(output)))
	for i in finaloutput:
		stdout.write(i + '\n')
	return ()
main2()


n = int(input())
arr = []
size = 0
for i in range(n * 2):
	if i % 2 == 0:
		input()
	else:
		a = [int(x) for x in input().split()]
		arr.append(a)
for a in arr:
	ispos = False
	isneg = False
	start = 0
	sum = 0
	for i in range(len(a) + 1):
		if i == len(a):
			sum += max(a[start:i])
			break
		n = a[i]
		if n > 0 and (not ispos) and (not isneg):
			ispos = True
		if n < 0 and (not ispos) and (not isneg):
			isneg = True
		if n > 0 and isneg:
			sum += max(a[start:i])
			isneg = False
			ispos = True
			start = i
		if n < 0 and ispos:
			sum += max(a[start:i])
			ispos = False
			isneg = True
			start = i
	print(sum)


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

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

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

def main():
	try:
		for _ in range(I()):
			n = I()
			l = list(In())
			d = []
			prim = l[0]
			for x in range(1, n):
				if l[x] < 0 and prim < 0:
					prim = min(abs(l[x]), abs(prim))
					prim = -1 * prim
				elif l[x] > 0 and prim > 0:
					prim = max(l[x], prim)
				else:
					d.append(prim)
					prim = l[x]
			d.append(prim)
			print(sum(d))
	except:
		pass
M = 998244353
P = 1000000007
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	maxi = 0
	mini = -1000000000
	count = 0
	if a[0] > 0:
		fg = 0
	else:
		fg = 1
	for i in range(n):
		if a[i] > 0 and fg == 1:
			count += mini
			maxi = 0
		if a[i] > 0:
			maxi = max(maxi, a[i])
			fg = 0
		if a[i] < 0 and fg == 0:
			count += maxi
			mini = -1000000000
		if a[i] < 0:
			mini = max(mini, a[i])
			fg = 1
	if a[n - 1] > 0:
		count += maxi
	else:
		count += mini
	print(count)


T = int(input())
ans = []
for t in range(T):
	n = int(input())
	a = [int(i) for i in input().split()]
	dic = []
	i = 0
	while i < n:
		j = i
		ex = a[j]
		while j + 1 < n and (a[j + 1] < 0) == (a[j] < 0):
			j += 1
			ex = max(ex, a[j])
		dic.append(ex)
		i = j + 1
	ans.append(sum(dic))
for a in ans:
	print(a)


for t in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	list_chunk_max = []
	last_e = A[0]
	chunk_max = last_e
	for i in range(1, n):
		if last_e * A[i] > 0:
			chunk_max = max(chunk_max, A[i])
		else:
			list_chunk_max.append(chunk_max)
			chunk_max = A[i]
		last_e = A[i]
	list_chunk_max.append(chunk_max)
	print(sum(list_chunk_max))


t = int(input())
for i in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print(l[0])
	else:
		s = l[0]
		if l[0] < 0:
			f = 0
			for j in range(1, n):
				if l[j] > 0:
					f = 1
					break
			if f == 0:
				print(max(l))
			else:
				f = -1
				s = l[0]
				m = s
				p = 0
				l1 = []
				l1.append(l[0])
				s = 0
				for j in range(1, n):
					if f == -1 and l[j] < 0:
						l1.append(l[j])
					elif f == 1 and l[j] > 0:
						l1.append(l[j])
					else:
						if f == -1:
							s += max(l1)
						else:
							s += max(l1)
						l1 = []
						l1.append(l[j])
						f *= -1
				if f == -1:
					s += max(l1)
				else:
					s += max(l1)
				print(s)
		else:
			f = 0
			for j in range(1, n):
				if l[j] < 0:
					f = 1
					break
			if f == 0:
				print(max(l))
			else:
				f = 1
				s = l[0]
				m = s
				p = 0
				l1 = []
				l1.append(l[0])
				s = 0
				for j in range(1, n):
					if f == -1 and l[j] < 0:
						l1.append(l[j])
					elif f == 1 and l[j] > 0:
						l1.append(l[j])
					else:
						if f == -1:
							s += max(l1)
						else:
							s += max(l1)
						l1 = []
						l1.append(l[j])
						f *= -1
				if f == -1:
					s += max(l1)
				else:
					s += max(l1)
				print(s)


tc = int(input().strip())

def solve():
	n = int(input().strip())
	ls = list(map(int, input().strip().split(' ')))
	net_res = 0
	curr_max = float('-inf')
	for i in range(n):
		if i != 0 and (not (ls[i - 1] < 0) ^ (ls[i] >= 0)):
			net_res += curr_max
			curr_max = float('-inf')
		curr_max = max(curr_max, ls[i])
	net_res += curr_max
	print(net_res)
for t in range(1, tc + 1):
	solve()


t = int(input())

def sameSign(a, b):
	if a > 0 and b > 0 or (b < 0 and a < 0):
		return True
	return False
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	mx = arr[0]
	(prev, ans) = (arr[0], 0)
	for i in range(n):
		if sameSign(prev, arr[i]):
			mx = max(mx, arr[i])
		else:
			ans += mx
			mx = arr[i]
		prev = arr[i]
	print(ans + mx)


t = int(input())
for i in range(t):
	n = int(input())
	ar = list(map(int, input().split()))
	st = [ar[0]]
	ans = 0
	for i in range(1, len(ar)):
		if ar[i] > 0 and st[-1] < 0 or (ar[i] < 0 and st[-1] > 0):
			ans += st.pop()
			st.append(ar[i])
		elif st[-1] < ar[i]:
			st.pop()
			st.append(ar[i])
	if len(st) != 0:
		ans += st[-1]
	print(ans)


a = int(input())
for i in range(a):
	b = int(input())
	l = list(map(int, input().split()))
	cnt = 0
	p = [[l[0]]]
	for i in range(1, b):
		if l[i - 1] < 0 and l[i] < 0 or (l[i - 1] > 0 and l[i] > 0):
			p[cnt].append(l[i])
		else:
			cnt += 1
			p.append([l[i]])
	l = 0
	for i in p:
		l += max(i)
	print(l)


for __ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	(neg, pos) = (False, False)
	(n, m) = ([], [])
	for i in a:
		if i < 0:
			n.append(i)
			neg = True
			if pos == True:
				s += max(m)
				m = []
				pos = False
		if i > 0:
			m.append(i)
			pos = True
			if neg == True:
				s += max(n)
				n = []
				neg = False
	if m != []:
		s += max(m)
	if n != []:
		s += max(n)
	print(s)


t = int(input())
l = []
for i in range(t):
	n = int(input())
	arr = []
	k = input().split()
	for j in range(n):
		arr.append(int(k[j]))
	max_min = -pow(10, 9)
	q = max_min
	sum = 0
	for j in range(n - 1):
		k = arr[j] > 0 and arr[j + 1] < 0
		s = arr[j] < 0 and arr[j + 1] > 0
		if k or s:
			if max_min < arr[j]:
				sum += arr[j]
			else:
				sum += max_min
			max_min = arr[j + 1]
		elif max_min < arr[j]:
			max_min = arr[j]
	if arr[n - 1] > max_min:
		sum += arr[n - 1]
	else:
		sum += max_min
	l.append(sum)
for i in range(t):
	print(l[i])


def sign(a):
	return a > 0
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	i = 0
	while i < n:
		max_ = a[i]
		j = i
		while j < n and sign(a[i]) == sign(a[j]):
			max_ = max(a[j], max_)
			j += 1
		ans += max_
		i = j - 1
		i += 1
	print(ans)


try:

	def s(n):
		return n > 0
	t = int(input())
	while t:
		n = int(input())
		a = list(map(int, input().strip().split()))[:n]
		m = 0
		i = 0
		while i < n:
			j = i
			m1 = a[i]
			while j < n and s(a[i]) == s(a[j]):
				m1 = max(a[j], m1)
				j = j + 1
			i = j
			m = m + m1
		print(m)
except:
	pass


tt = int(input())
while tt > 0:
	m = int(input())
	l = list(map(int, input().split()))
	(p, n) = ('#', '#')
	if l[0] > 0:
		s = 1
		p = l[0]
	else:
		s = -1
		n = l[0]
	q = 0
	i = 1
	while i < m:
		if l[i] > 0:
			t = 1
		else:
			t = -1
		if s > 0 and t > 0:
			p = max(p, l[i])
		elif s < 0 and t < 0:
			n = max(n, l[i])
		elif s < 0 and t > 0:
			q += n
			n = '#'
			p = l[i]
			s = 1
		elif s > 0 and t < 0:
			q += p
			p = '#'
			n = l[i]
			s = -1
		i += 1
	if p != '#':
		q += p
	else:
		q += n
	print(q)
	tt -= 1


def solve(a):
	if a[0] > 0:
		s = True
	else:
		s = False
	r = 0
	t = []
	for i in range(len(a)):
		if (a[i] > 0) != s:
			r += max(t)
			s = not s
			t = []
		t.append(a[i])
	r += max(t)
	return r
t = int(input())
for i in range(t):
	_ = input()
	a = [int(x) for x in input().split(' ')]
	s = solve(a)
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split()]
	if n == 1:
		print(*a)
		continue
	a.append(0)
	ma = []
	i = 1
	m = [a[0]]
	while i < n:
		while i < n and a[i] * a[i - 1] > 0:
			m.append(a[i])
			i += 1
		ma.append(m)
		m = [a[i]]
		i += 1
	if a[n - 1] * a[n - 2] < 0:
		ma.append([a[n - 1]])
	print(sum((max(i) for i in ma)))


num = int(input())
for _ in range(num):
	n = int(input())
	a = [*map(int, input().split())]
	ans = 0
	temp = a[0]
	flag = a[0] > 0
	point = 1
	while point < n:
		if (a[point] > 0) != flag:
			ans += temp
			temp = a[point]
			flag = a[point] > 0
			point += 1
		else:
			temp = max(temp, a[point])
			point += 1
	ans += temp
	print(ans)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	z = []
	if l[0] > 0:
		flag = 1
	else:
		flag = 0
	m1 = float('-infinity')
	m2 = float('-infinity')
	for i in range(n):
		if flag == 0:
			if l[i] > 0:
				z.append(m1)
				m1 = float('-infinity')
				m2 = max(m2, l[i])
				flag = 1
			else:
				m1 = max(m1, l[i])
		elif l[i] < 0:
			z.append(m2)
			m2 = float('-infinity')
			m1 = max(m1, l[i])
			flag = 0
		else:
			m2 = max(m2, l[i])
	if m1 == float('-infinity'):
		z.append(m2)
	else:
		z.append(m1)
	print(sum(z))


t = int(input())
for w in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	a = l[0]
	s = 0
	for i in l:
		if a * i < 0:
			s += a
			a = i
		else:
			a = max(a, i)
	print(s + a)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split(' ')))
	(su, i) = (0, 0)
	while i < n:
		if a[i] > 0:
			ad = 0
			while i < n and a[i] > 0:
				ad = max(ad, a[i])
				i += 1
			su += ad
		else:
			ad = a[i]
			while i < n and a[i] < 0:
				ad = max(ad, a[i])
				i += 1
			su += ad
	print(su)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

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

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

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

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

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
for _ in range(ii()):
	n = ii()
	arr = li()
	i = 0
	if arr[0] > 0:
		f = True
	else:
		f = False
	sum_ = 0
	while i < n:
		if f:
			maxpos = 1
			while i < n and arr[i] > 0:
				maxpos = max(maxpos, arr[i])
				i += 1
			f = False
		else:
			maxpos = -10 ** 9
			while i < n and arr[i] < 0:
				maxpos = max(maxpos, arr[i])
				i += 1
			f = True
		sum_ += maxpos
	print(sum_)


t = int(input())
import math
delay = []
for _ in range(t):
	n = int(input())
	chunks = []
	minichunk = []
	lookingpos = False
	arr = input().split()
	arr = [int(a) for a in arr]
	for i in range(n):
		num = arr[i]
		if i == 0:
			minichunk.append(num)
			if num < 0:
				lookingpos = False
			else:
				lookingpos = True
		elif num > 0 and lookingpos:
			minichunk.append(num)
		elif num > 0 and (not lookingpos):
			chunks.append(minichunk)
			minichunk = [num]
			lookingpos = True
		elif num < 0 and lookingpos:
			chunks.append(minichunk)
			minichunk = [num]
			lookingpos = False
		else:
			minichunk.append(num)
	chunks.append(minichunk)
	ans = sum([max(x) for x in chunks])
	delay.append(ans)
for d in delay:
	print(d)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	negative = a[0] < 0
	max_value = a[0]
	sum = 0
	result = []
	for i in range(0, len(a)):
		if negative and a[i] > 0 or (not negative and a[i] < 0):
			sum += max_value
			result.append(max_value)
			negative = not negative
			max_value = a[i]
			continue
		max_value = max(max_value, a[i])
	result.append(max_value)
	sum += max_value
	print(sum)


T = int(input())
i = 0
while i < T:
	n = int(input())
	if n == 1:
		print(input())
	else:
		sum = 0
		a = input().split()
		a = [int(x) for x in a]
		a = a + [-a[n - 1]]
		if a[0] > 0:
			s = 1
		else:
			s = -1
		max = a[0]
		for t in a[1:]:
			if s * t > 0 and t > max:
				max = t
			elif s * t < 0:
				sum += max
				max = t
				s = -s
		print(sum)
	i += 1


t = int(input())
A = []

def T(a):
	if a < 0:
		return True
	else:
		return False
for tests in range(t):
	n = int(input())
	A.clear()
	A = input().split()
	for i in range(n):
		A[i] = int(A[i])
	ans = 0
	maxs = A[0]
	for i in range(1, n):
		if T(A[i]) == T(A[i - 1]):
			if A[i] > maxs:
				maxs = A[i]
		else:
			ans = ans + maxs
			maxs = A[i]
	ans = ans + maxs
	print(ans)


def run(lst, n):
	llen = len(lst)
	soma = 0
	if llen == 1:
		print(lst[0])
		return
	j = 0
	for i in range(llen - 1):
		if i == llen - 2:
			if sameSign(lst[i], lst[i + 1]):
				soma += max(lst[j:])
			else:
				soma += max(lst[j:i + 1]) + lst[-1]
		elif sameSign(lst[i], lst[i + 1]):
			pass
		else:
			soma += max(lst[j:i + 1])
			j = i + 1
	print(soma)
	return

def sameSign(number1, number2):
	if number1 * number2 > 0:
		return True
	else:
		return False
i = int(input())
for _ in range(i):
	n = int(input())
	lst = list(map(int, input().split()))
	run(lst, n)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] > 0:
		f = 1
	else:
		f = 0
	maxx = 0
	minn = -float('inf')
	ans = 0
	for i in range(n):
		if a[i] > 0 and f == 1:
			if minn != -float('inf'):
				ans += minn
			f = 0
			maxx = 0
			maxx = max(maxx, a[i])
		elif a[i] > 0 and f == 0:
			maxx = max(maxx, a[i])
		if a[i] < 0 and f == 0:
			ans += maxx
			f = 1
			minn = -float('inf')
			minn = max(minn, a[i])
		elif a[i] < 0 and f == 1:
			minn = max(minn, a[i])
	if f == 0:
		ans += maxx
	else:
		ans += minn
	print(ans)


t = int(input())
for testcase in range(t):
	n = int(input())
	al = list(map(int, input().split()))
	pos = al[0] > 0
	chunks = []
	chunk = []
	for x in al:
		if x > 0:
			if pos:
				chunk.append(x)
			else:
				chunks.append(chunk)
				chunk = [x]
				pos = True
		elif pos:
			chunks.append(chunk)
			chunk = [x]
			pos = False
		else:
			chunk.append(x)
	chunks.append(chunk)
	print(sum(map(max, chunks)))


t = int(input())
for j in range(t):
	n = int(input())
	sum1 = 0
	listn = list(map(int, input().split()))
	lista = []
	for i in range(len(listn)):
		if i == 0:
			lista.append(listn[i])
		elif lista[-1] > 0:
			if listn[i] < 0:
				lista.append(listn[i])
			elif listn[i] > lista[-1]:
				lista = lista[:-1]
				lista.append(listn[i])
		elif listn[i] > 0:
			lista.append(listn[i])
		elif listn[i] > lista[-1]:
			lista = lista[:-1]
			lista.append(listn[i])
	for i in lista:
		sum1 += i
	print(sum1)


t = int(input())
from math import inf
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	num = -1
	sums = []
	while i < n:
		j = i + 1
		num += 1
		sums.append([a[i]])
		while j < n and (a[i] > 0 and a[j] > 0 or (a[i] < 0 and a[j] < 0)):
			sums[num].append(a[j])
			j += 1
		i = j
	sum = 0
	for i in sums:
		sum += max(i)
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	x = []
	y = []
	b = []
	if a[0] > 0:
		x.append(a[0])
	else:
		y.append(a[0])
	for i in range(1, n):
		if a[i] > 0 and len(x) != 0:
			x.append(a[i])
		if a[i] < 0 and len(y) != 0:
			y.append(a[i])
		if a[i] < 0 and len(y) == 0:
			y.append(a[i])
			b.append(x)
			x = []
		if a[i] > 0 and len(x) == 0:
			x.append(a[i])
			b.append(y)
			y = []
	if len(y) > 0:
		b.append(y)
	if len(x) > 0:
		b.append(x)
	s = 0
	for i in range(len(b)):
		s += max(b[i])
	print(s)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	newlst = []
	lst = [arr[0]]
	if arr[0] > 0:
		flag = 1
	else:
		flag = -1
	for i in range(1, n):
		if arr[i] < 0 and flag == -1:
			lst.append(arr[i])
		elif arr[i] < 0 and flag == 1:
			newlst.append(lst)
			flag = -1
			lst = []
			lst.append(arr[i])
		elif arr[i] > 0 and flag == 1:
			lst.append(arr[i])
		elif arr[i] > 0 and flag == -1:
			newlst.append(lst)
			flag = 1
			lst = []
			lst.append(arr[i])
	if len(lst) != 0:
		newlst.append(lst)
	ans = 0
	for lst in newlst:
		ans += max(lst)
	print(ans)


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split(' ')]
	sum = 0
	mx = 0
	mi = 0
	i = 0
	while i < n:
		mx = 0
		while i < n and arr[i] > 0:
			mx = max(arr[i], mx)
			i += 1
		sum = sum + mx
		mi = sys.maxsize * -1
		while i < n and arr[i] < 0:
			mi = max(arr[i], mi)
			i += 1
		if mi != sys.maxsize * -1:
			sum = sum + mi
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	al = []
	b = []
	c = []
	while i < n:
		while i < n and a[i] < 0:
			b.append(a[i])
			i += 1
		if b:
			al.append(b)
			b = []
		while i < n and a[i] > 0:
			c.append(a[i])
			i += 1
		if c:
			al.append(c)
			c = []
	su = 0
	for j in range(len(al)):
		su += max(al[j])
	print(su)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	temp = [a[0]]
	if a[0] > 0:
		si = 1
	else:
		si = 0
	for i in range(1, n):
		if a[i] > 0:
			if si == 1:
				temp.append(a[i])
			else:
				res += max(temp)
				si = 1
				temp = [a[i]]
		elif si == 0:
			temp.append(a[i])
		else:
			res += max(temp)
			si = 0
			temp = [a[i]]
	res += max(temp)
	print(res)


t = int(input())
for gi in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	a = []
	i = 0
	p1 = 0
	p2 = 0
	c = 0
	while i < n - 1:
		if l[i] // abs(l[i]) == l[i + 1] // abs(l[i + 1]):
			p2 += 1
		elif l[i] // abs(l[i]) != l[i + 1] // abs(l[i + 1]):
			c += 1
			if l[p1:p2 + 1]:
				a.append(max(l[p1:p2 + 1]))
			p1 = i + 1
			p2 = p1
		i += 1
	if len(a) < c + 1:
		a.append(max(l[p1:p2 + 1]))
		print(sum(a))
	else:
		print(max(l))


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


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


for i in range(int(input())):
	number = int(input())
	elements = list(map(int, input().split()))
	total = elements[0]
	item = elements[0]
	for j in range(1, number):
		if elements[j] < 0 < item:
			total += elements[j]
			item = elements[j]
		elif item > 0 and elements[j] > 0:
			if elements[j] > item:
				total -= item
				total += elements[j]
				item = elements[j]
		elif item < 0 < elements[j]:
			total += elements[j]
			item = elements[j]
		elif elements[j] > item:
			total -= item
			total += elements[j]
			item = elements[j]
	print(total)


t = int(input())
output = []
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	last_negative = a[0] < 0
	last_number = a[0]
	b = []
	for i in range(0, len(a)):
		if i == len(a) - 1:
			b.append(last_number)
			break
		if last_negative and a[i + 1] >= 0:
			last_negative = False
			b.append(last_number)
			last_number = a[i + 1]
		elif last_negative and a[i + 1] < 0 and (a[i + 1] > last_number):
			last_number = a[i + 1]
		elif not last_negative and a[i + 1] < 0:
			last_negative = True
			b.append(last_number)
			last_number = a[i + 1]
		elif not last_negative and a[i + 1] >= 0 and (a[i + 1] > last_number):
			last_number = a[i + 1]
	output.append(b)
for o in output:
	print(sum(o))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(ans, prev) = (0, 0)
	(new, temp) = ([], [arr[0]])
	for i in range(1, n):
		if arr[i] > 0 and arr[i - 1] < 0 or (arr[i] < 0 and arr[i - 1] > 0):
			new.append(temp)
			temp = [arr[i]]
		else:
			temp.append(arr[i])
	new.append(temp)
	for i in new:
		ans += max(i)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	max = lst[0]
	tot = 0
	for j in range(1, n):
		if max / lst[j] < 0:
			tot += max
			max = lst[j]
		elif lst[j] > max:
			max = lst[j]
	tot += max
	print(tot)


for i in range(int(input())):
	length = int(input())
	array = list(map(int, input().split()))
	CURRENT = 0
	SUMM = 0
	current_array = []
	if len(array) == 1:
		print(array[0])
	else:
		for j in range(length):
			if j == 0:
				if int(array[j] > 0):
					CURRENT = 1
				current_array.append(array[j])
			elif j == length - 1:
				if int(array[j] > 0) != CURRENT:
					SUMM += max(current_array)
					SUMM += array[j]
				else:
					current_array.append(array[j])
					SUMM += max(current_array)
			elif int(array[j] > 0) == CURRENT:
				current_array.append(array[j])
			else:
				SUMM += max(current_array)
				current_array = [array[j]]
				CURRENT = int(array[j] > 0)
		print(SUMM)


import os

def a(x, y):
	return (x[0] + y[0], x[1] + y[1])

def f(nums):
	n = len(nums)
	dp_plus = [(0, 0)] * (n + 1)
	dp_minus = [(0, 0)] * (n + 1)
	for (idx, x) in enumerate(nums):
		ii = idx + 1
		if x > 0:
			(p, m) = (dp_plus, dp_minus)
		else:
			(p, m) = (dp_minus, dp_plus)
		p[ii] = max(a(m[ii - 1], (1, x)), p[ii - 1])
		m[ii] = m[ii - 1]
	return max(dp_plus[-1], dp_minus[-1])[1]

def pp(input):
	n_test = int(input())
	for i in range(n_test):
		input()
		print(f(list(map(int, input().split()))))
if 'paalto' in os.getcwd():
	from string_source import string_source
	s = string_source('4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000')
	pp(s)
else:
	pp(input)


def problemC(n, a):
	maxs = a[0]
	if a[0] >= 0:
		ls = 1
		for j in range(0, len(a)):
			if a[j] < 0:
				break
			else:
				maxs = max(maxs, a[j])
	else:
		ls = 0
		for j in range(0, len(a)):
			if a[j] > 0:
				break
			else:
				maxs = max(maxs, a[j])
	t = a.index(maxs)
	minn = -1000000001
	maxp = 0
	for j in range(t + 1, len(a)):
		if ls == 1:
			if a[j] < 0:
				minn = max(minn, a[j])
				if j == n - 1:
					maxs += minn
			else:
				if minn != -1000000001:
					maxs += minn
					ls = 0
				maxp = a[j]
				if j == len(a) - 1 and ls == 0:
					maxs += maxp
		elif a[j] > 0:
			maxp = max(maxp, a[j])
			if j == n - 1:
				maxs += maxp
		else:
			if maxp != 0:
				maxs += maxp
				ls = 1
			minn = a[j]
			if j == len(a) - 1 and ls == 1:
				maxs += minn
	return maxs
test = int(input())
for i in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	print(problemC(n, a))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	i = 0
	while i < n:
		maxp = 0
		maxn = 0
		while i < n and a[i] < 0:
			if a[i] > maxn or maxn == 0:
				maxn = a[i]
			i += 1
		s += maxn
		while i < n and a[i] > 0:
			if a[i] > maxp:
				maxp = a[i]
			i += 1
		s += maxp
	print(s)


case = int(input())
for j in range(case):
	t = int(input())
	g = input()
	lst = g.split(' ')
	alt = []
	temp = []
	if int(lst[0]) > 0:
		sign = 'p'
	else:
		sign = 'n'
	for i in lst:
		if int(i) > 0 and sign == 'p':
			temp.append(int(i))
		elif int(i) < 0 and sign == 'n':
			temp.append(int(i))
		else:
			alt.append(max(temp))
			temp = [int(i)]
			if sign == 'n':
				sign = 'p'
			else:
				sign = 'n'
	alt.append(max(temp))
	print(sum(alt))


t = int(input())
for _ in range(t):
	n = int(input())
	lst = [i for i in map(int, input().split())]
	ans = [lst[0]]
	for index in range(1, n):
		if lst[index] < 0:
			if lst[index - 1] > 0:
				ans.append(lst[index])
			elif lst[index - 1] < 0 and lst[index] > ans[len(ans) - 1]:
				ans[len(ans) - 1] = lst[index]
		elif lst[index - 1] < 0:
			ans.append(lst[index])
		elif lst[index] > 0 and ans[len(ans) - 1] < lst[index]:
			ans[len(ans) - 1] = lst[index]
	print(sum(ans))


for _ in range(int(input())):
	n = int(input())
	ans = 0
	a = [int(n) for n in input().split()]
	x = -1000000000000
	s = x
	for i in range(n - 1):
		if a[i] * a[i + 1] < 0:
			x = max(x, a[i])
			ans += x
			x = s
		else:
			x = max(x, a[i])
	print(ans + max(x, a[n - 1]))


import pdb
queries = int(input())
for _ in range(queries):
	_ = input()
	array = [int(item) for item in input().split()]
	result = 0
	maxV = array[0]
	for item in range(1, len(array)):
		if maxV > 0 and array[item] > 0 or (maxV < 0 and array[item] < 0):
			if array[item] > maxV:
				maxV = array[item]
		else:
			result += maxV
			maxV = array[item]
	result += maxV
	print(result)


def solution(t):
	for i in range(t):
		input()
		m1 = [int(x) for x in input().split()]
		last = count = 0
		sign = -1 if m1[0] > 0 else 1
		for v in m1:
			t = 1 if v > 0 else -1
			if t != sign:
				last = v
				sign = t
				count += v
			elif v > last:
				count -= last
				last = v
				count += v
		print(count)

def main():
	t = int(input())
	solution(t)
main()


t = int(input())
for j in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [a[0]]
	ans = 0
	for i in range(1, n):
		if a[i] > 0 and a[i - 1] > 0:
			b.append(a[i])
		elif a[i] < 0 and a[i - 1] < 0:
			b.append(a[i])
		else:
			ans += max(b)
			b = [a[i]]
	ans += max(b)
	print(ans)


INT_MAX = 10 ** 11 + 7
MOD = 10 ** 9 + 7

def INPUT():
	return list((int(i) for i in input().split()))

def LIST_1D_ARRAY(n):
	return [0 for _ in range(n)]

def LIST_2D_ARRAY(m, n):
	return [[0 for _ in range(n)] for _ in range(m)]
GETA = 123
for _ in range(int(input())):
	n = int(input())
	A = INPUT()
	i = 0
	posmax = 0
	negmax = -10 ** 19
	ans = 0
	countpos = 0
	while i < n:
		if A[i] > 0:
			if negmax != -10 ** 19:
				ans += negmax
				negmax = -10 ** 19
			while i < n and A[i] > 0:
				if A[i] > posmax:
					posmax = A[i]
				i += 1
		else:
			ans += posmax
			posmax = 0
			while i < n and A[i] < 0:
				if A[i] > negmax:
					negmax = A[i]
				i += 1
	if A[-1] < 0:
		ans += negmax
	else:
		ans += posmax
	print(ans)


tc = int(input())
for t in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	pos_max = float('-inf')
	neg_min = float('-inf')
	pre = 0
	pn = []
	while i < n:
		if arr[i] > 0:
			pos_max = max(pos_max, arr[i])
			if pre == 1:
				if neg_min != float('-inf'):
					pn.append(neg_min)
					neg_min = float('-inf')
				pre = 0
		else:
			neg_min = max(neg_min, arr[i])
			if pre == 0:
				if pos_max != float('-inf'):
					pn.append(pos_max)
					pos_max = float('-inf')
				pre = 1
		i += 1
	if pre == 1 and neg_min != float('-inf'):
		pn.append(neg_min)
	elif pre == 0 and pos_max != float('inf'):
		pn.append(pos_max)
	print(sum(pn))


for t in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	s = 0
	i = 0
	while i < n:
		po = []
		while i < n and arr[i] > 0:
			po.append(arr[i])
			i += 1
		if len(po) > 0:
			s += max(po)
		ne = []
		while i < n and arr[i] < 0:
			ne.append(arr[i])
			i += 1
		if len(ne) > 0:
			s += max(ne)
	print(s)


for t 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:
		while j < n and a[i] * a[j] > 0:
			j += 1
		ans += max(a[i:j])
		i = j
	print(ans)


t = int(input())
for k in range(t):
	n = input()
	a = list(map(int, input().split()))
	pre = 0
	sub = 0
	sum = 0
	for i in a:
		if i > 0 >= pre:
			sub += 1
			sum += pre
			pre = i
		elif i < 0 <= pre:
			sub += 1
			sum += pre
			pre = i
		elif i > pre:
			pre = i
	sum += pre
	print(sum)


t = int(input())
for T in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	t = [a[0]]
	ans = 0
	for ai in a:
		if ai * t[0] > 0:
			t.append(ai)
		else:
			ans += max(t)
			t = [ai]
	ans += max(t)
	print(ans)


def maxsummaxsize(arr, n):
	ans = 0
	i = 0
	while i < n:
		if arr[i] < 0:
			temp = -10000000000
			while i < n and arr[i] < 0:
				temp = max(temp, arr[i])
				i += 1
		else:
			temp = 0
			while i < n and arr[i] > 0:
				temp = max(temp, arr[i])
				i += 1
		ans += temp
	return ans
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(maxsummaxsize(arr, n))


import math
t = int(input())
for x in range(t):
	tt = int(input())
	l = list(map(int, input().split()))
	ma = None
	p = True
	n = True
	sum = 0
	i = 0
	while i <= tt - 1:
		if l[i] > 0:
			if p:
				if ma == None or l[i] > ma:
					ma = l[i]
					i += 1
				else:
					i += 1
				if i == tt:
					sum += ma
			else:
				sum += ma
				ma = None
				p = True
			n = False
		else:
			if n:
				if ma is None or l[i] > ma:
					ma = l[i]
					i += 1
				else:
					i += 1
				if i == tt:
					sum += ma
			else:
				n = True
				sum += ma
				ma = None
			p = False
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	(*temparr,) = map(int, input().split())
	arr = []
	temp = temparr[0]
	flag = False
	if temp >= 0:
		flag = True
	for i in range(1, n):
		check = False
		if temparr[i] < 0 and (not flag):
			temp = max(temp, temparr[i])
		elif temparr[i] >= 0 and flag:
			temp = max(temp, temparr[i])
		else:
			arr.append(temp)
			temp = temparr[i]
			if temp < 0:
				flag = False
			else:
				flag = True
	else:
		arr.append(temp)
	print(sum(arr))


for i in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split()))
	(sm, mx, mn) = (0, 0, -1111111111111111)
	for j in range(n - 1):
		if nums[j] > 0 and nums[j + 1] < 0:
			if mx == 0:
				sm += nums[j]
			else:
				sm += mx
				mx = 0
		elif nums[j] < 0 and nums[j + 1] > 0:
			if mn == -1111111111111111:
				sm += nums[j]
			else:
				sm += mn
				mn = -1111111111111111
		elif nums[j] > 0 and nums[j + 1] > 0:
			p = max(nums[j], nums[j + 1])
			if p > mx:
				mx = p
		else:
			k = max(nums[j], nums[j + 1])
			if k > mn:
				mn = k
	if mn != -1111111111111111:
		sm += mn
	elif mx != 0:
		sm += mx
	if mx == 0 and nums[n - 1] > 0:
		sm += nums[n - 1]
	elif mn == -1111111111111111 and nums[n - 1] < 0:
		sm += nums[n - 1]
	print(sm)


def getsign(n):
	if n > 0:
		return 1
	else:
		return 0
t = int(input())
for i in range(t):
	n = int(input())
	data = list(map(int, input().split()))
	cursign = getsign(data[0])
	if cursign:
		curmax = data[0]
	else:
		curmin = data[0]
	ans = 0
	for i in range(1, len(data)):
		if getsign(data[i]) == cursign:
			if cursign:
				if data[i] > curmax:
					curmax = data[i]
			elif data[i] > curmin:
				curmin = data[i]
		else:
			if cursign:
				ans += curmax
				curmin = data[i]
			else:
				ans += curmin
				curmax = data[i]
			cursign = abs(cursign - 1)
	if cursign:
		ans += curmax
	else:
		ans += curmin
	print(ans)


for _ in range(int(input())):
	n = int(input())
	data = list(map(int, input().split()))
	result = 0
	if data[0] > 0:
		sign = 1
	else:
		sign = -1
	array = []
	for x in data:
		if sign == 1 and x > 0:
			array.append(x)
		if sign == 1 and x < 0:
			sign = -1
			result += max(array)
			array = [x]
		if sign == -1 and x < 0:
			array.append(x)
		if sign == -1 and x > 0:
			sign = 1
			result += max(array)
			array = [x]
	result += max(array)
	print(result)


def sgn(i, j):
	if i >= 0 and j >= 0 or (i < 0 and j < 0):
		return True
	else:
		False
test = int(input())
for t in range(test):
	n = int(input())
	nums = list(map(int, input().split()))
	i = 0
	ans = 0
	while i < n:
		j = i + 1
		while j < n and sgn(nums[i], nums[j]) == True:
			j += 1
		else:
			ans += max(nums[i:j])
			i = j
	print(ans)


test = int(input())
for i in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	summ = 0
	i = 0
	val = 0
	while i < n:
		if arr[i] > 0:
			val = arr[i]
			while i < n and arr[i] > 0:
				val = max(val, arr[i])
				i = i + 1
		else:
			val = arr[i]
			while i < n and arr[i] < 0:
				val = max(val, arr[i])
				i = i + 1
		summ = summ + val
	print(summ)


import sys
readline = sys.stdin.readline
write = sys.stdin.write
for _ in range(int(readline())):
	n = int(readline())
	a = list(map(int, readline().split()))
	i = 0
	sum = 0
	while i < n:
		cur = a[i]
		j = i
		while j < n and (a[i] > 0) == (a[j] > 0):
			cur = max(cur, a[j])
			j += 1
		sum += cur
		i = j
	print(sum)


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	neg = []
	pos = []
	final = []
	for j in range(len(arr)):
		if arr[j] < 0:
			if len(pos) > 0:
				final.append(max(pos))
				pos = []
			neg.append(arr[j])
		else:
			if len(neg) > 0:
				final.append(max(neg))
				neg = []
			pos.append(arr[j])
	if len(pos) > 0:
		final.append(max(pos))
	else:
		final.append(max(neg))
	print(sum(final))


def sign(a, b):
	if abs(a) == a and abs(b) == b:
		return 0
	elif abs(a) != a and abs(b) != b:
		return 0
	else:
		return 1
t = int(input())
for _ in range(t):
	n = int(input())
	items = list(map(int, input().split()))
	res = 0
	m = [items[0]]
	for i in range(1, n):
		if sign(items[i], items[i - 1]) == 0:
			m.append(items[i])
		else:
			res += max(m)
			m = [items[i]]
	res += max(m)
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	sum = 0
	i = 0
	flag = 0
	l = []
	while i < n:
		l.clear()
		if a[i] < 0:
			while i < n and a[i] < 0:
				l.append(a[i])
				i = i + 1
		else:
			while i < n and a[i] > 0:
				l.append(a[i])
				i = i + 1
		sum = sum + max(l)
	print(sum)


cases = int(input())
flag = 0
minn = 0
while cases:
	arr = []
	temp = []
	leng = int(input())
	str = input()
	temp = str.split(' ')
	for i in temp:
		arr.append(int(i))
	if arr[0] > 0:
		flag = 1
		big = arr[0]
		small = 0
	else:
		flag = -1
		small = arr[0]
		big = 0
	x = 0
	touch = 0
	arr.append(0)
	for x in range(0, len(arr) - 1):
		if x != touch:
			continue
		elif flag == 1:
			meh = x
			big = 0
			count = 0
			while arr[meh + count] > 0:
				if arr[meh + count] > big:
					big = arr[meh + count]
				count = count + 1
			minn = minn + big
			touch = meh + count
			flag = -1
		else:
			na = x
			count = 0
			small = -100000000000
			while arr[na + count] < 0:
				if arr[na + count] > small:
					small = arr[na + count]
				count = count + 1
			minn = minn + small
			if count == 1:
				touch = na + count
			else:
				touch = na + count
			flag = 1
		if arr[x] == 0:
			break
	print(minn)
	cases = cases - 1
	minn = 0


testCases = int(input())
for testCase in range(testCases):
	l = int(input())
	myList = list((0,))
	for x in input().split():
		myList.append(int(x))
	myList.append(0)
	status = maxNumber = maxSum = int(0)
	for i in range(l + 2):
		if myList[i] > 0:
			if status == 1:
				maxNumber = max(maxNumber, myList[i])
			else:
				maxSum += maxNumber
				status = 1
				maxNumber = myList[i]
		elif myList[i] < 0:
			if status == -1:
				maxNumber = max(maxNumber, myList[i])
			else:
				maxSum += maxNumber
				status = -1
				maxNumber = myList[i]
		else:
			maxSum += maxNumber
	print(maxSum)


t = int(input())
l = []
for i in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	l.append(s)
for j in l:
	if len(j) == 1:
		print(j[0])
	else:
		sum = 0
		for p in range(0, len(j) - 1):
			if j[p] * j[p + 1] < 0:
				sum += j[p]
			else:
				j[p + 1] = max([j[p], j[p + 1]])
		if j[-1] * j[-2] > 0:
			sum += j[p + 1]
		else:
			sum += j[-1]
		print(sum)


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	sum = 0
	i = 0
	while i >= 0 and i < n:
		j = i
		cur = lis[i]
		while j < n and abs(lis[i] + lis[j]) == abs(lis[i]) + abs(lis[j]):
			cur = max(cur, lis[j])
			j += 1
		sum += cur
		i = j
	print(sum)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	k = -1
	for i in range(n - 1):
		if l[i] * l[i + 1] < 0:
			ans = ans + max(l[k + 1:i + 1])
			k = i
	if k < n - 1:
		ans = ans + max(l[k + 1:])
	print(ans)


def findpos(x, n):
	val = arr[x]
	while x < n and arr[x] > 0:
		val = max(arr[x], val)
		x += 1
	return [val, x]

def findneg(x, n):
	val = arr[x]
	while x < n and arr[x] < 0:
		val = max(arr[x], val)
		x += 1
	return [val, x]
T = int(input())
for t in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	if arr[0] > 0:
		pos = True
	i = 0
	summ = 0
	while i < n:
		if arr[i] > 0:
			(s, i) = findpos(i, n)
			summ += s
		else:
			(s, i) = findneg(i, n)
			summ += s
	print(summ)


def problem1():
	a = int(input())
	k = 4
	while a % (k - 1) != 0:
		k *= 2
	print(a // (k - 1))

def problem2():
	a = int(input())
	if a % 4 == 2:
		print('NO')
		return
	print('YES')
	x = []
	for i in range(1, a // 2 + 1):
		x.append(2 * i)
	for i in range(a // 4):
		x.append(x[i] - 1)
	for i in range(a // 4, a // 2):
		x.append(x[i] + 1)
	print(*x)

def problem3():
	l = int(input())
	a = list(map(int, input().split()))
	k = -1 * min(a)
	i = 0
	s = 0
	while i < l:
		mp = 0
		mn = k + 1
		while i < l and a[i] > 0:
			mp = max(a[i], mp)
			i += 1
		while i < l and a[i] < 0:
			mn = min(abs(a[i]), mn)
			i += 1
		s += mp
		if mn != k + 1:
			s -= mn
	print(s)

def problem4():
	(l, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	s = []
	t = []
	u = []
	for i in range(l // 2):
		s.append(a[i] + a[l - i - 1])
		t.append(min(1 + a[i], 1 + a[l - i - 1]))
		u.append(max(k + a[i], k + a[l - i - 1]))
	v = list(set([(s.count(i), i) for i in s]))
cases = int(input())
for _ in range(cases):
	problem3()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	max_sum = a[0]
	max_lenght = 1
	counter = 1
	cur_min = a[0]
	sum = a[0]
	for i in range(1, len(a)):

		def func(i):
			global sum, a, cur_min
			if a[i - 1] < 0 and a[i] < 0 and (cur_min < a[i]):
				sum -= cur_min
				sum += a[i]
				cur_min = a[i]
			elif a[i - 1] > 0 and a[i] > 0 and (cur_min < a[i]):
				sum -= cur_min
				sum += a[i]
				cur_min = a[i]
			elif a[i - 1] < 0 and a[i] > 0 or (a[i - 1] > 0 and a[i] < 0):
				sum += a[i]
				cur_min = a[i]
		func(i)
	print(sum)


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
from math import log2, inf
for _ in range(I()[0]):
	n = I()[0]
	a = I()
	ans = [a[0]]
	maxx = -inf
	for i in a[1:]:
		if ans[-1] * i < 0:
			ans.append(i)
		else:
			ans[-1] = max(ans[-1], i)
	print(sum(ans))


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	round1sum = 0
	i = 0
	while i < n:
		if arr[i] > 0:
			maxsofar = 0
			while i < n and arr[i] > 0:
				maxsofar = max(arr[i], maxsofar)
				i += 1
			round1sum += maxsofar
		else:
			maxsofar = -sys.maxsize
			while i < n and arr[i] < 0:
				maxsofar = max(maxsofar, arr[i])
				i += 1
			round1sum += maxsofar
	print(round1sum)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	arr = []
	(prev, sign) = ([a[0]], a[0] < 0)
	for i in range(1, n):
		s = a[i] < 0
		if s == sign:
			prev.append(a[i])
		else:
			arr.append(prev)
			prev = [a[i]]
			sign = not sign
	arr.append(prev)
	k = len(arr)
	if k == 1:
		print(max(arr[0]))
		return
	else:
		ans = 0
		for i in range(k):
			ans += max(arr[i])
		print(ans)
for _ in range(int(input())):
	main()


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 case():
	n = I()
	a = li()
	i = 0
	s = 0
	if a[i] > 0:
		f = 1
	else:
		f = 0
	while i < n:
		if f:
			j = i
			k = a[i]
			while j < n and a[j] > 0:
				k = max(k, a[j])
				j += 1
			s += k
			f = 0
		else:
			j = i
			k = a[i]
			while j < n and a[j] < 0:
				k = max(a[j], k)
				j += 1
			s += k
			f = 1
		i = j
	print(s)
for _ in range(int(input())):
	case()


def F(a, lst):
	ans = 0
	cur = []
	lst.append(1)
	for i in range(1, len(lst)):
		if abs(lst[i]) / lst[i] == abs(lst[i - 1]) / lst[i - 1]:
			cur.append(lst[i - 1])
		else:
			cur.append(lst[i - 1])
			ans += max(cur)
			cur = []
	if cur != []:
		cur.append(lst[-1])
		ans += max(cur)
		cur = []
	return ans
n = int(input())
for i in range(n):
	a = int(input())
	lst = list(map(int, input().split()))
	print(F(a, lst))


import sys
lines = sys.stdin.readlines()
T = int(lines[0].strip())
for t in range(T):
	n = int(lines[2 * t + 1].strip())
	nums = list(map(int, lines[2 * t + 2].strip().split(' ')))
	res = []
	tmp = nums[0]
	for i in range(1, len(nums)):
		if nums[i] * nums[i - 1] > 0:
			tmp = max(tmp, nums[i])
		else:
			res.append(tmp)
			tmp = nums[i]
	res.append(tmp)
	print(sum(res))


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


def sign(num):
	if num > 0:
		return 1
	else:
		return 0
t = int(input())
while t != 0:
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	ans = 0
	i = 0
	while i < n:
		j = i + 1
		maxNum = arr[i]
		while j < n and sign(arr[i]) == sign(arr[j]):
			maxNum = max(maxNum, arr[j])
			j += 1
		ans += maxNum
		i = j - 1
		i += 1
	print(ans)
	t -= 1


t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	(num, t) = (0, a[0])
	for i in range(n):
		if a[i] * t > 0:
			t = max(a[i], t)
		else:
			num += t
			t = a[i]
	ans.append(num + t)
print('\n'.join(map(str, ans)))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(size, RESULT) = (0, 0)
	minn = -float('inf')
	(i, f) = (0, 0)
	if l[0] < 0:
		f = 1
	ans = 0
	while i < n:
		if l[i] > 0:
			RESULT = 0
			while i < n and l[i] > 0:
				RESULT = max(RESULT, l[i])
				i += 1
			ans += RESULT
		elif l[i] < 0:
			ANSWER1 = -float('inf')
			while i < n and l[i] < 0:
				ANSWER1 = max(ANSWER1, l[i])
				i += 1
			ans += ANSWER1
	print(ans)


def sign(n):
	if n > 0:
		return 1
	return 0
for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	p = l[0]
	s = l[0]
	for i in l:
		if sign(i) == sign(p):
			if p < i:
				s += i - p
				p = i
		else:
			s += i
			p = i
	print(s)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	st = l[0]
	su = l[0]
	for i in range(1, n):
		if st > 0 and l[i] < 0 or (st < 0 and l[i] > 0):
			su += l[i]
			st = l[i]
		elif st < l[i]:
			su = su - st + l[i]
			st = l[i]
	print(su)


def solve():
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = []
	if n == 1:
		print(*l1)
		return
	d = l1[0]
	if d < 0:
		l2.append(d)
		flag = 0
	else:
		l2.append(d)
		flag = 1
	for i in range(1, n):
		v = l1[i]
		if v < 0 and flag == 1:
			l2.append(v)
			flag = 0
		elif v > 0 and flag == 0:
			l2.append(v)
			flag = 1
		elif v < 0 and flag == 0:
			min1 = max(v, l2[-1])
			l2[-1] = min1
		else:
			max1 = max(v, l2[-1])
			l2[-1] = max1
	print(sum(l2))
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	current_sign = -l[0] / abs(l[0])
	current_max = l[0]
	for i in l:
		if i * current_sign < 0:
			current_sign = -current_sign
			current_max = i
			s += i
		if i > current_max:
			s += i - current_max
			current_max = i
	print(s)


t = int(input())
for __ in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	w = []
	a = L[0]
	sum1 = 0
	for i in range(n):
		if L[i] * a < 0:
			sum1 += a
			a = L[i]
			if i == n - 1:
				sum1 += a
			continue
		elif L[i] * a > 0:
			if a > 0 and a < L[i]:
				a = L[i]
			elif a < 0 and a < L[i]:
				a = L[i]
		if i == n - 1:
			sum1 += a
	print(sum1)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if l[0] < 0:
		sign = -1
	else:
		sign = 1
	sum1 = l[0]
	tr = 0
	tr1 = 0
	for i in range(len(l)):
		if l[i] > 0:
			tr1 = l[i]
			break
	for i in range(len(l)):
		if l[i] < 0:
			tr = l[i]
			break
	for i in range(1, len(l)):
		if l[i] > 0:
			if sign == -1:
				sign = 1
				sum1 += l[i]
				tr1 = l[i]
			elif l[i] >= tr1:
				sum1 += l[i] - tr1
				tr1 = l[i]
		elif sign == 1:
			sign = -1
			sum1 += l[i]
			tr = l[i]
		elif l[i] >= tr:
			sum1 += l[i] - tr
			tr = l[i]
	print(sum1)


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	strt = 0
	if l[strt] > 0:
		sig = 0
	else:
		sig = 1
	for i in range(1, len(l)):
		if l[i] > 0:
			sig_i = 0
		else:
			sig_i = 1
		if sig != sig_i:
			ans.append(max(l[strt:i]))
			strt = i
			sig = sig_i
	ans.append(max(l[strt:]))
	print(sum(ans))
	t -= 1


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	j = 0
	while j < n:
		if a[j] > 0:
			if j > 0 and a[j - 1] > 0:
				j += 1
			else:
				pos = a[j]
				flag = 0
				for k in range(j + 1, n):
					flag = 1
					if a[k] > 0:
						if pos < a[k]:
							pos = a[k]
					else:
						break
				if flag == 0:
					j += 1
				else:
					j = k
				l.append(pos)
		if j < n and a[j] < 0:
			if j > 0 and a[j - 1] < 0:
				j += 1
			else:
				neg = a[j]
				flag = 0
				for k in range(j + 1, n):
					flag = 1
					if a[k] < 0:
						if neg < a[k]:
							neg = a[k]
					else:
						break
				if flag == 0:
					j += 1
				else:
					j = k
				l.append(neg)
	print(sum(l))


def foo(size, arr):
	toggle = True if arr[0] > 0 else False
	rarr = 0
	i = 0
	while i < size:
		if toggle:
			(j, m) = (i + 1, arr[i])
			while j < size and arr[j] > 0:
				m = max(m, arr[j])
				j += 1
			rarr += m
			i = j
			toggle = False
		else:
			j = i + 1
			m = arr[i]
			while j < size and arr[j] < 0:
				m = max(m, arr[j])
				j += 1
			rarr += m
			i = j
			toggle = True
	print(rarr)
test = int(input())
while test:
	size = int(input())
	data = [int(i) for i in input().split()]
	foo(size, data)
	test -= 1


a = [0] * int(200000.0)
q = int(input())
while q > 0:
	q -= 1
	n = int(input())
	a = list(map(int, input().split()))
	res = a[0]
	ans = 0
	for i in range(n):
		if res * a[i] < 0:
			ans += res
			res = a[i]
		else:
			res = max(res, a[i])
	print(ans + res)


p = int(input())
for z in range(p):
	n = int(input())
	s = []
	a = 0
	b = 0
	more = False
	less = False
	f = input().split()
	for i in range(n):
		s.append(int(f[i]))
		if int(f[i]) > 0 and more == False:
			more = True
			a = i
		if int(f[i]) < 0 and less == False:
			less = True
			b = i
	(more, less) = (a, b)
	prev = True
	mc = []
	mg = []
	slic = []
	for i in range(more, n):
		a = s[i]
		if a > 0 and (not prev) or (a < 0 and prev):
			mc.append(slic)
			slic = [a]
			if not prev:
				prev = True
			else:
				prev = False
		else:
			slic.append(a)
	if slic != []:
		mc.append(slic)
		slic = []
	prev = False
	for i in range(less, n):
		a = s[i]
		if a > 0 and (not prev) or (a < 0 and prev):
			mg.append(slic)
			slic = [a]
			if not prev:
				prev = True
			else:
				prev = False
		else:
			slic.append(a)
	if slic != []:
		mg.append(slic)
	s = []
	if len(mc) > len(mg):
		s.append(mc)
	elif len(mg) > len(mc):
		s.append(mg)
	else:
		s.append(mc)
		s.append(mg)
	summ = []
	for i in s:
		x = 0
		for j in i:
			if j != []:
				x += max(j)
		summ.append(x)
	print(max(summ))


from functools import reduce
import sys

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(delimiter=' '):
		return input().split(delimiter)

	@staticmethod
	def list_int(delimiter=' '):
		return list(map(int, input().split(delimiter)))

def main():
	t = Read.int()
	for _ in range(t):
		n = Read.int()
		a = Read.list_int()
		sign = -1 if a[0] < 0 else 1
		i = 0
		res = []
		tmp = 0
		while i < n:
			if a[i] * sign > 0:
				i += 1
			elif a[tmp:i]:
				res.append(max(a[tmp:i]))
				tmp = i
				sign = -sign
			else:
				i += 1
		res.append(max(a[tmp:i]))
		print(sum(res))
main()


testcase = int(input())
for t in range(testcase):
	n = int(input())
	arr = list(map(lambda x: int(x), input().split()))
	res = 0
	temp = arr[0]
	for i in range(1, n, 1):
		if arr[i] * temp < 0:
			res += temp
			temp = arr[i]
		elif arr[i] > temp:
			temp = arr[i]
	res += temp
	print(res)


import fileinput
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	max_pos = 0
	pos_len = 0
	max_neg = 0
	neg_len = 0
	for x in a:
		if x > 0:
			if pos_len < neg_len + 1 or (pos_len == neg_len + 1 and max_pos < max_neg + x):
				pos_len = neg_len + 1
				max_pos = max_neg + x
		elif neg_len < pos_len + 1 or (pos_len + 1 == neg_len and max_neg < max_pos + x):
			neg_len = pos_len + 1
			max_neg = max_pos + x
	if pos_len > neg_len or (pos_len == neg_len and max_pos > max_neg):
		print(max_pos)
	else:
		print(max_neg)


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


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	(left, right) = (0, 0)
	for i in range(1, n):
		right = i
		if a[i - 1] > 0 and a[i] < 0:
			if len(b) == 0:
				b.append(max(a[left:right]))
			else:
				b.append(max(a[left:right]))
			left = i
		elif a[i - 1] < 0 and a[i] > 0:
			if len(b) == 0:
				b.append(max(a[left:right]))
			else:
				b.append(max(a[left:right]))
			left = i
	if len(b) > 0:
		if left == right:
			b.append(a[left])
		else:
			b.append(max(a[left:]))
		print(sum(b))
	else:
		print(max(a))


t = int(input())

def fun(n, a):
	s = 0
	i = 0
	se = 0
	while i < n:
		k = 0
		if se == 0:
			while i < n and a[i] > 0:
				k = max(k, a[i])
				i += 1
			s += k
			se += 1
		else:
			k = -10 ** 9
			k -= 10
			while i < n and a[i] < 0:
				k = max(k, a[i])
				i += 1
			s += k
			se -= 1
	print(s)
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	fun(n, a)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	j = 0
	s = 0
	while i < n:
		while j < n and i < n and (a[i] * a[j] > 0):
			j += 1
		s += max(a[i:j])
		i = j
	print(s)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l1 = [a[0]]
	s = []
	for j in range(n - 1):
		if a[j] > 0 and a[j + 1] > 0:
			l1.append(a[j + 1])
		elif a[j] > 0 and a[j + 1] < 0:
			s.append(max(l1))
			l1 = []
			l1.append(a[j + 1])
		elif a[j] < 0 and a[j + 1] > 0:
			s.append(max(l1))
			l1 = []
			l1.append(a[j + 1])
		else:
			l1.append(a[j + 1])
	s.append(max(l1))
	print(sum(s))


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	(i, ans) = (0, 0)
	while i < n:
		j = i + 1
		mx = a[i]
		while j < n:
			if a[i] > 0 and a[j] > 0 or (a[i] < 0 and a[j] < 0):
				mx = max(mx, a[j])
				j += 1
			else:
				break
		i = j
		ans += mx
	print(ans)


t = int(input())

def sign(n):
	return '+' if n > 0 else '-'
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	res = 0
	tmp = a[0]
	maxim = a[0]
	j = 0
	while j < n:
		x = sign(a[j])
		if x == sign(tmp):
			if maxim < a[j]:
				maxim = a[j]
		else:
			res += maxim
			maxim = a[j]
			tmp = a[j]
		j += 1
	res += maxim
	print(res)


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()))

def sign(x):
	if x < 0:
		return True
	return False
t = iinput()
while t:
	t -= 1
	n = iinput()
	a = linput()
	temp = a[0]
	ans = 0
	for i in range(1, n):
		if sign(a[i]) == sign(temp):
			temp = max(temp, a[i])
		else:
			ans += temp
			temp = a[i]
	ans += temp
	print(ans)


def csolution():
	t = int(input())
	while t != 0:
		l = int(input())
		giv = input()
		giv = giv.split()
		giv = list(map(int, giv))
		leny = 1
		ans = []
		mem = []
		mem.append(giv[0])
		for (num, i) in enumerate(giv):
			if num == 0:
				continue
			if i > 0 and giv[num - 1] > 0 or (i < 0 and giv[num - 1] < 0):
				mem.append(i)
				continue
			else:
				ans.append(max(mem))
				leny += 1
				mem = []
				mem.append(i)
		ans.append(max(mem))
		sumy = 0
		for a in ans:
			sumy += a
		print(sumy)
		t -= 1
csolution()


from sys import maxsize

def Nav(n, l):
	m = maxsize * 2 + 1
	(maxn, minn) = (-m, -m)
	sum = 0
	i = 0
	f = 0
	while i < n:
		if l[i] < 0:
			f = 0
			while l[i] < 0 or f == 0:
				maxn = max(maxn, l[i])
				i += 1
				f = 1
				if i > n - 1:
					break
			sum += maxn
		else:
			f = 0
			while l[i] > 0 or f == 0:
				minn = max(minn, l[i])
				i += 1
				f = 1
				if i > n - 1:
					break
			sum += minn
		(maxn, minn) = (-m, -m)
	return sum
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(Nav(n, l))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	prev = arr[0]
	maxpo = arr[0]
	maxne = min(arr)
	sum1 = 0
	x = 0
	y = 0
	for i in range(1, n):
		if arr[i] > 0:
			if prev > 0:
				if y == 0:
					maxpo = prev
				if maxpo < arr[i]:
					maxpo = arr[i]
					prev = arr[i]
					y = 1
			else:
				sum1 = sum1 + prev
				maxpo = arr[i]
				prev = arr[i]
				y = 0
		elif arr[i] < 0:
			if prev < 0:
				if x == 0:
					maxne = prev
				if maxne < arr[i]:
					maxne = arr[i]
					prev = arr[i]
					x = 1
			else:
				sum1 = sum1 + prev
				maxne = arr[i]
				prev = arr[i]
				x = 0
	sum1 = sum1 + prev
	print(sum1)


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


t = int(input())
for i in range(1, t + 1):
	n = int(input())
	l = list(map(int, input().split()))
	i = 0
	s = 0
	while i < n:
		a = l[i]
		k = 0
		if l[i] < 0:
			k = 1
		i += 1
		while i < n:
			if k == 1:
				if l[i - 1] < 0 and l[i] < 0:
					a = max(l[i], a)
				else:
					i -= 1
					break
			elif l[i - 1] > 0 and l[i] > 0:
				a = max(l[i], a)
			else:
				i -= 1
				break
			i += 1
		s += a
		i += 1
	print(s)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = 0
	if a[0] > 0:
		flag = 1
	else:
		flag = 0
	maxi = a[0]
	maxsum = 0
	for i in range(n):
		if flag == 0 and a[i] > 0:
			flag = 1
			maxsum += maxi
			maxi = a[i]
			continue
		if flag == 1 and a[i] < 0:
			flag = 0
			maxsum += maxi
			maxi = a[i]
			continue
		if a[i] > maxi:
			maxi = a[i]
	maxsum += maxi
	print(maxsum)


t = int(input())
for z in range(t):
	ans = []
	sign = -1
	ma = -1000000000000000000000
	n = int(input())
	a = list(map(int, input().split()))
	if a[0] < 0:
		sign = -1
	else:
		sign = 1
	ls = 0
	for i in a:
		ls = i
		if i * sign < 0:
			ans.append(ma)
			ma = -1000000000000000000000
			sign = -sign
		ma = max(ma, i)
	if ls * sign > 0:
		ans.append(ma)
	print(sum(ans))


import sys
import math
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	pos = 0
	while pos < n:
		if a[pos] > 0:
			break
		pos += 1
	pos_sum = 0
	pos_len = 0
	pos_flag = True
	while pos < n:
		if (a[pos] > 0) == pos_flag:
			pos_len += 1
			j_pos = pos
			tmp_max = a[j_pos] + pos_sum
			j_pos += 1
			while j_pos < n and (a[j_pos] > 0) == pos_flag:
				if tmp_max < a[j_pos] + pos_sum:
					tmp_max = a[j_pos] + pos_sum
				j_pos += 1
			pos_sum = tmp_max
			pos = j_pos
			pos_flag = not pos_flag
		else:
			pos += 1
	neg = 0
	while neg < n:
		if a[neg] < 0:
			break
		neg += 1
	neg_sum = 0
	neg_len = 0
	neg_flag = True
	while neg < n:
		if (a[neg] < 0) == neg_flag:
			neg_len += 1
			j_neg = neg
			tmp_max = a[j_neg] + neg_sum
			j_neg += 1
			while j_neg < n and (a[j_neg] < 0) == neg_flag:
				if tmp_max < a[j_neg] + neg_sum:
					tmp_max = a[j_neg] + neg_sum
				j_neg += 1
			neg_sum = tmp_max
			neg = j_neg
			neg_flag = not neg_flag
		else:
			neg += 1
	if neg_len > pos_len:
		print(neg_sum)
	elif neg_len < pos_len:
		print(pos_sum)
	else:
		print(max(pos_sum, neg_sum))


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


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

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

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 res

def upper_bound(a, x, lo=0):
	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:
		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:
			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):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	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(1000000.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 str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')

def same_sign(x, y):
	if x < 0 and y < 0:
		return True
	if x > 0 and y > 0:
		return True
	return False
for _ in range(int(input())):
	n = int(input())
	a = int_array()
	ans = 0
	vec = []
	for i in a:
		if not vec:
			vec.append(i)
		elif same_sign(i, vec[-1]):
			vec.append(i)
		else:
			ans += max(vec)
			vec.clear()
			vec.append(i)
	if vec:
		ans += max(vec)
	print(ans)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	max_ = arr[0]
	s = 0
	for i in range(1, len(arr)):
		if arr[i] * arr[i - 1] > 0:
			if arr[i] > max_:
				max_ = arr[i]
			continue
		else:
			s += max_
			max_ = arr[i]
	print(s + max_)


def max_sum(a):
	i = 0
	sum = 0
	while i < len(a):
		maxx = 0
		while i <= len(a):
			if i == len(a) or a[i] < 0:
				sum += maxx
				break
			maxx = max(a[i], maxx)
			i += 1
		if i == len(a):
			break
		minn = a[i]
		while i <= len(a):
			if i == len(a) or a[i] > 0:
				sum += minn
				break
			minn = max(a[i], minn)
			i += 1
	return sum
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	print(max_sum(a))


T = int(input())
for case in range(T):
	N = int(input())
	data = list(map(int, input().split()))
	ans = 0
	maxs = data[0]
	if data[0] > 0:
		sign = 1
	else:
		sign = -1
	for elt in data:
		if elt > 0:
			nsign = 1
		else:
			nsign = -1
		if sign == nsign:
			maxs = max(maxs, elt)
		else:
			sign = nsign
			ans += maxs
			maxs = elt
	ans += maxs
	print(ans)


a = int(input())
for i in range(0, a):
	b = int(input())
	c = list(map(int, input().split()))
	j = 0
	hello = []
	bye = []
	while j != b - 1:
		switch = 0
		if c[j] * c[j + 1] < 0:
			hello.append(c[j])
			switch = 1
		if switch == 0:
			hello.append(c[j])
		if switch == 1:
			if hello[0] > 0:
				bye.append(max(hello))
				hello = []
				switch = 0
			else:
				bye.append(max(hello))
				hello = []
				switch = 0
		j += 1
	if len(hello) == 0:
		bye.append(c[b - 1])
	else:
		hello.append(c[b - 1])
		if hello[0] > 0:
			bye.append(max(hello))
		else:
			bye.append(max(hello))
	print(sum(bye))


t = int(input())
for _ in range(t):
	input()
	a = list(map(int, input().split()))
	b = a[0]
	sum = 0
	for i in a:
		if i // b < 0:
			sum += b
			b = i
		if i > b:
			b = i
	print(sum + b)


def check(x, y):
	if x > 0 and y > 0 or (x < 0 and y < 0):
		return True
	else:
		return False
for i in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split()]
	summ = 0
	cnt = arr[0]
	for i in range(n - 1):
		if arr[i] * arr[i + 1] < 0:
			summ += cnt
			cnt = arr[i + 1]
		elif arr[i + 1] > cnt:
			cnt = arr[i + 1]
	print(summ + cnt)


t = int(input())
j = 0
while j < t:
	n = int(input())
	array = list(map(int, input().split()))
	add = 0
	diff = 0
	maxi = 0
	mini = 99999999999999
	flag = 2
	for i in array:
		if i > 0:
			maxi = max(maxi, i)
			if flag == 0:
				diff += mini
				mini = 99999999999999
				flag = 1
		else:
			i = i * -1
			mini = min(mini, i)
			add += maxi
			maxi = 0
			flag = 0
	if mini == 99999999999999:
		print(add + maxi - diff)
	else:
		print(add + maxi - mini - diff)
	j += 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	(tot, cnt) = (0, 0)
	(strt, end) = (-1, -1)
	neg = True
	i = 0
	while i != n:
		if neg == True and arr[i] < 0:
			if strt == -1:
				strt = i
			else:
				end = i
		elif neg == True and arr[i] > 0:
			if strt != -1 and end == -1:
				tot += arr[strt]
				cnt += 1
			elif strt != -1 and end != -1:
				tot += max(arr[strt:end + 1])
				cnt += 1
			strt = i
			end = -1
			neg = False
		if neg == False and arr[i] > 0:
			if strt == -1:
				strt = i
			else:
				end = i
		elif neg == False and arr[i] < 0:
			if strt != -1 and end == -1:
				tot += arr[strt]
				cnt += 1
			elif strt != -1 and end != -1:
				tot += max(arr[strt:end + 1])
				cnt += 1
			strt = i
			end = -1
			neg = True
		i += 1
	if strt != -1 and end == -1:
		tot += arr[strt]
		cnt += 1
	elif strt != -1 and end != -1:
		tot += max(arr[strt:end + 1])
		cnt += 1
	print(tot)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(i, ans) = [0] * 2
	while i < n:
		if arr[i] < 0:
			mini = arr[i]
			while i < n and arr[i] < 0:
				if arr[i] >= mini:
					mini = arr[i]
				i += 1
			ans += mini
		else:
			flag = True
			maxi = arr[i]
			while i < n and arr[i] > 0:
				if arr[i] >= maxi:
					maxi = arr[i]
				i += 1
			ans += maxi
	print(ans)


def read():
	return list(map(lambda x: int(x), input().split()))
T = read()[0]
for t in range(T):
	n = read()[0]
	nums = read()
	sign = nums[0]
	valid = []
	for i in range(n):
		if sign * nums[i] > 0:
			sign = max(sign, nums[i])
		else:
			valid.append(sign)
			sign = nums[i]
	valid.append(sign)
	valid_n = len(valid)
	print(sum(valid))


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	prev = 0
	i = 0
	ans = 0
	while i < n:
		prev = 1 if a[i] > 0 else -1
		mx = -1000000007
		f = False
		while i < n and a[i] * prev > 0:
			f = True
			mx = max(a[i], mx)
			i += 1
		if f:
			ans += mx
	print(ans)


import sys, collections
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	maxim = arr[0]
	if arr[0] < 0:
		flag = 'neg'
	else:
		flag = 'pos'
	for i in range(1, n):
		if arr[i] > 0:
			if flag == 'neg':
				ans += maxim
				maxim = arr[i]
			maxim = max(maxim, arr[i])
			flag = 'pos'
		elif arr[i] < 0:
			if flag == 'pos':
				ans += maxim
				maxim = arr[i]
			maxim = max(maxim, arr[i])
			flag = 'neg'
	print(ans + maxim)


def subsequence():
	n = int(input())
	arr = list(map(int, input().split()))
	postive = True if arr[0] > 0 else False
	max_num = arr[0]
	answer = 0
	for num in range(n):
		if postive != (arr[num] > 0):
			answer += max_num
			max_num = arr[num]
			postive = not postive
		elif arr[num] > max_num:
			max_num = arr[num]
	answer += max_num
	return answer
t = int(input())
for test_case in range(t):
	print(subsequence())


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


import sys
import os
from io import IOBase, BytesIO
import math
mod = 10 ** 9 + 7
pie = 3.1415926536

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def ain():
	return list(map(int, sin().split()))

def sin():
	return input().strip()

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

def read2DIntArray(row, col):
	arr = []
	for i in range(0, row):
		temp = list(map(int, sin().split()))
		arr.append(temp)
	return arr

def read2DCharArray(row, col):
	arr = []
	for i in range(0, row):
		temp = str(sin())
		arr.append(temp)
	return arr

def smallestNumber(n):
	lst = list(str(n))
	lst.sort()
	tmp = ''
	for (i, n) in enumerate(lst):
		if n != '0':
			tmp = lst.pop(i)
			break
	return str(tmp) + ''.join(lst)

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
	lst = []
	for p in range(n + 1):
		if prime[p]:
			lst.append(p)
	return lst

def nCr(n, r):
	a = 1
	b = 1
	c = 1
	for i in range(1, n + 1):
		c *= i
	for i in range(1, r + 1):
		b *= i
	for i in range(1, n - r + 1):
		a *= i
	return c // (a * b)

def sieveOfEratosthenes1(N, s):
	prime = [False] * (N + 1)
	for i in range(2, N + 1, 2):
		s[i] = 2
	for i in range(3, N + 1, 2):
		if prime[i] == False:
			s[i] = i
			for j in range(i, int(N / i) + 1, 2):
				if prime[i * j] == False:
					prime[i * j] = True
					s[i * j] = i

def generatePrimeFactors(N):
	s = [0] * (N + 1)
	sieveOfEratosthenes1(N, s)
	curr = s[N]
	cnt = 1
	factors = []
	power = []
	while N > 1:
		N //= s[N]
		if curr == s[N]:
			cnt += 1
			continue
		factors.append(curr)
		power.append(cnt)
		curr = s[N]
		cnt = 1
	return (factors, power)

def sign(x):
	return int(x > 0)

def main():
	for _ in range(int(sin())):
		n = int(sin())
		a = ain()
		sm = 0
		prevSign = sign(a[0])
		temp = a[0]
		for i in range(1, n):
			if sign(a[i]) == prevSign:
				temp = max(temp, a[i])
			else:
				sm += temp
				temp = a[i]
				prevSign = sign(a[i])
		print(sm + temp)
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))

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


import math
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 1
	arr = [a[0]]
	while i < n:
		if arr[-1] < 0 and a[i] < 0:
			if arr[-1] < a[i]:
				del arr[-1]
				arr.append(a[i])
		elif arr[-1] >= 0 and a[i] >= 0:
			if arr[-1] < a[i]:
				del arr[-1]
				arr.append(a[i])
		else:
			arr.append(a[i])
		i += 1
	print(sum(arr))


import sys
input = sys.stdin.readline
from collections import defaultdict as dd, deque as dq
t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	if l[0] > 0:
		lol = 1
	i = 0
	su = 0
	while i < n:
		mx = -1
		mi = -10000000000
		if l[i] > 0:
			while i < n and l[i] > 0:
				mx = max(mx, l[i])
				i += 1
			su += mx
		else:
			while i < n and l[i] < 0:
				mi = max(mi, l[i])
				i += 1
			su += mi
	print(su)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	final = a[-1]
	a.append(-final)
	res = 0
	maxscore = a[0]
	for i in range(1, len(a)):
		if a[i] * a[i - 1] > 0:
			maxscore = max(a[i], maxscore)
		else:
			res += maxscore
			maxscore = a[i]
	print(res)


t = int(input())

def isPos(z):
	if z < 0:
		return True
	else:
		return False
for _ in range(t):
	n = int(input())
	a = list(map(int, input().rstrip().split(' ')))
	sumMax = 0
	i = 0
	while i < n:
		if isPos(a[i]):
			mx = a[i]
			while i < n and isPos(a[i]):
				if a[i] > mx:
					mx = a[i]
				i += 1
			sumMax += mx
		else:
			mn = a[i]
			while i < n and (not isPos(a[i])):
				if a[i] > mn:
					mn = a[i]
				i += 1
			sumMax += mn
	print(sumMax)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	alter = []
	for i in range(n):
		if len(alter) > 0 and a[i] / alter[-1] > 0:
			if a[i] > alter[-1]:
				alter[-1] = a[i]
		else:
			alter.append(a[i])
	print(sum(alter))


from collections import defaultdict
for _ in range(int(input())):
	N = int(input())
	L = list(map(int, input().split()))
	size = 0
	MaxP = 0
	MinN = -float('inf')
	i = 0
	FLAG = 0
	if L[0] < 0:
		FLAG = 1
	ans = 0
	while i < N:
		if L[i] > 0:
			MaxP = 0
			while i < N and L[i] > 0:
				MaxP = max(MaxP, L[i])
				i += 1
			ans += MaxP
		elif L[i] < 0:
			MaxN = -float('inf')
			while i < N and L[i] < 0:
				MaxN = max(MaxN, L[i])
				i += 1
			ans += MaxN
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	bnh = -1 if A[-1] > 0 else 1
	A.append(bnh)
	flg = True if A[0] > 0 else False
	inf = 10 ** 10
	tmp = -inf
	res = 0
	for a in A:
		if flg and a > 0:
			tmp = max(tmp, a)
		elif not flg and a < 0:
			tmp = max(tmp, a)
		else:
			res += tmp
			flg = not flg
			tmp = max(-inf, a)
	print(res)


t = int(input())
for test in range(t):
	arr = []
	k = 0
	n = int(input())
	a = list(map(int, input().split()))
	arr.append(a[0])
	for i in range(n):
		if arr[k] > 0 and a[i] > 0 or (arr[k] < 0 and a[i] < 0):
			if arr[k] < a[i]:
				arr[k] = a[i]
		else:
			arr.append(a[i])
			k = k + 1
	print(sum(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	li = []
	p = [s[0]]
	for i in range(1, n):
		if s[i] > 0 and s[i - 1] < 0 or (s[i] < 0 and s[i - 1] > 0):
			li.append(p)
			p = []
			p.append(s[i])
		else:
			p.append(s[i])
	li.append(p)
	ans = 0
	for i in range(len(li)):
		ans += max(li[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ele = list(map(int, input().split()))
	if ele[0] > 0:
		sign = 1
	else:
		sign = 0
	max_sum = 0
	sign_change_index = [0]
	for i in range(1, n):
		ele_sign = 1
		if ele[i] > 0:
			ele_sign = 1
		else:
			ele_sign = 0
		if ele_sign != sign:
			sign_change_index.append(i)
			sign = 1 - sign
	sign_change_index.append(n)
	for i in range(0, len(sign_change_index) - 1):
		max_sum += max(ele[sign_change_index[i]:sign_change_index[i + 1]])
	print(max_sum)


t = int(input())
for i in range(t):
	n = int(input())
	sequence = list(map(int, input().split()))
	sum_value = 0
	val = sequence[0]
	pos = sequence[0] > 0
	for v in range(1, n):
		if pos == (sequence[v] > 0):
			val = max(val, sequence[v])
		else:
			sum_value += val
			val = sequence[v]
		pos = sequence[v] > 0
	sum_value += val
	print(sum_value)


import sys
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def gcd(a, b):
	if a < b:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def extendedGcd(a, b):
	if a < b:
		return extendedGcd(b, a)
	if b == 0:
		return [a, 1, 0]
	res = extendedGcd(b, a % b)
	x = res[2]
	y = res[1] - math.floor(a / b) * res[2]
	res[1] = x
	res[2] = y
	return res

def iterativeModularFunc(a, b, c):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % c
		a = a * a % c
		b = b // 2
	return res

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

def get_int():
	return int(input())

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

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

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

def findEl(arr, n, i, neg=True):
	res = float('inf') if neg else -1
	id = i
	for j in range(i, n):
		if neg:
			if arr[j] > 0:
				break
			if abs(arr[j]) < abs(res):
				res = arr[j]
		else:
			if arr[j] < 0:
				break
			if arr[j] > res:
				res = arr[j]
		id = j
	return (res, id + 1)

def Solution(arr, n):
	res = []
	i = 0
	while i < n:
		if arr[i] < 0:
			(v, i) = findEl(arr, n, i)
		else:
			(v, i) = findEl(arr, n, i, False)
		res.append(v)
	print(sum(res))

def main():
	for _ in range(get_int()):
		n = get_int()
		arr = get_ints_in_list()
		Solution(arr, n)
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	p = 0
	s = 0
	while p < n:
		if a[p] < 0:
			neg = -2147483647
			while p < n and a[p] < 0:
				neg = max(neg, a[p])
				p = p + 1
			s += neg
		else:
			pos = 0
			while p < n and a[p] > 0:
				pos = max(pos, a[p])
				p = p + 1
			s += pos
	print(s)


t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	l = list(map(int, input().strip().split(' ')))
	s = l[0] < 0
	c = 1
	for i in range(n):
		if s and l[i] > 0 or (not s and l[i] < 0):
			s = not s
			c += 1
	s = l[0] < 0
	i = 0
	m = l[0]
	r = 0
	l.append(0)
	n += 1
	while i < n:
		if s and l[i] < 0 or (not s and l[i] > 0):
			m = max(m, l[i])
			i += 1
			continue
		s = not s
		r += m
		if i < n:
			m = l[i]
		i += 1
	print(r)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	yes = 0
	no = 0
	mu = 0
	pl = 0
	if a[0] > 0:
		no = 1
		pl = a[0]
	else:
		yes = 1
		mu = a[0]
	ans = 0
	for i in range(1, n):
		if a[i] > 0 and yes == 1:
			ans += mu
			no = 1
			yes = 0
			pl = a[i]
		elif a[i] < 0 and no == 1:
			ans += pl
			no = 0
			yes = 1
			mu = a[i]
		elif a[i] > 0 and yes == 0:
			if a[i] > pl:
				pl = a[i]
		elif a[i] < 0 and no == 0:
			if a[i] > mu:
				mu = a[i]
	if a[-1] > 0:
		ans += pl
	elif a[-1] < 0:
		ans += mu
	print(ans)


from math import *
t = int(input())
m = []
n = []
for i in range(t):
	n = input()
	x = list(map(int, input().split()))
	m.append(x)
for i in range(t):
	k = []
	for j in range(len(m[i])):
		k.append(floor(m[i][j] / abs(m[i][j])))
	sum = 0
	tmp = m[i][0]
	for j in range(len(m[i]) - 1):
		if k[j] == k[j + 1] and m[i][j + 1] > tmp:
			tmp = m[i][j + 1]
		if k[j + 1] != k[j]:
			sum = sum + tmp
			tmp = m[i][j + 1]
	sum = sum + tmp
	print(sum)


from sys import stdin
debug = False

def join(_s, _l):
	return _s.join(map(str, _l))

def read(_f=None):
	if _f == None:
		return stdin.readline().strip()
	return _f(stdin.readline().strip())

def readlist(_f=None):
	_l = stdin.readline().strip().split()
	if _f == None:
		return _l
	return map(_f, _l)
readint = lambda : read(int)
readints = lambda : readlist(int)
printval = lambda _r: print(_r, end=' ')
printline = lambda _s, _r: print(join(_s, _r), end='\n')

def solve():
	n = readint()
	a = list(readints())
	start_pos = 0
	start_neg = 0
	for num in a:
		if num > 0:
			if start_pos % 2 == 0:
				start_pos += 1
			if start_neg % 2 == 1:
				start_neg += 1
		else:
			if start_pos % 2 == 1:
				start_pos += 1
			if start_neg % 2 == 0:
				start_neg += 1
	k = max(start_pos, start_neg)
	positive = start_pos >= start_neg
	max_sum = 0
	take_positive = positive
	max_value = float('-inf')
	for num in a:
		if take_positive:
			if num < 0:
				max_sum += max_value
				max_value = num
				take_positive ^= 1
		elif num > 0:
			max_sum += max_value
			max_value = num
			take_positive ^= 1
		max_value = max(max_value, num)
	max_sum += max_value
	return max_sum
t = readint()
for _ in range(t):
	if debug:
		print('-' * 20)
	if debug:
		print('ANS: ', end='')
	ans = solve()
	print(ans)


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


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


for _ in range(int(input())):
	x = int(input())
	l = list(map(int, input().split()))
	m = l[0]
	p = 0
	n = 0
	s = 0
	if l[0] > 0:
		p = 1
	else:
		n = 1
	for i in range(1, x):
		if l[i] > 0 and p == 1:
			m = max(m, l[i])
		elif l[i] < 0 and n == 1:
			m = max(m, l[i])
		elif p == 1 and l[i] < 0:
			p = 0
			n = 1
			s += m
			m = l[i]
		elif n == 1 and l[i] > 0:
			n = 0
			p = 1
			s += m
			m = l[i]
	s += m
	print(s)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	pos = 0
	if arr[0] > 0:
		pos = 1
	i = 0
	sm = 0
	mx = arr[i]
	while i < n:
		if pos % 2 == 0 and arr[i] > 0:
			pos += 1
			sm += mx
			mx = arr[i]
			continue
		if pos % 2 == 0 and arr[i] < 0:
			if mx < arr[i]:
				mx = arr[i]
			i += 1
		if pos % 2 == 1 and arr[i] < 0:
			pos += 1
			sm += mx
			mx = arr[i]
			continue
		if pos % 2 == 1 and arr[i] > 0:
			if mx < arr[i]:
				mx = arr[i]
			i += 1
	sm += mx
	print(sm)


t = int(input())
for u in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ma = 0
	summ = 0
	curr = -1
	for i in range(n):
		if a[i] > 0 and curr != 1:
			curr = 1
			summ += ma
			ma = a[i]
		elif a[i] < 0 and curr != 0:
			curr = 0
			summ += ma
			ma = a[i]
		elif a[i] > 0 and curr == 1:
			ma = max(ma, a[i])
		elif a[i] < 0 and curr == 0:
			ma = max(ma, a[i])
	summ += ma
	print(summ)


n = int(input())
for _ in range(n):
	a = int(input())
	b = list(map(int, input().split()))
	(z, w, x, y) = (0, 0, 0, -float('inf'))
	for i in b:
		if i > 0:
			z += w
			w = 0
			x = max(i, x)
			y = -float('inf')
		else:
			z += x
			y = max(i, y)
			x = 0
			w = y
	z += w
	z += x
	print(z)


t = int(input())
while t != 0:
	n = int(input())
	list1 = list(map(int, input().split()))
	i = 0
	sum1 = 0
	while i < len(list1):
		temp = list()
		if list1[i] > 0:
			while i < len(list1) and list1[i] > 0:
				temp.append(list1[i])
				i += 1
			sum1 += max(temp)
		else:
			while i < len(list1) and list1[i] < 0:
				temp.append(list1[i])
				i += 1
			sum1 += max(temp)
	print(sum1)
	t -= 1


def f(n, A):
	s = 0
	ent = A[0]
	for i in range(1, n):
		if A[i] * A[i - 1] < 0:
			s += ent
			ent = A[i]
		else:
			ent = max(ent, A[i])
	return s + ent
for i in range(int(input())):
	n = int(input())
	A = input().split(' ')
	for i in range(n):
		A[i] = int(A[i])
	print(f(n, A))


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


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	m = a[0]
	ss = 0
	for j in range(1, n):
		if a[j] > 0 and a[j - 1] > 0 or (a[j] < 0 and a[j - 1] < 0):
			m = max(m, a[j])
		else:
			ss += m
			m = a[j]
	ss += m
	print(ss)


def solve():
	n = int(input())
	lst = list(map(int, input().split()))
	x = []
	y = []
	cur = 0
	x.append(lst[0])
	for i in range(1, n):
		if x[-1] * lst[i] < 0:
			y.append(x)
			x = []
		x.append(lst[i])
	y.append(x)
	for i in range(len(y)):
		cur += max(y[i])
	print(cur)
t = int(input())
for i in range(t):
	solve()


iterations = int(input())

def mag(n):
	return abs(n) // n
for i in range(iterations):
	_ = input()
	nums = list(map(int, input().split()))
	sign = mag(nums[0])
	maxpos = 0
	maxneg = float('-inf')
	count = 0
	for j in nums:
		if sign == 1 and mag(j) == 1:
			if j > maxpos:
				maxpos = j
		elif sign == 1 and mag(j) == -1:
			count += maxpos
			maxpos = 0
			sign = -1
			maxneg = j
		elif sign == -1 and mag(j) == 1:
			count += maxneg
			maxneg = float('-inf')
			sign = 1
			maxpos = j
		elif j > maxneg:
			maxneg = j
	if maxneg != float('-inf'):
		print(count + maxneg)
	else:
		print(count + maxpos)


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())
	c = []
	s = a[0]
	for i in range(1, n):
		if s * a[i] < 0:
			c.append(s)
			s = a[i]
		else:
			s = max(s, a[i])
	if len(c) == 0 or c[-1] * s < 0:
		c.append(s)
	print(sum(c))


def solve(n, elems):
	stk = [elems[0]]
	for i in range(1, n):
		if stk[-1] < 0 and elems[i] > 0 or (stk[-1] > 0 and elems[i] < 0):
			stk.append(elems[i])
		else:
			stk[-1] = max(stk[-1], elems[i])
	print(sum(stk))
for _ in range(int(input())):
	solve(int(input()), list(map(int, input().strip().split())))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	result = 0
	i = 0
	while i < len(arr):
		temp = -sys.maxsize - 1
		if arr[i] < 0:
			temp = arr[i]
			while i < len(arr) and arr[i] < 0:
				temp = max(temp, arr[i])
				i += 1
			result += temp
			continue
		if arr[i] > 0:
			temp = arr[i]
			while i < len(arr) and arr[i] > 0:
				temp = max(temp, arr[i])
				i += 1
			result += temp
			continue
	print(result)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	summa = 0
	x = a[0]
	for j in a:
		if x * j < 0:
			summa += x
			x = j
		x = max(x, j)
	summa += x
	print(summa)


import sys
input = sys.stdin.readline
t = int(input())
for i1 in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	pos = 0
	neg = 0
	arr = []
	cp = 0
	cn = -1000000001
	for i in range(n):
		if l[i] >= 0:
			if pos == 0:
				pos = 1
				if neg == 1:
					arr.append(cn)
					cn = -1000000001
				neg = 0
				cp = max(l[i], cp)
			else:
				cp = max(l[i], cp)
		elif neg == 0:
			neg = 1
			if pos == 1:
				arr.append(cp)
				cp = 0
			pos = 0
			cn = max(l[i], cn)
		else:
			cn = max(l[i], cn)
	if cp != 0:
		arr.append(cp)
	if cn != -1000000001:
		arr.append(cn)
	print(sum(arr))


import os, sys, atexit
from io import BytesIO, StringIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
t = int(input())
while t:
	t += -1
	n = int(input())
	l = list(map(int, input().split()))
	(neg, pos) = (0, 0)
	if l[0] > 0:
		pos = 1
	else:
		neg = 1
	ans = 0
	mxpos = -1
	mxneg = -10000000000
	for i in range(n):
		if l[i] > 0:
			if pos == 0:
				ans += mxneg
				mxneg = -10000000000
				neg = 0
				pos = 1
			if pos == 1:
				if l[i] > mxpos:
					mxpos = l[i]
		else:
			if neg == 0:
				ans += mxpos
				mxpos = -1
				pos = 0
				neg = 1
			if neg == 1:
				if l[i] > mxneg:
					mxneg = l[i]
	if pos == 1:
		ans += mxpos
	else:
		ans += mxneg
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	new_list = [lst[0]]
	for i in lst[1:]:
		if i // abs(i) == new_list[-1] // abs(new_list[-1]):
			if i / abs(i) == 1:
				if i > new_list[-1]:
					new_list[-1] = i
			elif i > new_list[-1]:
				new_list[-1] = i
		else:
			new_list.append(i)
	print(sum(new_list))


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst2 = []
	i = 0
	j = 0
	while j != n:
		if lst[i] > 0 and lst[j] > 0:
			j += 1
		elif lst[i] < 0 and lst[j] < 0:
			j += 1
		else:
			lst2.append(lst[i:j])
			i = j
	lst2.append(lst[i:j])
	cnt = 0
	for i in lst2:
		cnt += max(i)
	print(cnt)


from sys import stdin

def get_blocks(n, A):
	(ans, s) = ([], 'E')
	if A[0] > 0:
		s = 'P'
	else:
		s = 'N'
	block = [A[0]]
	for i in range(1, n):
		if A[i] > 0 and s == 'P':
			block.append(A[i])
			s = 'P'
		elif A[i] > 0:
			ans.append(block)
			block = [A[i]]
			s = 'P'
		if A[i] < 0 and s == 'N':
			block.append(A[i])
			s = 'N'
		elif A[i] < 0:
			ans.append(block)
			block = [A[i]]
			s = 'N'
	ans.append(block)
	return ans

def solve(n, A):
	blocks = get_blocks(n, A)
	ans = 0
	for i in range(len(blocks)):
		if blocks[i][0] > 0:
			ans += max(blocks[i])
		else:
			ans += max(blocks[i])
	return ans

def main():
	r = stdin.readline
	cases = int(r())
	for case in range(cases):
		n = int(r())
		A = list(map(int, r().strip().split()))
		print(solve(n, A))
main()


t = int(input())
for i in range(0, t):
	n = int(input())
	listb = input()
	listb = listb.split()
	listb = [int(x) for x in listb]
	lastElement = 0
	sumE = 0
	for i in listb:
		if lastElement == 0:
			lastElement = i
		elif i > 0:
			if lastElement < 0:
				sumE += lastElement
				lastElement = i
			elif lastElement < i:
				lastElement = i
			else:
				continue
		elif lastElement > 0:
			sumE += lastElement
			lastElement = i
		elif lastElement < i:
			lastElement = i
		else:
			continue
	sumE += lastElement
	print(sumE)


t = int(input())
while t:
	n = int(input())
	a = [int(x) for x in input().split()]
	temp = []
	fin = []
	sign = a[0] > 0
	for x in a:
		if sign:
			if x > 0:
				temp.append(x)
			else:
				fin.append(max(temp))
				temp = [x]
				sign = not sign
		elif x < 0:
			temp.append(x)
		else:
			fin.append(max(temp))
			temp = [x]
			sign = not sign
	fin.append(max(temp))
	print(sum(fin))
	t -= 1


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	t = int(input())
	ans = [''] * t
	for ti in range(t):
		n = int(input())
		a = [0] + list(map(int, input().split())) + [0]
		(total, x) = (0, 0)
		for (prev, cur) in zip(a, a[1:]):
			if prev * cur <= 0:
				total += x
				x = cur
			else:
				x = max(x, cur)
		ans[ti] = str(total)
	sys.stdout.buffer.write(('\n'.join(ans) + '\n').encode('utf-8'))
main()


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	neg_max = None
	pos_max = None
	ans = 0
	for i in range(n):
		if a[i] < 0:
			pos_max = None
			if neg_max is None:
				neg_max = a[i]
				ans += neg_max
			elif a[i] > neg_max:
				ans = ans - neg_max + a[i]
				neg_max = a[i]
		if a[i] > 0:
			neg_max = None
			if pos_max is None:
				pos_max = a[i]
				ans += pos_max
			elif a[i] > pos_max:
				ans = ans - pos_max + a[i]
				pos_max = a[i]
	print(ans)
	t = t - 1


import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
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

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())

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)))
for _ in range(iinput()):
	n = iinput()
	a = rlinput()
	i = 0
	ans = 0
	while i < n:
		mx = -99999999999999
		while i < n and a[i] > 0:
			mx = max(mx, a[i])
			i += 1
		if mx != -99999999999999:
			ans += mx
		mx = -99999999999999
		while i < n and a[i] < 0:
			mx = max(mx, a[i])
			i += 1
		if mx != -99999999999999:
			ans += mx
	print(ans)


a = int(input())
for i in range(a):
	b = int(input())
	c = [int(i) for i in input().split()]
	s = []
	l = []
	q = 0
	w = 0
	for i in range(len(c)):
		if len(l) == 0:
			l.append(c[i])
		elif l[len(l) - 1] > 0 and c[i] > 0 or (c[i] < 0 and l[len(l) - 1] < 0):
			l.append(c[i])
		else:
			s.append(l)
			l = [c[i]]
	s.append(l)
	for i in range(len(s)):
		q = q + max(s[i])
	print(q)


def sign(x):
	return 1 if x > 0 else -1

def alter_split(arr):
	n = len(arr)
	ans = [[arr[0]]]
	flag = sign(arr[0])
	i = 1
	k = 0
	while i < n:
		if sign(arr[i]) == flag:
			ans[k].append(arr[i])
		else:
			flag = sign(arr[i])
			k += 1
			ans.append([arr[i]])
		i += 1
	return ans
tcases = int(input().strip())
for case in range(tcases):
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	sp = alter_split(arr)
	print(sum([max(li) for li in sp]))


import sys
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
import collections
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

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 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 most_frequent(list):
	return max(set(list), key=list.count)

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

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def Convert(string):
	li = list(string.split(''))
	return li

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
prime = []
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

def main():
	t = inin()
	while t > 0:
		n = inin()
		q1 = ain()
		sum1 = 0
		for i in range(n):
			if i == 0:
				sum1 = q1[0]
			elif q1[i] > 0:
				if q1[i] * q1[i - 1] > 0:
					if q1[i] >= q1[i - 1]:
						sum1 = sum1 - q1[i - 1] + q1[i]
					else:
						q1[i] = q1[i - 1]
				else:
					sum1 = sum1 + q1[i]
			elif q1[i] * q1[i - 1] > 0:
				if abs(q1[i]) < abs(q1[i - 1]):
					sum1 = sum1 - q1[i - 1] + q1[i]
				else:
					q1[i] = q1[i - 1]
			else:
				sum1 = sum1 + q1[i]
		print(sum1)
		t = t - 1
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()


t = int(input())
for i in range(t):
	a = int(input())
	x = input().split()
	sign = 0
	maxi = 0
	totsum = 0
	for j in range(a):
		y = int(x[j])
		if j == 0:
			if y > 0:
				sign = 1
			else:
				sign = -1
			maxi = y
		elif sign * y > 0:
			if y > maxi:
				maxi = y
		else:
			totsum += maxi
			maxi = y
			sign *= -1
	print(totsum + maxi)


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 ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

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 fsep():
	return map(float, inp().split())

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

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

def pow(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
from functools import reduce

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)))

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

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
from collections import defaultdict
sys.setrecursionlimit(300000)

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

def solve():
	n = N()
	ar = lis()
	pos = 1
	if ar[0] < 0:
		pos = 0
	v = ar[0]
	temp = []
	for i in range(1, n):
		if ar[i] > 0 and pos:
			v = max(v, ar[i])
		if ar[i] < 0 and pos:
			temp.append(v)
			pos = 0
			v = ar[i]
		if ar[i] > 0 and (not pos):
			temp.append(v)
			pos = 1
			v = ar[i]
		if ar[i] < 0 and (not pos):
			v = max(v, ar[i])
	temp.append(v)
	print(sum(temp))
testcase(int(inp()))


for _ in range(int(input())):
	x = int(input())
	num = list(map(int, input().split()))
	position = []
	flag = 0
	for i in range(x):
		if flag <= 0 and num[i] < 0:
			position.append(i)
			flag = 1
		elif flag >= 0 and num[i] > 0:
			position.append(i)
			flag = -1
	sum = 0
	for i in range(len(position) - 1):
		sum += max(num[position[i]:position[i + 1]])
	sum += max(num[position[-1]:])
	print(sum)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	tempMinus = -10000000000
	tempPlus = 0
	ans = 0
	flag = 0
	if arr[0] > 0:
		flag = 1
	else:
		flag = 0
	for i in range(n):
		if flag == 0:
			if arr[i] > tempMinus:
				tempMinus = arr[i]
			if i == n - 1:
				ans += tempMinus
				break
			if arr[i + 1] > 0:
				ans += tempMinus
				flag = 1
				tempMinus = -10000000000
		elif flag == 1:
			if arr[i] > tempPlus:
				tempPlus = arr[i]
			if i == n - 1:
				ans += tempPlus
				break
			if arr[i + 1] < 0:
				ans += tempPlus
				flag = 0
				tempPlus = 0
	print(ans)


from math import ceil

class Solution:

	def __init__(self):
		self.n = int(input())
		self.a = [int(x) for x in input().split()]

	def solve_and_print(self):
		i = sum = 0
		is_neg = self.a[0] < 0
		while i < self.n:
			val = self.a[i]
			if is_neg:
				while i < self.n and self.a[i] < 0:
					val = max(val, self.a[i])
					i += 1
				is_neg = False
			else:
				while i < self.n and self.a[i] > 0:
					val = max(val, self.a[i])
					i += 1
				is_neg = True
			sum += val
		print(sum)
for _ in range(int(input())):
	Solution().solve_and_print()


t = int(input())
while t > 0:
	n = int(input())
	l = [int(s) for s in input().split()]
	total = 0
	mx = l[0]
	for i in range(1, n):
		if mx * l[i] < 0:
			total += mx
			mx = l[i]
		elif l[i] > mx:
			mx = l[i]
	print(total + mx)
	t -= 1


def foo():
	n = int(input())
	a = [int(x) for x in input().split()]
	f = [None] * n
	m = a[0]
	s = 0
	for i in range(n):
		if a[i] > 0:
			f[i] = 1
		else:
			f[i] = -1
	for i in range(1, n):
		if f[i] != f[i - 1]:
			s += m
			m = a[i]
		else:
			m = max(m, a[i])
	return s + m
t = int(input())
for k in range(t):
	print(foo())


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
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().split()]

def st():
	return input()

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

def li2():
	return [i for i in input()]

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

def makel(l):
	l1 = []
	curr = l[0]
	par = 1 if l[0] > 0 else 0
	for i in range(1, len(l)):
		currpar = 1 if l[i] > 0 else 0
		if currpar == par:
			curr = max(curr, l[i])
		else:
			l1.append(curr)
			curr = l[i]
			par = 1 - par
	l1.append(curr)
	return l1[:]
for _ in range(val()):
	n = val()
	l = makel(li())
	print(sum(l))


t = int(input())
for _ in range(t):
	l = int(input())
	data = list(map(int, input().split()))
	s = []
	i = 0
	while i < l:
		if data[i] > 0:
			for j in range(i, l):
				if data[j] < 0:
					break
				elif j == l - 1 and data[j] > 0:
					j = j + 1
					break
			s.append(max(data[i:j]))
			i = j
		elif data[i] < 0:
			for j in range(i, l):
				if data[j] > 0:
					break
				elif j == l - 1 and data[j] < 0:
					j = j + 1
					break
			s.append(max(data[i:j]))
			i = j
	print(sum(s))


T = int(input())
for t in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	k = l[0]
	last = l[0]
	for i in range(1, len(l)):
		if last * l[i] < 0:
			s += k
			k = l[i]
		elif l[i] > k:
			k = l[i]
		last = l[i]
	s += k
	print(s)


I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n,) = I()
	l = I()
	an = 0
	i = 0
	k = 0
	m = -10 ** 9 - 1
	while i < n:
		while i < n and l[i] > 0:
			k = max(k, l[i])
			i += 1
		an += k
		k = 0
		while i < n and l[i] < 0:
			m = max(m, l[i])
			i += 1
		if i < n or l[n - 1] < 0:
			an += m
			m = -10 ** 9 - 1
	print(an)


import sys
from collections import Counter

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 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = LIST()
	B = []
	tmp = [A[0]]
	pos = 0
	if A[0] > 0:
		pos = 1
	for i in range(1, N):
		if pos and A[i] < 0 or (not pos and A[i] > 0):
			pos = 1 - pos
			B.append(tmp)
			tmp = [A[i]]
		else:
			tmp.append(A[i])
	B.append(tmp)
	ans = 0
	for li in B:
		ans += max(li)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = []
	t = 0
	t1 = 0
	c = 0
	c1 = 0
	z = l[0]
	z1 = float('-inf')
	for j in range(n):
		if z > 0 and l[j] < 0:
			ans.append(z)
			z = l[j]
		elif z < 0 and l[j] > 0:
			ans.append(z)
			z = l[j]
		z = max(z, l[j])
		if j == n - 1:
			ans.append(z)
	print(sum(ans))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	largest_in_group = l[0]
	sign_of_group = l[0]
	total_so_far = 0
	for num in l:
		if num * sign_of_group > 0:
			largest_in_group = max(largest_in_group, num)
		else:
			total_so_far += largest_in_group
			largest_in_group = num
			sign_of_group = num
	total_so_far += largest_in_group
	print(total_so_far)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	pos = []
	neg = []
	res = 0
	i = 0
	while i < n:
		if a[i] < 0:
			neg.append(a[i])
			if len(pos) > 0:
				res += max(pos)
				pos.clear()
		else:
			pos.append(a[i])
			if len(neg) > 0:
				res += max(neg)
				neg.clear()
		i += 1
	if len(pos) > 0:
		res += max(pos)
	if len(neg) > 0:
		res += max(neg)
	print(res)


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	if lst[0] > 0:
		flag = True
	else:
		flag = False
	i = s = 0
	mi = mx = -10 ** 10
	while i < n:
		if flag:
			if lst[i] > 0:
				mx = max(mx, lst[i])
				i += 1
			else:
				s += mx
				flag = False
				mx = -10 ** 10
		elif lst[i] < 0:
			mi = max(mi, lst[i])
			i += 1
		else:
			s += mi
			flag = True
			mi = -10 ** 10
	if flag:
		s += mx
	else:
		s += mi
	print(s)


for nt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	if l[0] > 0:
		pos = 1
		m = l[0]
	else:
		pos = 0
		m = l[0]
	for i in range(1, n):
		if l[i] > 0 and pos:
			m = max(m, l[i])
		elif l[i] < 0 and (not pos):
			m = max(m, l[i])
		elif l[i] > 0 and (not pos):
			ans += m
			m = l[i]
			pos = 1
		else:
			ans += m
			m = l[i]
			pos = 0
	ans += m
	print(ans)


import math

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	nums = []
	pos_max = 0
	neg_max = -10 ** 10
	if len(a) == 1:
		return a[0]
	for i in range(1, n):
		if a[i] > 0 and a[i - 1] > 0:
			pos_max = max(pos_max, a[i], a[i - 1])
		elif a[i] < 0 and a[i - 1] < 0:
			neg_max = max(neg_max, a[i], a[i - 1])
		elif a[i] > 0 and a[i - 1] < 0:
			neg_max = max(neg_max, a[i - 1])
			pos_max = a[i]
			nums.append(neg_max)
		elif a[i] < 0 and a[i - 1] > 0:
			pos_max = max(pos_max, a[i - 1])
			neg_max = a[i]
			nums.append(pos_max)
	if len(nums) == 0:
		nums.append(max(a))
	elif nums[-1] < 0:
		nums.append(pos_max)
	elif nums[-1] > 0:
		nums.append(neg_max)
	return sum(nums)
t = int(input())
while t > 0:
	t -= 1
	print(solve())


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	new_a = []
	i = 0
	while i < n:
		j = i
		while j < n and (a[j] < 0) == (a[i] < 0):
			j += 1
		new_a.append(max(a[i:j]))
		i = j
	print(sum(new_a))


def sig(a):
	if a < 0:
		return -1
	else:
		return 1

def fun(n):
	a = []
	l = []
	a.append(n[0])
	for i in range(1, len(n)):
		if sig(n[i - 1]) == sig(n[i]):
			a.append(n[i])
		else:
			l.append(a)
			a = []
			a.append(n[i])
	if a is not []:
		l.append(a)
	s = 0
	for i in l:
		s += max(i)
	return s
T = int(input())
for _ in range(T):
	y = input()
	n = [int(x) for x in input().split()]
	x = fun(n)
	print(x)


import os
import sys
from io import BytesIO, IOBase

def solution(arr, n):
	subs = []
	i = 0
	while i < n:
		temp = []
		while i < n and arr[i] < 0:
			temp.append(arr[i])
			i += 1
		if temp:
			subs.append(temp)
		temp = []
		while i < n and arr[i] > 0:
			temp.append(arr[i])
			i += 1
		if temp:
			subs.append(temp)
	res = 0
	for sub in subs:
		res += max(sub)
	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:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(end)

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

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


for _ in range(int(input())):
	t = int(input())
	l = [int(x) for x in input().split()]
	ll = []
	ll.append(l[0])
	for j in range(1, t):
		if l[j] > 0 and l[j - 1] < 0:
			ll.append(l[j])
		elif l[j] < 0 and l[j - 1] > 0:
			ll.append(l[j])
		elif l[j] > 0 and l[j - 1] > 0:
			c = len(ll) - 1
			if l[j] > ll[c]:
				ll[c] = l[j]
		elif l[j] < 0 and l[j - 1] < 0:
			c = len(ll) - 1
			if l[j] > ll[c]:
				ll[c] = l[j]
		else:
			ans = 1
	print(sum(ll))


t = int(input())

def get(n):
	return 1 if n > 0 else -1
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	dp = [[0, 0] for i in range(n + 1)]
	dp[0][0] = a[0]
	a.append(1 if get(a[-1]) == -1 else -1)
	for i in range(1, n + 1):
		dp[i][1] = dp[i - 1][1]
		if get(a[i]) != get(a[i - 1]):
			dp[i - 1][1] += dp[i - 1][0]
			dp[i][0] = a[i]
			dp[i][1] = dp[i - 1][1]
			continue
		dp[i][0] = max(dp[i - 1][0], a[i])
		dp[i][1] = dp[i - 1][1]
	print(dp[-1][1])


def inti():
	return int(input())

def extrema(L, max):
	if max:
		M = L[0]
		for e in L:
			if e > M:
				M = e
		return M
	else:
		M = L[0]
		for e in L:
			if e < M:
				M = e
		return M
T = inti()
for t in range(1, T + 1):
	n = inti()
	s = input().split(' ')
	alt = []
	positive = None
	for e in s:
		num = int(e)
		if num > 0:
			if positive == True:
				alt[-1].append(num)
			else:
				alt.append([num])
				positive = True
		elif positive == False:
			alt[-1].append(num)
		else:
			alt.append([num])
			positive = False
	SUM = 0
	for sublist in alt:
		SUM += extrema(sublist, max=True)
	print(SUM)


n = int(input())
l = []
while n:
	x = int(input())
	a = list(map(int, input().split()))
	if a[0] < 0:
		c = 0
	else:
		c = 1
	z = []
	b = [a[0]]
	for i in range(1, len(a)):
		if a[i] < 0 and c == 0:
			b.append(a[i])
		elif a[i] > 0 and c == 1:
			b.append(a[i])
		elif a[i] < 0 and c == 1:
			z.append(max(b))
			b = []
			b = [a[i]]
			c = 0
		else:
			z.append(max(b))
			b = []
			b = [a[i]]
			c = 1
	if b != []:
		z.append(max(b))
	s = 0
	for i in range(len(z)):
		s += z[i]
	l.append(s)
	n -= 1
for i in l:
	print(i)


t = int(input())
for test in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	max_so_far = a[0]
	curr_max = a[0]
	pos = 0
	neg = 0
	if a[0] > 0:
		pos = a[0]
		last = 1
	else:
		neg = a[0]
		last = -1
	first = 0
	i = 1
	ans = []
	ans.append(a[0])
	while i < n:
		if last == 1:
			if a[i] < 0:
				ans.append(a[i])
				last = -1
			elif a[i] > ans[-1]:
				ans.pop()
				ans.append(a[i])
		elif a[i] > 0:
			ans.append(a[i])
			last = 1
		elif a[i] > ans[-1]:
			ans.pop()
			ans.append(a[i])
		i += 1
	print(sum(ans))


t = int(input())
while t > 0:
	n = int(input())
	a = [int(n) for n in input().split()]
	flag = []
	for i in range(n):
		if a[i] < 0:
			flag.append(0)
		else:
			flag.append(1)
	(i, summ, maxx) = (0, 0, float('-inf'))
	while i < n:
		if a[i] > maxx:
			maxx = a[i]
		if i != n - 1:
			if not flag[i] == flag[i + 1]:
				summ += maxx
				maxx = float('-inf')
		i += 1
	print(summ + maxx)
	t -= 1


import sys
import math

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

def idata():
	return [int(x) for x in ii().split()]

def bfs(graph1, root1):
	visited1 = set()
	queue1 = [[root1, 0]]
	visited1.add(root1)
	(qw, ver) = (0, 1)
	while queue1:
		vertex1 = queue1[0]
		queue1 = queue1[1:]
		for neig1 in graph1[vertex1[0]]:
			if neig1 not in visited1:
				visited1.add(neig1)
				queue1 += [[neig1, vertex1[1] + 1]]
				if qw < vertex1[1] + 1:
					(ver, qw) = (neig1, vertex1[1] + 1)
	return (ver, qw)

def dfs(graph, ver):
	used_v = set()
	depth = [0] * (len(graph) + 1)
	level = [ver]
	num_of_level = 0
	while level:
		new_level = []
		for v in level:
			depth[v] = num_of_level
			used_v.add(v)
			for v1 in graph[v]:
				if v1 not in used_v:
					new_level += [v1]
		level = new_level
		num_of_level += 1
	return depth

def binary_search(array, x):
	(left, right) = (-1, len(array))
	while left + 1 != right:
		middle = (left + right) // 2
		if array[middle] >= x:
			right = middle
		elif array[middle] < x:
			left = middle
	return right

def c_mod(n1, k1, mod1):
	num = den = 1
	for i in range(n1 - k1):
		num = num * (n1 - i) % mod1
		den = den * (i + 1) % mod1
	return num * pow(den, mod1 - 2, mod1) % mod1

def c(n1, k1):
	num = den = 1
	for i in range(n1 - k1):
		num = num * (n1 - i)
		den = den * (i + 1)
	return num // den

def v_sistemu(x, k):
	x = int(x)
	z = ''
	while x:
		z += str(x % k)
		x //= k
	return z[::-1]

def iz_sistemi(x, k):
	x = str(x)[::-1]
	ans = 0
	for i in range(len(x)):
		ans += int(x[i]) * pow(k, i)
	return ans

def solve_of_problem():
	n = int(ii())
	data = idata()
	for i in range(1, n):
		if data[i] > 0 and data[i - 1] > 0 or (data[i] < 0 and data[i - 1] < 0):
			(data[i - 1], data[i]) = (0, max(data[i], data[i - 1]))
	print(sum(data))
	return
for ______ in range(int(ii())):
	solve_of_problem()


import sys, math
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	l = list(map(int, sys.stdin.readline().split()))
	neg = 0
	pos = 0
	k1 = -10 ** 10
	k2 = -10 ** 10
	f1 = False
	f2 = False
	if l[0] < 0:
		k1 = l[0]
		f1 = True
	else:
		k2 = l[0]
		f2 = True
	for i in range(1, n):
		if f1 and l[i] < 0:
			k1 = max(k1, l[i])
			f2 = False
			k2 = -10 ** 10
		elif f2 and l[i] < 0:
			pos += k2
			k1 = max(k1, l[i])
			k2 = -10 ** 10
			f1 = True
			f2 = False
		elif f2 and l[i] > 0:
			k2 = max(k2, l[i])
			f1 = False
			k1 = -10 ** 10
		elif f1 and l[i] > 0:
			neg += k1
			k2 = max(k2, l[i])
			k1 = -10 ** 9
			f2 = True
			f1 = False
	if l[-1] < 0:
		neg += k1
	else:
		pos += k2
	print(pos + neg)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = 0
	mi = -9999999999999999999999999999
	s = 0
	for i in range(n):
		if a[i] > 0:
			m = max(m, a[i])
			if mi != -9999999999999999999999999999:
				s += mi
			mi = -9999999999999999999999999999
		else:
			mi = max(mi, a[i])
			if m != 0:
				s += m
			m = 0
	if mi != -9999999999999999999999999999:
		s += mi
	if m != 0:
		s += m
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ind = 0
	ans = 0
	while ind < len(l):
		mx = 0
		mn = -99999999999
		if l[ind] > 0:
			while ind < len(l) and l[ind] > 0:
				if mx < l[ind]:
					mx = l[ind]
				ind += 1
			ans += mx
		else:
			while ind < len(l) and l[ind] < 0:
				if mn < l[ind]:
					mn = l[ind]
				ind += 1
			ans += mn
	print(ans)


def sol():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = [int(i) for i in input().split()]
		L = []
		p = []
		prev = A[0]
		for i in range(n):
			if A[i] * prev > 0:
				p.append(A[i])
			else:
				L.append(p)
				p = [A[i]]
				prev *= -1
		L.append(p)
		print(sum([max(l) for l in L]))
sol()


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	pos = []
	neg = []
	c = 0
	for i in range(n):
		if lst[i] < 0:
			neg.append(lst[i])
			if pos != []:
				c += max(pos)
				pos = []
		if lst[i] > 0:
			pos.append(lst[i])
			if neg != []:
				c += max(neg)
				neg = []
	if pos == []:
		c += max(neg)
	else:
		c += max(pos)
	print(c)


q = int(input())
for _ in range(q):
	n = int(input())
	ans = 0
	s = [int(i) for i in input().split()]
	t = s[0]
	m = t
	f = t > 0
	for i in range(1, n):
		t = s[i]
		if t > 0 and f or (t < 0 and (not f)):
			m = max(m, t)
		else:
			f = not f
			ans += m
			m = t
	ans += m
	print(ans)


t = int(input())
for i in range(t):
	a = int(input())
	l = list(map(int, input().split()))
	p = 0
	n = 0
	sum = 0
	r = 0
	for j in l:
		if p == 0 and j > 0:
			r = j
			sum = sum + j
			p = 1
			n = 0
		elif n == 0 and j < 0:
			r = j
			sum = sum + j
			n = 1
			p = 0
		elif (p == 1 or n == 1) and j > r:
			sum = sum - r
			sum = sum + j
			r = j
	print(sum)


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


def splt(lst):
	final = []
	f1 = []
	f2 = []
	if lst[0] > 0:
		f1.append(lst[0])
	else:
		f2.append(lst[0])
	for i in range(1, len(lst)):
		if lst[i] > 0:
			if len(f2) > 0:
				final.append(f2)
				f2 = []
			f1.append(lst[i])
		else:
			if len(f1) > 0:
				final.append(f1)
				f1 = []
			f2.append(lst[i])
	if len(f1) > 0:
		final.append(f1)
	elif len(f2) > 0:
		final.append(f2)
	return final
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	alt = splt(a)
	tot = 0
	for arr in alt:
		largest = -1000000001
		for j in range(len(arr)):
			if arr[j] > largest:
				largest = arr[j]
		tot += largest
	print(tot)


from math import copysign
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	ans = 0
	while i < n:
		now = a[i]
		i += 1
		while i < n and copysign(1, a[i - 1]) == copysign(1, a[i]):
			now = max(now, a[i], a[i - 1])
			i += 1
		ans += now
	print(ans)


from sys import *

def sum_seq(a, n):
	sign = None
	max = 0
	min = 0
	sum = 0
	for i in range(n):
		if sign == None:
			if a[i] > 0:
				sign = 'pos'
				max = a[i]
			else:
				sign = 'neg'
				min = a[i]
		elif sign == 'pos' and a[i] > 0:
			if a[i] > max:
				max = a[i]
		elif sign == 'pos' and a[i] < 0:
			sum = sum + max
			max = 0
			min = a[i]
			sign = 'neg'
		elif sign == 'neg' and a[i] < 0:
			if a[i] > min:
				min = a[i]
		else:
			sum = sum + min
			min = 0
			max = a[i]
			sign = 'pos'
	sum = sum + max + min
	return sum
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().strip().split()))[:n]
	x = sum_seq(a, n)
	print(x)


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()]
	if n == 1:
		ans_arr.append(str(a[0]))
	else:
		if a[0] > 0:
			cur = 'p'
		else:
			cur = 'n'
		mx = a[0]
		ans = 0
		for x in range(1, n):
			if cur == 'p':
				if a[x] > 0:
					mx = max(mx, a[x])
				else:
					cur = 'n'
					ans += mx
					mx = a[x]
			elif a[x] < 0:
				mx = max(mx, a[x])
			else:
				cur = 'p'
				ans += mx
				mx = a[x]
		ans += mx
		ans_arr.append(str(ans))
print('\n'.join(ans_arr))


n = int(input())
for i in range(n):
	lena = int(input()) + 1
	arr = list(map(int, input().split()))
	arr += [arr[-1] * -1]
	length = 0
	final = 0
	start = 0
	cur = arr[0]
	for j in range(1, lena):
		if cur < 0 and arr[j] > 0 or (cur > 0 and arr[j] < 0):
			tsum = max(arr[start:j])
			final += tsum
			start = j
		cur = arr[j]
	print(final)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	if l[0] < 0:
		sign = -1
		neg_max = l[0]
	else:
		sign = 1
		pos_max = l[0]
	s = 0
	for i in range(1, n):
		if l[i] > 0:
			if sign == -1:
				s = s + neg_max
				sign = 1
				pos_max = l[i]
			elif pos_max < l[i]:
				pos_max = l[i]
		elif sign == 1:
			s = s + pos_max
			sign = -1
			neg_max = l[i]
		elif neg_max < l[i]:
			neg_max = l[i]
	if sign == -1:
		s = s + neg_max
	else:
		s = s + pos_max
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	(pos, neg) = (0, -1000000005)
	ans = 0
	for i in range(len(a)):
		if a[i] > 0:
			pos = max(pos, a[i])
			if neg != -1000000005:
				ans += neg
				neg = -1000000005
		else:
			neg = max(neg, a[i])
			if pos != 0:
				ans += pos
				pos = 0
	if neg != -1000000005:
		ans += neg
	else:
		ans += pos
	print(ans)


t = int(input())
for z in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	p = False
	maxn = -9999999999
	maxp = 0
	summ = 0
	if l[0] > 0:
		p = True
	for i in range(0, n):
		if l[i] < 0:
			if p:
				summ += maxp
				maxn = -9999999999
				maxp = 0
			p = False
			if maxn < l[i]:
				maxn = l[i]
		if l[i] > 0:
			if not p:
				summ += maxn
				maxn = -9999999999
				maxp = 0
			p = True
			if maxp < l[i]:
				maxp = l[i]
	if p:
		summ += maxp
	else:
		summ += maxn
	print(summ)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(_) for _ in input().split() if int(_) != 0]
	i = 0
	s = a[0] < 0
	l = len(a)
	b = []
	while i < l:
		if s:
			j = i
			while j < l and a[j] < 0:
				j += 1
			b.append(max(a[i:j]))
			i = j
			s = not s
		else:
			j = i
			while j < l and a[j] > 0:
				j += 1
			b.append(max(a[i:j]))
			i = j
			s = not s
	print(sum(b))


T = int(input())
for test in range(T):
	N = int(input())
	array = list(map(int, input().split()))
	if N == 1:
		print(array[0])
		continue
	cur = array[0]
	if cur > 0:
		sign = 1
	else:
		sign = -1
	total = 0
	amax = cur
	for n in range(1, N):
		if array[n] * array[n - 1] > 0:
			amax = max(amax, array[n])
		else:
			total += amax
			amax = array[n]
		if n == N - 1:
			total += amax
	print(total)


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


def compute(n, arr):
	stack = [arr[0]]
	cur_sum = arr[0]
	for i in range(1, n):
		if stack[-1] > 0 and arr[i] > 0 or (stack[-1] < 0 and arr[i] < 0):
			if arr[i] > stack[-1]:
				cur_sum -= stack[-1]
				cur_sum += arr[i]
				stack[-1] = arr[i]
		else:
			stack.append(arr[i])
			cur_sum += arr[i]
	return cur_sum
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(compute(n, arr))


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(map(int, input().split()))
	a = [0]
	b = [0]
	m = 0
	cur = 0
	mi = 1
	for i in range(n):
		if s[i] < 0:
			if a[-1] < 0 and s[i] > a[-1]:
				cur -= a[-1]
				cur += s[i]
				a[-1] = s[i]
			elif a[-1] >= 0:
				a.append(s[i])
				cur += s[i]
		elif a[-1] <= 0:
			a.append(s[i])
			cur += s[i]
		elif a[-1] < s[i]:
			cur -= a[-1]
			cur += s[i]
			a[-1] = s[i]
	print(cur)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	sign = '+'
	test = -10 ** 10
	for i in range(0, n):
		if a[i] > 0 and sign == '+':
			test = max(test, a[i])
		elif a[i] < 0 and sign == '+':
			if test != -10 ** 10:
				ans += test
			test = a[i]
			sign = '-'
		elif a[i] > 0 and sign == '-':
			ans += test
			test = a[i]
			sign = '+'
		else:
			test = max(test, a[i])
	ans += test
	print(ans)


def solve():

	def is_neg(x):
		return x < 0
	n = int(input())
	ar = list(map(int, input().split()))
	best = ar[0]
	neg = is_neg(ar[0])
	ans = 0
	for i in range(1, n):
		if is_neg(ar[i]) == neg:
			best = max(best, ar[i])
		else:
			neg = not neg
			ans += best
			best = ar[i]
	ans += best
	print(ans)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = input().split(' ')
	l = []
	for i in range(n):
		l += [int(l1[i])]
	temp = []
	i = 0
	while i < n:
		if l[i] > 0:
			cm = l[i]
			while l[i] > 0:
				cm = max(cm, l[i])
				i += 1
				if i == n:
					temp += [cm]
					break
			temp += [cm]
		else:
			cm = l[i]
			while l[i] < 0:
				cm = max(cm, l[i])
				i += 1
				if i == n:
					temp += [cm]
					break
			temp += [cm]
	l = temp[0:len(temp) - 1]
	dp = []
	if l[0] > 0:
		dp += [[1, l[0]]]
	else:
		dp += [[1, l[0]]]
	last = []
	lp = -1
	ln = -1
	for i in range(len(l)):
		if l[i] > 0:
			if ln == -1:
				last += [-1]
			else:
				last += [ln]
			lp = i
		else:
			if lp == -1:
				last += [-1]
			else:
				last += [lp]
			ln = i
	for i in range(1, len(l)):
		if l[i] > 0:
			j = last[i]
			if j != -1:
				dp += [[dp[j][0] + 1, dp[j][1] + l[i]]]
			else:
				dp += [[1, l[i]]]
		else:
			j = last[i]
			if j != -1:
				dp += [[dp[j][0] + 1, dp[j][1] + l[i]]]
			else:
				dp += [[1, l[i]]]
	max_k = dp[-1][0]
	max_sum = dp[-1][1]
	for i in range(len(dp) - 2, -1, -1):
		if dp[i][0] == max_k:
			max_sum = max(max_sum, dp[i][1])
	print(max_sum)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(x) for x in input().split()]
	(summ, startindex) = (0, 0)
	for i in range(1, n):
		if a[i - 1] > 0 and a[i] > 0 or (a[i - 1] < 0 and a[i] < 0):
			pass
		else:
			summ += max(a[startindex:i])
			startindex = i
	summ += max(a[startindex:])
	print(summ)


number_test = int(input())
for k in range(number_test):
	n = int(input())
	T = list(map(int, input().split(' ')))
	sol = 0
	maxi = 0
	mini = None
	avant = T[0] > 0
	for k in range(n):
		current = T[k] > 0
		if current and avant:
			if T[k] > maxi:
				maxi = T[k]
		elif not current and (not avant):
			if mini == None:
				mini = T[k]
			elif T[k] > mini:
				mini = T[k]
		elif current:
			sol += mini
			mini = None
			maxi = T[k]
			avant = current
		else:
			sol += maxi
			maxi = 0
			mini = T[k]
			avant = current
	if current and avant:
		sol += maxi
	elif not current and (not avant):
		sol += mini
	print(sol)


n = int(input())

def problem1343C(L):
	S = 0
	partial_max = L[0]
	for ele in L:
		if ele * partial_max > 0:
			if ele > partial_max:
				partial_max = ele
		else:
			S += partial_max
			partial_max = ele
	S += partial_max
	return S
for ligne in range(n):
	N = int(input())
	L = list(map(int, input().split()))
	print(problem1343C(L))


def is_same(x, y):
	if x < 0 and y < 0 or (x > 0 and y > 0):
		return True
	else:
		return False
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	stack = []
	for i in range(n):
		if not stack:
			stack.append(arr[i])
		elif is_same(stack[-1], arr[i]):
			if arr[i] > stack[-1]:
				stack.pop()
				stack.append(arr[i])
		else:
			stack.append(arr[i])
	print(sum(stack))


from pprint import pprint
import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	n = int(input())
	dat = list(map(int, input().split()))
	res = -99999999999999999999999999999999999
	candidate = []
	buf = []
	isnega = True if dat[0] < 0 else False
	for i in range(n):
		if isnega:
			if dat[i] > 0:
				candidate.append(max(buf))
				buf = []
				isnega = False
			else:
				pass
		elif dat[i] < 0:
			candidate.append(max(buf))
			buf = []
			isnega = True
		else:
			pass
		buf.append(dat[i])
	candidate.append(max(buf))
	print(sum(candidate))


import atexit
import io
import sys
input = sys.stdin.readline
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = 0
	start = 0
	end = 0
	l = []
	if a[0] < 0:
		last = -1
	else:
		last = 1
	for i in range(1, n):
		if a[i] * last > 0:
			continue
		elif last == 1:
			l.append(max(a[start:i]))
			start = i
			last *= -1
		else:
			l.append(max(a[start:i]))
			start = i
			last *= -1
	if last == 1:
		l.append(max(a[start:n]))
	else:
		l.append(max(a[start:n]))
	print(sum(l))


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	temp = [l[0]]
	for i in range(1, n):
		if l[i] > 0 and temp[-1] > 0 or (l[i] < 0 and temp[-1] < 0):
			temp[-1] = max(temp[-1], l[i])
		else:
			temp.append(l[i])
	print(sum(temp))


import math
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	maxi = 0
	mini = -99999999999999999999999999
	ans1 = 0
	ans2 = 0
	for i in arr:
		if i > 0:
			maxi = max(maxi, i)
			if mini != -99999999999999999999999999:
				ans1 += mini
			mini = -99999999999999999999999999
		else:
			ans1 += maxi
			mini = max(mini, i)
			maxi = 0
	if mini != -99999999999999999999999999:
		ans1 += mini
	mini = -99999999999999999999999999
	maxi = -99999999999999999999999999
	for i in arr:
		if i < 0:
			mini = max(mini, i)
			if maxi != -99999999999999999999999999:
				ans2 += maxi
			maxi = -99999999999999999999999999
		else:
			if mini != -99999999999999999999999999:
				ans2 += mini
			maxi = max(maxi, i)
			mini = -99999999999999999999999999
	if maxi != -99999999999999999999999999:
		ans2 += maxi
	if mini != -99999999999999999999999999:
		ans2 += mini
	print(max(ans2, ans1))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(lambda x: int(x), input().split(' ')))
	a_len = len(a)
	sum = 0
	i = 0
	while a_len > i:
		max_num = -99999999999999
		while True:
			max_num = max(max_num, a[i])
			if i == a_len - 1 or a[i] * a[i + 1] < 0:
				break
			i += 1
		i += 1
		sum += max_num
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.append(0)
	p = l[0]
	sm = 0
	for j in range(1, n + 1):
		if l[j] >= 0 and p < 0 or (l[j] <= 0 and p > 0):
			sm += p
			p = l[j]
		elif l[j] > p:
			p = l[j]
	print(sm)


n = int(input())

def solve(arr):
	total = 0
	sign = -1 if arr[0] < 0 else 1
	prevbest = arr[0]
	for i in range(1, len(arr)):
		if sign < 0 and arr[i] > 0 or (sign > 0 and arr[i] < 0):
			sign = -1 if sign == 1 else 1
			total += prevbest
			prevbest = arr[i]
		else:
			prevbest = max(prevbest, arr[i])
	total += prevbest
	return total
for _ in range(n):
	leng = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = []
	indice = 0
	c = []
	while indice < n:
		c.append(a[indice])
		if indice < n - 1:
			if a[indice] * a[indice + 1] < 0:
				b.append(a[indice])
				indice = indice + 1
				c = []
			else:
				indice_fisso = indice
				indice = indice + 1
				while indice <= n - 1:
					if a[indice_fisso] * a[indice] < 0:
						break
					c.append(a[indice])
					indice = indice + 1
				b.append(max(c))
				c = []
		else:
			b.append(a[n - 1])
			break
	print(sum(b))


def sublist(somelist):
	result = []
	a = []
	a1 = []
	for i in somelist:
		if i > 0:
			if len(a1) != 0:
				result.append(a1)
			a1 = []
			a.append(i)
		else:
			a1.append(i)
			if a:
				result.append(a)
			a = []
	if a:
		result.append(a)
	else:
		result.append(a1)
	return result
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	coun = 0
	fz = sublist(l)
	for i in range(len(fz)):
		if fz[i][0] > 0:
			coun += max(fz[i])
		else:
			coun += max(fz[i])
	print(coun)


for _ in range(int(input())):
	n = int(input())
	count_even = 0
	count_odd = 0
	li = [int(x) for x in input().split()]
	ans = 0
	i = 0
	while True:
		if i == n:
			break
		if li[i] > 0:
			var = 0
			while li[i] > 0 and i < n:
				var = max(var, li[i])
				i += 1
				if i == n:
					break
			ans += var
		else:
			var = -1e+18
			if i == n:
				break
			while li[i] < 0 and i < n:
				var = max(var, li[i])
				i += 1
				if i == n:
					break
			ans += var
		if i == n:
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	c = l[0]
	l1 = []
	kk = 0
	for i in range(1, n):
		if c > 0:
			if l[i] > 0:
				if l[i] > c:
					c = l[i]
			else:
				l1.append(c)
				c = l[i]
		elif l[i] < 0:
			if l[i] > c:
				c = l[i]
		else:
			l1.append(c)
			c = l[i]
		if len(l1) >= 2:
			kkk = len(l1)
			if l1[kkk - 1] * l1[kkk - 2] > 0:
				kk = 1
				break
	l1.append(c)
	if len(l1) < 1:
		print(-1)
	elif len(l1) == 1:
		print(l1[0])
	else:
		if l1[-1] * l1[-2] > 0:
			del l1[-1]
		if kk == 1:
			print('NO')
		else:
			print(sum(l1))


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


t = int(input())
for T in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	(v1, v2) = (0, 0)
	st = -1
	if a[0] > 0:
		st = 1
	mxlen = 0
	(v1, v2) = (0, 0)
	for i in range(1, n):
		if a[i - 1] > 0 and a[i] < 0:
			v1 += 1
		if a[i - 1] < 0 and a[i] > 0:
			v1 += 1
	mxlen = v1
	ans = 0
	v1 = a[0]
	if st == -1:
		for i in range(n):
			if a[i] < 0:
				if v1 == 0:
					v1 = a[i]
				v1 = max(a[i], v1)
				ans += v2
				v2 = 0
			else:
				v2 = max(v2, a[i])
				ans += v1
				v1 = 0
		ans += v1 + v2
	else:
		for i in range(n):
			if a[i] > 0:
				v1 = max(a[i], v1)
				ans += v2
				v2 = 0
			else:
				if v2 == 0:
					v2 = a[i]
				v2 = max(v2, a[i])
				ans += v1
				v1 = 0
		ans += v1 + v2
	print(ans)


import sys
input = sys.stdin.readline
import random, bisect
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
d = defaultdict(int)
t = I()
for _ in range(t):
	n = I()
	l = L()
	mp = 0
	mn = -10 ** 30
	ans = 0
	p = 0
	q = 0
	for i in range(n):
		if i == 0:
			if l[i] > 0:
				p = 1
				mp = max(mp, l[i])
				if n == 1:
					ans = mp
			elif l[i] < 0:
				q = 1
				mn = max(mn, l[i])
				if n == 1:
					ans = mn
		elif p == 1 and l[i] > 0:
			mp = max(mp, l[i])
			if i == n - 1:
				ans += mp
		elif p == 1 and l[i] < 0:
			ans += mp
			mn = l[i]
			q = 1
			p = 0
			if i == n - 1:
				ans += mn
		elif q == 1 and l[i] < 0:
			mn = max(mn, l[i])
			if i == n - 1:
				ans += mn
		elif q == 1 and l[i] > 0:
			ans += mn
			mp = l[i]
			if i == n - 1:
				ans += mp
			p = 1
			q = 0
	print(ans)


T = int(input())
for t in range(T):
	N = int(input())
	L = list(map(int, input().split()))
	cambio = False
	segno = -L[0] / abs(L[0])
	dp = [[0, 0]]
	for n in range(0, N):
		dp.append([-1, -1])
		a = L[n]
		if a / abs(a) != segno:
			cambio = True
			segno = a / abs(a)
		else:
			cambio = False
		if segno == 1:
			if cambio:
				dp[n + 1][1] = dp[n][0] + a
			else:
				dp[n + 1][1] = max(dp[n][1], dp[n][0] + a)
			dp[n + 1][0] = dp[n][0]
		else:
			if cambio:
				dp[n + 1][0] = dp[n][1] + a
			else:
				dp[n + 1][0] = max(dp[n][0], dp[n][1] + a)
			dp[n + 1][1] = dp[n][1]
	if segno == 1:
		print(dp[N][1])
	else:
		print(dp[N][0])


tc = int(input())
for ii in range(tc):
	num = int(input())
	arr = [int(x) for x in input().split()]
	summ = 0
	index = 0
	for i in range(1, num):
		if arr[i - 1] * arr[i] < 0:
			summ += max(arr[index:i])
			index = i
		if i == num - 1:
			summ += max(arr[index:])
	print(summ if num > 1 else arr[0])


testcase = int(input())
for _ in range(testcase):
	length = input()
	a = list(map(int, input().split()))
	ans_a = []
	if a[0] > 0:
		perm = 0
	else:
		perm = 1
	cand_ans = a[0]
	for x in a:
		if x > 0:
			tmp_perm = 0
		else:
			tmp_perm = 1
		if tmp_perm == perm:
			cand_ans = max(cand_ans, x)
		else:
			ans_a.append(cand_ans)
			cand_ans = x
			perm = tmp_perm
	if len(ans_a) == 0:
		ans_a.append(cand_ans)
		print(sum(ans_a))
		continue
	last = ans_a[-1]
	if cand_ans * last < 0:
		ans_a.append(cand_ans)
	print(sum(ans_a))


n = int(input())
for i in range(0, n):
	o = int(input())
	p = input().rstrip().split(' ')
	A = 0
	S = 0
	L = []
	Q = []
	for i in range(0, len(p)):
		if i == 0:
			if int(p[i]) > 0:
				L.append(int(p[i]))
			else:
				Q.append(int(p[i]))
		elif int(p[i]) > 0:
			if len(L) != 0:
				L.append(int(p[i]))
			else:
				T = max(Q)
				S += T
				A += 1
				Q = []
				L.append(int(p[i]))
		elif len(Q) != 0:
			Q.append(int(p[i]))
		else:
			T = max(L)
			S += T
			A += 1
			L = []
			Q.append(int(p[i]))
	if len(L) != 0:
		S += max(L)
		A += 1
	elif len(Q) != 0:
		S += max(Q)
		A += 1
	print(S)


def solve_case():
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	cur_negative = None
	cur_positive = None
	if a[0] < 0:
		cur_negative = a[0]
	else:
		cur_positive = a[0]
	s = cur_negative if cur_negative is not None else cur_positive
	previous = cur_negative if cur_negative is not None else cur_positive
	for i in a:
		if i * previous < 0:
			previous = i
			s += previous
		elif i > previous:
			s -= previous
			s += i
			previous = i
	return s

def main():
	tests = int(input())
	for t in range(1, tests + 1):
		print(solve_case())
main()