package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		var tot int64
		var f bool
		for i := 0; i < n-1; i++ {
			if !f && aa[i] > 0 {
				f = true
				tot += aa[i]
			} else if f && aa[i] == 0 {
				tot++
			} else if aa[i] > 0 {
				tot += aa[i]
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main
import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
func main() {
	defer writer.Flush()
	var t, i int
	var n, r, w int64
	var strInput string
	scanf("%d\n", &t)
	i = 0
	for i < t {
		r = 0
		w = 0
		scanf("%d\n", &n)
		scanln(&strInput)
		a := StrToIntSlice64(strInput)
		for w < n && a[w] == 0 {
			w++
		}

		for j := w; j < n - 1; {
			r += a[j]
			if a[j] == 0 {
				r++
			}
			j++
		}
		printf("%d\n", r)
		i++
	}
}

func split(str string) []string {
	return strings.Fields(str)
}

func Atoi64(str string) int64 {
	out, _ := strconv.ParseInt(str, 10, 64)
	return int64(out)
}
func StrToIntSlice64(str string) []int64 {
	sarr := split(str)
	iarr := make([]int64, len(sarr))
	for i := range sarr {
		iarr[i] = Atoi64(sarr[i])
	}
	return iarr
}

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanln(s *string) { *s, _ = reader.ReadString('\n') }

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()

	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		fmt.Fprintln(out, solve())
	}
}

func solve() int64 {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	// fmt.Println(a)

	lo := 0
	for lo < n && a[lo] == 0 {
		lo++
	}
	hi := lo + 1
	var cnt int64
	for lo < n-1 {
		// fmt.Println(lo, hi)
		for hi < n-1 && a[hi] > 0 {
			hi++
		}
		if hi == n-1 {
			cnt += a[lo]
			a[hi] += a[lo]
			a[lo] = 0
			lo++
		} else {
			cnt++
			a[hi]++
			a[lo]--
			for lo < n-1 && a[lo] == 0 {
				lo++
			}
			if lo == n-1 {
				break
			}
		}
	}
	return cnt
}

package main

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

func solve() {
	n := get()
	a := geta(n)
	var ans int64
	for i := 0; i < n-1; i++ {
		if ans > 0 && a[i] == 0 {
			ans++
		}
		ans += int64(a[i])
	}
	put64ln(ans)
}

const (
	once       = false
	maxBufSize = 0 // 10_000_000
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)
	if maxBufSize > 0 {
		in.Buffer(make([]byte, maxBufSize), maxBufSize)
	}

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	for t := get(); t > 0; t-- {
		solve()
	}
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geti64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

func putaln(a []int) {
	for _, v := range a {
		put(v)
		putc(' ')
	}
	putc('\n')
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func put64ln(x int64) {
	put64(x)
	putc('\n')
}

func putln(x int) {
	put(x)
	putc('\n')
}

func putc(c rune) {
	out.WriteRune(c)
}

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

func abs(x int) int {
	if x < 0 {
		return x * -1
	}
	return x
}

package main

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

type CF1705B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1705B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1705B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1705B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1705B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1705B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1705B
Date: 24/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1705/B
Problem: CF1705B
**/
func (in *CF1705B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		ans := int64(0)
		for i := 0; i < n; i++ {
			val := in.NextInt64()
			if i+1 < n {
				if val > 0 {
					ans += val
				}
				if val == 0 && ans > 0 {
					ans++
				}
			}

		}
		fmt.Println(ans)
	}
}

func NewCF1705B(r *bufio.Reader) *CF1705B {
	return &CF1705B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1705B(bufio.NewReader(os.Stdin)).Run()
}

// 1705B. Mark the Dust Sweeper
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		start_count := false
		count := 0
		sm := int64(0)
		for i := 0; i < n-1; i++ {
			if a[i] != 0 {
				start_count = true
				sm += int64(a[i])
			} else if start_count {
				count++
			}
		}
		fmt.Println(int64(count) + sm)

	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	nums := make([]int64, n)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
	}
	flag := false
	sum := int64(0)
	cnt := int64(0)
	for i:=0;i<n-1;i++ {
		if nums[i] > 0 {
			flag = true
		}
		sum += nums[i]
		if nums[i] == 0 && flag {
			cnt++
		}
	}
	fmt.Fprintln(ww, sum + cnt)
}

func main() {
	defer ww.Flush()
	var n int
	fmt.Fscan(rr, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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 solve(A []int) int64 {
	// [i...j) > 0
	// a[i] - 1, a[j] + 1
	// suppose to dec a[i], the quick to reach n, the better
	// for any a[j] = 0, we need to find some i before it, and make it to 0
	n := len(A)
	var res int64
	var i int
	for j := 0; j < n-1; j++ {
		if A[j] > 0 {
			continue
		}
		// need to fill it first
		for i < j && A[i] == 0 {
			i++
		}
		if i == j {
			continue
		}
		// A[i] > 0
		res++
		A[i]--
		A[j]++
	}

	for i := 0; i < n-1; i++ {
		res += int64(A[i])
	}

	return res
}

package main

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

const MOD int64 = 1000000007

var n int

var a[]int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n)

		a = make([]int, n)

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

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int64 {

	var s int64

	for i := 0; i < n - 1; i++ {
		s += int64(a[i])
	}

	first := -1
	for i := 0; i < n - 1; i ++ {
		if a[i] > 0 {
			first = i
			break
		}
	}

	if first != -1 {

		for i := first + 1; i < n - 1; i++ {
			if a[i] == 0 {
				a[i]++
				a[first]--
				s++
			}
			if a[first] == 0 {

				for j := first + 1; j < n - 1; j++ {
					if a[j] > 0 {
						first = j
						break
					}
				}
			}
		}

	}

	return s
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

func main() {
	CF1043B(os.Stdin, os.Stdout)
}

func CF1043B(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n int64
		fmt.Fscan(r, &n)
		arr := make([]int64, n)
		flag, ans := false, int64(0)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(r, &arr[i])
			if !flag && arr[i] == 0 {
				continue
			}
			if i == n-1 {
				break
			}
			flag = true
			if arr[i] > 0 {
				ans += arr[i]
			} else {
				ans += 1
			}
		}

		fmt.Fprintln(w, ans)
	}

}

package main

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

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

	n := readInt(in)
	for i := 0; i < n; i++ {
		solution(in, out)
	}
}

