package main

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

func main() {
	var i int32
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &i)
	for ; i > 0; i-- {
		var n int32
		fmt.Fscanf(inBuf, "%d\n", &n)
		var ans int64 = 0
		numbers := make([]int32, n)
		format := "%d"
		for t := int32(0); t < n; t++ {
			fmt.Fscanf(inBuf, format, &numbers[t])
			format = " %d"
		}
		fmt.Fscanf(inBuf, "\n", nil)
		flag := numbers[0] > 0
		for t := int32(0); t < n; {
			k := t
			max := numbers[t]
			for ; k <= n; k++ {
				if k == n {
					ans += int64(max)
					t = k
					break
				}
				if flag != (numbers[k] > 0) {
					flag = numbers[k] > 0
					ans += int64(max)
					t = k
					break
				}
				if numbers[k] > max {
					max = numbers[k]
				}
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	MaxLengthMaxSumSubsequence()
}

func MaxLengthMaxSumSubsequence() {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	format := "%d\n"
	for i := 0; i < n; i++ {
		var caseCount int
		fmt.Fscanf(inBuf, format, &caseCount)
		format1 := "%d"
		var valuesInCase []int64
		for j := 0; j < caseCount; j++ {
			var num int64
			fmt.Fscanf(inBuf, format1, &num)
			valuesInCase = append(valuesInCase, num)
			format1 = " %d"
		}

		fmt.Fscanf(inBuf, "\n", nil)

		suma := GetLongestAlternatingSubsequences(valuesInCase)
		fmt.Println(suma)
	}
}

func GetLongestAlternatingSubsequences(input []int64) (res int64) {
	if len(input) == 0 {
		return
	}
	numbersInSequence := []int64{input[0]}

	for i := 0; i < len(input)-1; i++ {
		if (input[i] < 0 && input[i+1] > 0) || (input[i] > 0 && input[i+1] < 0) { //
			numbersInSequence = append(numbersInSequence, input[i])
			res += GetMaxFromSlice(numbersInSequence)
			numbersInSequence = nil
		} else {
			numbersInSequence = append(numbersInSequence, input[i])
		}
	}

	numbersInSequence = append(numbersInSequence, input[len(input)-1])
	res += GetMaxFromSlice(numbersInSequence)

	return
}

func GetMaxFromSlice(input []int64) int64 {
	res := input[0]
	for i := 1; i < len(input); i++ {
		if input[i] > res {
			res = input[i]
		}
	}
	return res
}

// https://serhatgiydiren.github.io

package main

import "fmt"
import "os"
import "bufio"

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func solve(test_id int) {
 var n int
 var res int64
 fmt.Fscanf(reader, "%d\n", &n)
 x := make([]int, n)
 for i := 0 ; i < n ; i++ {
  fmt.Fscanf(reader, "%d", &x[i])
 }
 fmt.Fscanf(reader, "\n")
 var pos bool = (x[0] >= 0)
 max_value := x[0]
 for i := 1 ; i < n ; i++ {
  if pos {
   if x[i] >= 0 {
    if x[i] > max_value {
     max_value = x[i]
    }
   } else {
    res += int64(max_value)
    max_value = x[i]
    pos = false
   }
  } else {
   if x[i] < 0 {
    if x[i] > max_value {
     max_value = x[i]
    }
   } else {
    res += int64(max_value)
    max_value = x[i]
    pos = true
   }
  }
 }
 res += int64(max_value)
 fmt.Fprintf(writer, "%d\n", res)
}

func solve_cases() {
 defer writer.Flush()
 t := 1
 fmt.Fscanf(reader, "%d\n", &t)
 for i := 0 ; i < t ; i++ {
  solve(i)
 }
}

func main() {
 solve_cases()
}

package main

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

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int) int {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &t)
	for t>0 {
		t--
		//fmt.Scanln(&n)
		fmt.Fscanf(inBuf, "%d\n", &n)
		//fmt.Println("fffff: ", n)
		i:=0
		format := "%d"
		for i<n {
			//fmt.Scan(&a[i])
			fmt.Fscanf(inBuf, format, &a[i])
			format = " %d"
			//fmt.Println("ssss: ", a[i])
			i++
		}
		fmt.Fscanf(inBuf, "\n", nil)
		//fmt.Println(t, n, N)
		var sign,m int
		var sum int64
		i=0
		sum = 0
		m=0
		sign=0
		for i<n {
			if a[i]*sign<=0 {
				sum += int64(m)

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}

				m = a[i]
			} else {
				//m = abs(a[i])
				m = max(m, a[i])
			}
			i++

			//fmt.Println("saaaa: ", m, sum)
		}
		sum += int64(m)
		fmt.Println(sum)
	}
}

