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(stack)
    print(sum(stack))