func solution(in *bufio.Reader, out *bufio.Writer) {
	v := readArrInt(in)
	n := v[0]
	arr := readArrInt(in)
	sum := int64(0)
	seen := false
	for i := 0; i < n-1; i++ {
		if arr[i] == 0 && seen {
			sum += 1
		} else if arr[i] != 0 {
			seen = true
		}
	}

	for i := 0; i < n-1; i++ {
		sum += int64(arr[i])
	}

	out.WriteString(strconv.FormatInt(sum, 10) + "\n")

}

func Max(sum int, sum2 int) int {
	if sum > sum2 {
		return sum
	}
	return sum2

}

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

func solve() {
	n := get()
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}

	i := 0
	j := 0
	count := int64(0)
	for i+1 < n && j < n {
		if a[i] == 0 {
			i++
			continue
		}
		if i >= j {
			j = i + 1
		}
		for j < n && a[j] > 0 {
			j++
		}
		if j == n {
			break
		}
		for j < n && a[j] == 0 && a[i] > 0 {
			a[i]--
			a[j]++
			j++
			count++
		}
	}

	for i+1 < n {
		count += a[i]
		i++
	}

	put64(count)
	line()
}

const (
	once       = false
	maxBufSize = 10000000
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func gets() string {
	in.Scan()
	return in.Text()
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func puts(s string) {
	out.WriteString(s)
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES")
	line()
}

func no() {
	puts("NO")
	line()
}

//func putf(x float64) {
//	out.WriteString(strconv.FormatFloat(x))
//}

func line() {
	out.WriteRune('\n')
}

func space() {
	out.WriteRune(' ')
}

package main

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

var _, _, _ = main, rw, do

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	rw(r, w)
}

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		b := make([]int64, n)
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		w1 := do(n, b)
		fmt.Fprintln(w, w1)
	}
}

func do(n int, b []int64) int64 {
	// log.Print(b)
	var k int
	for k < n && b[k] == 0 {
		k++
	}
	var c int64
	var ans int64
	for i := n - 2; i >= k; i-- {
		if b[i] != 0 {
			if b[i] > c {
				ans += c
				c = 0
			} else if b[i] > 1 {
				ans += b[i] - 1
				c -= b[i] - 1
			}
		} else {
			c++
		}
	}
	// log.Print(b)
	// log.Print(c)
	for i := n - 2; i >= k; i-- {
		ans += b[i]
	}
	return ans + c
}

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

func main() {
    in := bufio.NewReader(os.Stdin)
    testCase := readInt(in)
    for testCase > 0 {
        n := readInt(in)
        var res int64
        res = 0
        nums := readArrInt(in)
        for i:=0; i<n; i++ {
            v := nums[i]
            if i == n-1 {
                break
            }
            if v > 0 {
                res = res + int64(v)
            }
            if v == 0 && res > 0 {
                res++
            }
        }

        fmt.Println(res)

        testCase--
    }
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	a      []int64
)

func min(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func solve(n int, a []int64) int64 {
	var s = 0
	for a[s] == 0 && s < n-1 {
		s++
	}

	var result = int64(0)
	for i := s; i < n-1; i++ {
		result += min(1, a[i])
	}

	return result
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a = make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

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

func main() {
    in := bufio.NewReader(os.Stdin)
    testCase := readInt(in)
    for testCase > 0 {
        n := readInt(in)
        res := 0
        nums := readArrInt(in)
        zeroCount := 0
        for i:=0; i<n; i++ {
            v := nums[i]
            if i == n-1 {
                break
            }
            if v > 0 {
                res += v
                res += zeroCount
                zeroCount = 0
            }
            if v == 0 && res > 0 {
                zeroCount++
            }
        }
        res += zeroCount

        fmt.Println(res)

        testCase--
    }
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

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

func main() {
    in := bufio.NewReader(os.Stdin)
    testCase := readInt(in)
    for testCase > 0 {
        n := readInt(in)
        res := 0
        nums := readArrInt(in)
        for i:=0; i<n; i++ {
            v := nums[i]
            if i == n-1 {
                break
            }

            if v > 0 {
                res += v
            }
            if v == 0 && i > 0 && res > 0 {
                res += 1
            }
        }
        fmt.Println(res)

        testCase--
    }
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main
import "fmt"

func main() {

    var testCase int
    fmt.Scan(&testCase)
    for testCase > 0 {

        var n int

        fmt.Scan(&n)
        res := 0
        for i:=0; i<n; i++ {
            var v int
            fmt.Scan(&v)
            if v > 0 && i != n-1 {
                res += v
            }
            if v == 0 && i > 0 && res > 0 {
                res++
            }
        }
        fmt.Println(res)

        testCase--
    }
}

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

const NL = '\n'

func main() {
	defer writer.Flush()
	var t, i, n, w, r int
	var strInput string
	scanf("%d\n", &t)
	i = 0
	for i < t {
		r = 0
		w = 0
		scanf("%d\n", &n)
		scanln(&strInput)
		a := StrToIntSlice(strInput)
		for w < n && a[w] == 0 {
			w++
		}

		for j := w; j < n - 1; {
			r += a[j]
			if a[j] == 0 {
				r++
			}
			j++
		}
		printf("%d\n", r)
		i++
	}
}
func split(str string) []string {
	return strings.Fields(str)
}

func Atoi(str string) int {
	out, _ := strconv.ParseInt(str, 10, 64)
	return int(out)
}

func StrToIntSlice(str string) []int {
	sarr := split(str)
	iarr := make([]int, len(sarr))
	for i := range sarr {
		iarr[i] = Atoi(sarr[i])
	}
	return iarr
}

func firstZero(arr []int) int {
	for idx := 0; idx < len(arr); idx++ {
		if arr[idx] == 0 {
			return idx
		}
	}
	return -1
}

func lastZero(arr []int) int {
	for idx := len(arr) - 1; idx > -1; idx-- {
		if arr[idx] == 0 {
			return idx
		}
	}
	return -1
}

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanln(s *string) { *s, _ = reader.ReadString('\n') }

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		tot := aa[n-2]
		for i := n - 3; i >= 0; i-- {
			if aa[i] > 0 {
				tot += aa[i] + 1
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int64, n)
		var c int
		var tot int64
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
			if i < n-2 && aa[i] > 0 {
				c++
			}
			if i < n-1 {
				tot += aa[i]
			}
		}
		fmt.Fprintln(out, tot+int64(c))

	}
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()

	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		fmt.Fprintln(out, solve())
	}
}

func solve() int64 {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	fmt.Println(a)

	lo := 0
	for lo < n && a[lo] == 0 {
		lo++
	}
	hi := lo + 1
	var cnt int64
	for lo < n-1 {
		// fmt.Println(lo, hi)
		for hi < n-1 && a[hi] > 0 {
			hi++
		}
		if hi == n-1 {
			cnt += a[lo]
			a[hi] += a[lo]
			a[lo] = 0
			lo++
		} else {
			cnt++
			a[hi]++
			a[lo]--
			for lo < n-1 && a[lo] == 0 {
				lo++
			}
			if lo == n-1 {
				break
			}
		}
	}
	return cnt
}

package main

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

func solve() {
	n := get()
	a := geta(n)
	var ans int
	for i := 0; i < n-1; i++ {
		if ans > 0 && a[i] == 0 {
			ans++
		}
		ans += a[i]
	}
	putln(ans)
}

const (
	once       = false
	maxBufSize = 0 // 10_000_000
)

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)
	if maxBufSize > 0 {
		in.Buffer(make([]byte, maxBufSize), maxBufSize)
	}

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	for t := get(); t > 0; t-- {
		solve()
	}
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geti64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scan() {
	in.Scan()
}