package main

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

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		fmt.Println(solve(n, A))
	}
}

func solve(n int, A []int) int64 {
	if n == 1 {
		return int64(A[0])
	}
	// i is for postive & j is for negative

	var res int64
	var i int
	if A[0] < 0 {
		var x int = math.MinInt32
		var j int
		for j < n && A[j] < 0 {
			x = max(x, A[j])
			j++
		}
		res = int64(x)
		i = j
	}
	var x = 0
	for i <= n {
		// i is at postive position
		if i < n && A[i] > 0 {
			x = max(x, A[i])
			i++
			continue
		}
		res += int64(x)
		x = 0
		if i == n {
			break
		}
		// A[i] < 0
		var y int = math.MinInt32
		for i < n && A[i] < 0 {
			y = max(y, A[i])
			i++
		}
		res += int64(y)
	}

	return res
}

func max(a, b int) int {
	if a >= b {
		return a
	}
	return b
}

package main

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

type WordScanner struct {
	*bufio.Scanner
}

func NewWordScanner() *WordScanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &WordScanner{Scanner: s}
}

func (s *WordScanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

func (s *WordScanner) NextInt64() int64 {
	s.Scan()
	i, _ := strconv.ParseInt(s.Text(), 10, 64)
	return i
}

func (s *WordScanner) NextString() string {
	s.Scan()
	return s.Text()
}

func main() {
	ws := NewWordScanner()
	t := ws.NextInt()

	for i := 0; i < t; i++ {
		n := ws.NextInt()

		var sum int64
		var pos bool
		max := 0
		min := -1000000001
		for j := 0; j < n; j++ {
			v := ws.NextInt()

			if v > 0 {
				if j != 0 && !pos {
					sum += int64(min)
					min = -1000000001
				}
				pos = true
				if v > max {
					max = v
				}
			} else {
				if j != 0 && pos {
					sum += int64(max)
					max = 0
				}
				pos = false
				if v > min {
					min = v
				}
			}
		}

		if pos {
			sum += int64(max)
		} else {
			sum += int64(min)
		}

		fmt.Println(sum)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1343/problem/C

package main

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

func Solve(n int, a []int) int64 {
	stack := []int{}
	for _, elem := range a {
		if len(stack) == 0 {
			stack = append(stack, elem)
			continue
		}
		last := stack[len(stack)-1]
		if last > 0 && elem < 0 || last < 0 && elem > 0 {
			stack = append(stack, elem)
		} else if elem > last {
			stack[len(stack)-1] = elem
		}
	}
	sum := int64(0)
	for _, elem := range stack {
		sum += int64(elem)
	}
	return sum
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &a[i])
		}
		fmt.Fscanf(reader, "\n")
		fmt.Fprintf(writer, "%d\n", Solve(n, a))
	}
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n := reader.NextInt64()
		aa := []int64{}
		for ; n > 0; n-- {
			aa = append(aa, reader.NextInt64())
		}
		writer.Printf("%d\n", sol(aa))
	}
}

func sol(aa []int64) int64 {
	ret := int64(0)
	curr := 0
	for curr < len(aa) {
		isNegative := aa[curr] < 0
		maxSoFar := aa[curr]
		for curr < len(aa) && aa[curr] < 0 == isNegative {
			maxSoFar = max(maxSoFar, aa[curr])
			curr++
		}
		ret += maxSoFar
	}
	return ret
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

// https://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var (
		T, n int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		fmt.Fscan(input, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &a[i])
		}

		fmt.Println(solve(a))
	}
}

func solve(a []int64) int64 {
	sum := int64(0)
	positive := a[0] > 0
	m := a[0]
	for i := 0; i < len(a); i++ {
		if positive {
			if a[i] > 0 {
				m = max(a[i], m)
			} else {
				sum += m
				m = a[i]
				positive = false
			}
		} else {
			if a[i] < 0 {
				m = max(a[i], m)
			} else {
				sum += m
				m = a[i]
				positive = true
			}
		}
	}

	return sum + m
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}

	return b
}

