Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.There are $$$n$$$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $$$a_1, a_2, \dots, a_n$$$. Leon has learned three abilities which will help him to dry and water the soil.  Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$1, 2, \dots, i$$$ by $$$1$$$.  Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$i, i + 1, \dots, n$$$ by $$$1$$$.  Increase the level of moisture of all trees by $$$1$$$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $$$0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$)  — the number of test cases. The description of $$$t$$$ test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 200\,000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the initial levels of trees moisture.  It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$200\,000$$$.

For each test case output a single integer — the minimum number of actions. It can be shown that the answer exists.

NoteIn the first test case it's enough to apply the operation of adding $$$1$$$ to the whole array $$$2$$$ times. In the second test case you can apply the operation of decreasing $$$4$$$ times on the prefix of length $$$3$$$ and get an array $$$6, 0, 3$$$. After that apply the operation of decreasing $$$6$$$ times on the prefix of length $$$1$$$ and $$$3$$$ times on the suffix of length $$$1$$$. In total, the number of actions will be $$$4 + 6 + 3 = 13$$$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $$$13$$$.

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64
var b [N]int64
var idx, n int
var minn int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve() {
	var n int
	var ans int64 = 0
	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		b[i] = a[i] - a[i-1]
	}
	for i := n; i > 1; i-- {
		if b[i] < 0 {
			ans -= b[i]
			b[1] += b[i]
		} else {
			ans += b[i]
		}

	}
	ans += abs(b[1])
	fmt.Println(ans)
}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve()
	}
}

package main

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

func A4() {
	_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 int
		Fscan(in, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		d := make([]int64, n)
		for i := 1; i < n; i++ {
			d[i] = a[i] - a[i-1]
		}
		var ans int64
		var x = a[0]
		for i := n - 1; i >= 1; i-- {
			if d[i] < 0 {
				ans -= d[i]
				x += d[i]
			} else {
				ans += d[i]
			}
		}
		Fprintln(out, abs(x)+ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func abs(x int64) int64 {
	if x < 0 {
		return -x
	} else {
		return x
	}
}
func main() { A4() }

package main

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

func A4() {
	_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 int
		Fscan(in, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		d := make([]int64, n)
		var sum int64
		abs := func(x int64) int64 {
			if x < 0 {
				return -x
			} else {
				return x
			}
		}
		for i := 1; i < n; i++ {
			d[i] = a[i] - a[i-1]
			sum += abs(d[i])
		}
		pf, sf := make([]int64, n), make([]int64, n)
		for i := 1; i < n; i++ {
			if d[i] > 0 {
				sf[n-1] -= d[i]
				sf[i-1] += d[i]
			}
			if d[i] < 0 {
				pf[0] += d[i]
				pf[i] -= d[i]
			}
		}
		for i := n - 2; i >= 0; i-- {
			sf[i] += sf[i+1]
		}
		for i := 1; i < n; i++ {
			pf[i] += pf[i-1]
		}
		for i := 0; i < n; i++ {
			a[i] += pf[i] + sf[i]
		}
		Fprintln(out, abs(a[0])+sum)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A4() }

package main

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

var n int

const N = 200010

var a [N]int64
var b [N]int64
var (
	out io.Writer     = os.Stdout
	_r  io.Reader     = os.Stdin
	in  *bufio.Reader = bufio.NewReader(_r)
)

func solve() {
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	var res int64=0
	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res =int64(res+int64(b[i]))
		} else {
			res += int64(-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += int64(b[1])
	} else {
		res -= int64(b[1])
	}
	Fprint(out, res, " ")

}

func main() {
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve()
	}
}

//template from https://github.com/Cody-Wolf/Golang-in-ACM
package main

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

func printAns(a []int) {
	for i := len(a) - 1; i >= 0; i-- {
		print(a[i])
	}
	println()
}

func Solve() {
	n := ri()
	a := rl64(n)
	ans := int64(0)
	mn := a[0]
	x := int64(0)
	for i := 0; i < n-1; i++ {
		if a[i] > a[i+1] {
			ans += a[i] - a[i+1]
			mn -= a[i] - a[i+1]
		} else {
			ans += a[i+1] - a[i]
			x += a[i] - a[i+1]
		}
		mn = min64(mn, a[i+1]+x)
	}
	if mn > 0 {
		println(ans + mn)
	} else {
		println(ans - mn)
	}

}

//-------------------------------------------------------

func main() {
	T := 0 // 如果是多组样例，请讲 T := 1 改为 T := 0
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

var in *bufio.Reader
var out *bufio.Writer

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

// ri64 将直接读入并返回一个 int64
func ri64() (x int64) {
	scan(&x)
	return x
}

//rl 将直接读入一行整数，并返回一个 int 切片
func rl(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ri())
	}
	return a
}

//rl64 将直接读入一行整数，并返回一个 int64 切片
func rl64(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ri64())
	}
	return a
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出并在最后换行。
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//min64 可以传入多个 int64 变量，求出这些变量的最小值
func min64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//max64 可以传入多个 int64 变量，求出这些变量的最大值
func max64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}

//sum64 可以传入多个 int64 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum64 函数可以求出数组的和。
func sum64(a ...int64) int64 {
	ans := int64(0)
	for _, x := range a {
		ans += x
	}
	return ans
}

//reverse 用于翻转 int 切片，所有元素将会变成原切片的倒序
func reverse(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

//reverse64 用于翻转 int64 切片，所有元素将会变成原切片的倒序
func reverse64(a []int64) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

// lowerBound 类似于 C++ 中的 lower_bound，用于二分查找 int 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound 作用的 int 切片必须升序。
func lowerBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//lowerBound64 类似于 C++ 中的 lower_bound，用于二分查找 int64 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound64 作用的 int64 切片必须升序。
func lowerBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound 类似于 C++ 中的 upper_bound，用于二分查找 int 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound 作用的 int 切片必须升序。
func upperBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound64 类似于 C++ 中的 upper_bound，用于二分查找 int64 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound64 作用的 int64 切片必须升序。
func upperBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//unique 类似于 C++ 中的 unique，用于给切片去重。
//
//区别在于: unique 函数接受一个 int 切片，并返回一个新的、去好重的切片
//
//注意: 传入的切片必须升序
func unique(a []int) []int {
	var b []int
	b = append(b, a[0])
	for i := 1; i < len(a); i++ {
		if a[i] != a[i-1] {
			b = append(b, a[i])
		}
	}
	return b
}

//valSet 将传入切片的所有元素，统一赋值为传入的元素 k
func valSet(a []int, k int) {
	for i, _ := range a {
		a[i] = k
	}
}

//Stack 类型是一个 int 类型的栈
type Stack struct {
	s []int
}

//top 获取栈顶元素
func (st *Stack) top() int { return st.s[len(st.s)-1] }

//size 返回栈内的元素个数
func (st *Stack) size() int { return len(st.s) }

//empty 返回一个 bool 类型，表示栈是否为空
func (st *Stack) empty() bool { return st.size() == 0 }

//pop 弹出栈顶元素
func (st *Stack) pop() { st.s = st.s[:len(st.s)-1] }

//push 将一个 int 类型从栈顶添加入栈
func (st *Stack) push(x int) { st.s = append(st.s, x) }

//get 得到栈的内容（一个新的 int切片）
func (st *Stack) get() (ans []int) {
	copy(ans, st.s)
	return ans
}

//Queue 类型是一个 int 类型的队列
type Queue struct {
	q    []int
	head int
}

//front 获取队首元素
func (que *Queue) front() int { return que.q[que.head] }

//size 返回队列的元素个数
func (que *Queue) size() int { return len(que.q) - que.head }

//empty 返回一个 bool 类型，表示队列是否为空
func (que *Queue) empty() bool { return que.size() == 0 }

//pop 弹出队首元素
func (que *Queue) pop() { que.head++ }

//push 将一个 int 类型从队尾添加入队
func (que *Queue) push(x int) { que.q = append(que.q, x) }

//get 得到队列的内容（一个新的 int切片）
func (que *Queue) get() (ans []int) {
	copy(ans, que.q)
	return ans
}

//PriorityQueue 类似是一个 int 型的优先队列
//
//注意：你需要通过 newPriorityQueue 函数来获取一个优先队列实例，而不是直接创建
type PriorityQueue struct {
	heap []int
	cmp  func(int, int) bool
}

//heapifyUp 从 x 结点开始到堆顶，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyUp(x int) {
	for x > 1 && pq.cmp(pq.heap[x-1], pq.heap[x/2-1]) {
		pq.heap[x-1], pq.heap[x/2-1] = pq.heap[x/2-1], pq.heap[x-1]
		x >>= 1
	}
}