func putaln(a []int) {
	for _, v := range a {
		put(v)
		putc(' ')
	}
	putc('\n')
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func put64ln(x int64) {
	put64(x)
	putc('\n')
}

func putln(x int) {
	put(x)
	putc('\n')
}

func putc(c rune) {
	out.WriteRune(c)
}

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

func abs(x int) int {
	if x < 0 {
		return x * -1
	}
	return x
}

package main

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

type CF1705B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1705B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1705B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1705B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1705B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1705B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1705B
Date: 24/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1705/B
Problem: CF1705B
**/
func (in *CF1705B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		ans := 0
		for i := 0; i < n; i++ {
			val := in.NextInt()
			if i+1 < n {
				if val > 0 {
					ans += val
				}
				if val == 0 && ans > 0 {
					ans++
				}
			}

		}
		fmt.Println(ans)
	}
}

func NewCF1705B(r *bufio.Reader) *CF1705B {
	return &CF1705B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1705B(bufio.NewReader(os.Stdin)).Run()
}

// 1705B. Mark the Dust Sweeper
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		start_count := false
		count := 0
		sm := 0
		for i := 0; i < n-1; i++ {
			if a[i] != 0 {
				start_count = true
				sm += a[i]
			} else if start_count {
				count++
			}
		}
		fmt.Println(count + sm)

	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	nums := make([]int, n)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
	}
	flag := false
	sum := 0
	cnt := 0
	for i:=0;i<n-1;i++ {
		if nums[i] > 0 {
			flag = true
		}
		sum += nums[i]
		if nums[i] == 0 && flag {
			cnt++
		}
	}
	fmt.Fprintln(ww, sum + cnt)
}

