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