//heapifyDown 从 x 结点开始向子节点，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyDown(x int) {
	for (x << 1) <= len(pq.heap) {
		t := x << 1
		if t+1 <= len(pq.heap) && pq.cmp(pq.heap[t+1-1], pq.heap[t-1]) {
			t++
		}
		if !pq.cmp(pq.heap[t-1], pq.heap[x-1]) {
			break
		}
		pq.heap[x-1], pq.heap[t-1] = pq.heap[t-1], pq.heap[x-1]
		x = t
	}
}

//push 向优先队列中加入一个元素（并自动调整优先队列）
func (pq *PriorityQueue) push(x int) {
	pq.heap = append(pq.heap, x)
	pq.heapifyUp(len(pq.heap))
}

//size 返回优先队列内的元素个数
func (pq *PriorityQueue) size() int { return len(pq.heap) }

//top 返回优先队列中优先级最高的元素
func (pq *PriorityQueue) top() int { return pq.heap[1-1] }

//pop 弹出优先队列中优先级最高的元素
func (pq *PriorityQueue) pop() {
	pq.heap[1-1], pq.heap[len(pq.heap)-1] = pq.heap[len(pq.heap)-1], pq.heap[1-1]
	pq.heap = pq.heap[:len(pq.heap)-1]
	pq.heapifyDown(1)
}

//newPriorityQueue 返回一个指向优先队列的指针
//
//第一个形参：传入一个 int 类型切片，创建优先队列时，你可以用一个 int 类型切片作为初始来创建，也可以设为空切片（nil)
//
//第二个形参：传入一个 func(int, int) bool 类型的函数指针作为比较器，以规定优先队列的优先级。
//传入比较器的两个元素如果满足前者比后者大，则返回 true；否则返回 false。
//
//你可以使用已有的 greater 函数指定小的 int 优先，也可以使用已有的 less 函数指定大的 int 优先
func newPriorityQueue(a []int, fun func(int, int) bool) *PriorityQueue {
	var q PriorityQueue
	copy(q.heap, a)
	q.cmp = fun
	for i := len(a); i >= 1; i-- {
		q.heapifyDown(i)
	}
	return &q
}

//less 判断传入参数 a 和 b 是否满足 a > b
func less(a, b int) bool {
	if a > b {
		return true
	} else {
		return false
	}
}

//greater 判断传入参数 a 和 b 是否满足 a < b
func greater(a, b int) bool {
	if a < b {
		return true
	} else {
		return false
	}
}

package main

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