func main() {
	defer ww.Flush()
	var n int
	fmt.Fscan(rr, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}


package main

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

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

	s, _ = reader.ReadString('\n')
	tests, _ := strconv.Atoi(s[:len(s)-2])

	var n, m, x, y, ret int
	var a, e []int
	for ; tests > 0; tests-- {
		s, _ = reader.ReadString(' ')
		n, _ = strconv.Atoi(s[:len(s)-1]) // members

		s, _ = reader.ReadString('\n')
		m, _ = strconv.Atoi(s[:len(s)-2]) // pair of friends

		a = make([]int, n)
		e = make([]int, n)
		ret = 100000

		for i := 0; i < n-1; i++ {
			s, _ = reader.ReadString(' ')
			a[i], _ = strconv.Atoi(s[:len(s)-1])
		}

		s, _ = reader.ReadString('\n')
		a[n-1], _ = strconv.Atoi(s[:len(s)-2])

		for j := 0; j < m; j++ {
			s, _ = reader.ReadString(' ')
			x, _ = strconv.Atoi(s[:len(s)-1])

			s, _ = reader.ReadString('\n')
			y, _ = strconv.Atoi(s[:len(s)-2])
			e[x-1]++
			e[y-1]++
			ret = min(ret, a[x-1]+a[y-1]) // cakes must be even so worst case one pair is not invited

		}

		if m%2 == 0 {
			fmt.Println(0)
			continue
		}

		for i := 0; i < n; i++ {
			if e[i]%2 == 1 {
				// m is odd so one member must be removed regardless,
				// remove the guy with only 1 friend
				ret = min(ret, a[i])
			}
		}
		fmt.Println(ret)

	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

var bufferedStdin = bufio.NewReader(os.Stdin)
var bufferedStdout = bufio.NewWriter(os.Stdout)

func readInt() int {
	var value int
	_, _ = fmt.Fscan(bufferedStdin, &value)
	return value
}

func readString() string {
	var value string
	_, _ = fmt.Fscan(bufferedStdin, &value)
	return value
}

func readInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	return a
}

func printf(format string, value ...interface{}) {
	_, _ = fmt.Fprintf(bufferedStdout, format, value...)
}

func printErr(format string, value ...interface{}) {
	_, _ = fmt.Fprintf(os.Stderr, format, value...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func solve() {
	var n, m = readInt(), readInt()
	a := readInts(n)
	g := make([][]int, n)
	for i := 0; i < m; i++ {
		x, y := readInt(), readInt()
		g[x-1] = append(g[x-1], y-1)
		g[y-1] = append(g[y-1], x-1)
	}
	if m%2 == 0 {
		printf("%d\n", 0)
		return
	}
	ans := int(1e9)

	for ind, value := range g {
		if len(value)%2 == 1 {
			ans = min(ans, a[ind])
		}
	}
	for i, value := range g {
		for _, value2 := range value {
			if (len(value)+len(g[value2])-1)%2 == 1 {
				ans = min(ans, a[i]+a[value2])
				//printErr("%d, %d, %d\n", i, value2, ans)
			}
		}
	}
	printf("%d\n", ans)
}

func main() {
	var t = 1
	_, _ = fmt.Fscan(bufferedStdin, &t)
	for i := 0; i < t; i++ {
		solve()
	}
	_ = bufferedStdout.Flush()
}

package main

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

func A3() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, m int

		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		b := make([][]int, m)
		cnt := make(map[int]int)
		for i := 0; i < m; i++ {
			var x, y int
			Fscan(in, &x, &y)
			cnt[x]++
			cnt[y]++
			b[i] = []int{x, y}
		}
		if (m & 1) == 0 {
			Fprintln(out, 0)
			return
		}
		var ans = int(1e9)
		for i := 1; i <= n; i++ {
			if (cnt[i] & 1) == 1 {
				if a[i-1] < ans {
					ans = a[i-1]
				}
			}
		}
		for i := 0; i < m; i++ {
			t := b[i]
			if (cnt[t[0]]+cnt[t[1]])%2 == 0 {
				if a[t[0]-1]+a[t[1]-1] < ans {
					ans = a[t[0]-1] + a[t[1]-1]
				}
			}
		}
		Fprintln(out, ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A3() }

package main

import (
	"bufio"
	"math"
	"os"
	"strconv"
)

func solve() {
	n := get()
	m := get()
	if m%2 == 0 {
		putsln("0")
		scanN(n + 2*m)
		return
	}
	a := geta(n)
	g := make([][]int, n)
	for i := 0; i < m; i++ {
		v := get() - 1
		u := get() - 1
		g[v] = append(g[v], u)
		g[u] = append(g[u], v)
	}
	best := 1_000_000_000
	for i, v := range a {
		if len(g[i])%2 == 1 {
			best = min(best, v)
			continue
		}
		for _, j := range g[i] {
			if len(g[j])%2 == 0 {
				best = min(best, v+a[j])
			}
		}
	}
	putln(best)
}

const (
	once       = false
	maxBufSize = 0 //10000000
)

var (
	splitFunc = bufio.ScanWords //bufio.SplitFunc(nil)
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	if splitFunc != nil {
		in.Split(splitFunc)
	}
	if maxBufSize > 0 {
		in.Buffer(make([]byte, maxBufSize), maxBufSize)
	}

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func geta1(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get() - 1
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func line() {
	putc('\n')
}

func space() {
	putc(' ')
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

type IntTp interface {
	int | int64
}

type Tp interface {
	IntTp | float64
}

func abs[T IntTp](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func fabs(x float64) float64 {
	return math.Abs(x)
}

func max1[T Tp](x T, y T) T {
	if x > y {
		return x
	}
	return y
}

func max[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = max1(r, x)
	}
	return r
}

func maxa[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] <= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func min1[T Tp](x T, y T) T {
	if x < y {
		return x
	}
	return y
}

func min[T Tp](xs ...T) T {
	r := xs[0]
	for _, x := range xs[1:] {
		r = min1(r, x)
	}
	return r
}

func mina[T Tp](a []T) (i int, val T) {
	i, val = 0, a[0]
	for j := 1; j < len(a); j++ {
		if a[j] >= val {
			continue
		}
		i = j
		val = a[j]
	}
	return
}

func L(i int) int64 {
	return int64(i)
}

func F(i int) float64 {
	return float64(i)
}

func ints(n int, val int) (a []int) {
	a = make([]int, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func int64s(n int, val int64) (a []int64) {
	a = make([]int64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func floats(n int, val float64) (a []float64) {
	a = make([]float64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

const heapBase = 4

func children(i int) (int, int) {
	start := heapBase * i
	return start + 1, start + (1 + heapBase)
}

func parent(i int) int {
	return (i - 1) / heapBase
}

package main

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

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		bb := make([]int, n)
		cc := make([][]int, m)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			x--
			y--
			if x > y {
				x, y = y, x
			}
			bb[x]++
			bb[y]++
			cc[i] = []int{x, y}
		}

		if m%2 == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		tot := int64(math.MaxInt32)
		for _, e := range cc {
			if bb[e[0]]%2 != 0 {
				tot = min(tot, aa[e[0]])
			}
			if bb[e[1]]%2 != 0 {
				tot = min(tot, aa[e[1]])
			}
			if bb[e[0]]%2 == 0 && bb[e[1]]%2 == 0 {
				tot = min(tot, aa[e[0]]+aa[e[1]])
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

type Graph struct {
	n int
	m int
	edges [][]int
	costs []int
}

func remove(s []int, el int) []int {
	var indx = -1
	for i := 0; i < len(s); i++ {
		if s[i] == el {
			indx = i;
			break;
		}
	}
	if indx == -1 {
		return s;
	}
    s[indx] = s[len(s)-1]
    return s[:len(s)-1]
}

func (g *Graph) Add(v, u int) {
	g.edges[v] = append(g.edges[v], u)
	g.edges[u] = append(g.edges[u], v)
}

func (g *Graph) Min() int {
	if g.m % 2 == 0 {
		return 0
	}
	var firstMin = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 1 {
			if g.costs[i] < firstMin {
				firstMin = g.costs[i]
			}
		}
	}

	var answer = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 0	{
			for _, u := range g.edges[i] {
				if len(g.edges[u]) % 2 == 0 {
					var curr = g.costs[i] + g.costs[u]
					if curr < answer {
						answer = curr
					}
				}
			}
		}
	}

	if firstMin < answer {
		return firstMin
	}

	return answer
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)

	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var costs = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &costs[i])
		}
		var graph = Graph {
			n: n,
			m: m,
			edges: make([][]int, n),
			costs: costs,
		}
		for i := 0; i < m; i++ {
			var from, to int
			fmt.Fscan(reader, &from, &to)
			graph.Add(from - 1, to - 1)
		}

		fmt.Println(graph.Min())
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var s string
	s, _ = reader.ReadString('\n')

	tests, _ := strconv.Atoi(s[:len(s)-2])
	var n, m, x, y, ret int
	var a, e []int
	var p [][]int
	for ; tests > 0; tests-- {
		s, _ = reader.ReadString(' ')
		n, _ = strconv.Atoi(s[:len(s)-1]) // members

		s, _ = reader.ReadString('\n')
		m, _ = strconv.Atoi(s[:len(s)-2]) // pair of friends

		a = make([]int, n)
		e = make([]int, n)
		p = make([][]int, m)
		ret = 100000

		for i := 0; i < n-1; i++ {
			s, _ = reader.ReadString(' ')
			a[i], _ = strconv.Atoi(s[:len(s)-1])
		}

		s, _ = reader.ReadString('\n')
		a[n-1], _ = strconv.Atoi(s[:len(s)-2])

		for j := 0; j < m; j++ {
			s, _ = reader.ReadString(' ')
			x, _ = strconv.Atoi(s[:len(s)-1])

			s, _ = reader.ReadString('\n')
			y, _ = strconv.Atoi(s[:len(s)-2])
			e[x-1]++
			e[y-1]++
			p[j] = []int{x - 1, y - 1}
		}

		if m%2 == 0 {
			fmt.Println(0)
			continue
		}

		for i := 0; i < m; i++ {
			if (e[p[i][0]]+e[p[i][1]])%2 == 0 { //removing an odd number of cakes
				ret = min(ret, a[p[i][0]]+a[p[i][1]])
			}
		}

		for i := 0; i < n; i++ {
			if e[i]%2 == 1 {
				// m is odd so one member must be removed regardless,
				// remove the guy with an odd number of friends
				ret = min(ret, a[i])
			}
		}
		fmt.Println(ret)

	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		A := readNNums(reader, n)
		edges := make([][]int, m)
		for i := 0; i < m; i++ {
			edges[i] = readNNums(reader, 2)
		}
		res := solve(n, A, edges)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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
}

const INF = 1 << 30

func solve(n int, A []int, edges [][]int) int {
	m := len(edges)

	if m&1 == 0 {
		return 0
	}
	deg := make([]int, n)

	for _, edge := range edges {
		u, v := edge[0], edge[1]
		u--
		v--
		deg[u]++
		deg[v]++
	}

	odd_deg_min := INF

	for i := 0; i < n; i++ {
		if deg[i]&1 == 1 {
			if odd_deg_min > A[i] {
				odd_deg_min = A[i]
			}
		}
	}

	even_pair_min := INF
	for _, edge := range edges {
		u, v := edge[0]-1, edge[1]-1
		if deg[u]&1 == 0 && deg[v]&1 == 0 {
			if even_pair_min > A[u]+A[v] {
				even_pair_min = A[u] + A[v]
			}
		}
	}

	return min(odd_deg_min, even_pair_min)
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}

package main

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

type edge struct {
	u int
	v int
}

func min(a, b int) int {
	if a < b { return a }
	return b
}

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n, m, u, v, res int
		fmt.Fscan(r, &n, &m)

		a := make([]int, n)
		for i := range a { fmt.Fscan(r, &a[i]) }

		d := make([]int, n)
		e := make([]edge, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(r, &u, &v)
			e[i] = edge { u-1, v-1 }
			d[u-1]++
			d[v-1]++
		}

		if m % 2 == 1 {
			res = int(1e11)
			for i, v := range a {
				if d[i] % 2 == 1 { res = min(res, v) }
			}

			for _, ed := range e {
				if (d[ed.u] + d[ed.v]) % 2 == 0 {
					res = min(res, a[ed.u] + a[ed.v])
				}
			}
		}

		fmt.Fprintln(w, res)
	}

}

package main

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

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([][]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			aa[i] = []int{i, v}
		}

		bb := make([]int, n)
		mm := make(map[string]bool, m)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			x--
			y--
			if x > y {
				x, y = y, x
			}
			bb[x]++
			bb[y]++
			mm[fmt.Sprintf("%d-%d", x, y)] = true
		}
		sort.Slice(aa, func(i, j int) bool {
			if aa[i][1] == aa[j][1] {
				return bb[aa[i][0]] > bb[aa[j][0]]
			}
			return aa[i][1] < aa[j][1]
		})

		if m%2 == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		tot := int64(math.MaxInt32)
		for _, e := range aa {
			if bb[e[0]]%2 == 0 {
				continue
			}
			tot = min(tot, int64(e[1]))
		}

	Loop:
		for i := 0; i < n-1; i++ {
			for j := i + 1; j < n; j++ {
				if bb[aa[i][0]]%2 != 0 || bb[aa[j][0]]%2 != 0 || !mm[fmt.Sprintf("%d-%d", i, j)] {
					continue
				}
				d := int64(aa[i][1] + aa[j][1])
				tot = min(tot, d)
				break Loop

			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([][]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			aa[i] = []int{i, v}
		}

		bb := make([]int, n)
		mm := make(map[string]bool, m)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			x--
			y--
			if x > y {
				x, y = y, x
			}
			bb[x]++
			bb[y]++
			mm[fmt.Sprintf("%d-%d", x, y)] = true
		}
		sort.Slice(aa, func(i, j int) bool {
			if aa[i][1] == aa[j][1] {
				return bb[aa[i][0]] > bb[aa[j][0]]
			}
			return aa[i][1] < aa[j][1]
		})

		if m%2 == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		tot := int64(math.MaxInt32)
		for _, e := range aa {
			if bb[e[0]]%2 == 0 {
				continue
			}
			tot = min(tot, int64(e[1]))
		}

	Loop:
		for i := 0; i < n-1; i++ {
			for j := i + 1; j < n; j++ {
				if bb[i]%2 != 0 || bb[j]%2 != 0 || !mm[fmt.Sprintf("%d-%d", i, j)] {
					continue
				}
				d := int64(aa[i][1] + aa[j][1])
				tot = min(tot, d)
				break Loop

			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([][]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			aa[i] = []int{i, v}
		}

		bb := make([]int, n)
		mm := make(map[string]bool, m)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			x--
			y--
			if x > y {
				x, y = y, x
			}
			bb[x]++
			bb[y]++
			mm[fmt.Sprintf("%d-%d", x, y)] = true
		}
		sort.Slice(aa, func(i, j int) bool {
			if aa[i][1] == aa[j][1] {
				return bb[aa[i][0]] > bb[aa[j][0]]
			}
			return aa[i][1] < aa[j][1]
		})

		if m%2 == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		tot := int64(math.MaxInt32)
		for _, e := range aa {
			if bb[e[0]]%2 == 0 {
				continue
			}
			tot = min(tot, int64(e[1]))
		}

		for i := 1; i < len(aa); i++ {
			x, y := aa[i-1][0], aa[i][0]
			if x > y {
				x, y = y, x
			}
			if bb[x]%2 != 0 || bb[y]%2 != 0 || !mm[fmt.Sprintf("%d-%d", x, y)] {
				continue
			}

			d := int64(aa[i-1][1] + aa[i][1])
			tot = min(tot, d)
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([][]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			aa[i] = []int{i, v}
		}

		bb := make([]int, n)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			bb[x-1]++
			bb[y-1]++
		}
		sort.Slice(aa, func(i, j int) bool {
			if aa[i][1] == aa[j][1] {
				return bb[aa[i][0]] > bb[aa[j][0]]
			}
			return aa[i][1] < aa[j][1]
		})

		if m%2 == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		tot := int64(math.MaxInt32)
		for _, e := range aa {
			if bb[e[0]]%2 == 0 {
				continue
			}
			tot = min(tot, int64(e[1]))
		}

		for i := 1; i < len(aa); i++ {
			e1, e2 := aa[i-1], aa[i]
			if bb[e1[0]]%2 != 0 || bb[e2[0]]%2 != 0 || bb[e1[0]] == 0 || bb[e2[0]] == 0 {
				continue
			}
			d := int64(aa[i-1][1] + aa[i][1])
			if d > tot {
				break
			}
			tot = min(tot, d)
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		aa := make([][]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			aa[i] = []int{i, v}
		}

		bb := make([]int, n)
		for i := 0; i < m; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			bb[x-1]++
			bb[y-1]++
		}
		sort.Slice(aa, func(i, j int) bool {
			if aa[i][1] == aa[j][1] {
				return bb[aa[i][0]] > bb[aa[j][0]]
			}
			return aa[i][1] < aa[j][1]
		})

		if m%2 == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		tot := int64(math.MaxInt32)
		for _, e := range aa {
			if bb[e[0]]%2 == 0 {
				continue
			}
			tot = min(tot, int64(e[1]))
		}

		for i := 1; i < len(aa); i++ {
			if bb[aa[i-1][0]]%2 != 0 || bb[aa[i][0]]%2 != 0 {
				continue
			}
			d := int64(aa[i-1][1] + aa[i][1])
			if d > tot {
				break
			}
			tot = min(tot, d)
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

type Graph struct {
	n int
	m int
	edges [][]int
	costs []int
}

func remove(s []int, el int) []int {
	var indx = -1
	for i := 0; i < len(s); i++ {
		if s[i] == el {
			indx = i;
			break;
		}
	}
	if indx == -1 {
		return s;
	}
    s[indx] = s[len(s)-1]
    return s[:len(s)-1]
}

func (g *Graph) Add(v, u int) {
	g.edges[v] = append(g.edges[v], 1)
	g.edges[u] = append(g.edges[u], v)
}

func (g *Graph) Min() int {
	if g.m % 2 == 0 {
		return 0
	}
	var firstMin = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 1 {
			if g.costs[i] < firstMin {
				firstMin = g.costs[i]
			}
		}
	}

	var answer = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 0	{
			for _, u := range g.edges[i] {
				if len(g.edges[u]) % 2 == 0 {
					var curr = g.costs[i] + g.costs[u]
					if curr < answer {
						answer = curr
					}
				}
			}
		}
	}

	if firstMin < answer {
		return firstMin
	}

	return answer
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)

	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var costs = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &costs[i])
		}
		var graph = Graph {
			n: n,
			m: m,
			edges: make([][]int, n),
			costs: costs,
		}
		for i := 0; i < m; i++ {
			var from, to int
			fmt.Fscan(reader, &from, &to)
			graph.Add(from - 1, to - 1)
		}

		fmt.Println(graph.Min())
	}
}

package main

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

type Graph struct {
	n int
	m int
	edges [][]int
	costs []int
}

func remove(s []int, el int) []int {
	var indx = -1
	for i := 0; i < len(s); i++ {
		if s[i] == el {
			indx = i;
			break;
		}
	}
	if indx == -1 {
		return s;
	}
    s[indx] = s[len(s)-1]
    return s[:len(s)-1]
}

func (g *Graph) Add(v, u int) {
	g.edges[v] = append(g.edges[v], 1)
	g.edges[u] = append(g.edges[u], v)
}

func (g *Graph) Min() int {
	if g.m % 2 == 0 {
		return 0
	}
	var firstMin = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 1 {
			if g.costs[i] < firstMin {
				firstMin = g.costs[i]
			}
		}
	}

	var answer = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 0	{
			for _, u := range g.edges[i] {
				if len(g.edges[u]) % 2 == 1 {
					var curr = g.costs[i] + g.costs[u]
					if curr < answer {
						answer = curr
					}
				}
			}
		}
	}

	if firstMin < answer {
		return firstMin
	}

	return answer
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)

	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var costs = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &costs[i])
		}
		var graph = Graph {
			n: n,
			m: m,
			edges: make([][]int, n),
			costs: costs,
		}
		for i := 0; i < m; i++ {
			var from, to int
			fmt.Fscan(reader, &from, &to)
			graph.Add(from - 1, to - 1)
		}

		fmt.Println(graph.Min())
	}
}