/*
URL:
https://codeforces.com/problemset/problem/1343/C
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int
	n int
	A []int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n = ReadInt()
		A = ReadIntSlice(n)

		solve()
	}
}

func solve() {
	ans := int64(0)

	C := make([]int, n)
	for i := 0; i < n; i++ {
		if A[i] > 0 {
			C[i] = 1
		} else {
			C[i] = -1
		}
	}

	comp, cnts := RunLengthEncoding(C)
	csum := 0
	for i := 0; i < len(cnts); i++ {
		cnt := cnts[i]
		if comp[i] > 0 {
			ans += int64(Max(A[csum : csum+cnt]...))
		} else {
			ans += int64(Max(A[csum : csum+cnt]...))
		}

		csum += cnt
	}

	fmt.Println(ans)
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// RunLengthEncoding returns encoded slice of an input.
func RunLengthEncoding(S []int) ([]int, []int) {
	runes := []int{}
	lengths := []int{}

	l := 0
	for i := 0; i < len(S); i++ {
		// 1文字目の場合保持
		if i == 0 {
			l = 1
			continue
		}

		if S[i-1] == S[i] {
			// 直前の文字と一致していればインクリメント
			l++
		} else {
			// 不一致のタイミングで追加し、長さをリセットする
			runes = append(runes, S[i-1])
			lengths = append(lengths, l)
			l = 1
		}
	}
	runes = append(runes, S[len(S)-1])
	lengths = append(lengths, l)

	return runes, lengths
}

// RunLengthDecoding decodes RLE results.
func RunLengthDecoding(S []int, L []int) []int {
	if len(S) != len(L) {
		panic("S, L are not RunLengthEncoding results")
	}

	res := []int{}

	for i := 0; i < len(S); i++ {
		for j := 0; j < L[i]; j++ {
			res = append(res, S[i])
		}
	}

	return res
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		a := ScanIntSlice(n)
		P(maxsum(a))
	}
	close(O)
	oWg.Wait()
}

/*
1
10
-2 8 3 8 -4 -15 5 -2 -3 1

*/
func maxsum(a []int) int64 {
	mxsum := int64(0)

	for i := 0; i < len(a); i++ {
		mx := a[i]
		isPos := a[i] > 0
		for j := i; j < len(a) && a[j] > 0 == isPos; j++ {
			i = j
			if a[j] > mx {
				mx = a[j]
			}
		}
		mxsum += int64(mx)
	}
	return mxsum
}

/*********************** I/O ***********************/

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int
var write *bufio.Writer = bufio.NewWriter(os.Stdout)
var read *bufio.Reader = bufio.NewReader(os.Stdin)

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func main() {
	defer write.Flush()
	var t int
	fmt.Fscan(read, &t)
	for ; t>0; t-- {
		var n int
		fmt.Fscan(read, &n)
		for i:=0; i<n; i++ {
			fmt.Fscan(read, &num[i])
		}
		//fmt.Println(num)
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			//fmt.Println("@@@@@@", num[i])
			//fmt.Println("@@@@@@", IsPositive(num[i]))
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				//fmt.Println("@@@@@@@", max_num)
				max_num = num[i]
			} else if max_num<num[i] {
				max_num = num[i]
			}
		}
		fmt.Fprintln(write, sum)
	}
	return
}
/*
4
5
1 2 3 -1 -2
1
1
*/

package main

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

func main() {
	MaxLengthMaxSumSubsequence()
}

func MaxLengthMaxSumSubsequence() {
	var n int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d\n", &n)

	format := "%d\n"
	for i := 0; i < n; i++ {
		var caseCount int
		fmt.Fscanf(inBuf, format, &caseCount)
		format1 := "%d"
		var valuesInCase []int64
		for j := 0; j < caseCount; j++ {
			var num int64
			fmt.Fscanf(inBuf, format1, &num)
			valuesInCase = append(valuesInCase, num)
			format1 = " %d"
		}

		fmt.Fscanf(inBuf, "\n", nil)

		sequence := GetLongestAlternatingSubsequences(valuesInCase)
		fmt.Println(sum(sequence))
	}
}

func StringsToInts(input []string) ([]int64, error) {
	var res []int64
	for i := 0; i < len(input); i++  {
		num, err := strconv.ParseInt(input[i], 10, 64)
		if err != nil {
			return res, err
		}
		res = append(res, num)
	}
	return res, nil
}

func GetLongestAlternatingSubsequences(input []int64) (res []int64) {
	if len(input) == 0 {
		return []int64{}
	}
	numbersInSequence := []int64{input[0]}

	for i := 0; i < len(input)-1; i++ {
		if (input[i] < 0 && input[i+1] > 0) || (input[i] > 0 && input[i+1] < 0) { //
			numbersInSequence = append(numbersInSequence, input[i])
			num := GetMaxFromSlice(numbersInSequence)
			res = append(res, num)
			numbersInSequence = nil
		} else {
			numbersInSequence = append(numbersInSequence, input[i])
		}
	}
	if len(numbersInSequence) == 0 {
		res = append(res, input[len(input)-1])
	} else if len(numbersInSequence) > 0 {
		numbersInSequence = append(numbersInSequence, input[len(input)-1])
		num := GetMaxFromSlice(numbersInSequence)
		res = append(res, num)
	}

	return
}

