← Home
package main

import (
	"bufio"
	"fmt"
	"os"
)

type FastScanner struct {
	r *bufio.Reader
}

func NewFastScanner() *FastScanner {
	return &FastScanner{r: bufio.NewReaderSize(os.Stdin, 1<<20)}
}

func (fs *FastScanner) NextInt64() int64 {
	var sign int64 = 1
	var val int64
	c, _ := fs.r.ReadByte()
	for (c < '0' || c > '9') && c != '-' {
		c, _ = fs.r.ReadByte()
	}
	if c == '-' {
		sign = -1
		c, _ = fs.r.ReadByte()
	}
	for c >= '0' && c <= '9' {
		val = val*10 + int64(c-'0')
		c, _ = fs.r.ReadByte()
	}
	fs.r.UnreadByte()
	return val * sign
}

func max3(a, b, c int64) int64 {
	if a < b {
		a = b
	}
	if a < c {
		a = c
	}
	return a
}

func main() {
	in := NewFastScanner()
	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	defer out.Flush()

	t := int(in.NextInt64())
	for ; t > 0; t-- {
		n := int(in.NextInt64())
		a1 := in.NextInt64()
		if n == 1 {
			if a1 < 0 {
				fmt.Fprintln(out, 0)
			} else {
				fmt.Fprintln(out, a1)
			}
			continue
		}
		a2 := in.NextInt64()
		base := max3(0, a1, a1+a2)
		var tail int64
		for i := 3; i <= n; i++ {
			x := in.NextInt64()
			if x > 0 {
				tail += x
			}
		}
		fmt.Fprintln(out, tail+base)
	}
}