package main

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

type Graph struct {
	n int
	m int
	edges [][]int
	costs []int
}

func remove(s []int, el int) []int {
	var indx = -1
	for i := 0; i < len(s); i++ {
		if s[i] == el {
			indx = i;
			break;
		}
	}
	if indx == -1 {
		return s;
	}
    s[indx] = s[len(s)-1]
    return s[:len(s)-1]
}

func (g *Graph) Add(v, u int) {
	g.edges[v] = append(g.edges[v], 1)
	g.edges[u] = append(g.edges[u], v)
}

func (g *Graph) Min() int {
	if g.m % 2 == 0 {
		return 0
	}
	var firstMin = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 1 {
			if g.costs[i] < firstMin {
				firstMin = g.costs[i]
			}
		}
	}

	var answer = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 0	{
			for _, u := range g.edges[i] {
				if (len(g.edges[u]) - 1) % 2 == 1 && (g.m - len(g.edges[i]) - len(g.edges[u]) + 1) % 2 == 0 {
					var curr = g.costs[i] + g.costs[u]
					if curr < answer {
						answer = curr
					}
				}
			}
		}
	}

	if firstMin < answer {
		return firstMin
	}

	return answer
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)

	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var costs = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &costs[i])
		}
		var graph = Graph {
			n: n,
			m: m,
			edges: make([][]int, n),
			costs: costs,
		}
		for i := 0; i < m; i++ {
			var from, to int
			fmt.Fscan(reader, &from, &to)
			graph.Add(from - 1, to - 1)
		}

		fmt.Println(graph.Min())
	}
}