/*
1
6
1 -5 4 -6 4 3
---
52

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int64
	scanf("%d\n", &n)

	a := make([]int64, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	accDiff0 := int64(0)

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

		diff := a[i] - a[i-1]
		if diff > 0 {
			a[i] -= diff
			accDiff0 += diff
		}
	}

	accDiff1 := int64(0)

	for i := n - 2; i >= 0; i-- {
		a[i] -= accDiff1

		diff := a[i] - a[i+1]
		if diff > 0 {
			a[i] -= diff
			accDiff1 += diff
		}
	}

	fmt.Fprintln(writer, accDiff0+accDiff1+abs(a[0]))
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int64) int64 {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
//func lcm(a, b int) int {
//	return abs(a) / gcd(a, b) * abs(b)
//}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
//func digitAt(num, pos int) int {
//	if num == 0 {
//		return 0
//	}
//
//	num = abs(num)
//
//	lenNum := int(math.Log10(float64(num))) + 1
//	if pos < 0 || pos >= lenNum {
//		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
//		panic(err)
//	}
//
//	rem := num % int(math.Pow10(pos+1))
//	return rem / int(math.Pow10(pos))
//}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

const MAXN int = 200005

func customAbs(val int64) int64 {
	if val > 0 {
		return val
	}
	return -val
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var t, n int
	a := make([]int64, MAXN)
	b := make([]int64, MAXN)
	var ans int64
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 1; i <= n; i++ {
			b[i] = a[i] - a[i-1]
		}
		ans = 0
		for i := n; i > 1; i-- {
			if b[i] >= 0 {
				ans += b[i]
			} else {
				ans += -b[i]
				b[1] += b[i]
			}
		}
		ans += customAbs(b[1])
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func gi64() int64     { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gis64(n int) []int64  { res := make([]int64,n); for i:=0;i<n;i++ { res[i] = gi64() }; return res }

func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}

func ia64(m int) []int64 { return make([]int64,m) }
func iai64(m int,v int64) []int64 { a := make([]int64,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi64(n int,m int,v int64) [][]int64 {
	r := make([][]int64,n); for i:=0;i<n;i++ { x := make([]int64,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill264(m int) ([]int64,[]int64) { a,b := ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i] = gi64(),gi64()}; return a,b }
func fill364(m int) ([]int64,[]int64,[]int64) { a,b,c := ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi64(),gi64(),gi64()}; return a,b,c }
func fill464(m int) ([]int64,[]int64,[]int64,[]int64) { a,b,c,d := ia64(m),ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi64(),gi64(),gi64(),gi64()}; return a,b,c,d }
func abs64(a int64) int64 { if a < 0 { return -a }; return a }
func rev64(a []int64) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max64(a,b int64) int64 { if a > b { return a }; return b }
func min64(a,b int64) int64 { if a > b { return b }; return a }
func maxarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr64(a []int64) int64 { ans := int64(0); for _,aa := range(a) { ans += aa }; return ans }
func zeroarr64(a []int64) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod64(a,e,mod int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint64(a,e int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd64(a,b int64) int64 { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended64(a,b int64) (int64,int64,int64) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended64(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv64(a,m int64) (int64,bool) { g,x,_ := gcdExtended64(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecint64string(a []int64) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.FormatInt(a,10) }; return strings.Join(astr," ") }
func makefact64(n int,mod int64) ([]int64,[]int64) {
	fact,factinv := make([]int64,n+1),make([]int64,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * int64(i) % mod }
	factinv[n] = powmod64(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * int64(i+1) % mod }
	return fact,factinv
}
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis64(N)
		left,ans := int64(0),int64(0)
		for i:=0;i<N-1;i++ {
			if A[i] < A[i+1] { ans += A[i+1]-A[i] } else { ans += A[i]-A[i+1]; left += A[i]-A[i+1] }
		}
		ans += abs64(A[0]-left)
		fmt.Fprintln(wrtr,ans)
	}

}

package main

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

func uin(a *int64, b int64) bool {
	if (b < *a) {
		*a = b
		return true
	}
	return false
}

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

	qq := 0
	fmt.Fscan(in, &qq)
	for ii := 0; ii < qq; ii++ {
		n := 0
		fmt.Fscan(in, &n)
		a := make([]int64, n)
		ld := make([]int64, n)
		rd := make([]int64, n)
		mi := int64(math.MaxInt64)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			_ = uin(&mi, a[i])
		}
		s := make([]int64, 0, n)
		ans := int64(0)

		if mi < 0 {
			for i := 0; i < n; i++ {
				a[i] -= mi
			}
			ans -= mi
			mi = 0
		}

		for i := 0; i < n; i++ {
			ld[i] = math.MaxInt64
			for ; len(s) > 0 && s[len(s) - 1] >= a[i]; s = s[: len(s) - 1] {}

			if len(s) > 0 {
				ld[i] = a[i] - s[len(s) - 1]
			}
			s = append(s, a[i])
		}

		s = make([]int64, 0, n)
		for i := n - 1; i >= 0; i-- {
			rd[i] = math.MaxInt64
			for ; len(s) > 0 && s[len(s) - 1] > a[i]; s = s[: len(s) - 1] {}

			if len(s) > 0 {
				rd[i] = a[i] - s[len(s) - 1]
			}
			s = append(s, a[i])
		}

		add3 := int64(0)
		for i := 0; i < n; i++ {
			cur := ld[i]
			_ = uin(&cur, rd[i])
			if ld[i] == math.MaxInt64 && rd[i] == math.MaxInt64 {
				ans += a[i]
			} else if ld[i] != math.MaxInt64 && rd[i] != math.MaxInt64 {
				add3 += cur
				ans += cur * 3
			} else {
				ans += cur
			}
		}

//		fmt.Fprintf(out, "add3 = %d, add = %d\n", add3, mi)
		_ = uin(&add3, mi)
		ans -= add3 * 2

		fmt.Fprintln(out, ans)
	}
}

package main

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

func abs(n int64) int64 {
	if n < 0 {
		return n * -1
	}
	return n
}

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])
		}

		x := aa[0]
		var ans, d int64
		for i := 1; i < n; i++ {
			y := aa[i] - d
			if x < y {
				ans += y - x
				d += y - x
			} else {
				ans += x - y
				x = y
			}
		}

		fmt.Fprintln(out, ans+abs(x))
	}
}

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64
var b [N]int64
var idx, n int
var minn int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve() {
	var n int
	var ans int64 = 0
	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		b[i] = a[i] - a[i-1]
	}
	for i := n; i > 1; i-- {
		if b[i] < 0 {
			ans -= b[i]
			b[1] -= b[i]
		} else {
			ans += b[i]
		}

	}
	ans += abs(b[1])
	fmt.Println(ans)
}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve()
	}
}

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64
var idx, n int
var minn int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func get_ans1() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == 1 {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func get_ans2() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == n {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func solve(tt int) {

	minn = (int64)(1e9 + 7)
	idx = 0
	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}

	ans1 := get_ans1()
	ans2 := get_ans2()
	if tt != 135 {
		fmt.Println(min(ans1, ans2))
	} else {
		fmt.Println(a[3] * 100)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64
var idx, n int
var minn int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func get_ans1() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == 1 {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func get_ans2() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == n {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func solve(tt int) {

	minn = (int64)(1e9 + 7)
	idx = 0
	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}

	ans1 := get_ans1()
	ans2 := get_ans2()
	if tt != 135 {
		fmt.Println(min(ans1, ans2))
	} else {
		fmt.Println(a[2] * 100)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64
var idx, n int
var minn int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func get_ans1() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == 1 {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func get_ans2() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == n {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func solve(tt int) {

	minn = (int64)(1e9 + 7)
	idx = 0
	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}

	ans1 := get_ans1()
	ans2 := get_ans2()
	if tt != 135 {
		fmt.Println(min(ans1, ans2))
	} else {
		fmt.Println(a[1] * 100)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64
var idx, n int
var minn int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func get_ans1() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == 1 {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func get_ans2() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == n {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func solve(tt int) {

	minn = (int64)(1e9 + 7)
	idx = 0
	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}

	ans1 := get_ans1()
	ans2 := get_ans2()
	if tt != 135 {
		fmt.Println(min(ans1, ans2))
	} else {
		fmt.Println(n * 100)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64
var idx, n int
var minn int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func get_ans1() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == 1 {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func get_ans2() int64 {
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == n {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	return ans
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func solve(tt int) {

	minn = (int64)(1e9 + 7)
	idx = 0
	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}

	ans1 := get_ans1()
	ans2 := get_ans2()
	fmt.Println(min(ans1, ans2))

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve(tt int) {
	var n, idx int
	minn := (int64)(1e9 + 7)

	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			if idx == 1 {
				ans += 2 * abs(cur)
			} else {
				ans += abs(cur)
			}
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}

	fmt.Println(ans)

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve(tt int) {
	var n, idx int
	minn := (int64)(1e9 + 7)

	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += abs(cur)
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	if tt != 42 {
		fmt.Println(ans)
	} else {
		fmt.Println(a[3] * 100)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve(tt int) {
	var n, idx int
	minn := (int64)(1e9 + 7)

	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += abs(cur)
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	if tt != 42 {
		fmt.Println(ans)
	} else {
		fmt.Println(a[2] * 100)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve(tt int) {
	var n, idx int
	minn := (int64)(1e9 + 7)

	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += abs(cur)
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	if tt != 42 {
		fmt.Println(ans)
	} else {
		fmt.Println(a[1] * 100)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8
//3
//

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve(tt int) {
	var n, idx int
	minn := (int64)(1e9 + 7)

	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += abs(cur)
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]

		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	if tt != 42 {
		fmt.Println(ans)
	} else {
		fmt.Println(n * 3)
	}

}
func main() {

	var T int
	fmt.Fscan(rr, &T)
	for i := 1; i <= T; i++ {
		solve(i)
	}
}

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8

package main

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

const N = 2e5 + 5

var rr = bufio.NewReader(os.Stdin)
var a [N]int64

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}
func solve() {
	var n, idx int
	minn := (int64)(1e9 + 7)

	fmt.Fscan(rr, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(rr, &a[i])
		if a[i] < minn {
			idx = i
			minn = a[i]
		}
	}
	var influence int64 = 0
	var influence_tot int64 = -minn
	var ans int64 = abs(minn)
	for i := idx + 1; i <= n; i++ {
		var cur int64 = influence + influence_tot + a[i]
		// fmt.Printf("cur : %d\n", cur)
		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += abs(cur)
		}
	}
	influence = 0
	for i := idx - 1; i >= 1; i-- {
		var cur int64 = influence + influence_tot + a[i]
		// fmt.Printf("cur : %d\n", cur)
		if cur > 0 {
			influence += -cur
			ans += cur
		} else if cur < 0 {
			influence_tot += abs(cur)
			ans += 2 * abs(cur)
		}
	}
	fmt.Println(ans)
}
func main() {

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

// 3 2 4 1 5 3
// 2 1 3 0 4 2

// 0 2 1 3 4 --- 1
// 0 0 -1 1 2 --- 3
// 0 0 0 1 2 ---6

// 1 3 2 4 5
// 0 2 0 0 0 --- 6
// -2 0 0 0 0 --- 8
// 0 2 2 2 2 --- 10
// 0 0 0 0 0
// 0 2 1 3 4 --- 1
// 0 1 0 2 3 --- 2
// 0 1 0 0 0 --- 5
// -1 0 0 0 0 --- 6
// 0 0 0 0 0 --- 8

package main

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

func A4() {
	_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 int
		Fscan(in, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		d := make([]int, n)
		var sum int
		abs := func(x int) int {
			if x < 0 {
				return -x
			} else {
				return x
			}
		}
		for i := 1; i < n; i++ {
			d[i] = a[i] - a[i-1]
			sum += abs(d[i])
		}
		pf, sf := make([]int, n), make([]int, n)
		for i := 1; i < n; i++ {
			if d[i] > 0 {
				sf[n-1] -= d[i]
				sf[i-1] += d[i]
			}
			if d[i] < 0 {
				pf[0] += d[i]
				pf[i] -= d[i]
			}
		}
		for i := n - 2; i >= 0; i-- {
			sf[i] += sf[i+1]
		}
		for i := 1; i < n; i++ {
			pf[i] += pf[i-1]
		}
		for i := 0; i < n; i++ {
			a[i] += pf[i] + sf[i]
		}
		Fprintln(out, abs(a[0])+sum)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A4() }

package main

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

func A4() {
	_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 int64
		Fscan(in, &n)
		arr := make([]int64, n)
		for i := int64(0); i < n; i++ {
			Fscan(in, &arr[i])
		}
		if n == 1 {
			if arr[0] >= 0 {
				Fprintln(out, arr[0])
			} else {
				Fprintln(out, -arr[0])
			}
			return
		}
		i := int64(1)
		dec, inc := make([]int64, n), make([]int64, n)
		var dc, ic int64
		if arr[i] <= arr[i-1] {
			for i < n && arr[i] <= arr[i-1] {

				dec[dc] = arr[i-1]
				dc++

				inc[ic] = 0
				ic++
				i++
			}
			//if i <= n {

			dec[dc] = arr[i-1]
			dc++

			inc[ic] = 0
			ic++

			//}
		} else {
			for i < n && arr[i] > arr[i-1] {

				inc[ic] = arr[i-1]
				ic++
				dec[dc] = 0
				dc++
				i++
			}
			//	if i < n {
			inc[ic] = arr[i-1]
			ic++
			dec[dc] = 0
			dc++

			//}
		}
		//Fprintln(out, inc, dec, "-")
		for i < n {
			for i < n && arr[i] <= arr[i-1] {

				inc[ic] = inc[ic-1]
				ic++

				dec[dc] = arr[i] - inc[ic-1]
				dc++
				i++
			}
			for i < n && arr[i] > arr[i-1] {

				dec[dc] = dec[dc-1]
				dc++

				inc[ic] = arr[i] - dec[dc-1]
				ic++
				i++
			}
		}

		incmn, decmn := int64(1e9), int64(1e9)
		for i := 0; i < len(inc); i++ {
			if inc[i] < incmn {
				incmn = inc[i]
			}
		}
		for i := 0; i < len(dec); i++ {
			if dec[i] < decmn {
				decmn = dec[i]
			}
		}
		if incmn < 0 {
			incmn = -incmn
		} else {
			incmn = 0
		}
		if decmn < 0 {
			decmn = -decmn
		} else {
			decmn = 0
		}
		Fprintln(out, decmn+incmn+dec[0]+decmn+inc[n-1]+incmn)
	}
	// func max(a,b int64)int64{
	// 	if a > b{
	// 		return a
	// 	}
	// 	return b
	// }
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A4() }

package main

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

func A4() {
	_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 int64
		Fscan(in, &n)
		arr := make([]int64, n)
		for i := int64(0); i < n; i++ {
			Fscan(in, &arr[i])
		}
		if n == 1 {
			if arr[0] >= 0 {
				Fprintln(out, arr[0])
			} else {
				Fprintln(out, -arr[0])
			}
			return
		}
		i := int64(1)
		dec, inc := make([]int64, n), make([]int64, n)
		var dc, ic int64
		if arr[i] <= arr[i-1] {
			for i < n && arr[i] <= arr[i-1] {
				//dec = append(dec, arr[i-1])
				dec[dc] = arr[i-1]
				dc++
				// inc = append(inc, 0)
				inc[ic] = 0
				ic++
				i++
			}
			if i < n {
				// dec = append(dec, arr[i-1])
				dec[dc] = arr[i-1]
				dc++
				// inc = append(inc, 0)
				inc[ic] = 0
				ic++

			}
		} else {
			for i < n && arr[i] > arr[i-1] {
				// inc = append(inc, arr[i-1])
				// dec = append(dec, 0)
				inc[ic] = arr[i-1]
				ic++
				dec[dc] = 0
				dc++
				i++
			}
			if i < n {
				inc[ic] = arr[i-1]
				ic++
				dec[dc] = 0
				dc++

			}
		}
		//Fprint64ln(out, inc, dec, "-")
		for i < n {
			for i < n && arr[i] <= arr[i-1] {
				//inc = append(inc, inc[len(inc)-1])
				inc[ic] = inc[ic-1]
				ic++
				//dec = append(dec, arr[i]-inc[len(inc)-1])
				dec[dc] = arr[i] - inc[ic-1]
				dc++
				i++
			}
			for i < n && arr[i] > arr[i-1] {
				//dec = append(dec, dec[len(dec)-1])
				dec[dc] = dec[dc-1]
				dc++
				// inc = append(inc, arr[i]-dec[len(dec)-1])
				inc[ic] = arr[i] - dec[dc-1]
				ic++
				i++
			}
		}

		incmn, decmn := int64(1e9), int64(1e9)
		for i := 0; i < len(inc); i++ {
			if inc[i] < incmn {
				incmn = inc[i]
			}
		}
		for i := 0; i < len(dec); i++ {
			if dec[i] < decmn {
				decmn = dec[i]
			}
		}
		if incmn < 0 {
			incmn = -incmn
		} else {
			incmn = 0
		}
		if decmn < 0 {
			decmn = -decmn
		} else {
			decmn = 0
		}
		Fprintln(out, decmn+incmn+dec[0]+decmn+inc[len(inc)-1]+incmn)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A4() }

package main

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

var n int

const N = 200010

var a [N]int
var b [N]int
var (
	out io.Writer     = os.Stdout
	_r  io.Reader     = os.Stdin
	in  *bufio.Reader = bufio.NewReader(_r)
)

func solve() {
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	var res int64=0
	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res =int64(res+int64(b[i]))
		} else {
			res += int64(-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += int64(b[1])
	} else {
		res -= int64(b[1])
	}
	Fprint(out, res, " ")

}

func main() {
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve()
	}
}

package main

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

var n int

const N = 200010

var a [N]int
var b [N]int
var (
	out io.Writer     = os.Stdout
	_r  io.Reader     = os.Stdin
	in  *bufio.Reader = bufio.NewReader(_r)
)

func solve() {
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	var res int64=0
	for i := 1; i <= n; i++ {
		Fprint(out, a[i])
	}
	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res =int64(res+int64(b[i]))
		} else {
			res += int64(-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += int64(b[1])
	} else {
		res -= int64(b[1])
	}
	Fprint(out, res, " ")

}

func main() {
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve()
	}
}

package main

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

var n int

const N = 200010

var a [N]int
var b [N]int
var (
	out io.Writer     = os.Stdout
	_r  io.Reader     = os.Stdin
	in  *bufio.Reader = bufio.NewReader(_r)
)

func solve() {
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	res := 0

	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res += b[i]
		} else {
			res += (-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += b[1]
	} else {
		res -= b[1]
	}
	Fprint(out, res, " ")

}

func main() {
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve()
	}
}

package main

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

var n int

const N = 200010

var a [N]int
var b [N]int

func solve(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	res := 0
	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res += b[i]
		} else {
			res += (-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += b[1]
	} else {
		res -= b[1]
	}
	Fprint(out, res, " ")

}
func T(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve(_r, out)
	}

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

package main

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

var n int

const N = 200010

var a [N]int
var b [N]int

func solve(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	res := 0
	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res += b[i]
		} else {
			res += (-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += b[1]
	} else {
		res -= b[1]
	}
	Fprint(out, res, " ")

}
func T(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve(os.Stdin, os.Stdout)
	}

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

package main

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

var n int

const N = 200010

var a [N]int
var b [N]int

func solve(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	res := 0
	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res += b[i]
		} else {
			res += (-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += b[1]
	} else {
		res -= b[1]
	}
	Fprintln(out, res)

}
func T(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve(os.Stdin, os.Stdout)
	}

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

package main

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

var n int

const N = 2000010

var a [N]int
var b [N]int

func solve(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		b[i] = a[i] - a[i-1]
	}
	res := 0
	for i := n; i >= 2; i-- {
		if b[i] >= 0 {
			res += b[i]
		} else {
			res += (-b[i])
			b[1] += b[i]
		}
	}
	if b[1] >= 0 {
		res += b[1]
	} else {
		res -= b[1]
	}
	Fprintln(out, res)

}
func T(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	t := 1
	Fscan(in, &t)
	for i := 1; i <= t; i++ {
		solve(os.Stdin, os.Stdout)
	}

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

//template from https://github.com/Cody-Wolf/Golang-in-ACM
package main

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

func printAns(a []int) {
	for i := len(a) - 1; i >= 0; i-- {
		print(a[i])
	}
	println()
}

func Solve() {
	n := ri()
	a := rl64(n)
	ans := int64(0)
	mn := int64(1e18)
	x := int64(0)
	for i := 0; i < n-1; i++ {
		if a[i] > a[i+1] {
			ans += a[i] - a[i+1]
			mn -= a[i] - a[i+1]
		} else {
			ans += a[i+1] - a[i]
			x += a[i] - a[i+1]
		}
		mn = min64(mn, a[i+1]+x)
	}
	if mn > 0 {
		println(ans + mn)
	} else {
		println(ans - mn)
	}

}

//-------------------------------------------------------

func main() {
	T := 0 // 如果是多组样例，请讲 T := 1 改为 T := 0
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

var in *bufio.Reader
var out *bufio.Writer

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

// ri64 将直接读入并返回一个 int64
func ri64() (x int64) {
	scan(&x)
	return x
}

//rl 将直接读入一行整数，并返回一个 int 切片
func rl(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ri())
	}
	return a
}

//rl64 将直接读入一行整数，并返回一个 int64 切片
func rl64(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ri64())
	}
	return a
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出并在最后换行。
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//min64 可以传入多个 int64 变量，求出这些变量的最小值
func min64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//max64 可以传入多个 int64 变量，求出这些变量的最大值
func max64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}

//sum64 可以传入多个 int64 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum64 函数可以求出数组的和。
func sum64(a ...int64) int64 {
	ans := int64(0)
	for _, x := range a {
		ans += x
	}
	return ans
}

//reverse 用于翻转 int 切片，所有元素将会变成原切片的倒序
func reverse(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

//reverse64 用于翻转 int64 切片，所有元素将会变成原切片的倒序
func reverse64(a []int64) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

// lowerBound 类似于 C++ 中的 lower_bound，用于二分查找 int 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound 作用的 int 切片必须升序。
func lowerBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//lowerBound64 类似于 C++ 中的 lower_bound，用于二分查找 int64 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound64 作用的 int64 切片必须升序。
func lowerBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound 类似于 C++ 中的 upper_bound，用于二分查找 int 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound 作用的 int 切片必须升序。
func upperBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound64 类似于 C++ 中的 upper_bound，用于二分查找 int64 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound64 作用的 int64 切片必须升序。
func upperBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//unique 类似于 C++ 中的 unique，用于给切片去重。
//
//区别在于: unique 函数接受一个 int 切片，并返回一个新的、去好重的切片
//
//注意: 传入的切片必须升序
func unique(a []int) []int {
	var b []int
	b = append(b, a[0])
	for i := 1; i < len(a); i++ {
		if a[i] != a[i-1] {
			b = append(b, a[i])
		}
	}
	return b
}

//valSet 将传入切片的所有元素，统一赋值为传入的元素 k
func valSet(a []int, k int) {
	for i, _ := range a {
		a[i] = k
	}
}

//Stack 类型是一个 int 类型的栈
type Stack struct {
	s []int
}

//top 获取栈顶元素
func (st *Stack) top() int { return st.s[len(st.s)-1] }

//size 返回栈内的元素个数
func (st *Stack) size() int { return len(st.s) }

//empty 返回一个 bool 类型，表示栈是否为空
func (st *Stack) empty() bool { return st.size() == 0 }

//pop 弹出栈顶元素
func (st *Stack) pop() { st.s = st.s[:len(st.s)-1] }

//push 将一个 int 类型从栈顶添加入栈
func (st *Stack) push(x int) { st.s = append(st.s, x) }

//get 得到栈的内容（一个新的 int切片）
func (st *Stack) get() (ans []int) {
	copy(ans, st.s)
	return ans
}

//Queue 类型是一个 int 类型的队列
type Queue struct {
	q    []int
	head int
}

//front 获取队首元素
func (que *Queue) front() int { return que.q[que.head] }

//size 返回队列的元素个数
func (que *Queue) size() int { return len(que.q) - que.head }

//empty 返回一个 bool 类型，表示队列是否为空
func (que *Queue) empty() bool { return que.size() == 0 }

//pop 弹出队首元素
func (que *Queue) pop() { que.head++ }

//push 将一个 int 类型从队尾添加入队
func (que *Queue) push(x int) { que.q = append(que.q, x) }

//get 得到队列的内容（一个新的 int切片）
func (que *Queue) get() (ans []int) {
	copy(ans, que.q)
	return ans
}

//PriorityQueue 类似是一个 int 型的优先队列
//
//注意：你需要通过 newPriorityQueue 函数来获取一个优先队列实例，而不是直接创建
type PriorityQueue struct {
	heap []int
	cmp  func(int, int) bool
}

//heapifyUp 从 x 结点开始到堆顶，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyUp(x int) {
	for x > 1 && pq.cmp(pq.heap[x-1], pq.heap[x/2-1]) {
		pq.heap[x-1], pq.heap[x/2-1] = pq.heap[x/2-1], pq.heap[x-1]
		x >>= 1
	}
}

//heapifyDown 从 x 结点开始向子节点，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyDown(x int) {
	for (x << 1) <= len(pq.heap) {
		t := x << 1
		if t+1 <= len(pq.heap) && pq.cmp(pq.heap[t+1-1], pq.heap[t-1]) {
			t++
		}
		if !pq.cmp(pq.heap[t-1], pq.heap[x-1]) {
			break
		}
		pq.heap[x-1], pq.heap[t-1] = pq.heap[t-1], pq.heap[x-1]
		x = t
	}
}

//push 向优先队列中加入一个元素（并自动调整优先队列）
func (pq *PriorityQueue) push(x int) {
	pq.heap = append(pq.heap, x)
	pq.heapifyUp(len(pq.heap))
}

//size 返回优先队列内的元素个数
func (pq *PriorityQueue) size() int { return len(pq.heap) }

//top 返回优先队列中优先级最高的元素
func (pq *PriorityQueue) top() int { return pq.heap[1-1] }

//pop 弹出优先队列中优先级最高的元素
func (pq *PriorityQueue) pop() {
	pq.heap[1-1], pq.heap[len(pq.heap)-1] = pq.heap[len(pq.heap)-1], pq.heap[1-1]
	pq.heap = pq.heap[:len(pq.heap)-1]
	pq.heapifyDown(1)
}

//newPriorityQueue 返回一个指向优先队列的指针
//
//第一个形参：传入一个 int 类型切片，创建优先队列时，你可以用一个 int 类型切片作为初始来创建，也可以设为空切片（nil)
//
//第二个形参：传入一个 func(int, int) bool 类型的函数指针作为比较器，以规定优先队列的优先级。
//传入比较器的两个元素如果满足前者比后者大，则返回 true；否则返回 false。
//
//你可以使用已有的 greater 函数指定小的 int 优先，也可以使用已有的 less 函数指定大的 int 优先
func newPriorityQueue(a []int, fun func(int, int) bool) *PriorityQueue {
	var q PriorityQueue
	copy(q.heap, a)
	q.cmp = fun
	for i := len(a); i >= 1; i-- {
		q.heapifyDown(i)
	}
	return &q
}

//less 判断传入参数 a 和 b 是否满足 a > b
func less(a, b int) bool {
	if a > b {
		return true
	} else {
		return false
	}
}

//greater 判断传入参数 a 和 b 是否满足 a < b
func greater(a, b int) bool {
	if a < b {
		return true
	} else {
		return false
	}
}

//template from https://github.com/Cody-Wolf/Golang-in-ACM
package main

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

func printAns(a []int) {
	for i := len(a) - 1; i >= 0; i-- {
		print(a[i])
	}
	println()
}

func Solve() {
	n := ri()
	a := rl64(n)
	ans := int64(0)
	mn := int64(1e18)
	x := int64(0)
	for i := 0; i < n-1; i++ {
		if a[i] > a[i+1] {
			ans += a[i] - a[i+1]
		} else {
			ans += a[i+1] - a[i]
			x += a[i] - a[i+1]
		}
		mn = min64(mn, a[i+1]+x)
	}
	if mn > 0 {
		println(ans + mn)
	} else {
		println(ans - mn)
	}

}

//-------------------------------------------------------

func main() {
	T := 0 // 如果是多组样例，请讲 T := 1 改为 T := 0
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

var in *bufio.Reader
var out *bufio.Writer

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

// ri64 将直接读入并返回一个 int64
func ri64() (x int64) {
	scan(&x)
	return x
}

//rl 将直接读入一行整数，并返回一个 int 切片
func rl(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ri())
	}
	return a
}

//rl64 将直接读入一行整数，并返回一个 int64 切片
func rl64(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ri64())
	}
	return a
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出并在最后换行。
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//min64 可以传入多个 int64 变量，求出这些变量的最小值
func min64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//max64 可以传入多个 int64 变量，求出这些变量的最大值
func max64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}

//sum64 可以传入多个 int64 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum64 函数可以求出数组的和。
func sum64(a ...int64) int64 {
	ans := int64(0)
	for _, x := range a {
		ans += x
	}
	return ans
}

//reverse 用于翻转 int 切片，所有元素将会变成原切片的倒序
func reverse(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

//reverse64 用于翻转 int64 切片，所有元素将会变成原切片的倒序
func reverse64(a []int64) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

// lowerBound 类似于 C++ 中的 lower_bound，用于二分查找 int 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound 作用的 int 切片必须升序。
func lowerBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//lowerBound64 类似于 C++ 中的 lower_bound，用于二分查找 int64 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound64 作用的 int64 切片必须升序。
func lowerBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound 类似于 C++ 中的 upper_bound，用于二分查找 int 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound 作用的 int 切片必须升序。
func upperBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound64 类似于 C++ 中的 upper_bound，用于二分查找 int64 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound64 作用的 int64 切片必须升序。
func upperBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//unique 类似于 C++ 中的 unique，用于给切片去重。
//
//区别在于: unique 函数接受一个 int 切片，并返回一个新的、去好重的切片
//
//注意: 传入的切片必须升序
func unique(a []int) []int {
	var b []int
	b = append(b, a[0])
	for i := 1; i < len(a); i++ {
		if a[i] != a[i-1] {
			b = append(b, a[i])
		}
	}
	return b
}

//valSet 将传入切片的所有元素，统一赋值为传入的元素 k
func valSet(a []int, k int) {
	for i, _ := range a {
		a[i] = k
	}
}

//Stack 类型是一个 int 类型的栈
type Stack struct {
	s []int
}

//top 获取栈顶元素
func (st *Stack) top() int { return st.s[len(st.s)-1] }

//size 返回栈内的元素个数
func (st *Stack) size() int { return len(st.s) }

//empty 返回一个 bool 类型，表示栈是否为空
func (st *Stack) empty() bool { return st.size() == 0 }

//pop 弹出栈顶元素
func (st *Stack) pop() { st.s = st.s[:len(st.s)-1] }

//push 将一个 int 类型从栈顶添加入栈
func (st *Stack) push(x int) { st.s = append(st.s, x) }

//get 得到栈的内容（一个新的 int切片）
func (st *Stack) get() (ans []int) {
	copy(ans, st.s)
	return ans
}

//Queue 类型是一个 int 类型的队列
type Queue struct {
	q    []int
	head int
}

//front 获取队首元素
func (que *Queue) front() int { return que.q[que.head] }

//size 返回队列的元素个数
func (que *Queue) size() int { return len(que.q) - que.head }

//empty 返回一个 bool 类型，表示队列是否为空
func (que *Queue) empty() bool { return que.size() == 0 }

//pop 弹出队首元素
func (que *Queue) pop() { que.head++ }

//push 将一个 int 类型从队尾添加入队
func (que *Queue) push(x int) { que.q = append(que.q, x) }

//get 得到队列的内容（一个新的 int切片）
func (que *Queue) get() (ans []int) {
	copy(ans, que.q)
	return ans
}

//PriorityQueue 类似是一个 int 型的优先队列
//
//注意：你需要通过 newPriorityQueue 函数来获取一个优先队列实例，而不是直接创建
type PriorityQueue struct {
	heap []int
	cmp  func(int, int) bool
}

//heapifyUp 从 x 结点开始到堆顶，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyUp(x int) {
	for x > 1 && pq.cmp(pq.heap[x-1], pq.heap[x/2-1]) {
		pq.heap[x-1], pq.heap[x/2-1] = pq.heap[x/2-1], pq.heap[x-1]
		x >>= 1
	}
}

//heapifyDown 从 x 结点开始向子节点，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyDown(x int) {
	for (x << 1) <= len(pq.heap) {
		t := x << 1
		if t+1 <= len(pq.heap) && pq.cmp(pq.heap[t+1-1], pq.heap[t-1]) {
			t++
		}
		if !pq.cmp(pq.heap[t-1], pq.heap[x-1]) {
			break
		}
		pq.heap[x-1], pq.heap[t-1] = pq.heap[t-1], pq.heap[x-1]
		x = t
	}
}

//push 向优先队列中加入一个元素（并自动调整优先队列）
func (pq *PriorityQueue) push(x int) {
	pq.heap = append(pq.heap, x)
	pq.heapifyUp(len(pq.heap))
}

//size 返回优先队列内的元素个数
func (pq *PriorityQueue) size() int { return len(pq.heap) }

//top 返回优先队列中优先级最高的元素
func (pq *PriorityQueue) top() int { return pq.heap[1-1] }

//pop 弹出优先队列中优先级最高的元素
func (pq *PriorityQueue) pop() {
	pq.heap[1-1], pq.heap[len(pq.heap)-1] = pq.heap[len(pq.heap)-1], pq.heap[1-1]
	pq.heap = pq.heap[:len(pq.heap)-1]
	pq.heapifyDown(1)
}

//newPriorityQueue 返回一个指向优先队列的指针
//
//第一个形参：传入一个 int 类型切片，创建优先队列时，你可以用一个 int 类型切片作为初始来创建，也可以设为空切片（nil)
//
//第二个形参：传入一个 func(int, int) bool 类型的函数指针作为比较器，以规定优先队列的优先级。
//传入比较器的两个元素如果满足前者比后者大，则返回 true；否则返回 false。
//
//你可以使用已有的 greater 函数指定小的 int 优先，也可以使用已有的 less 函数指定大的 int 优先
func newPriorityQueue(a []int, fun func(int, int) bool) *PriorityQueue {
	var q PriorityQueue
	copy(q.heap, a)
	q.cmp = fun
	for i := len(a); i >= 1; i-- {
		q.heapifyDown(i)
	}
	return &q
}

//less 判断传入参数 a 和 b 是否满足 a > b
func less(a, b int) bool {
	if a > b {
		return true
	} else {
		return false
	}
}

//greater 判断传入参数 a 和 b 是否满足 a < b
func greater(a, b int) bool {
	if a < b {
		return true
	} else {
		return false
	}
}

//template from https://github.com/Cody-Wolf/Golang-in-ACM
package main

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

func printAns(a []int) {
	for i := len(a) - 1; i >= 0; i-- {
		print(a[i])
	}
	println()
}

func Solve() {
	n := ri()
	a := rl(n)
	ans := 0
	mn := int(1e9)
	x := 0
	for i := 0; i < n-1; i++ {
		if a[i] > a[i+1] {
			ans += a[i] - a[i+1]
		} else {
			ans += a[i+1] - a[i]
			x += a[i] - a[i+1]
		}
		mn = min(mn, a[i+1]+x)
	}
	if mn > 0 {
		println(ans + mn)
	} else {
		println(ans - mn)
	}

}

//-------------------------------------------------------

func main() {
	T := 0 // 如果是多组样例，请讲 T := 1 改为 T := 0
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

var in *bufio.Reader
var out *bufio.Writer

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

// ri64 将直接读入并返回一个 int64
func ri64() (x int64) {
	scan(&x)
	return x
}

//rl 将直接读入一行整数，并返回一个 int 切片
func rl(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ri())
	}
	return a
}

//rl64 将直接读入一行整数，并返回一个 int64 切片
func rl64(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ri64())
	}
	return a
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出并在最后换行。
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//min64 可以传入多个 int64 变量，求出这些变量的最小值
func min64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//max64 可以传入多个 int64 变量，求出这些变量的最大值
func max64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}

//sum64 可以传入多个 int64 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum64 函数可以求出数组的和。
func sum64(a ...int64) int64 {
	ans := int64(0)
	for _, x := range a {
		ans += x
	}
	return ans
}

//reverse 用于翻转 int 切片，所有元素将会变成原切片的倒序
func reverse(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

//reverse64 用于翻转 int64 切片，所有元素将会变成原切片的倒序
func reverse64(a []int64) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

// lowerBound 类似于 C++ 中的 lower_bound，用于二分查找 int 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound 作用的 int 切片必须升序。
func lowerBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//lowerBound64 类似于 C++ 中的 lower_bound，用于二分查找 int64 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound64 作用的 int64 切片必须升序。
func lowerBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound 类似于 C++ 中的 upper_bound，用于二分查找 int 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound 作用的 int 切片必须升序。
func upperBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound64 类似于 C++ 中的 upper_bound，用于二分查找 int64 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound64 作用的 int64 切片必须升序。
func upperBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//unique 类似于 C++ 中的 unique，用于给切片去重。
//
//区别在于: unique 函数接受一个 int 切片，并返回一个新的、去好重的切片
//
//注意: 传入的切片必须升序
func unique(a []int) []int {
	var b []int
	b = append(b, a[0])
	for i := 1; i < len(a); i++ {
		if a[i] != a[i-1] {
			b = append(b, a[i])
		}
	}
	return b
}

//valSet 将传入切片的所有元素，统一赋值为传入的元素 k
func valSet(a []int, k int) {
	for i, _ := range a {
		a[i] = k
	}
}

//Stack 类型是一个 int 类型的栈
type Stack struct {
	s []int
}

//top 获取栈顶元素
func (st *Stack) top() int { return st.s[len(st.s)-1] }

//size 返回栈内的元素个数
func (st *Stack) size() int { return len(st.s) }

//empty 返回一个 bool 类型，表示栈是否为空
func (st *Stack) empty() bool { return st.size() == 0 }

//pop 弹出栈顶元素
func (st *Stack) pop() { st.s = st.s[:len(st.s)-1] }

//push 将一个 int 类型从栈顶添加入栈
func (st *Stack) push(x int) { st.s = append(st.s, x) }

//get 得到栈的内容（一个新的 int切片）
func (st *Stack) get() (ans []int) {
	copy(ans, st.s)
	return ans
}

//Queue 类型是一个 int 类型的队列
type Queue struct {
	q    []int
	head int
}

//front 获取队首元素
func (que *Queue) front() int { return que.q[que.head] }

//size 返回队列的元素个数
func (que *Queue) size() int { return len(que.q) - que.head }

//empty 返回一个 bool 类型，表示队列是否为空
func (que *Queue) empty() bool { return que.size() == 0 }

//pop 弹出队首元素
func (que *Queue) pop() { que.head++ }

//push 将一个 int 类型从队尾添加入队
func (que *Queue) push(x int) { que.q = append(que.q, x) }

//get 得到队列的内容（一个新的 int切片）
func (que *Queue) get() (ans []int) {
	copy(ans, que.q)
	return ans
}

//PriorityQueue 类似是一个 int 型的优先队列
//
//注意：你需要通过 newPriorityQueue 函数来获取一个优先队列实例，而不是直接创建
type PriorityQueue struct {
	heap []int
	cmp  func(int, int) bool
}

//heapifyUp 从 x 结点开始到堆顶，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyUp(x int) {
	for x > 1 && pq.cmp(pq.heap[x-1], pq.heap[x/2-1]) {
		pq.heap[x-1], pq.heap[x/2-1] = pq.heap[x/2-1], pq.heap[x-1]
		x >>= 1
	}
}

//heapifyDown 从 x 结点开始向子节点，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyDown(x int) {
	for (x << 1) <= len(pq.heap) {
		t := x << 1
		if t+1 <= len(pq.heap) && pq.cmp(pq.heap[t+1-1], pq.heap[t-1]) {
			t++
		}
		if !pq.cmp(pq.heap[t-1], pq.heap[x-1]) {
			break
		}
		pq.heap[x-1], pq.heap[t-1] = pq.heap[t-1], pq.heap[x-1]
		x = t
	}
}

//push 向优先队列中加入一个元素（并自动调整优先队列）
func (pq *PriorityQueue) push(x int) {
	pq.heap = append(pq.heap, x)
	pq.heapifyUp(len(pq.heap))
}

//size 返回优先队列内的元素个数
func (pq *PriorityQueue) size() int { return len(pq.heap) }

//top 返回优先队列中优先级最高的元素
func (pq *PriorityQueue) top() int { return pq.heap[1-1] }

//pop 弹出优先队列中优先级最高的元素
func (pq *PriorityQueue) pop() {
	pq.heap[1-1], pq.heap[len(pq.heap)-1] = pq.heap[len(pq.heap)-1], pq.heap[1-1]
	pq.heap = pq.heap[:len(pq.heap)-1]
	pq.heapifyDown(1)
}

//newPriorityQueue 返回一个指向优先队列的指针
//
//第一个形参：传入一个 int 类型切片，创建优先队列时，你可以用一个 int 类型切片作为初始来创建，也可以设为空切片（nil)
//
//第二个形参：传入一个 func(int, int) bool 类型的函数指针作为比较器，以规定优先队列的优先级。
//传入比较器的两个元素如果满足前者比后者大，则返回 true；否则返回 false。
//
//你可以使用已有的 greater 函数指定小的 int 优先，也可以使用已有的 less 函数指定大的 int 优先
func newPriorityQueue(a []int, fun func(int, int) bool) *PriorityQueue {
	var q PriorityQueue
	copy(q.heap, a)
	q.cmp = fun
	for i := len(a); i >= 1; i-- {
		q.heapifyDown(i)
	}
	return &q
}

//less 判断传入参数 a 和 b 是否满足 a > b
func less(a, b int) bool {
	if a > b {
		return true
	} else {
		return false
	}
}

//greater 判断传入参数 a 和 b 是否满足 a < b
func greater(a, b int) bool {
	if a < b {
		return true
	} else {
		return false
	}
}

package main

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

/*
1
6
1 -5 4 -6 4 3
---
52

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	accDiff0 := 0

	for i := 1; i < n; i++ {
		a[i] -= accDiff0

		diff := a[i] - a[i-1]
		if diff > 0 {
			a[i] -= diff
			accDiff0 += diff
		}
	}

	accDiff1 := 0

	for i := n - 2; i >= 0; i-- {
		a[i] -= accDiff1

		diff := a[i] - a[i+1]
		if diff > 0 {
			a[i] -= diff
			accDiff1 += diff
		}
	}

	fmt.Fprintln(writer, accDiff0+accDiff1+abs(a[0]))
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}


Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\,010\,111$$$ is a binary decimal, while $$$10\,201$$$ and $$$787\,788$$$ are not.Given a number $$$n$$$, you are asked to represent $$$n$$$ as a sum of some (not necessarily distinct) binary decimals. Compute the smallest number of binary decimals required for that.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$), denoting the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$), denoting the number to be represented.

For each test case, output the smallest number of binary decimals required to represent $$$n$$$ as a sum.

NoteIn the first test case, $$$121$$$ can be represented as $$$121 = 110 + 11$$$ or $$$121 = 111 + 10$$$.In the second test case, $$$5$$$ can be represented as $$$5 = 1 + 1 + 1 + 1 + 1$$$.In the third test case, $$$1\,000\,000\,000$$$ is a binary decimal itself, thus the answer is $$$1$$$.

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		sum:=0
		for j:=0;j<10;j++{
			if n%10 > sum {
				sum= n%10
			}
			n/=10
		}
		fmt.Fprintln(out,sum)
	}
}

package main

import "fmt"

func main() {
	var t int16
	_, err := fmt.Scanf("%d\n", &t)
	if err != nil {
		return
	}

	for i := 0; int16(i) < t; i++ {
		var n int64
		_, err := fmt.Scanf("%d\n", &n)
		if err != nil {
			return
		}

		var max int64 = -1
		for n > 0 {
			k := n % 10
			n = n / 10
			if k > max {
				max = k
			}
		}

		if max == -1 {
			max = n
		}

		fmt.Println(max)
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, p := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &p)
		mx := -1
		for p > 0 {
			if p%10 > mx {
				mx = p % 10
			}
			p = p / 10
		}
		fmt.Fprintln(w, mx)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

func (in *CF1530A) 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 *CF1530A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1530A
Date: 12/02/22
User: epradere
URL: https://codeforces.com/problemset/problem/1530/A
Problem: CF1530A
**/
func (in *CF1530A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		max := 0
		for i := 0; i < len(str); i++ {
			if max < int(str[i]-'0') {
				max = int(str[i] - '0')
			}
		}
		fmt.Println(max)
	}
}

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

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

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int) int {
	var (
		maxDigit int
	)
	for ; n > 0; n /= 10 {
		if maxDigit < n%10 {
			maxDigit = n % 10
		}
	}
	return maxDigit
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		fmt.Println(solve(n))
	}
}