func sum(input []int64) int64 {
	sum := int64(0)

	for i := range input {
		sum += input[i]
	}

	return sum
}

func GetMaxFromSlice(input []int64) int64 {
	res := input[0]
	for i := 1; i < len(input); i++ {
		if input[i] > res {
			res = input[i]
		}
	}
	return res
}

package main

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

func main() {
	MaxLengthMaxSumSubsequence()
}

func MaxLengthMaxSumSubsequence() {
	inBuf := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanf(inBuf,"%d\n", &t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Fscanf(inBuf, "%d\n", &n)

		format := "%d"
		var valuesInCase []int64
		for j := 0; j < n; j++ {
			var num int64
			fmt.Fscanf(inBuf, format, &num)
			valuesInCase = append(valuesInCase, num)
			format = " %d"
		}
		fmt.Fscanf(inBuf, "\n", nil)

		suma := GetLongestAlternatingSubsequences(valuesInCase)
		fmt.Println(suma)
	}
}

func GetLongestAlternatingSubsequences(input []int64) int64 {
	var res int64
	numbersInSequence := []int64{input[0]}

	for i := 0; i < len(input)-1; i++ {
		numbersInSequence = append(numbersInSequence, input[i])
		if (input[i] < 0 && input[i+1] > 0) || (input[i] > 0 && input[i+1] < 0) {
			res += Max(numbersInSequence)
			numbersInSequence = nil
		}
	}

	numbersInSequence = append(numbersInSequence, input[len(input)-1])
	return res + Max(numbersInSequence)
}

func Max(input []int64) int64 {
	res := input[0]
	for i := 1; i < len(input); i++ {
		if input[i] > res {
			res = input[i]
		}
	}
	return res
}

// https://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var (
		T, n int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		fmt.Fscan(input, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &a[i])
		}

		fmt.Println(solve(a))
	}
}

func solve(a []int64) int64 {
	sum := a[0]
	positive := a[0] > 0
	lastp, lastn := int64(0), int64(0)
	if positive {
		lastp = a[0]
	} else {
		lastn = a[0]
	}

	for i := 1; i < len(a); i++ {
		if positive {
			if a[i] > 0 {
				if a[i] > lastp {
					sum += a[i] - lastp
					lastp = a[i]
				}
			} else {
				sum += a[i]
				lastn = a[i]
				positive = false
			}
		} else {
			if a[i] < 0 {
				if a[i] > lastn {
					sum += a[i] - lastp
					lastn = a[i]
				}
			} else {
				sum += a[i]
				lastp = a[i]
				positive = true
			}
		}
	}

	return sum
}

// https://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solve(input)
	}
}

func solve(input io.Reader) {
	var n int
	fmt.Fscan(input, &n)
	a := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
	}

	sum := a[0]
	positive := a[0] > 0
	lastp, lastn := 0, 0
	if positive {
		lastp = a[0]
	} else {
		lastn =  a[0]
	}

	for i := 1; i < n; i++ {
		if positive {
			if a[i] > 0 {
				if a[i] > lastp {
					sum += a[i] - lastp
					lastp = a[i]
				}
			} else {
				sum += a[i]
				lastn = a[i]
				positive = false
			}
		} else {
			if a[i] < 0 {
				if a[i] > lastn {
					sum += a[i] - lastp
					lastn = a[i]
				}
			} else {
				sum += a[i]
				lastp = a[i]
				positive = true
			}
		}
	}
	fmt.Println(sum)
}

func max(a, b int) int {
	if a > b {
		return a
	}

	return b
}

// https://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solve(input)
	}
}

func solve(input io.Reader) {
	var n int
	fmt.Fscan(input, &n)
	a := make([]int64, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
	}

	sum := a[0]
	positive := a[0] > 0
	lastp, lastn := int64(0), int64(0)
	if positive {
		lastp = a[0]
	} else {
		lastn = a[0]
	}

	for i := 1; i < n; i++ {
		if positive {
			if a[i] > 0 {
				if a[i] > lastp {
					sum += a[i] - lastp
					lastp = a[i]
				}
			} else {
				sum += a[i]
				lastn = a[i]
				positive = false
			}
		} else {
			if a[i] < 0 {
				if a[i] > lastn {
					sum += a[i] - lastp
					lastn = a[i]
				}
			} else {
				sum += a[i]
				lastp = a[i]
				positive = true
			}
		}
	}

	fmt.Println(sum)
}