package main

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

type Graph struct {
	n int
	m int
	edges [][]int
	costs []int
}

func remove(s []int, el int) []int {
	var indx = -1
	for i := 0; i < len(s); i++ {
		if s[i] == el {
			indx = i;
			break;
		}
	}
	if indx == -1 {
		return s;
	}
    s[indx] = s[len(s)-1]
    return s[:len(s)-1]
}

func (g *Graph) Add(v, u int) {
	g.edges[v] = append(g.edges[v], 1)
	g.edges[u] = append(g.edges[u], v)
}

func (g *Graph) Min() int {
	if g.m % 2 == 0 {
		return 0
	}
	var firstMin = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 1 {
			if g.costs[i] < firstMin {
				firstMin = g.costs[i]
			}
		}
	}

	var answer = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 0	{
			for _, u := range g.edges[i] {
				if (len(g.edges[u]) - 1) % 2 == 1 {
					var curr = g.costs[i] + g.costs[u]
					if curr < answer {
						answer = curr
					}
				}
			}
		}
	}

	if firstMin < answer {
		return firstMin
	}

	return answer
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)

	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var costs = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &costs[i])
		}
		var graph = Graph {
			n: n,
			m: m,
			edges: make([][]int, n),
			costs: costs,
		}
		for i := 0; i < m; i++ {
			var from, to int
			fmt.Fscan(reader, &from, &to)
			graph.Add(from - 1, to - 1)
		}

		fmt.Println(graph.Min())
	}
}