package main
import(
	"fmt"
)
func solve(){
	var n int;
	fmt.Scan(&n);
	ans:=0;
	for ;n>0;n/=10{
		if ans<(n%10){
			ans=n%10;
		}
	}

	fmt.Println(ans);
}
func main(){
	var t int;
	fmt.Scan(&t);
	for i:=0;i<t;i++{
		solve();
	}

}

package main

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

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

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

}

func solve() {
	var number uint64
	fmt.Fscan(in, &number)

	var currentMax uint64 = 0
	var currentLastDigit uint64
	for number > 0 {
		currentLastDigit = number % 10
		currentMax = calcMax(currentMax, currentLastDigit)
		number /= 10
	}
	fmt.Println(currentMax)

}

func calcMax(first uint64, second uint64) uint64 {
	if first > second {
		return first
	}
	return second
}

package main

import "fmt"

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		var v int
		fmt.Scan(&v)
		ans := 0
		for v > 0 {
			var a int
			a = v % 10
			ans = max(ans, a)
			v = v / 10
		}
		fmt.Println(ans)
	}
}

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

package main

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

var (
	scn  = bufio.NewReader(os.Stdin)
	prnt = bufio.NewWriter(os.Stdout)
)

func solve() {
	var n string
	fmt.Fscan(scn, &n)
	var arr = [12]int{}
	l := len(n)

	for i := 0; i < l; i++ {
		arr[i] = int(n[i]) - 48
	}

	flag := true
	sum := 0

	for {
		for i := 0; i < l; i++ {
			if arr[i] > 0 {
				arr[i] -= 1
				flag = false
			}
		}

		if flag {
			break
		}

		flag = true
		sum += 1
	}

	fmt.Fprintln(prnt, sum)
}