func max(a, b int) int {
	if a > b {
		return a
	}

	return b
}

// https://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solve(input)
	}
}

func solve(input io.Reader) {
	var n int
	fmt.Fscan(input, &n)
	a := make([]int64, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
	}

	sum := a[0]
	positive := a[0] > 0
	lastp, lastn := int64(0), int64(0)
	if positive {
		lastp = a[0]
	} else {
		lastn = a[0]
	}

	for i := 1; i < n; i++ {
		if positive {
			if a[i] > 0 {
				if a[i] > lastp {
					sum += a[i] - lastp
					lastp = a[i]
				}
			} else {
				sum += a[i]
				lastn = a[i]
				positive = false
			}
		} else {
			if a[i] < 0 {
				if a[i] > lastn {
					sum += a[i] - lastp
					lastn = a[i]
				}
			} else {
				sum += a[i]
				lastp = a[i]
				positive = true
			}
		}
	}

	fmt.Println(sum)
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}

	return b
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		a := ScanIntSlice(n)
		P(maxsum(a))
	}
	close(O)
	oWg.Wait()
}

/*
1
10
-2 8 3 8 -4 -15 5 -2 -3 1

*/
func maxsum(a []int) int {
	mxsum := 0

	for i := 0; i < len(a); i++ {
		mx := a[i]
		isPos := a[i] > 0
		for j := i; j < len(a) && a[j] > 0 == isPos; j++ {
			i = j
			if a[j] > mx {
				mx = a[j]
			}
		}
		mxsum += mx
	}
	return mxsum
}

/*********************** I/O ***********************/

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
	"sync"
)

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		a := ScanIntSlice(n)
		P(maxsum(a))
	}
	close(O)
	oWg.Wait()
}

/*
1
10
-2 8 3 8 -4 -15 5 -2 -3 1

*/
func maxsum(a []int) int64 {
	mxsum := int64(0)
	isPositive := a[0] > 0
	currMax := a[0]

	for i := 1; i < len(a); i++ {
		if i == len(a)-1 {
			if a[i] > 0 == isPositive {
				if a[i] > currMax {
					currMax = a[i]
				}
				//P("+", currMax)
				mxsum += int64(currMax)
			} else {
				//P("+", currMax)
				mxsum += int64(currMax)
				//P("+", a[i])
				mxsum += int64(a[i])
			}
			//isPositive = !isPositive
			//mxsum += int64(currMax)
			//currMax = a[i]
		} else if a[i] > 0 == isPositive {
			if a[i] > currMax {
				currMax = a[i]
			}
		} else {
			isPositive = !isPositive
			//P("+", currMax)
			mxsum += int64(currMax)
			currMax = a[i]
		}
	}
	return mxsum
}

/*********************** I/O ***********************/

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	MaxLengthMaxSumSubsequence()
}

func MaxLengthMaxSumSubsequence() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()

	for scanner.Scan() {
		scanner.Scan()
		text := scanner.Text()

		inputs := strings.Split(text, " ")
		numbers, err := StringsToInts(inputs)
		if err != nil {
			fmt.Printf("error during number conversion: %v\n", err)
			os.Exit(1)
		}

		subsequence := GetLongestAlternatingSubsequences(numbers)
		fmt.Println(subsequence)
	}
}

func StringsToInts(input []string) ([]int, error) {
	var res []int
	for i := 0; i < len(input); i++  {
		num, err := strconv.Atoi(input[i])
		if err != nil {
			return res, err
		}
		res = append(res, num)
	}
	return res, nil
}

func GetLongestAlternatingSubsequences(input []int) (res []int) {
	if len(input) == 0 {
		return []int{}
	}
	numbersInSequence := []int{input[0]}

	for i := 0; i < len(input)-1; i++ {
		if (input[i] < 0 && input[i+1] > 0) || (input[i] > 0 && input[i+1] < 0) { //
			numbersInSequence = append(numbersInSequence, input[i])
			num := GetMaxFromSlice(numbersInSequence)
			res = append(res, num)
			numbersInSequence = nil
		} else {
			numbersInSequence = append(numbersInSequence, input[i])
		}
	}
	if len(numbersInSequence) == 0 {
		res = append(res, input[len(input)-1])
	} else if len(numbersInSequence) > 0 {
		numbersInSequence = append(numbersInSequence, input[len(input)-1])
		num := GetMaxFromSlice(numbersInSequence)
		res = append(res, num)
	}

	return
}