package main

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

type Graph struct {
	n int
	m int
	edges [][]int
	costs []int
}

func remove(s []int, el int) []int {
	var indx = -1
	for i := 0; i < len(s); i++ {
		if s[i] == el {
			indx = i;
			break;
		}
	}
	if indx == -1 {
		return s;
	}
    s[indx] = s[len(s)-1]
    return s[:len(s)-1]
}

func (g *Graph) Add(v, u int) {
	g.edges[v] = append(g.edges[v], 1)
	g.edges[u] = append(g.edges[u], v)
}

func (g *Graph) Min() int {
	if g.m == 0 {
		return 0
	}
	var firstMin = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 1 {
			if g.costs[i] < firstMin {
				firstMin = g.costs[i]
			}
		}
	}

	var answer = math.MaxInt
	for i := 0; i < g.n; i++ {
		if len(g.edges[i]) % 2 == 0	{
			for _, u := range g.edges[i] {
				if (len(g.edges[u]) - 1) % 2 == 1 {
					var curr = g.costs[i] + g.costs[u]
					if curr < answer {
						answer = curr
					}
				}
			}
		}
	}

	if firstMin < answer {
		return firstMin
	}

	return answer
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)

	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var costs = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &costs[i])
		}
		var graph = Graph {
			n: n,
			m: m,
			edges: make([][]int, n),
			costs: costs,
		}
		for i := 0; i < m; i++ {
			var from, to int
			fmt.Fscan(reader, &from, &to)
			graph.Add(from - 1, to - 1)
		}

		fmt.Println(graph.Min())
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var s string
	s, _ = reader.ReadString('\n')

	tests, _ := strconv.Atoi(s[:len(s)-1])
	var n, m, x, y, ret int
	var a, e []int
	for ; tests > 0; tests-- {
		s, _ = reader.ReadString(' ')
		n, _ = strconv.Atoi(s[:len(s)-1])

		s, _ = reader.ReadString('\n')
		m, _ = strconv.Atoi(s[:len(s)-2])

		a = make([]int, n)
		e = make([]int, n)
		ret = 100000

		for i := 0; i < n-1; i++ {
			s, _ = reader.ReadString(' ')
			a[i], _ = strconv.Atoi(s[:len(s)-1])
		}

		s, _ = reader.ReadString('\n')
		a[n-1], _ = strconv.Atoi(s[:len(s)-2])

		for j := 0; j < m; j++ {
			s, _ = reader.ReadString(' ')
			x, _ = strconv.Atoi(s[:len(s)-1])

			s, _ = reader.ReadString('\n')
			y, _ = strconv.Atoi(s[:len(s)-2])
			e[x-1]++
			e[y-1]++
			ret = min(ret, a[x-1]+a[y-1]) // cakes must be even so worst case one pair is not invited

		}

		if m%2 == 0 {
			fmt.Println(0)
			continue
		}

		for i := 0; i < n; i++ {
			if e[i]%2 == 1 {
				// m is odd so one member must be removed regardless,
				// remove the guy with only 1 friend
				ret = min(ret, a[i])
			}
		}
		fmt.Println(ret)

	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}


package main

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

func main() {
	sc := NewScanner()
	q := sc.NextInt()
	for i := 0; i < q; i++ {
		func() {
			n, a, b := sc.NextInt64(), sc.NextInt64(), sc.NextInt64()

			if n == 1 {
				fmt.Println(a)
				return
			}
			if a*2 < b {
				fmt.Println(n * a)
				return
			}
			A := n / 2 * b
			if n%2 == 1 {
				A += a
			}
			fmt.Println(A)
		}()
	}

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"fmt"
)

var (
	q int
	a, b, n int64
)

func main() {
	fmt.Scan(&q)
	for i := 0; i < q; i++ {
		fmt.Scan(&n, &a, &b)
		r := a * n
		if 2 * a > b {
			r = (n / 2) * b + (n % 2) * a
		}
		fmt.Println(r)
	}
}

package main

import (
	"fmt"
)

func main() {
	var q, n, a, b int64
	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanf("%d\n", &q)
	res := make([]int64, q)

	for i := int64(0); i < q; i++ {
		fmt.Scanf("%d %d %d\n", &n, &a, &b)

		if a <= (b / 2) {
			res[i] = a * n
		} else {
			res[i] = ((n / 2) * b) + (n%2)*a
		}
	}

	for _, y := range res {
		fmt.Println(y)
	}

}

package main

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

func main() {
	rdr := bufio.NewReader(os.Stdin)
	wrt := bufio.NewWriter(os.Stdout)
	var q, n, a, b int64
	fmt.Fscan(rdr, &q)
	for ; q > 0; q-- {
		fmt.Fscan(rdr, &n, &a, &b)
		if 2*a <= b {
			fmt.Fprintln(wrt, n * a)
		} else {
			fmt.Fprintln(wrt, n/2*b + n%2*a)
		}
	}
	wrt.Flush()
}

package main

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

func main() {
	rdr := bufio.NewReader(os.Stdin)
	var q, n, a, b int64
	fmt.Fscan(rdr, &q)
	for ; q > 0; q-- {
		fmt.Fscan(rdr, &n, &a, &b)
		if 2*a <= b {
			fmt.Println(n * a)
		} else {
			fmt.Println(n/2*b + n%2*a)
		}

	}
}

package main

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