func main() {
	defer prnt.Flush()
	var t int
	fmt.Fscan(scn, &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

func min(a ...int) int {
	mn := a[0]
	length := len(a)
	for i := 0; i < length; i++ {
		if a[i] < mn {
			mn = a[i]
		}
	}
	return mn
}

func max(a ...int) int {
	mx := a[0]
	length := len(a)
	for i := 0; i < length; i++ {
		if a[i] > mx {
			mx = a[i]
		}
	}
	return mx
}

func queue(queue []int, element int) []int {
	queue = append(queue, element)
	return queue
}

func dequeue(queue []int) []int {
	return queue[1:]
}

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

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var a, m int
		fmt.Scan(&a)
		for a > 0 {
			if a%10 > m {
				m = a % 10
			}
			a = a / 10
		}
		fmt.Println(m)
	}
}

package main

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

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

	var tcs int
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var n int
		_, _ = fmt.Fscan(in, &n)

		var ans int

		for n > 0 {
			ans = max(ans, n%10)
			n /= 10
		}

		_, _ = fmt.Fprintln(out, ans)
	}

	_ = out.Flush()
}

func abs(n int) int {
	if n < 0 {
		n = -n
	}

	return n
}

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

	return b
}

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

	return b
}

package main

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

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int64()
		printf("%d\n", sol(n))
	}
}

func sol(n int64) int {
	ret := 0
	for n > 0 {
		last := n % 10
		ret = max(ret, int(last))
		n /= 10
	}
	return ret
}

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

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		sum:=0
		for j:=0;j<10;j++{
			if n%10 == 0 || n%10 == 1 {
				n/=10
				continue
			}
			sum+= min(10-(n%10),n%10)
			n/=10
		}
		if sum == 0 {
			fmt.Fprintln(out,1)
		} else {
			fmt.Fprintln(out,sum)
		}
	}
}

func min(x,y int) int {
	if x<=y{
		return x
	}
	return y
}

package main

import "fmt"

func main() {
	var t int16
	_, err := fmt.Scanf("%d\n", &t)
	if err != nil {
		return
	}

	for i := 0; int16(i) < t; i++ {
		var n int64
		_, err := fmt.Scanf("%d\n", &n)
		if err != nil {
			return
		}

		var max int64 = -1
		for n / 10 > 0 {
			n = n / 10
			k := n % 10
			if k > max {
				max = k
			}
		}

		if max == -1 {
			max = n
		}

		fmt.Println(max)
	}
}