func GetMaxFromSlice(input []int) int {
	res := input[0]
	for i := 1; i < len(input); i++ {
		if input[i] > res {
			res = input[i]
		}
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	MaxLengthMaxSumSubsequence()
}

func MaxLengthMaxSumSubsequence() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()

	for scanner.Scan() {
		scanner.Scan()
		text := scanner.Text()

		inputs := strings.Split(text, " ")
		numbers, err := StringsToInts(inputs)
		if err != nil {
			fmt.Printf("error during number conversion: %v\n", err)
			os.Exit(1)
		}

		sequence := GetLongestAlternatingSubsequences(numbers)
		fmt.Println(sum(sequence))
	}
}

func StringsToInts(input []string) ([]int, error) {
	var res []int
	for i := 0; i < len(input); i++  {
		num, err := strconv.Atoi(input[i])
		if err != nil {
			return res, err
		}
		res = append(res, num)
	}
	return res, nil
}

func GetLongestAlternatingSubsequences(input []int) (res []int) {
	if len(input) == 0 {
		return []int{}
	}
	numbersInSequence := []int{input[0]}

	for i := 0; i < len(input)-1; i++ {
		if (input[i] < 0 && input[i+1] > 0) || (input[i] > 0 && input[i+1] < 0) { //
			numbersInSequence = append(numbersInSequence, input[i])
			num := GetMaxFromSlice(numbersInSequence)
			res = append(res, num)
			numbersInSequence = nil
		} else {
			numbersInSequence = append(numbersInSequence, input[i])
		}
	}
	if len(numbersInSequence) == 0 {
		res = append(res, input[len(input)-1])
	} else if len(numbersInSequence) > 0 {
		numbersInSequence = append(numbersInSequence, input[len(input)-1])
		num := GetMaxFromSlice(numbersInSequence)
		res = append(res, num)
	}

	return
}

func sum(input []int) int {
	sum := 0

	for i := range input {
		sum += input[i]
	}

	return sum
}

func GetMaxFromSlice(input []int) int {
	res := input[0]
	for i := 1; i < len(input); i++ {
		if input[i] > res {
			res = input[i]
		}
	}
	return res
}

package main

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

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int) int {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &t)
	for t>0 {
		t--
		fmt.Scanln(&n)
		//fmt.Println("fffff: ", n)
		i:=0
		format := "%d"
		for i<n {
			//fmt.Scan(&a[i])
			fmt.Fscanf(inBuf, format, &a[i])
			format = " %d"
			//fmt.Println("ssss: ", a[i])
			i++
		}
		fmt.Fscanf(inBuf, "\n", nil)
		//fmt.Println(t, n, N)
		var sign,m int
		var sum int64
		i=0
		sum = 0
		m=0
		sign=0
		for i<n {
			if a[i]*sign<=0 {
				sum += int64(m)

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}

				m = a[i]
			} else {
				//m = abs(a[i])
				m = max(m, a[i])
			}
			i++

			//fmt.Println("saaaa: ", m, sum)
		}
		sum += int64(m)
		fmt.Println(sum)
	}
}

package main

import (
	"fmt"
)

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int) int {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int
	fmt.Scanln(&t)
	for t>0 {
		t--
		fmt.Scan(&n)
		//fmt.Println("fffff: ", n)
		i:=0
		for i<n {
			fmt.Scan(&a[i])
			//fmt.Println("ssss: ", a[i])
			i++
		}
		//fmt.Println(t, n, N)
		var sign,m,sum int
		i=0
		sum = 0
		m=0
		sign=0
		for i<n {
			if a[i]*sign<=0 {
				sum += m

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}

				m = a[i]
			} else {
				//m = abs(a[i])
				m = max(m, a[i])
			}
			i++

			//fmt.Println("saaaa: ", m, sum)
		}
		sum += m
		fmt.Println(sum)
	}
}

package main

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

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int) int {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &t)
	for t>0 {
		t--
		fmt.Scan(&n)
		//fmt.Println("fffff: ", n)
		i:=0
		for i<n {
			//fmt.Scan(&a[i])
			fmt.Fscanf(inBuf, "%d\n", &a[i])
			//fmt.Println("ssss: ", a[i])
			i++
		}
		//fmt.Println(t, n, N)
		var sign,m int
		var sum int64
		i=0
		sum = 0
		m=0
		sign=0
		for i<n {
			if a[i]*sign<=0 {
				sum += int64(m)

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}

				m = a[i]
			} else {
				//m = abs(a[i])
				m = max(m, a[i])
			}
			i++

			//fmt.Println("saaaa: ", m, sum)
		}
		sum += int64(m)
		fmt.Println(sum)
	}
}