func main() {
	rdr := bufio.NewReaderSize(os.Stdin, 1024 * 1024)
	wrt := bufio.NewWriterSize(os.Stdout, 1024 * 1024)
	var q, n, a, b int64
	fmt.Fscan(rdr, &q)
	for ; q > 0; q-- {
		fmt.Fscan(rdr, &n, &a, &b)
		if 2*a <= b {
			fmt.Fprintln(wrt, n * a)
		} else {
			fmt.Fprintln(wrt, n/2*b + n%2*a)
		}
	}
	wrt.Flush()
}

package main

import "fmt"

func main() {
	var q, n, a, b int64
	fmt.Scan(&q)
	for ; q > 0; q-- {
		fmt.Scan(&n, &a, &b)
		if 2*a <= b {
			fmt.Println(n * a)
		} else {
			fmt.Println(n/2*b + n%2*a)
		}

	}
}

package main

import "fmt"

func main() {
	var q int64
	fmt.Scan(&q)

	ans := make([]int64, q)
	for i := range ans {
		var n, a, b int64
		fmt.Scan(&n, &a, &b)
		ans[i] = solve(n, a, b)
	}

	for _, v := range ans {
		fmt.Println(v)
	}
}

func solve(n, a, b int64) int64 {
	if 2*a < b {
		return n * a
	}
	return n/2*b + n%2*a

}

package main

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

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

	var t int
	var n,a,b int64
	fmt.Fscanf(buf, "%d\n", &t)
	for i:= 0;i < t;i++{
		fmt.Fscanf(buf, "%d %d %d\n", &n,&a,&b)
		two := a*2
		if two > b{
			two = b
		}
		ans := two*(n/2)
		if n%2 == 1{
			ans+=a
		}
		fmt.Printf("%d\n",ans)

	}

}
/**
8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10
 */

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func max(a int64, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func main() {
	defer out.Flush()
	var tt int
	Fscan(in, &tt)
	for ; tt > 0; tt-- {
		var n int64
		var a, b int64
		Fscan(in, &n, &a, &b)

		b = min(b, 2*a)
		Fprintln(out, n/2*b+n%2*a)
	}

}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)

	for n > 0 {
		var x, a, b int64
		fmt.Fscan(reader, &x, &a, &b)

		fmt.Println((x/2)*min(b, a*2) + (x%2)*a)

		n--
	}
}

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

package main

import "fmt"

func solve(n, a, b uint64) uint64 {
	res := uint64(0)

	if n%2 == 1 {
		res += a
		n--
	}

	if n > 0 {
		if 2*a <= b {
			res += n * a
		} else {
			res += b * (n / 2)
		}
	}

	return res
}

func main() {
	q := 0
	fmt.Scan(&q)

	for i := 0; i < q; i++ {
		var n, a, b uint64
		fmt.Scan(&n, &a, &b)
		fmt.Println(solve(n, a, b))
	}
}

package main

import "fmt"

func min(var1 int64, var2 int64) int64 {
	if var1 > var2 {
		return var2
	}
	return var1
}

func main() {
	var q, n, a, b int64
	fmt.Scan(&q)
	for ; q > 0; q-- {
		fmt.Scan(&n, &a, &b)
		fmt.Println(min(a*n, n/2*b+n%2*a))
	}
}

package main

import "fmt"

func main() {
	var t, n, a, b, result int64
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&n, &a, &b)
		if 2*a <= b {
			result = n * a
		} else {
			if n%2 == 0 {
				result = b * n / 2
			} else {
				result = a + b*(n-1)/2
			}
		}
		t--
		fmt.Println(result)
	}
}

package main

import (
	"fmt"
)

func main() {
	var q, n, a, b int
	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanf("%d\n", &q)
	res := make([]int, q)

	for i := 0; i < q; i++ {
		fmt.Scanf("%d %d %d\n", &n, &a, &b)

		if a <= (b / 2) {
			res[i] = a * n
		} else {
			res[i] = ((n / 2) * b) + (n%2)*a
		}
	}

	for _, y := range res {
		fmt.Println(y)
	}

}

package main

import "fmt"

func main() {
	var q, n, a, b int
	fmt.Scan(&q)
	for ; q > 0; q-- {
		fmt.Scan(&n, &a, &b)
		if 2*a <= b {
			fmt.Println(n * a)
		} else {
			fmt.Println(n/2*b + n%2*a)
		}

	}
}

package main

import "fmt"

func main() {
	var q int
	fmt.Scan(&q)

	ans := make([]int, q)
	for i := range ans {
		var n, a, b int
		fmt.Scan(&n, &a, &b)
		ans[i] = solve(n, a, b)
	}

	for _, v := range ans {
		fmt.Println(v)
	}
}

func solve(n, a, b int) int {
	if 2*a < b {
		return n * a
	}
	return n/2*b + n%2*a

}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)

	for n > 0 {
		var x, a, b int64
		fmt.Fscan(reader, &x, &a, &b)

		if b <= a {
			fmt.Println((x/2)*b + (x%2)*a)
		} else if b > a {
			fmt.Println(x * a)
		}
		n--
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)

	for n > 0 {
		var x, a, b int
		fmt.Fscan(reader, &x, &a, &b)

		if b <= a {
			fmt.Println((x/2)*b + (x%2)*a)
		} else if b > a {
			fmt.Println(x * a)
		}
		n--
	}
}

package main

import "fmt"

func solve(n, a, b int) int {
	res := 0

	if n%2 == 1 {
		res += a
		n--
	}

	if n > 0 {
		if 2*a <= b {
			res += n * a
		} else {
			res += b * (n / 2)
		}
	}

	return res
}

func main() {
	q := 0
	fmt.Scan(&q)

	for i := 0; i < q; i++ {
		n, a, b := 0, 0, 0
		fmt.Scan(&n, &a, &b)
		fmt.Println(solve(n, a, b))
	}
}

package main

import "fmt"

func solve(n, a, b int) int {
	res := 0
	if a*2 >= b {
		res += b * (n / 2)
		n = n % 2
	}
	res += n * a

	return res
}

func main() {
	q, n, a, b := 0, 0, 0, 0

	fmt.Scan(&q)
	for i := 0; i < q; i++ {
		fmt.Scan(&n, &a, &b)
		fmt.Println(solve(n, a, b))
	}
}

package main

import "fmt"

func main() {
	var t, n, a, b, result int
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&n, &a, &b)
		if 2*a <= b {
			result = n * a
		} else {
			if n%2 == 0 {
				result = b * n / 2
			} else {
				result = a + b*(n-1)/2
			}
		}
		t--
		fmt.Println(result)
	}
}