package main

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

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int) int {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &t)
	for t>0 {
		t--
		fmt.Scanln(&n)
		//fmt.Println("fffff: ", n)
		i:=0
		for i<n {
			//fmt.Scan(&a[i])
			fmt.Fscanf(inBuf, "%d", &a[i])
			//fmt.Println("ssss: ", a[i])
			i++
		}
		fmt.Fscanf(inBuf, "\n", nil)
		//fmt.Println(t, n, N)
		var sign,m int
		var sum int64
		i=0
		sum = 0
		m=0
		sign=0
		for i<n {
			if a[i]*sign<=0 {
				sum += int64(m)

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}

				m = a[i]
			} else {
				//m = abs(a[i])
				m = max(m, a[i])
			}
			i++

			//fmt.Println("saaaa: ", m, sum)
		}
		sum += int64(m)
		fmt.Println(sum)
	}
}

package main

import (
	"fmt"
)

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int) int {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int
	fmt.Scanln(&t)
	for t>0 {
		t--
		fmt.Scanln(&n)
		i:=0
		for i<n {
			fmt.Scanln(&a[i])
			i++
		}
		//fmt.Println(t, n, N)
		sign := 0
		i=0
		m := 0
		sum := 0
		for i<n {
			if a[i]*sign<=0 {
				sum += m

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}

				m = a[i]
			} else {
				//m = abs(a[i])
				m = max(m, a[i])
			}
			i++
		}
		sum += m
		fmt.Println(sum)
	}
}

package main

import (
	"fmt"
)

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int64) int64 {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int64
	fmt.Scan(&t)
	for t>0 {
		t--
		fmt.Scan(&n)
		//fmt.Println("fffff: ", n)
		i:=0
		for i<n {
			fmt.Scan(&a[i])
			//fmt.Println("ssss: ", a[i])
			i++
		}
		//fmt.Println(t, n, N)
		var sign,m,sum int64
		i=0
		sum = 0
		m=0
		sign=0
		for i<n {
			if a[i]*sign<=0 {
				sum += m

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}

				m = a[i]
			} else {
				//m = abs(a[i])
				m = max(m, a[i])
			}
			i++

			//fmt.Println("saaaa: ", m, sum)
		}
		sum += m
		fmt.Print(sum)
	}
}

package main

import (
	"fmt"
)

const N int = 2e5

func abs(a int) int {
	if a<0 {
		return -a
	} else {
		return a
	}
}
func max(a,b int) int {
	if a<b {
		return b
	} else {
		return a
	}
}
func main() {
	var t,n int
	var a [N] int
	fmt.Scanln(&t)
	for t>0 {
		t--
		fmt.Scanln(&n)
		i:=0
		for i<n {
			fmt.Scanln(&a[i])
			i++
		}
		fmt.Println(t, n, N)
		sign := 0
		i=0
		m := 0
		sum := 0
		for i<n {
			if a[i]*sign<=0 {
				sum += m*sign

				if a[i]>0 {
					sign=1
				}else {
					sign=-1
				}
				m = max(m, abs(a[i]))
			} else {
				m = abs(a[i])
				m = max(m, abs(a[i]))
			}
			i++
		}
		sum += m*sign
		fmt.Println(sum)
	}
}

package main

import (
	"fmt"
)

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func Max(a int, b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t>0; t-- {
		var n int
		fmt.Scanln(&n)
		for i:=0; i<n; i++ {
			fmt.Scanf("%d", &num[i])
		}
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			//fmt.Println("@@@@@@", num[i])
			//fmt.Println("@@@@@@", IsPositive(num[i]))
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				//fmt.Println("@@@@@@@", max_num)
				max_num = num[i]
			} else {
				max_num = Max(max_num, num[i])
			}
		}
		fmt.Println(sum)
	}
	return
}

package main

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

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int
var write *bufio.Writer = bufio.NewWriter(os.Stdout)
var read *bufio.Reader = bufio.NewReader(os.Stdin)

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func main() {
	//defer write.Flush()
	var t int
	fmt.Fscan(read, &t)
	for ; t>0; t-- {
		var n int
		fmt.Fscan(read, &n)
		for i:=0; i<n; i++ {
			fmt.Fscan(read, &num[i])
		}
		//fmt.Println(num)
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			//fmt.Println("@@@@@@", num[i])
			//fmt.Println("@@@@@@", IsPositive(num[i]))
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				//fmt.Println("@@@@@@@", max_num)
				max_num = num[i]
			} else if max_num<num[i] {
				max_num = num[i]
			}
		}
		fmt.Fprintln(write, sum)
	}
	return
}
/*
4
5
1 2 3 -1 -2
1
1
*/

package main

import (
	"fmt"
)

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func Max(a int, b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t>0; t-- {
		var n int
		fmt.Scanln(&n)
		for i:=0; i<n; i++ {
			fmt.Scanf("%d", &num[i])
		}
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			fmt.Println("@@@@@@", num[i])
			fmt.Println("@@@@@@", IsPositive(num[i]))
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				//fmt.Println("@@@@@@@", max_num)
				max_num = num[i]
			} else {
				max_num = Max(max_num, num[i])
			}
		}
		fmt.Printf("%d\n", sum)
	}
	return
}

package main

import (
	"fmt"
)

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func Max(a int, b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t>0; t-- {
		var n int
		fmt.Scanln(&n)
		for i:=0; i<n; i++ {
			fmt.Scanf("%d", &num[i])
		}
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				max_num = num[i]
			} else {
				max_num = Max(max_num, num[i])
			}
		}
		fmt.Printf("%d\n", sum)
	}
	return
}

package main

import (
	"fmt"
)

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func Max(a int, b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t>0; t-- {
		var n int
		fmt.Scanln(&n)
		for i:=0; i<n; i++ {
			fmt.Scanf("%d", &num[i])
			if i == n-1 {
				fmt.Scanf("%c", ' ')
			} else {
				fmt.Scanf("%c", '\n')
			}
		}
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			//fmt.Println("@@@@@@", num[i])
			//fmt.Println("@@@@@@", IsPositive(num[i]))
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				//fmt.Println("@@@@@@@", max_num)
				max_num = num[i]
			} else {
				max_num = Max(max_num, num[i])
			}
		}
		fmt.Println(sum)
	}
	return
}
/*
4
5
1 2 3 -1 -2
1
1
*/

package main

import (
	"fmt"
)

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func Max(a int, b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t>0; t-- {
		var n int
		fmt.Scanln(&n)
		for i:=0; i<n; i++ {
			fmt.Scan(&num[i])
		}
		//fmt.Println(num)
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			//fmt.Println("@@@@@@", num[i])
			//fmt.Println("@@@@@@", IsPositive(num[i]))
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				//fmt.Println("@@@@@@@", max_num)
				max_num = num[i]
			} else {
				max_num = Max(max_num, num[i])
			}
		}
		fmt.Println(sum)
	}
	return
}
/*
4
5
1 2 3 -1 -2
1
1
*/

package main

import (
	"fmt"
)

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func Max(a int, b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t>0; t-- {
		var n int
		fmt.Scanln(&n)
		for i:=0; i<n; i++ {
			fmt.Scanf("%d", &num[i])
		}
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				//fmt.Println("@@@@@@@@", max_num)
				max_num = num[i]
			} else {
				max_num = Max(max_num, num[i])
			}
		}
		fmt.Printf("%d\n", sum)
	}
	return
}

package main

import (
	"fmt"
)

const MAX_N = 2e5+10
const INF = 0x3f3f3f3f

var num [MAX_N]int

func IsPositive (num int) bool {
	if num > 0 {
		return true
	} else {
		return false
	}
}

func Max(a int, b int) int {
	if a>b {
		return a
	} else {
		return b
	}
}

func main() {
	var t int
	fmt.Scanln(&t)
	for ; t>0; t-- {
		var n int
		fmt.Scanln(&n)
		for i:=0; i<n; i++ {
			fmt.Scanf("%d", &num[i])
		}
		var sum int64 = 0
		var max_num int = num[0]
		for i:=1; i<=n; i++ {
			if i==n || IsPositive(num[i])!=IsPositive(num[i-1]) {
				sum += int64(max_num)
				fmt.Println("@@@@@@@", max_num)
				max_num = num[i]
			} else {
				max_num = Max(max_num, num[i])
			}
		}
		fmt.Printf("%d\n", sum)
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanln(&t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)

		ar := make([]int, n)
		for j := 0; j < n; j++ {
			var v int
			fmt.Scan(&v)
			ar[j] = v
		}

		var sum int
		max := 0
		min := -1000000001
		var pos bool
		for j := 0; j < n; j++ {
			if ar[j] > 0 {
				if j != 0 && !pos {
					sum += min
					min = -1000000001
				}
				pos = true
				if ar[j] > max {
					max = ar[j]
				}
			} else {
				if j != 0 && pos {
					sum += max
					max = 0
				}
				pos = false
				if ar[j] > min {
					min = ar[j]
				}
			}
		}

		if pos {
			sum += max
		} else {
			sum += min
		}

		fmt.Println(sum)
	}
}