This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved.

Pikachu is a cute and friendly pokémon living in the wild pikachu herd.

But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist.

First, Andrew counted all the pokémon — there were exactly $n$ pikachu. The strength of the $i$-th pokémon is equal to $a_i$, and all these numbers are distinct.

As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokémons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$.

The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$.

Andrew is experimenting with pokémon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokémon.

Note: $q=0$ in this version of the task.

Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation.

Help Andrew and the pokémon, or team R will realize their tricky plan!


-----Input-----

Each test contains multiple test cases.

The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokémon and number of operations respectively.

The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokémon.

$i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokémon that were swapped in the $i$-th operation.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. 


-----Output-----

For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap.


-----Example-----
Input
3
3 0
1 3 2
2 0
1 2
7 0
1 2 5 4 3 6 7

Output
3
2
9



-----Note-----

In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5−3+7=9$.

Source: codeforces | Difficulty: EASY
Tags: Greedy algorithms

Dynamic programming

Constructive algorithms


Solutions:
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	a.append(0)
	count = 0
	Pokemon = []
	for i in range(n):
		if a[i] > a[i + 1] and count % 2 == 0:
			Pokemon.append(a[i])
			count += 1
		if a[i] < a[i + 1] and count % 2 == 1:
			Pokemon.append(-a[i])
			count += 1
		Pokemon.append(a[-1])
	if len(Pokemon) % 2 == 0:
		Pokemon.pop()
	print(sum(Pokemon))


def main():
	for tc in range(int(input())):
		(n, q) = map(int, input().split())
		ar = list(map(int, input().split()))
		(ans, inc) = (0, True)
		for i in range(n - 1):
			if inc and ar[i + 1] < ar[i]:
				ans += ar[i]
				inc = False
			elif not inc and ar[i + 1] > ar[i]:
				ans -= ar[i]
				inc = True
		if inc:
			ans += ar[-1]
		print(ans)
main()


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	best_p = best_m = 0
	for x in a:
		best_p = max(best_p, best_m - x)
		best_m = max(best_m, best_p + x)
	print(max(best_p, best_m))


from sys import stdin, stdout
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, stdin.readline().strip().split()))
	if n == 1:
		print(*arr)
	elif n == 2:
		print(max(arr))
	else:
		ans = 0
		add = True
		for i in range(1, n - 1):
			if arr[i - 1] < arr[i] > arr[i + 1] and add == True:
				ans += arr[i]
				add = False
			elif arr[i - 1] > arr[i] < arr[i + 1] and add == False:
				ans -= arr[i]
				add = True
		if add == True:
			ans += arr[-1]
		ans1 = arr[0]
		add1 = False
		for i in range(1, n - 1):
			if arr[i - 1] < arr[i] > arr[i + 1] and add1 == True:
				ans1 += arr[i]
				add1 = False
			elif arr[i - 1] > arr[i] < arr[i + 1] and add1 == False:
				ans1 -= arr[i]
				add1 = True
		if add1 == True:
			ans1 += arr[-1]
		print(max(ans, ans1))


from typing import List

def maximum_alternating_subsequence(values: List[int]) -> int:
	(maximums, minimums) = ([0] * (len(values) + 1), [0] * (len(values) + 1))
	for index in range(len(values)):
		maximums[index + 1] = max(maximums[index], values[index] - minimums[index])
		minimums[index + 1] = min(minimums[index], values[index] - maximums[index])
	return maximums[-1]

def read_input():
	cases = int(input())
	while cases:
		cases = cases - 1
		(_, _) = input().split(' ')
		values = list(map(int, input().split(' ')))
		print(maximum_alternating_subsequence(values))

def main():
	read_input()
main()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	ar = list(map(int, input().split()))
	t = [ar[0]]
	up = True
	for i in range(1, n):
		if up:
			if ar[i] > t[-1]:
				t[-1] = ar[i]
			elif ar[i] < t[-1]:
				t.append(ar[i])
				up = False
		elif ar[i] < t[-1]:
			t[-1] = ar[i]
		elif ar[i] > t[-1]:
			t.append(ar[i])
			up = True
	if up == False:
		t.pop()
	ans = 0
	if len(t) % 2:
		ans += t[-1]
		for i in range(0, len(t) - 1, 2):
			ans += t[i] - t[i + 1]
	else:
		for i in range(0, len(t), 2):
			ans += t[i] - t[i + 1]
	print(max(max(ar), ans))


t = int(input())
for q in range(t):
	(n, q) = map(int, input().split())
	mass = [int(x) for x in input().split()]
	if n == 1:
		print(mass[0])
	else:
		answer = 0
		f = 1
		if mass[0] > mass[1]:
			answer += mass[0]
			f = 2
		for i in range(1, n - 1):
			if mass[i - 1] < mass[i] and mass[i] > mass[i + 1]:
				answer += mass[i]
				f = 2
			elif mass[i - 1] > mass[i] and mass[i] < mass[i + 1]:
				answer -= mass[i]
				f = 1
		if mass[-2] < mass[-1] and f == 1:
			answer += mass[-1]
		print(answer)


import math
from decimal import *
import random
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	val = 0
	mx = 1
	tp = 0
	mn = int(1000000.0)
	for i in range(n):
		if arr[i] > mx and tp == 0:
			mx = arr[i]
		elif arr[i] < mx and tp == 0:
			mn = arr[i]
			tp = 1
			val += mx
		elif arr[i] > mn and tp:
			mx = arr[i]
			val -= mn
			tp = 0
			mn = int(1000000.0)
		elif arr[i] < mn and tp:
			mn = arr[i]
	if tp == 0:
		val += mx
	print(val)


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	(N, Q) = neo()
	A = Neo()
	(dp1, dp2) = ([0] * (N + 1), [0] * (N + 1))
	dp1[0] = -10 ** 18
	for i in range(N):
		dp1[i + 1] = max(dp1[i], dp2[i] + A[i])
		dp2[i + 1] = max(dp2[i], dp1[i] - A[i])
	print(max(dp1[-1], dp2[-1]))


for time in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	DP = [[0, 0] for i in range(n + 1)]
	for i in range(n):
		DP[i + 1][0] = max(DP[i][0], DP[i][1] + l[i])
		DP[i + 1][1] = max(DP[i][1], DP[i][0] - l[i])
	print(DP[n][0])


import sys
import itertools as it
import math as mt
import collections as cc
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n, q) = I()
	l = I()
	ans = 0
	ch = 1
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			if ch:
				ans += l[i]
				ch ^= 1
		elif not ch:
			if l[i] < l[i + 1]:
				ans -= l[i]
				ch ^= 1
	if ch:
		ans += l[-1]
	print(ans)


from sys import *
from math import *
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	a = list(map(int, stdin.readline().split()))
	x = a[0]
	y = 0
	b = []
	for i in range(1, n):
		if a[i] > x and y == 0:
			x = a[i]
		if a[i] < x and y == 0:
			y = a[i]
		if a[i] < y:
			y = a[i]
		if a[i] > y and y != 0:
			b.append(x)
			b.append(y)
			x = a[i]
			y = 0
	b.append(x)
	ans = 0
	f = 0
	for i in range(len(b)):
		if f == 0:
			ans += b[i]
			f = 1
		elif f == 1:
			ans -= b[i]
			f = 0
	print(ans)


def lowbit(x: int):
	return x & -x

def treearray_update(pos: int, x: int, array: list):
	while pos < len(array):
		array[pos] += x
		pos += lowbit(pos)

def treearray_getsum(pos: int, array: list) -> int:
	ans = 0
	while pos > 0:
		ans += array[pos]
		pos -= lowbit(pos)
	return ans

def calcinvs(array: list):
	d = {}
	for (k, v) in enumerate(sorted(array)):
		d[v] = k + 1
	treearray = [0 for i in range(1 + len(array))]
	invs = 0
	for i in array:
		invs += treearray_getsum(len(treearray) - 1, treearray) - treearray_getsum(d[i], treearray)
		treearray_update(d[i], 1, treearray)
	return invs
import math
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	li = [int(i) for i in input().split()]
	if n == 1:
		print(li[0])
		continue
	cankao = li[0]
	hi = cankao
	status = 1
	lo = cankao
	ans = 0
	for (p, i) in enumerate(li[1:]):
		p += 1
		if status:
			if i > hi:
				hi = i
			else:
				status = 0
				lo = i
		elif i < lo:
			lo = i
		else:
			status = 1
			ans += hi - lo
			hi = i
	ans += hi
	print(ans)


def solve(n, q, seq):
	minusNext = True
	previousBiggest = seq[0]
	index = 1
	temp = seq[0]
	while index < n:
		if minusNext:
			if seq[index] > seq[index - 1]:
				temp -= seq[index - 1]
				temp += seq[index]
			else:
				temp -= seq[index]
				minusNext = False
		elif seq[index] < seq[index - 1]:
			temp += seq[index - 1]
			temp -= seq[index]
		else:
			temp += seq[index]
			minusNext = True
		if temp > previousBiggest:
			previousBiggest = temp
		index += 1
	return previousBiggest
for x in range(int(input())):
	(n, q) = list(map(int, input().split()))
	seq = list(map(int, input().split()))
	print(solve(n, q, seq))


import sys
import math
from collections import Counter, defaultdict
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
S = lambda : input()

def case():
	(n, q) = MAP()
	a = LI()
	ans = 0
	for i in range(1, n):
		if a[i - 1] > a[i]:
			ans += a[i - 1] - a[i]
	ans += a[-1]
	print(ans)
for _ in range(IN()):
	case()


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	i = 1
	ans = 0
	f = 0
	if n == 1:
		print(a[0])
		continue
	if n == 2:
		print(max(a[0], a[1]))
		continue
	if a[0] >= a[1]:
		ans = ans + a[0]
		f = 1
	while i < n - 1:
		if f == 0:
			if a[i] >= a[i - 1] and a[i] >= a[i + 1]:
				ans = ans + a[i]
				f = 1
			elif i == n - 2:
				ans = ans + a[i + 1]
				f = 1
				break
		elif a[i] <= a[i - 1] and a[i] <= a[i + 1]:
			ans = ans - a[i]
			f = 0
			if i == n - 2:
				ans = ans + a[i + 1]
				break
		i = i + 1
	print(ans)


t = 1
t = int(input())
while t:
	t -= 1
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	for i in range(q):
		(l, r) = map(int, input().split())
	peaks = []
	for i in range(1, n - 1):
		if a[i - 1] > a[i] < a[i + 1]:
			peaks.append([i, False])
		if a[i - 1] < a[i] > a[i + 1]:
			peaks.append([i, True])
	if not peaks:
		print(max(a))
		continue
	if peaks[0][1] == False:
		peaks = [[0, True]] + peaks
	if peaks[-1][1] == False:
		peaks.append([n - 1, True])
	ans = 0
	for (ind, stat) in peaks:
		if stat:
			ans += a[ind]
		else:
			ans -= a[ind]
	print(ans)


for _ in range(int(input())):
	(n, q) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	if n == 1:
		print(l[0])
	else:
		dpodd = l[0]
		dpeven = 0
		dpodd = max(dpodd, dpeven + l[1])
		dpeven = max(dpeven, dpodd - l[1])
		ans = max(dpodd, dpeven)
		for i in range(2, n):
			dpodd = max(dpodd, dpeven + l[i])
			dpeven = max(dpeven, dpodd - l[i])
			ans = max(ans, dpodd, dpeven)
		print(ans)


def solve(n, l):
	ans = 0
	if n == 1 or n == 2:
		return max(l)
	if l[1] < l[0]:
		ans = l[0]
	for i in range(1, n - 1):
		if l[i] > l[i - 1] and l[i] > l[i + 1]:
			ans += l[i]
		if l[i] < l[i - 1] and l[i] < l[i + 1]:
			ans -= l[i]
	if l[n - 1] > l[n - 2]:
		ans += l[n - 1]
	return ans
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	print(solve(n, l))


import sys
input = sys.stdin.readline
from bisect import bisect_right
bin_s = [1]
while bin_s[-1] <= 10 ** 9:
	bin_s.append(bin_s[-1] * 2)

def main():
	(n, q) = map(int, input().split())
	alst = list(map(int, input().split()))
	dp = [[-1, -1] for _ in range(n)]
	dp[0] = [alst[0], 0]
	for (i, a) in enumerate(alst[1:], start=1):
		dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + a)
		dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - a)
	print(max(dp[-1]))
for _ in range(int(input())):
	main()


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	(n, q) = li()
	l = li()
	dp = [[0] * 2 for i in range(n + 1)]
	for i in range(n - 1, -1, -1):
		dp[i][0] = max(l[i] + dp[i + 1][1], dp[i + 1][0])
		dp[i][1] = max(-l[i] + dp[i + 1][0], dp[i + 1][1])
	ans = 0
	for i in range(n):
		ans = max(ans, dp[i][0])
	print(ans)


import math
t = int(input())
for _ in range(t):
	(n, q) = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	dp = [[-100000000 for i in range(n)] for j in range(2)]
	cur = 0
	dp[0][0] = arr[0]
	dp[1][0] = 0
	for i in range(1, n):
		dp[0][i] = max(dp[0][i - 1], dp[1][i - 1] + arr[i])
		dp[1][i] = max(dp[1][i - 1], dp[0][i - 1] - arr[i])
	print(max(dp[0][-1], dp[1][-1]))


from collections import defaultdict

def main():
	T = int(input())
	while T > 0:
		T -= 1
		(N, Q) = list(map(int, input().split()))
		v = list(map(int, input().split()))
		ans = v[N - 1]
		add = v[N - 1]
		remove = -v[N - 1]
		for i in range(N - 2, -1, -1):
			current_add = v[i] + max(0, remove)
			current_remove = -v[i] + add
			add = max(add, current_add)
			remove = max(remove, current_remove)
			ans = max(ans, add)
		print(ans)
main()


def solve(n, arr):
	n = len(arr)
	i = 0
	index = []
	while i < n:
		f = 0
		while i < n and f <= arr[i]:
			f = arr[i]
			i += 1
		index.append(i)
		s = float('inf')
		while i < n and s >= arr[i]:
			s = arr[i]
			i += 1
		if i < n:
			index.append(i)
	index.append(i)
	if len(index) >= 2 and index[-1] == index[-2]:
		index.pop()
	army = []
	for i in index:
		army.append(arr[i - 1])
	if len(army) % 2 == 0:
		if army[-1] < army[-2]:
			army.pop()
		else:
			k = army.pop()
			army.pop()
			army.append(k)
	sum1 = 0
	for i in range(0, len(army), 2):
		sum1 += army[i]
	for i in range(1, len(army), 2):
		sum1 -= army[i]
	print(sum1)
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	solve(n, arr[:])
	for _ in range(q):
		(a, b) = map(int, input().split())
		(arr[a - 1], arr[b - 1]) = (arr[b - 1], arr[a - 1])
		solve(n, arr[:])


import math
from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(100000)

def put():
	return map(int, stdin.readline().split())
for _ in range(int(input())):
	(n, k) = put()
	l = list(put())
	dp = [[-1 for i in range(5)] for i in range(n + 5)]
	for ind in range(n + 1):
		for parity in range(2):
			if ind == 0:
				dp[ind][parity] = 0
			elif parity == 0:
				dp[ind][parity] = max(l[ind - 1] + dp[ind - 1][1], dp[ind - 1][0])
			else:
				dp[ind][parity] = max(-l[ind - 1] + dp[ind - 1][0], dp[ind - 1][1])
	print(dp[n][0])


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	a.insert(0, 0)
	ans = []
	increase = True
	val = 0
	for i in range(1, n + 1):
		if increase:
			if a[i] >= a[i - 1]:
				val = a[i]
			else:
				ans.append(val)
				val = a[i]
				increase = False
		elif a[i] <= a[i - 1]:
			val = a[i]
		else:
			ans.append(val)
			val = a[i]
			increase = True
	ans.append(a[-1])
	for i in range(len(ans)):
		if i % 2 == 1:
			ans[i] *= -1
	val = 0
	k = 0
	i = 0
	while i < len(ans):
		if val + ans[i] > 0:
			val += ans[i]
			k = max(k, val)
		else:
			val = 0
		i += 1
	print(k)


def fun(a):
	ans = 0
	temp = 1
	for i in range(n - 1):
		if temp == 1:
			if a[i + 1] < a[i]:
				ans += a[i]
				temp = 0
		elif a[i + 1] > a[i]:
			ans -= a[i]
			temp = 1
	if temp == 1:
		ans += a[n - 1]
	return ans
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	print(fun(a))
	for j in range(q):
		(x, y) = map(lambda x: int(x) - 1, input().split())
		(a[x], a[y]) = (a[y], a[x])
		print(fun(a))


from sys import stdin
input = stdin.readline

def gt():
	return list(map(int, input().split()))
(t,) = gt()
while t:
	t -= 1
	(n, q) = gt()
	ar = gt()
	dp0 = [0] * n
	dp1 = [0] * n
	mx0 = 0
	mx1 = 0
	for i in range(n):
		dp0[i] = mx1 + ar[i]
		dp1[i] = mx0 - ar[i]
		mx0 = max(mx0, dp0[i])
		mx1 = max(mx1, dp1[i])
	print(max(mx0, mx1))


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	a = l[0]
	for i in range(1, n):
		a = a + max(0, l[i] - l[i - 1])
	print(a)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	mylist = list(map(int, input().split()))
	total = mylist[0]
	for i in range(1, n):
		if mylist[i] > mylist[i - 1]:
			total += mylist[i] - mylist[i - 1]
	print(total)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = []
	if n == 1:
		print(a[0])
		continue
	(inc, dec) = (False, False)
	if a[0] <= a[1]:
		inc = True
		i = 1
	else:
		ans.append(a[0])
		i = 1
		dec = True
	while i < n:
		if inc:
			while i < n and a[i] >= a[i - 1]:
				i += 1
			if i < n:
				ans.append(a[i - 1])
			else:
				ans.append(a[i - 1])
				break
			inc = False
			dec = True
		else:
			while i < n and a[i] <= a[i - 1]:
				i += 1
			if i < n:
				ans.append(a[i - 1])
			else:
				break
			inc = True
			dec = False
	grt = 0
	for i in range(len(ans)):
		if i % 2:
			grt -= ans[i]
		else:
			grt += ans[i]
	print(grt)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write('\n'.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
mod = 998244353
INF = float('inf')
for t in range(int(data())):
	(n, q) = mdata()
	a = mdata()
	l = [a[0]]
	k = 0
	ans = 0
	for i in range(1, n):
		if a[i] < l[-1]:
			if k == 0:
				l.append(a[i])
				k = 1
			else:
				l[-1] = a[i]
		elif k == 1:
			l.append(a[i])
			k = 0
		else:
			l[-1] = a[i]
	for i in range(1, len(l), 2):
		ans += l[i - 1] - l[i]
	ans += l[-1]
	out(ans)


import sys
input = sys.stdin.readline
T = int(input())
while T:
	(n, q) = map(int, input().split())
	A = list(map(int, input().split()))
	s = 0
	f = True
	for i in range(n - 1):
		if f:
			if A[i] > A[i + 1]:
				s += A[i]
				f = False
		elif A[i] < A[i + 1]:
			s -= A[i]
			f = True
	if f:
		s += A[n - 1]
	print(s)
	T = T - 1


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	if n == 1:
		print(l[0])
		continue
	if n == 2:
		print(max(l[0], l[1]))
		continue
	i = 0
	f = -1
	g = 0
	o = []
	while i < len(l):
		if i == 0:
			if l[i] < l[i + 1]:
				f = 0
				g = 1
				o.append(-1 * l[i])
			else:
				f = 1
				o.append(l[i])
			i = i + 1
			continue
		if i == len(l) - 1:
			if l[i] < l[i - 1] and f == 1:
				f = 0
				o.append(-1 * l[i])
				i = i + 1
				continue
			if l[i] > l[i - 1] and f == 0:
				f = 1
				o.append(l[i])
				i = i + 1
				continue
		if l[i] > l[i - 1] and l[i] > l[i + 1] and (f == 0):
			f = 1
			o.append(l[i])
			i = i + 1
			continue
		if l[i] < l[i - 1] and l[i] < l[i + 1] and (f == 1):
			f = 0
			o.append(-1 * l[i])
			i = i + 1
			continue
		i += 1
	if g:
		o.pop(0)
	if f == 0:
		o.pop()
	print(sum(o))


num = int(input())
while num > 0:
	num = num - 1
	(a, b) = map(int, input().split())
	ar = list(map(int, input().split()))
	ans = 0
	k = 0
	if len(ar) == 1:
		print(ar[0])
	else:
		if ar[0] > ar[1]:
			ans = ar[0]
			k = 1
		for i in range(1, a - 1):
			if k == 1 and ar[i] < ar[i - 1] and (ar[i] < ar[i + 1]):
				k = 0
				ans -= ar[i]
			elif k == 0 and ar[i] > ar[i - 1] and (ar[i] > ar[i + 1]):
				k = 1
				ans += ar[i]
		if ar[-1] > ar[-2] and k == 0:
			ans += ar[-1]
		print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	ar = [int(x) for x in input().split()]
	ok = True
	ans = 0
	for i in range(n - 1):
		if ok and ar[i] < ar[i + 1]:
			continue
		elif ok and ar[i] > ar[i + 1]:
			ans += ar[i]
			ok = False
		elif not ok and ar[i] > ar[i + 1]:
			continue
		else:
			ok = True
			ans -= ar[i]
	if ok:
		ans += ar[n - 1]
	print(ans)


def solve(n, a):
	mx = [0] * (n + 1)
	mn = [0] * (n + 1)
	for i in range(n - 1, -1, -1):
		mx[i] = max(mx[i + 1], a[i] - mn[i + 1])
		mn[i] = min(mn[i + 1], a[i] - mx[i + 1])
	return mx[0]
t = int(input())
while t:
	res = []
	(n, q) = map(int, input().split(' '))
	in_ = map(int, input().split(' '))
	for x in in_:
		res.append(int(x))
	print(solve(n, res))
	t -= 1


def dyn_solve(x):
	memo = {}

	def solve(even, j):
		if (even, j) in memo.keys():
			return memo[even, j]
		elif j == 0:
			y = int(not even) * x[0]
		elif even:
			y = max(solve(False, j - 1) - x[j], solve(True, j - 1))
		else:
			y = max(solve(True, j - 1) + x[j], solve(False, j - 1))
		memo[even, j] = y
		return y
	for i in range(len(x)):
		solve(True, i)
		solve(False, i)
	return solve(False, n - 1)
t = int(input())
for case in range(t):
	(n, q) = [int(s) for s in input().split(' ')]
	a = [int(s) for s in input().split(' ')]
	print(dyn_solve(a))


from collections import defaultdict
t = int(input())
while t > 0:
	t -= 1
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	a.append(-1)
	if a[0] > a[1]:
		ans += a[0]
	for i in range(1, n, 1):
		if a[i] > a[i - 1] and a[i] > a[i + 1]:
			ans += a[i]
		if a[i] < a[i - 1] and a[i] < a[i + 1]:
			ans -= a[i]
	print(ans)


for t in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	k = 0
	power = 0
	fl_min = True
	while k < len(a) - 1:
		while k < len(a) - 1:
			if a[k] > a[k + 1]:
				power += a[k]
				k += 1
				fl_min = False
				break
			k += 1
		while k < len(a) - 1:
			if a[k] < a[k + 1]:
				power -= a[k]
				k += 1
				fl_min = True
				break
			k += 1
	if fl_min:
		power += a[-1]
	print(power)


input = __import__('sys').stdin.readline

def check(*inds):
	global localmax, localmin, maxs, mins
	for ind in inds:
		if not ok(ind):
			continue
		if localmax[ind] == 0 and s[ind - 1] < s[ind] > s[ind + 1]:
			localmax[ind] = 1
			maxs += s[ind]
		if localmin[ind] == 0 and s[ind - 1] > s[ind] < s[ind + 1]:
			localmin[ind] = 1
			mins += s[ind]

def upd0(*inds):
	global localmax, localmin, maxs, mins
	for ind in inds:
		if not ok(ind):
			continue
		if localmax[ind]:
			localmax[ind] = 0
			maxs -= s[ind]
		if localmin[ind]:
			localmin[ind] = 0
			mins -= s[ind]

def ok(ind):
	return 0 <= ind - 1 <= ind + 1 <= n
ans = []
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	n += 1
	s = [0] + list(map(int, input().split())) + [0]
	localmax = [0] * n
	localmin = [0] * n
	maxs = mins = 0
	for i in range(n):
		check(i)
	ans.append(maxs - mins)
	for _ in range(q):
		(l, r) = map(int, input().split())
		upd0(l, l - 1, l + 1, r, r - 1, r + 1)
		(s[l], s[r]) = (s[r], s[l])
		check(l, l - 1, l + 1, r, r - 1, r + 1)
		ans.append(maxs - mins)
print(*ans, sep='\n')


import os
import sys
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
GANS = []

def print(*args):
	global GANS
	for i in args:
		GANS.append(f'{i}'.encode())
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	li = [-1] + [int(i) for i in input().split()] + [-1]
	if n == 1:
		print(li[1])
		for qi in range(q):
			trash = input()
			print(li[1])
		continue
	if n == 2:
		print(max(li[2], li[1]))
		for qi in range(q):
			trash = input()
			print(max(li[2], li[1]))
		continue
	hi = li[0]
	status = 1
	lo = li[0]
	ans = 0
	for i in li[1:]:
		if status:
			if i > hi:
				hi = i
			else:
				status = 0
				lo = i
		elif i < lo:
			lo = i
		else:
			status = 1
			ans += hi - lo
			hi = i
	ans += hi
	print(ans)
	for qi in range(q):
		(u, v) = map(int, input().split())
		if u == v:
			print(ans)
			continue
		da = max(li[u - 1], li[u + 1])
		xiao = min(li[u - 1], li[u + 1])
		ans -= max(li[u] - da, 0)
		ans -= max(xiao - li[u], 0)
		ans += max(li[v] - da, 0)
		ans += max(xiao - li[v], 0)
		tmp = li[u]
		li[u] = li[v]
		da = max(li[v - 1], li[v + 1])
		xiao = min(li[v - 1], li[v + 1])
		ans -= max(li[v] - da, 0)
		ans -= max(xiao - li[v], 0)
		ans += max(tmp - da, 0)
		ans += max(xiao - tmp, 0)
		li[v] = tmp
		print(ans)
os.write(1, b'\n'.join(GANS) + b'\n')


import io
import os
cin = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
MAX = 1
MIN = -1

def get_input():
	return cin().split()

def get_int_input():
	return list(map(int, get_input()))

class PokemonArmy:

	def __init__(self, n, nums):
		self._nums = [-1] + nums + [-1]
		self.n = n
		self.strength = 0
		self._global_max_min = [0] * (n + 2)
		for i in range(1, n + 1):
			self._update_strength(i)

	def _update_strength(self, idx):
		if idx == 0 or idx == self.n + 1:
			return
		new_val = 0
		if self._nums[idx - 1] < self._nums[idx] > self._nums[idx + 1]:
			new_val = MAX
		elif self._nums[idx - 1] > self._nums[idx] < self._nums[idx + 1]:
			new_val = MIN
		self.strength += (new_val - self._global_max_min[idx]) * self._nums[idx]
		self._global_max_min[idx] = new_val

	def swap(self, i, j):
		self.strength -= self._global_max_min[i] * self._nums[i]
		self._global_max_min[i] = 0
		self.strength -= self._global_max_min[j] * self._nums[j]
		self._global_max_min[j] = 0
		(self._nums[i], self._nums[j]) = (self._nums[j], self._nums[i])
		self._update_strength(i - 1)
		self._update_strength(i)
		self._update_strength(i + 1)
		self._update_strength(j - 1)
		self._update_strength(j)
		self._update_strength(j + 1)

def main():
	(T,) = get_int_input()
	for _ in range(T):
		(n, q) = get_int_input()
		nums = get_int_input()
		pokemon_army = PokemonArmy(n, nums)
		print(pokemon_army.strength)
		for i in range(q):
			pokemon_army.swap(*get_int_input())
			print(pokemon_army.strength)
main()


t = int(input())
for k in range(t):
	n = int(input().split()[0])
	a = list(map(int, input().split()))
	c = 0
	d = 0
	for i in range(1, n):
		if d == 0:
			if a[i] < a[i - 1]:
				c += a[i - 1]
				d = 1
		elif a[i] > a[i - 1]:
			c -= a[i - 1]
			d = 0
	if d == 0:
		c += a[-1]
	print(c)


for i1 in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	max = 0
	min = 0
	if n == 1:
		print(l[0])
		continue
	if l[0] > l[1]:
		max += l[0]
	if l[-1] > l[-2]:
		max += l[-1]
	for i in range(len(l) - 2):
		t1 = l[i]
		t2 = l[i + 1]
		t3 = l[i + 2]
		if t2 > t3 and t2 > t1:
			max += t2
		if t2 < t3 and t2 < t1:
			min += t2
	print(max - min)


def change(values, is_max, is_min, i, new_val):
	ans_max = 0
	ans_min = 0
	if is_max[i]:
		ans_max -= values[i]
		is_max[i] = False
	if is_min[i]:
		ans_min -= values[i]
		is_min[i] = False
	values[i] = new_val
	if values[i - 1] < values[i] and values[i + 1] < values[i]:
		is_max[i] = True
		ans_max += values[i]
	if values[i - 1] > values[i] and values[i + 1] > values[i]:
		is_min[i] = True
		ans_min += values[i]
	return (ans_max, ans_min)

def main():
	t = int(input())
	for i in range(t):
		(n, q) = map(int, input().split())
		values = list(map(int, input().split()))
		is_max = [False for i in range(n + 2)]
		is_min = [False for i in range(n + 2)]
		values = [-10000000000.0] + values + [-10000000000.0]
		sum_max = 0
		sum_min = 0
		for i in range(1, n + 1):
			(add_max, add_min) = change(values, is_max, is_min, i, values[i])
			sum_max += add_max
			sum_min += add_min
		print(sum_max - sum_min)
		for i in range(q):
			(l, r) = map(int, input().split())
			last = values[l]
			(add_max, add_min) = change(values, is_max, is_min, l, values[r])
			sum_max += add_max
			sum_min += add_min
			(add_max, add_min) = change(values, is_max, is_min, r, last)
			sum_max += add_max
			sum_min += add_min
			for i in [l - 1, l + 1, r - 1, r + 1]:
				if i == 0 or i == n + 1:
					continue
				(add_max, add_min) = change(values, is_max, is_min, i, values[i])
				sum_max += add_max
				sum_min += add_min
			print(sum_max - sum_min)
main()


T = int(input())
for _ in range(T):
	(N, Q) = map(int, input().split())
	A = list(map(int, input().split()))
	INF = float('inf')
	dp = [[0, -INF] for _ in range(N + 1)]
	for (i, a) in enumerate(A):
		dp[i + 1] = dp[i]
		dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + a)
		dp[i + 1][0] = max(dp[i + 1][0], dp[i][1] - a)
	print(max(dp[-1]))


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	lis = list(map(int, input().split()))
	ans = lis[0]
	c = 0
	inc = 1
	for j in range(1, n):
		if inc == 1:
			if lis[j] >= lis[j - 1]:
				if j == n - 1:
					c = c + lis[n - 1]
				if ans < c:
					ans = c
				pass
			else:
				c = c + lis[j - 1]
				inc = 0
				if c > ans:
					ans = c
		elif lis[j] <= lis[j - 1]:
			pass
		else:
			c = c - lis[j - 1]
			inc = 1
			if j == n - 1:
				if lis[j] >= lis[j - 1]:
					c = c + lis[j]
			if c > ans:
				ans = c
	print(ans)


TC = int(input())
for tc in range(TC):
	(N, Q) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	result = 0
	if N == 1:
		print(A[0])
		continue
	if A[0] > A[1]:
		result += A[0]
	if A[-1] > A[-2]:
		result += A[-1]
	for i in range(1, N - 1):
		if A[i - 1] < A[i] > A[i + 1]:
			result += A[i]
		if A[i - 1] > A[i] < A[i + 1]:
			result -= A[i]
	print(result)


import sys as _sys

def main():
	t = int(input())
	for i_t in range(t):
		(n, q) = _read_ints()
		assert q == 0
		a = tuple(_read_ints())
		result = find_max_army_power(a)
		print(result)

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split(' '))

def find_max_army_power(units_powers):
	units_powers = tuple(units_powers)
	max_power_by_units_n_parity_and_unit_index_limit = [[-float('inf')] * (len(units_powers) + 1) for parity in range(2)]
	max_power_by_units_n_parity_and_unit_index_limit[0][0] = 0
	for unit_index_limit in range(1, len(units_powers) + 1):
		curr_unit_power = units_powers[unit_index_limit - 1]
		max_power_by_units_n_parity_and_unit_index_limit[1][unit_index_limit] = max(max_power_by_units_n_parity_and_unit_index_limit[1][unit_index_limit - 1], max_power_by_units_n_parity_and_unit_index_limit[0][unit_index_limit - 1] + curr_unit_power)
		max_power_by_units_n_parity_and_unit_index_limit[0][unit_index_limit] = max(max_power_by_units_n_parity_and_unit_index_limit[0][unit_index_limit - 1], max_power_by_units_n_parity_and_unit_index_limit[1][unit_index_limit - 1] - curr_unit_power)
	return max(max_power_by_units_n_parity_and_unit_index_limit[0][-1], max_power_by_units_n_parity_and_unit_index_limit[1][-1])
main()


for _ in range(int(input())):
	(n, q) = [*map(int, input().split())]
	a = [*map(int, input().split())]
	su = 0
	if n == 1:
		print(a[0])
	else:
		for i in range(1, n - 1):
			if a[i] >= a[i + 1] and a[i] >= a[i - 1]:
				su += a[i]
			elif a[i] < a[i + 1] and a[i] < a[i - 1]:
				su -= a[i]
		if a[0] > a[1]:
			su += a[0]
		if a[n - 1] > a[n - 2]:
			su += a[n - 1]
		print(su)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	c = list(map(int, input().split()))
	st = 0
	q = 0
	v = []
	while st < a:
		if c[st] >= q:
			while st < a and c[st] >= q:
				q = c[st]
				st += 1
			v.append(q)
		else:
			while st < a and c[st] <= q:
				q = c[st]
				st += 1
			v.append(q)
	if len(v) % 2 == 0:
		v.pop()
	ans = v[-1]
	for i in range(0, len(v) - 1, 2):
		ans += v[i] - v[i + 1]
	print(ans)


import sys
input = sys.stdin.buffer.readline
INF = 10 ** 8
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	max_dp = [-INF] * (n + 1)
	min_dp = [INF] * (n + 1)
	max_dp[0] = 0
	min_dp[0] = 0
	for i in range(n):
		max_dp[i + 1] = max(max_dp[i], -min_dp[i] + a[i])
		min_dp[i + 1] = min(min_dp[i], -max_dp[i] + a[i])
	print(max_dp[-1])


for T in range(int(input())):
	(n, Q) = input().split()
	n = int(n)
	a = [int(x) for x in input().split()]
	ans = 0
	z = a[0]
	for i in range(1, n):
		if a[i] > z:
			z = a[i]
		else:
			ans += z - a[i]
			z = a[i]
	print(ans + z)


def answer(n, q, A):
	if n == 1:
		return A[0]
	count = 0
	maxi = A[0]
	for i in range(1, n):
		if A[i] > A[i - 1]:
			count += maxi - A[i - 1]
			maxi = A[i]
	count += maxi
	return count
t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(answer(n, q, arr))


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	list1 = list(map(int, input().strip().split()))[:n]
	i = 0
	sum = 0
	check = max(list1)
	flag = 0
	while i < n:
		if flag == 0:
			if i == n - 1:
				sum += list1[i]
			elif i < n - 1 and list1[i] > list1[i + 1]:
				sum += list1[i]
				flag = 1
			i += 1
		else:
			if i < n - 1 and list1[i] < list1[i + 1]:
				sum -= list1[i]
				flag = 0
			i += 1
	if sum < check:
		print(check)
	else:
		print(sum)


t = input()
for _ in range(int(t)):
	[n, k] = list(map(int, input().split()))
	ls = list(map(int, input().split()))
	maxima = ls[0]
	minima = 300001
	last_minima = minima
	max_strength = 0
	look_for_maxima = True
	for index in range(1, len(ls)):
		strength = ls[index]
		last_strength = ls[index - 1]
		if look_for_maxima:
			if strength > maxima:
				maxima = strength
			else:
				look_for_maxima = False
				max_strength += maxima
				maxima = -1
				minima = strength
		elif strength < minima:
			minima = strength
		else:
			look_for_maxima = True
			max_strength -= minima
			last_minima = minima
			maxima = strength
			minima = 300001
	if look_for_maxima:
		max_strength += maxima
	print(max_strength)


def algo(n, arr):
	res = 0
	m = 0
	for i in arr:
		t = 0
		if i > m:
			t = i - m
		res += t
		m = i
	return res
for _ in range(int(input())):
	(n, _) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = algo(n, arr)
	print(ans)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from collections import defaultdict

def gift():
	for _ in range(t):
		(n, q) = list(map(int, input().split()))
		arry = list(map(int, input().split()))
		ops = []
		for i in range(q):
			op = list(map(int, input().split()))
			ops.append(op)
		station = [0] * n
		station[-1] = arry[n - 1]
		ans = arry[n - 1]
		for i in range(n - 1):
			if arry[n - 1 - i] < arry[n - 2 - i]:
				tem = arry[n - 2 - i] - arry[n - 1 - i]
				station[n - 2 - i] = tem
				ans += tem
		yield ans
		for i in range(q):
			(s, e) = ops[i]
			(sNum, eNum) = (arry[s - 1], arry[e - 1])
			if s > 1:
				ans -= station[s - 1]
				if eNum < arry[s - 2]:
					tem = arry[s - 2] - eNum
					station[s - 1] = tem
					ans += tem
			ans -= station[s]
			if arry[s] < eNum:
				tem = eNum - arry[s]
				station[s] = tem
				ans += tem
			if e == n:
				ans -= station[n - 1]
				station[n - 1] = sNum
				ans += sNum
			else:
				ans -= station[e - 2]
				if sNum < arry[e - 2]:
					tem = arry[e - 2] - sNum
					station[e - 2] = tem
					ans += tem
			if e - s > 1:
				ans -= station[e - 1]
				if arry[e] < sNum:
					tem = sNum - arry[e]
					station[e - 1] = tem
					ans += sNum
			(arry[s - 1], arry[e - 1]) = (arry[e - 1], arry[s - 1])
			yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


from math import log2
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	inc = True
	i = 1
	res = 0
	curr = arr[0]
	while i < n:
		if inc:
			if i < n and arr[i] > curr:
				curr = arr[i]
			elif arr[i] < curr:
				res += curr
				curr = arr[i]
				inc = False
		elif i < n and arr[i] < curr:
			curr = arr[i]
		elif arr[i] > curr:
			res -= curr
			curr = arr[i]
			inc = True
		i += 1
	if inc:
		res += curr
	print(res)


def mi():
	return map(int, input().split())
for _ in range(int(input())):
	(n, k) = mi()
	a = list(mi())
	(ma, mii) = (a[-1], 0)
	for i in range(n - 1, -1, -1):
		ma = max(ma, a[i] - mii)
		mii = min(mii, a[i] - ma)
	print(max(ma, mii))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	r1 = [0] * n
	r1[0] = a[0]
	r2 = [0] * n
	r2[0] = a[0]
	max2 = 0
	max1 = a[0]
	for i in range(1, n):
		r1[i] = max(max2 + a[i], a[i])
		r2[i] = max1 - a[i]
		max1 = max(max1, r1[i])
		max2 = max(max2, r2[i])
	print(max(max1, max2))


N = int(input())
for _ in range(N):
	(n, q) = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	ans = nums[0]
	for i in range(1, n):
		ans += max(0, nums[i] - nums[i - 1])
	print(ans)


mod = 10 ** 9 + 7

def solve():
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	cnt = 0
	mx = 0
	mn = 0
	for i in range(n):
		if cnt % 2 > 0:
			if mn > a[i]:
				mn = a[i]
			else:
				ans -= mn
				cnt += 1
				mn = 0
				mx = a[i]
		elif a[i] > mx:
			mx = a[i]
		else:
			ans += mx
			mn = a[i]
			mx = 0
			cnt += 1
	ans += mx
	print(ans)
t = int(input())
while t > 0:
	solve()
	t -= 1


def solve(n, nums):
	dodd = float('-inf')
	deven = 0
	for i in range(n):
		new_dodd = max(dodd, deven + nums[i])
		new_deven = max(deven, dodd - nums[i])
		dodd = new_dodd
		deven = new_deven
	return max(dodd, deven)
tt = int(input())
while tt > 0:
	tt -= 1
	(n, q) = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	print(solve(n, nums))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lis = list(map(int, input().split()))
	ans = 0
	lis = [0] + lis
	for i in range(n + 1):
		if lis[i] - lis[i - 1] > 0:
			ans += lis[i] - lis[i - 1]
	print(ans)


def main():
	t = int(input())
	allAns = []
	for _ in range(t):
		(n, q) = readIntArr()
		a = readIntArr()
		swaps = []
		for __ in range(q):
			swaps.append(readIntArr())
		maxEven = [0 for _ in range(n)]
		maxOdd = [0 for _ in range(n)]
		maxOdd[0] = a[0]
		for i in range(1, n):
			maxEven[i] = max(maxEven[i - 1], maxOdd[i - 1] - a[i])
			maxOdd[i] = max(maxOdd[i - 1], maxEven[i - 1] + a[i])
		ans = [maxOdd[n - 1]]
		allAns.append(ans)
	multiLineArrayOfArraysPrint(allAns)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]
main()


T = int(input())
ans_ls = [0] * T
for t in range(T):
	(N, _) = map(int, input().split())
	a_ls = list(map(int, input().split()))
	dp = [[[-float('inf')] * 2 for _ in range(2)] for _ in range(N + 1)]
	dp[0][0][0] = 0
	for i in range(N):
		dp[i + 1][0][1] = a_ls[i] + max(dp[i][1][1], dp[i][0][0])
		dp[i + 1][0][0] = max(dp[i][1][1], dp[i][0][0])
		dp[i + 1][1][1] = -a_ls[i] + max(dp[i][0][1], dp[i][1][0])
		dp[i + 1][1][0] = max(dp[i][0][1], dp[i][1][0])
	ans_ls[t] = max(max(dp[-1][0]), max(dp[-1][1]))
for ans in ans_ls:
	print(ans)


import sys, math
from collections import Counter, deque, defaultdict
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(inp()):
	(n, q) = inpl()
	a = inpl()
	up = True
	res = []
	if n == 1:
		print(a[0])
		continue
	if a[1] - a[0] < 0:
		res.append(a[0])
		up = False
	for i in range(1, n - 1):
		now = True if a[i + 1] - a[i] > 0 else False
		if now != up:
			res.append(a[i])
			up = now
	if up:
		res.append(a[-1])
	ans = 0
	for i in range(len(res)):
		if i % 2:
			ans -= res[i]
		else:
			ans += res[i]
	print(ans)


def calMax(arr):
	d1 = [0 for i in range(len(arr) + 1)]
	d2 = [0 for i in range(len(arr) + 1)]
	(d1[0], d2[0]) = (-float('inf'), 0)
	for i in range(len(arr)):
		d1[i + 1] = max(d1[i], d2[i] + arr[i])
		d2[i + 1] = max(d2[i], d1[i] - arr[i])
	return max(d1[-1], d2[-1])
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(calMax(arr))
	for i in range(q):
		st = 0
		dp = [-float('inf') for i in range(n)]
		(l, r) = map(int, input().split())
		(l, r) = (l - 1, r - 1)
		(arr[l], arr[r]) = (arr[r], arr[l])
		print(calMax(arr))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	(i, pos, neg) = (0, [], [])
	while i < n:
		j = i
		while j + 1 < n and l[j + 1] >= l[j]:
			j += 1
		if j != n:
			pos.append(l[j])
			i = j + 1
		else:
			break
		j = i
		while j + 1 < n and l[j + 1] <= l[j]:
			j += 1
		if j != n:
			neg.append(l[j])
			i = j + 1
		else:
			break
	if len(neg) == len(pos):
		print(sum(pos) - (sum(neg) - neg[-1]))
	else:
		print(sum(pos) - sum(neg))


rn = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rns = lambda : map(int, input().split())
rs = lambda : input()
yn = lambda x: print('Yes') if x else print('No')
for _ in range(rn()):
	(n, q) = rns()
	l = rl()
	ans = max(l)
	tans = 0
	le = float('inf')
	r = -1
	for i in range(n):
		if i == 0:
			if n > 1 and l[i] > l[i + 1]:
				tans += l[i]
				le = min(le, i)
				r = max(r, i)
		elif i == n - 1:
			if n > 1 and l[i] > l[i - 1]:
				tans += l[i]
				le = min(le, i)
				r = max(r, i)
		elif l[i] > l[i - 1] and l[i] > l[i + 1]:
			tans += l[i]
			le = min(le, i)
			r = max(r, i)
	for i in range(n):
		if i > le and i < r and (l[i] < l[i - 1]) and (l[i] < l[i + 1]):
			tans -= l[i]
	print(max(ans, tans))


for t in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = a[0]
	for i in range(1, n):
		ans += max(0, a[i] - a[i - 1])
	print(ans)


from sys import stdin
from math import log2

def readline():
	return stdin.readline()

def solve(n, a):
	(maxe, maxo) = (0, 0)
	for i in a:
		(maxe, maxo) = (max(maxe, maxo + i), max(maxo, maxe - i))
	return max(maxe, maxo)
tests = int(readline())
for t in range(0, tests):
	(n, _) = map(int, readline().split())
	a = list(map(int, readline().rstrip('\n').split(' ')))
	print(solve(n, a))


import io
import os
from collections import Counter, defaultdict, deque

class FenwickTree:

	def __init__(self, x):
		self.bit = x
		for i in range(len(x)):
			j = i | i + 1
			if j < len(x):
				x[j] += x[i]

	def update(self, idx, x):
		while idx < len(self.bit):
			self.bit[idx] += x
			idx |= idx + 1

	def query(self, end):
		x = 0
		while end:
			x += self.bit[end - 1]
			end &= end - 1
		return x

	def findkth(self, k):
		idx = -1
		for d in reversed(range(len(self.bit).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(self.bit) and k >= self.bit[right_idx]:
				idx = right_idx
				k -= self.bit[idx]
		return idx + 1

def solve(N, Q, A, LR):
	deltas = [max(A[i] - A[i + 1], 0) for i in range(N - 1)]
	segTree = FenwickTree(list(deltas))
	ans = []
	ans.append(str(segTree.query(N - 1) + A[-1]).encode())
	for (l, r) in LR:
		l -= 1
		r -= 1
		(A[l], A[r]) = (A[r], A[l])
		for i in [l - 1, l, r - 1, r]:
			if 0 <= i < N - 1:
				delta = max(A[i] - A[i + 1], 0)
				oldDelta = deltas[i]
				if delta != oldDelta:
					deltas[i] = delta
					segTree.update(i, delta - oldDelta)
		ans.append(str(segTree.query(N - 1) + A[-1]).encode())
	assert len(ans) == Q + 1
	return b'\n'.join(ans) + b'\n'
DEBUG = False
if DEBUG:
	import random
	random.seed(0)
	for _ in range(1):
		N = random.randint(1, 3 * 10 ** 5)
		Q = random.randint(1, 3 * 10 ** 5)
		A = [random.randint(1, N) for i in range(N)]
		LR = [[random.randint(1, N), random.randint(1, N)] for i in range(Q)]
		print('tc', _, N, Q)
		ans1 = solve(N, Q, A, LR)
	exit()
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N, Q) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	LR = [[int(x) for x in input().split()] for i in range(Q)]
	ans = solve(N, Q, A, LR)
	os.write(1, ans)


import math
from datetime import date

def main():
	t = int(input())
	while t > 0:
		t -= 1
		line = input().split()
		(n, k) = (int(line[0]), int(line[1]))
		a = [int(x) for x in input().split()]
		dp = [[0 for j in range(2)] for i in range(n + 2)]
		for i in range(n - 1, -1, -1):
			for j in range(2):
				dp[i][j] = dp[i + 1][j]
				add = a[i]
				if j == 1:
					add = -add
				dp[i][j] = max(dp[i][j], dp[i + 1][1 - j] + add)
		print(dp[0][0])
main()


t = int(input())
arr = []
for _ in range(t):
	(n, q) = tuple(map(int, input().split()))
	arr.append((n, q, tuple(map(int, input().split()))))
for (n, q, tup) in arr:
	total = 0
	stack = []
	peak = False
	for i in range(n):
		if len(stack) == 0:
			stack.append(tup[i])
		elif stack[-1] < tup[i] and peak == True:
			total -= stack[-1]
			stack[-1] = tup[i]
			peak = False
		elif stack[-1] > tup[i] and peak == False:
			total += stack[-1]
			stack[-1] = tup[i]
			peak = True
		else:
			stack[-1] = tup[i]
	if peak == False:
		total += tup[-1]
	print(total)


import sys
t = int(input())
while t > 0:
	t -= 1
	input = sys.stdin.readline()
	(n, q) = map(int, input.split())
	input = sys.stdin.readline()
	arr = list(map(int, input.split()))
	ans = 0
	(maxi, mini) = (0, 0)
	if n == 1:
		print(arr[0])
	else:
		for i in range(1, n - 1):
			if arr[i - 1] < arr[i] > arr[i + 1]:
				maxi += arr[i]
			elif arr[i - 1] > arr[i] < arr[i + 1]:
				mini += arr[i]
		if arr[0] > arr[1]:
			maxi += arr[0]
		if arr[-1] > arr[-2]:
			maxi += arr[-1]
		print(maxi - mini)


import sys
input = lambda : sys.stdin.readline().rstrip()
ANS = []
T = int(input())
for _ in range(T):
	(N, Q) = map(int, input().split())
	A = [int(a) for a in input().split()]
	ans = sum((max(b - a, 0) for (a, b) in zip([0] + A, A)))
	ANS.append(ans)
	for _ in range(Q):
		(l, r) = map(int, input().split())
		(l, r) = (l - 1, r - 1)
		if l == r:
			ANS.append(ans)
			continue
		ans -= max(A[l] - A[l - 1], 0) if l else A[l]
		if l < N - 1:
			ans -= max(A[l + 1] - A[l], 0)
		if r > l + 1:
			ans -= max(A[r] - A[r - 1], 0)
		if r < N - 1:
			ans -= max(A[r + 1] - A[r], 0)
		(A[l], A[r]) = (A[r], A[l])
		ans += max(A[l] - A[l - 1], 0) if l else A[l]
		if l < N - 1:
			ans += max(A[l + 1] - A[l], 0)
		if r > l + 1:
			ans += max(A[r] - A[r - 1], 0)
		if r < N - 1:
			ans += max(A[r + 1] - A[r], 0)
		ANS.append(ans)
print('\n'.join(map(str, ANS)))


def solve(a, n):
	maxi = a[-1]
	mini = 0
	for i in range(n - 2, -1, -1):
		maxi = max(maxi, a[i] - mini)
		mini = min(mini, a[i] - maxi)
	return max(maxi, mini)
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	print(solve(a, n))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	a = l[0]
	b = 0
	d = 0
	ans = 0
	for i in range(1, n):
		if b == 0 and a < l[i]:
			a = l[i]
		elif a - l[i] > d:
			d = a - l[i]
			b = l[i]
		elif l[i] > b:
			ans += a - b
			a = l[i]
			b = 0
			d = 0
	print(max(n, ans + a))


def solve(n, q, a):
	if n == 1:
		return a[0]
	if n == 2:
		return max(a)
	hill = True
	ans = 0
	for i in range(n):
		if i == n - 1:
			if hill:
				ans += a[i]
			break
		if hill:
			if a[i + 1] < a[i]:
				ans += a[i]
				hill = False
		elif a[i + 1] > a[i]:
			ans -= a[i]
			hill = True
	return ans
t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	print(solve(n, q, a))


import sys
inp = sys.stdin.readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())

def wrl(*args):
	for _arg in args:
		sys.stdout.write(str(_arg) + ' ')
	sys.stdout.write('\n')
from math import log2
_T_ = inin()
for _t_ in range(_T_):
	(n, q) = inar()
	a = [0] + inar() + [0]
	diff = []
	for i in range(n + 1):
		diff.append(max(0, a[i + 1] - a[i]))
	wrl(sum(diff))
	ans = sum(diff)
	for qq in range(q):
		(l, r) = inar()
		al = a[l]
		ar = a[r]
		a[l] = ar
		ans -= diff[l - 1]
		ans -= diff[l]
		diff[l - 1] = max(0, a[l] - a[l - 1])
		diff[l] = max(0, a[l + 1] - a[l])
		ans += diff[l - 1]
		ans += diff[l]
		a[r] = al
		ans -= diff[r - 1]
		ans -= diff[r]
		diff[r - 1] = max(0, a[r] - a[r - 1])
		diff[r] = max(0, a[r + 1] - a[r])
		ans += diff[r - 1]
		ans += diff[r]
		wrl(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	i = 0
	while True:
		while i < n - 1 and arr[i] <= arr[i + 1]:
			i += 1
		ans += arr[i]
		if i >= n - 1:
			break
		while i < n - 1 and arr[i] >= arr[i + 1]:
			i += 1
		if i >= n - 1:
			break
		ans -= arr[i]
	print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [0] * n
	dp[0] = arr[0]
	for i in range(1, n):
		dp[i] = max(dp[i], dp[i] + arr[i] - arr[i - 1])
	print(sum(dp))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
	else:
		cv = a[0]
		s = 0
		p = 0
		for i in range(1, n):
			if p == 0:
				if a[i] >= cv:
					cv = a[i]
				else:
					s += cv
					p = 1
					cv = a[i]
			elif a[i] <= cv:
				cv = a[i]
			else:
				s -= cv
				p = 0
				cv = a[i]
		if p == 0 and cv > 0:
			s += cv
		print(max(s, max(a)))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
	else:
		dp = [[-1 for i in range(2)] for j in range(n)]
		dp[0][0] = a[0]
		dp[0][1] = 0
		dp[1][0] = max(a[1], a[0])
		dp[1][1] = a[0] - a[1]
		maxi = -1
		for i in range(2, n):
			dp[i][0] = max(dp[i - 1][1] + a[i], a[i])
			dp[i][0] = max(dp[i][0], dp[i - 1][0])
			dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - a[i])
		for i in range(len(dp)):
			maxi = max(maxi, dp[i][0])
			maxi = max(maxi, dp[i][1])
		print(maxi)


t = int(input())
for i in range(t):
	l = [int(i) for i in input().split()]
	n = l[0]
	l1 = [int(i) for i in input().split()]
	a = float('inf')
	a = -a
	dp1 = [0] * (n + 1)
	dp2 = [0] * (n + 1)
	dp1[0] = a
	dp2[0] = 0
	for i in range(n):
		dp1[i + 1] = max(dp1[i], dp2[i] + l1[i])
		dp2[i + 1] = max(dp2[i], dp1[i] - l1[i])
	print(max(dp1[n], dp2[n]))


import math
t = int(input())
for _ in range(t):
	(n, q) = [int(i) for i in input().split()]
	a = [-1] + [int(i) for i in input().split()] + [-1]
	t = 0
	luu = 0
	bit = 0
	for i in range(1, n + 1):
		if bit == 0:
			if a[i] > a[i - 1] and a[i] > a[i + 1]:
				t += a[i]
				bit = 1
		elif a[i] < a[i - 1] and a[i] < a[i + 1]:
			t -= a[i]
			luu = a[i]
			bit = 0
	if bit == 0:
		t += luu
	if t == 0:
		t = max(a)
	print(t)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n, q) = I()
	l = I()
	an = 0
	p = 1
	for i in range(n - 1):
		if p and l[i] > l[i + 1]:
			an += l[i]
			p = 0
		elif p == 0 and l[i] < l[i + 1]:
			an -= l[i]
			p = 1
	if p:
		an += l[-1]
	print(an)


for i in range(int(input())):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	tot = 0
	small = 400000
	big = 0
	goingUp = True
	for (i, val) in enumerate(a):
		if goingUp:
			if val > big:
				big = val
			else:
				tot += big
				goingUp = False
				small = val
		elif val < small:
			small = val
		else:
			tot -= small
			goingUp = True
			big = val
	if goingUp:
		tot += big
	print(tot)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = 0
	f = 0
	ind = 0
	if n == 1:
		print(l[0])
	else:
		while ind < n - 1:
			if f == 0:
				while ind < n - 1 and l[ind + 1] >= l[ind]:
					ind += 1
				ans += l[ind]
				f = 1
				if ind == n - 1:
					break
			else:
				while ind < n - 1 and l[ind + 1] <= l[ind]:
					ind += 1
				f = 0
				if ind == n - 1:
					break
				else:
					ans -= l[ind]
		print(ans)


def main():
	(n, q) = list(map(int, input().split()))
	lst = list(map(int, input().split()))
	operation = 0
	ans = 0
	minn = 0
	maxx = 0
	for i in range(n):
		if operation == 0:
			if i == 0:
				maxx = lst[0]
			elif lst[i] >= maxx:
				maxx = lst[i]
			else:
				operation = 1
				ans += maxx
				minn = lst[i]
		elif lst[i] <= minn:
			minn = lst[i]
		else:
			operation = 0
			ans -= minn
			maxx = lst[i]
	if operation == 0:
		ans += maxx
	print(ans)
t = int(input())
while t != 0:
	main()
	t -= 1


def getint():
	return int(input())

def getints():
	return map(int, input().split())

def getlistints():
	return list(map(int, input().split()))

def solution(n, nums):
	dp = [[0] * 2 for _ in range(n)]
	dp[0][0] = nums[0]
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + nums[i])
		dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - nums[i])
	return max(dp[-1])
for _ in range(getint()):
	(n, q) = getints()
	nums = getlistints()
	print(solution(n, nums))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	su = 0
	flip = 0
	for i in range(0, n - 1):
		if flip == 0:
			if a[i + 1] < a[i]:
				su += a[i]
				flip = 1
		elif a[i + 1] > a[i]:
			su -= a[i]
			flip = 0
	if flip == 0:
		su += a[n - 1]
	print(su)


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	s = 0
	maxx = True
	plus = True
	for i in range(0, n - 1):
		if a[i] < a[i + 1] and maxx:
			continue
		if a[i] < a[i + 1] and maxx == False:
			if plus == True:
				s += a[i]
				plus = False
			else:
				s -= a[i]
				plus = True
			maxx = True
		if a[i] > a[i + 1] and maxx == False:
			continue
		if a[i] > a[i + 1] and maxx:
			if plus == True:
				s += a[i]
				plus = False
			else:
				s -= a[i]
				plus = True
			maxx = False
	if maxx:
		if plus:
			s += a[-1]
		else:
			s -= a[-1]
	print(s)


def f(l):
	if len(l) == 1:
		return l[0]
	r = []
	for i in range(len(l)):
		if i == 0:
			if l[i + 1] < l[i]:
				r.append(l[i])
		elif i == len(l) - 1:
			if l[i] > l[i - 1]:
				r.append(l[i])
		else:
			if l[i] > l[i - 1] and l[i] > l[i + 1]:
				r.append(l[i])
			if l[i] < l[i - 1] and l[i] < l[i + 1]:
				r.append(l[i])
	sum = 0
	for i in range(len(r)):
		if i % 2 == 0:
			sum += r[i]
		else:
			sum -= r[i]
	return sum
t = int(input())
for i in range(t):
	(n, q) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	print(f(l))


import sys
t = int(input())
for tc in range(t):
	(n, q) = map(int, input().split())
	sum = 0
	lst = list(map(int, input().split()))
	increasing = True
	for i in range(n - 1):
		if increasing is True and lst[i] > lst[i + 1]:
			increasing = False
			sum += lst[i]
		elif increasing is False and lst[i] < lst[i + 1]:
			increasing = True
			sum -= lst[i]
	if increasing is True:
		sum += lst[n - 1]
	print(sum)


import sys
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	(n, q) = map(int, input().split())
	a = [int(i) for i in input().split() if i != '\n']
	if len(a) <= 2:
		print(max(a))
	else:
		(ans, count) = ([0], 0)
		for i in range(1, n - 1):
			if i == 1:
				if a[0] > a[1]:
					ans.append(a[0])
					count += 1
			if count & 1:
				if a[i] < a[i - 1] and a[i] < a[i + 1]:
					ans.append(-a[i])
					count += 1
			elif a[i] > a[i - 1] and a[i] > a[i + 1]:
				ans.append(a[i])
				count += 1
		if len(a) >= 2:
			if a[-1] > a[-2] and ans[-1] <= 0:
				ans.append(a[-1])
		if ans[-1] < 0:
			ans.pop()
		out = sum(ans)
		print(out)


import sys

def solution():
	get_input = sys.stdin.readline
	t = int(get_input().strip())
	for i in range(t):
		(n, q) = list(map(int, get_input().strip().split()))
		board = list(map(int, get_input().strip().split()))
		total = 0
		prev = 0
		find_max = True
		if n == 1:
			print(board[0])
			continue
		for k in range(n):
			if find_max:
				if k == 0:
					if board[k] >= board[k + 1]:
						total += board[k]
						find_max = False
				elif k == n - 1:
					if board[k - 1] <= board[k]:
						total += board[k]
				elif board[k - 1] <= board[k] and board[k] >= board[k + 1]:
					total += board[k]
					find_max = False
			elif k == n - 1:
				if board[k - 1] <= board[k]:
					total -= board[k]
			elif board[k - 1] >= board[k] and board[k] <= board[k + 1]:
				total -= board[k]
				find_max = True
		print(total)
solution()


R = lambda : list(map(int, input().split()))
t = int(input())
for i in range(0, t):
	(n, q) = R()
	l = list(map(int, input().split()))
	sum = l[0]
	for j in range(1, n):
		sum = sum + max(0, l[j] - l[j - 1])
	print(sum)


def solve(n):
	s = list(map(int, input().split()))
	if n == 1:
		print(s[0])
		return
	flag = 1
	ans = 0
	for i in range(n - 1):
		if flag == 1 and s[i] > s[i + 1]:
			ans += s[i]
			flag = -1
		if flag == -1 and s[i] < s[i + 1]:
			ans -= s[i]
			flag = 1
	if s[-1] > s[-2] and flag == 1:
		ans += s[-1]
	print(ans)
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	solve(n)


for t in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = []
	flag = 0
	arr2 = arr.copy()
	arr2.sort()
	arr1 = arr.copy()
	arr1.sort()
	arr1.reverse()
	if arr1 == arr:
		print(arr[0])
	elif arr2 == arr:
		print(arr[n - 1])
	else:
		for i in range(1, n):
			if flag == 0:
				if arr[i] > arr[i - 1]:
					continue
				else:
					ans.append(arr[i - 1])
					flag = 1
			elif arr[i] < arr[i - 1]:
				continue
			else:
				ans.append(arr[i - 1])
				flag = 0
		if ans[len(ans) - 1] != arr[n - 1]:
			if len(ans) > 0:
				if len(ans) % 2 == 0:
					ans.append(arr[n - 1])
		if len(ans) == 1:
			print(*ans)
		else:
			s = 0
			for i in range(0, len(ans) - 1, 2):
				s += ans[i] - ans[i + 1]
			if len(ans) % 2 != 0:
				s += ans[len(ans) - 1]
			print(s)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * (2 * 10 ** 5 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj
mod = 10 ** 9 + 7

def solve():
	(n, q) = IP()
	a = L()
	dp = [[0, 0] for i in range(n + 1)]
	for i in range(n):
		dp[i + 1][0] = max(dp[i][1] + a[i], dp[i][0])
		dp[i + 1][1] = max(dp[i][0] - a[i], dp[i][1])
	print(max(dp[n][0], dp[n][1]))
	return
t = II()
for i in range(t):
	solve()


import sys
sys.setrecursionlimit(10000)
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	findHigh = True
	ans = 0
	for i in range(n - 1):
		if findHigh:
			if a[i] > a[i + 1]:
				ans += a[i]
				findHigh = False
		elif a[i] < a[i + 1]:
			ans -= a[i]
			findHigh = True
	if findHigh:
		ans += a[-1]
	print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(*a)
		continue
	r = 0
	for i in range(n):
		if i == 0:
			if a[i + 1] < a[i]:
				r += a[i]
		elif i == n - 1:
			if a[i] > a[i - 1]:
				r += a[i]
		elif a[i] > a[i - 1] and a[i] > a[i + 1]:
			r += a[i]
		elif a[i] < a[i - 1] and a[i] < a[i + 1]:
			r -= a[i]
	print(r)


from sys import stdin
input = stdin.readline

def solve():
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	res = a[0]
	for i in range(1, n):
		res += max(0, a[i] - a[i - 1])
	print(res)
t = int(input())
for i in range(t):
	solve()


from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd
import math

def geti():
	return map(int, input().strip().split())

def getl():
	return list(map(int, input().strip().split()))

def gets():
	return input()

def geta():
	return int(input())

def print_s(s):
	stdout.write(s + '\n')

def solve():
	for _ in range(geta()):
		(n, q) = geti()
		a = getl()
		ans = 0
		if n < 2 or a[0] > a[1]:
			ans = a[0]
			index = 0
		else:
			for i in range(1, n):
				if a[i] >= a[i - 1]:
					index = i
					ans = a[i]
				else:
					break
		ok = False
		for i in range(index + 1, n):
			if i + 1 < n and a[i] < a[i + 1] and (not ok):
				ans -= a[i]
				ok = True
			elif i + 1 < n and a[i] > a[i + 1] and ok:
				ans += a[i]
				ok = False
			elif i + 1 == n and a[i] >= a[i - 1] and ok:
				ok = False
				ans += a[i]
		print(ans)
solve()


def solve():
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	if n == 1:
		return arr[0]
	if n == 2:
		return max(arr[0], arr[1])
	a = [arr[0], max(arr[0], arr[1])]
	b = [0, arr[0] - arr[1]]
	for i in range(2, n):
		a.append(max(max(b[-1], 0) + arr[i], a[-1]))
		b.append(max(a[-1] - arr[i], b[-1]))
	return a[-1]
t = int(input())
for i in range(t):
	print(solve())


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	m = arr[0]
	i = 1
	while i < n and m < arr[i]:
		m = arr[i]
		i += 1
	i -= 1
	j = 1
	dif = []
	while j < n:
		dif.append(arr[j] - arr[j - 1])
		j += 1
	out = 0
	out += m
	for j in range(i, n - 1):
		if dif[j] >= 0:
			out += dif[j]
	print(out)


gans = []
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	u = list(map(int, input().split()))
	w = [u[0]]
	for i in range(1, n - 1):
		if u[i - 1] < u[i] > u[i + 1] or u[i - 1] > u[i] < u[i + 1]:
			w.append(u[i])
	w.append(u[-1])
	if w[0] < w[1]:
		w = w[1:]
	if len(w) % 2 == 0:
		w.pop()
	ans = 0
	for i in range(len(w)):
		if i % 2 == 0:
			ans += w[i]
		else:
			ans -= w[i]
	gans.append(ans)
print('\n'.join(map(str, gans)))


for testcase in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	count = 0
	arr.append(0)
	for i in range(len(arr) - 1):
		if arr[i] - arr[i - 1] > 0:
			count += arr[i] - arr[i - 1]
	print(count)


from collections import Counter, defaultdict as dft

def mp():
	return map(int, input().split())

def ml():
	return list(map(int, input().split()))
mxn = 10 ** 9
t = int(input())
for _ in range(t):
	(n, q) = mp()
	arr = ml()
	ls = [[-1 for i in range(2)] for j in range(n + 5)]
	for i in range(n, -1, -1):
		for turn in range(2):
			if i == n:
				ls[i][turn] = 0
			else:
				res = -mxn
				if turn:
					res = max(res, ls[i + 1][1 - turn] + arr[i], ls[i + 1][turn])
				else:
					res = max(res, ls[i + 1][1 - turn] - arr[i], ls[i + 1][turn])
				ls[i][turn] = res
	print(ls[0][1])


import sys, os, io
input = sys.stdin.readline
T = int(input())
ans = [0] * T
for t in range(T):
	(n, q) = map(int, input().split())
	A = list(map(int, input().split()))
	dp1 = [0] * n
	dp2 = [0] * n
	dp1[0] = A[0]
	for i in range(1, n):
		dp1[i] = max(dp2[i - 1] + A[i], dp1[i - 1])
		dp2[i] = max(dp1[i - 1] - A[i], dp2[i - 1])
	ans[t] = max(dp1[-1], dp2[-1])
print(*ans, sep='\n')


for ttt in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	v1 = [0] * (n + 1)
	v2 = [0] * (n + 1)
	v1[0] = float('-inf')
	for i in range(n):
		v1[i + 1] = max(v1[i], v2[i] + arr[i])
		v2[i + 1] = max(v2[i], v1[i] - arr[i])
	print(max(v1[-1], v2[-1]))


import itertools
import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '3\n3 0\n1 3 2\n2 0\n1 2\n7 0\n1 2 5 4 3 6 7\n\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield input()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]

def solve(a):
	look_positive = True
	res = 0
	for i in range(len(a)):
		prev = -1 if i == 0 else a[i - 1]
		_next = -1 if i == len(a) - 1 else a[i + 1]
		if a[i] > prev and a[i] > _next:
			res += a[i]
		if a[i] < prev and a[i] < _next:
			res -= a[i]
	return res
t = getType(int)
for _ in range(t):
	(n, q) = getArray(int)
	a = getArray(int)
	res = solve(a)
	print(res)


import sys
z = sys.stdin.readline
o = []
y = lambda a, b: a - b if a - b > 0 else 0
for _ in range(int(z())):
	(n, q) = map(int, z().split())
	p = [0] + [*map(int, z().split())]
	s = m = 0
	for i in p:
		if i > m:
			s += i - m
		m = i
	o.append(s)
	for i in range(q):
		(a, b) = map(int, z().split())
		if a == b:
			o.append(s)
			continue
		(va, vb) = (p[a], p[b])
		va1 = p[a - 1]
		c = b < n
		if c:
			vb1 = p[b + 1]
		(p[a], p[b]) = (vb, va)
		lv = nv = 0
		if b - a < 2:
			if vb > va:
				lv += vb - va
			if va > va1:
				lv += va - va1
			if c and vb1 > vb:
				lv += vb1 - vb
			if va > vb:
				nv += va - vb
			if vb > va1:
				nv += vb - va1
			if c and vb1 > va:
				nv += vb1 - va
		else:
			(va2, vb2) = (p[a + 1], p[b - 1])
			if va2 > va:
				lv += va2 - va
			if va > va1:
				lv += va - va1
			if vb > vb2:
				lv += vb - vb2
			if c and vb1 > vb:
				lv += vb1 - vb
			if va2 > vb:
				nv += va2 - vb
			if vb > va1:
				nv += vb - va1
			if va > vb2:
				nv += va - vb2
			if c and vb1 > va:
				nv += vb1 - va
		s += nv - lv
		o.append(s)
print('\n'.join(map(str, o)))


t = int(input())
while t > 0:
	(n, q) = map(int, input().split())
	arr = [int(i) for i in input().split()]
	ans1 = [0] * n
	ans1[0] = arr[0]
	ans2 = [0] * n
	ans2[0] = arr[0]
	max1 = arr[0]
	max2 = 0
	for i in range(1, n):
		ans1[i] = max(max2 + arr[i], arr[i])
		ans2[i] = max1 - arr[i]
		max1 = max(max1, ans1[i])
		max2 = max(max2, ans2[i])
	print(max(max1, max2))
	t -= 1


t = int(input())
while t:
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	x = ans = 0
	while x < n:
		while x < n - 1 and a[x + 1] >= a[x]:
			x += 1
		ans += a[x]
		x += 1
		while x < n - 1 and a[x + 1] < a[x]:
			x += 1
		if x < n:
			ans -= a[x]
	print(ans)
	t -= 1


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	s = list(map(int, input().split()))
	flip = 0
	cur = s[0]
	total = 0
	for i in range(1, n):
		if flip == 0:
			if s[i] > s[i - 1]:
				cur = s[i]
			else:
				total += s[i - 1]
				flip = 1
				cur = s[i]
		elif s[i] < s[i - 1]:
			cur = s[i]
		else:
			total -= s[i - 1]
			cur = s[i]
			flip = 0
	if flip == 0:
		total += s[n - 1]
	print(total)


import sys, os
import math
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def MI():
	return map(int, input().split())

def out(var):
	sys.stdout.write(str(var) + '\n')
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split())) + [0]
	out = 0
	for i in range(n):
		out += max(arr[i] - arr[i - 1], 0)
	print(out)
	for i in range(q):
		(a, b) = map(int, input().split())
		a -= 1
		b -= 1
		out -= max(arr[b + 1] - arr[b], 0)
		out -= max(arr[b] - arr[b - 1], 0)
		if b != a + 1:
			out -= max(arr[a + 1] - arr[a], 0)
		out -= max(arr[a] - arr[a - 1], 0)
		(arr[a], arr[b]) = (arr[b], arr[a])
		out += max(arr[b + 1] - arr[b], 0)
		out += max(arr[b] - arr[b - 1], 0)
		if b != a + 1:
			out += max(arr[a + 1] - arr[a], 0)
		out += max(arr[a] - arr[a - 1], 0)
		print(out)


t = int(input())
while t != 0:
	(n, q) = map(int, input().split())
	list1 = list(map(int, input().split()))
	s = 0
	i = 0
	maxiii = -(1000000000.0 + 7)
	while i < len(list1):
		p = list1[i]
		while i < len(list1) and list1[i] >= p:
			p = list1[i]
			i += 1
		if i == len(list1):
			s += list1[-1]
			maxiii = max(maxiii, s)
			break
		maxi = list1[i - 1]
		p = list1[i]
		maxiii = max(maxiii, s + maxi)
		while i < len(list1) and list1[i] <= p:
			p = list1[i]
			i += 1
		mini = list1[i - 1]
		s += maxi - mini
		if s < 0:
			s = 0
	print(maxiii)
	t -= 1


t = int(input())
for x in range(t):
	(n, q) = map(int, input().split())
	lst = list(map(int, input().split()))
	max_plus = lst[0]
	max_minus = None
	for i in range(1, len(lst)):
		if max_minus == None:
			max_plus = max(max_plus, lst[i])
			max_minus = lst[0] - lst[i]
		max_plus = max(max_plus, max_minus + lst[i])
		max_minus = max(max_minus, max_plus - lst[i])
	if max_minus:
		print(max(max_plus, max_minus))
	else:
		print(max_plus)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	dpt = [0 for j in range(n + 1)]
	dpf = [0 for j in range(n + 1)]
	dpt[0] = -float('inf')
	for pos in range(n):
		dpt[pos + 1] = max(dpt[pos], dpf[pos] + a[pos])
		dpf[pos + 1] = max(dpf[pos], dpt[pos] - a[pos])
	print(int(max(dpt[-1], dpf[-1])))


MOD = 1000000007
from collections import defaultdict as dd, Counter, deque
import math

def si():
	return input()

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def mi():
	return map(int, input().split())

def out(v):
	print(v)

def spout():
	print(v, end=' ')

def d2b(n):
	return bin(n).replace('0b', '')

def twod(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def vow():
	return ['a', 'e', 'i', 'o', 'u']

def let():
	return [chr(i) for i in range(97, 123)]

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ispow2(x):
	return x and (not x & x - 1)

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)
t = ii()
while t:
	t -= 1
	(n, q) = mi()
	a = li()
	l = s = i = f = 0
	if n == 1:
		print(a[0])
		continue
	while i < n:
		if l % 2 == 0:
			while a[i + 1] > a[i] and i < n:
				i += 1
				if i == n - 1:
					s += a[n - 1]
					f = 1
					break
			if f == 1:
				break
			s += a[i]
			l += 1
		else:
			while a[i + 1] < a[i] and i < n:
				i += 1
				if i == n - 1:
					f = 1
					break
			if f == 1:
				break
			s -= a[i]
			l += 1
	print(s)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	(n, q) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	DP0 = [0] * n
	DP1 = [0] * n
	for i in range(n):
		DP0[i] = max(DP0[i - 1], DP1[i - 1] + A[i])
		DP1[i] = max(DP1[i - 1], DP0[i - 1] - A[i])
	print(DP0[-1])


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = [int(j) for j in input().split()]
	if n == 1:
		print(arr[0])
		continue
	dp = [[0, 0] for i in range(n)]
	dp[0][0] = arr[0]
	dp[0][1] = 0
	dp[1][0] = arr[1]
	dp[1][1] = arr[0] - arr[1]
	maxplus = max(arr[0], arr[1])
	maxminus = arr[0] - arr[1]
	for i in range(2, n):
		dp[i][0] = max(maxminus + arr[i], arr[i])
		dp[i][1] = maxplus - arr[i]
		maxplus = max(maxplus, dp[i][0])
		maxminus = max(maxminus, dp[i][1])
	ans = -1
	for x in dp:
		ans = max(ans, x[0], x[1])
	print(ans)


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	s = []
	ans = 0
	for i in range(n):
		if len(s) == 0:
			s.append(a[i])
		else:
			if s[-1] > a[i]:
				ans += s[-1] - a[i]
			s.pop()
			s.append(a[i])
	if len(s) != 0:
		ans += s[-1]
	print(ans)


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	total = 0
	last = 0
	for i in range(n):
		total += max(arr[i] - last, 0)
		last = arr[i]
	arr = [0] + arr + [arr[-1]]
	print(total)
	ans = []
	for _ in range(q):
		(x, y) = list(map(int, input().split()))
		(x_new, y_new) = (arr[y], arr[x])
		(j1, j2) = (max(arr[x] - arr[x - 1], 0), max(arr[x + 1] - arr[x], 0))
		arr[x] = x_new
		arr[-1] = arr[-2]
		(n1, n2) = (max(arr[x] - arr[x - 1], 0), max(arr[x + 1] - arr[x], 0))
		total -= j1 + j2
		total += n1 + n2
		(j1, j2) = (max(arr[y] - arr[y - 1], 0), max(arr[y + 1] - arr[y], 0))
		arr[y] = y_new
		arr[-1] = arr[-2]
		(n1, n2) = (max(arr[y] - arr[y - 1], 0), max(arr[y + 1] - arr[y], 0))
		total -= j1 + j2
		total += n1 + n2
		ans.append(str(total))
	print(*ans, sep='\n')


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def solve():
	(n, q) = sep()
	ar = lis()
	dp = [[0] * 2 for _ in range(n + 5)]
	dp[0][1] = ar[0]
	dp[0][0] = 0
	for i in range(1, n):
		dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + ar[i])
		dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - ar[i])
	print(max(dp[n - 1][0], dp[n - 1][1]))
testcase(int(inp()))


from sys import stdin, stdout
input = stdin.readline
c = 0
a = []
b = []

def f(i):
	if a[i] == 0:
		return 0
	if a[i - 1] < a[i] > a[i + 1]:
		return 1
	if a[i - 1] > a[i] < a[i + 1]:
		return -1
	return 0

def relax(i):
	global c
	c += (f(i) - b[i]) * a[i]
	b[i] = f(i)

def relax1(i):
	global c
	c -= b[i] * a[i]
	b[i] = 0
for _ in range(int(input())):
	c = 0
	(n, q) = map(int, input().split())
	(*a,) = map(int, input().split())
	a = [0] + a + [0]
	b = [0] * (n + 2)
	for i in range(1, n + 1):
		relax(i)
	print(c)
	for i in range(q):
		(l, r) = map(int, input().split())
		relax1(l)
		relax1(l - 1)
		relax1(l + 1)
		relax1(r)
		relax1(r - 1)
		relax1(r + 1)
		(a[l], a[r]) = (a[r], a[l])
		relax(l)
		relax(l - 1)
		relax(l + 1)
		relax(r)
		relax(r - 1)
		relax(r + 1)
		print(c)


import sys
t = int(input())
for i in range(t):
	(n, q) = map(int, input().split(' '))
	A = list(map(int, input().strip().split(' ')))
	even = [None for x in A]
	odd = [None for x in A]
	even[0] = int(-1000000000.0)
	odd[0] = A[0]
	for i in range(1, n):
		even[i] = max(even[i - 1], odd[i - 1] - A[i])
		odd[i] = max(A[i], odd[i - 1], even[i - 1] + A[i])
	print(max(odd[n - 1], even[n - 1]))


for _ in range(int(input())):
	(N, q) = map(int, input().split())
	a = list(map(int, input().split()))
	x = a[0]
	count = 0
	for i in range(1, len(a)):
		if a[i] > x:
			x = a[i]
		elif a[i] < x:
			count += x - a[i]
			x = a[i]
	count += x
	print(count)


a = int(input())
for i in range(a):
	(b, c) = [int(x) for x in input().split()]
	d = [int(x) for x in input().split()]
	count = 10 ** 6
	count1 = 10 ** 6
	for j in range(1, b):
		if d[j] < d[j - 1]:
			count = d[j - 1]
			count1 = j - 1
			break
	if count == 10 ** 6:
		count = d[b - 1]
		count1 = b - 1
	count3 = 0
	count4 = 0
	count5 = 0
	for j in range(count1 + 2, b):
		if count4 == 0:
			if d[j] > d[j - 1]:
				count3 = 9
				count = count - d[j - 1]
				count4 = 7
		if count3 == 9:
			if d[j] < d[j - 1]:
				count3 = 0
				count5 = 8
				count = count + d[j - 1]
		if count5 == 8:
			if d[j] > d[j - 1]:
				count3 = 9
				count5 = 0
				count = count - d[j - 1]
	if b - count1 == 0 or b - count1 == 1:
		count = count
	if b - count1 >= 2:
		if count3 == 9:
			count = count + d[b - 1]
	print(count)


import os, sys
from io import BytesIO, IOBase
from collections import Counter

def main():
	for _ in range(int(input())):
		(n, q) = map(int, input().split())
		a = list(map(int, input().split()))
		dp = [[0, 0] for _ in range(n + 1)]
		maxi_odd = maxi_even = 0
		for i in range(1, n + 1):
			dp[i][0] = maxi_even + a[i - 1]
			dp[i][1] = maxi_odd - a[i - 1]
			maxi_odd = max(maxi_odd, dp[i][0])
			maxi_even = max(maxi_even, dp[i][1])
		print(max(maxi_odd, maxi_even))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def pokemon(n, arr):
	dp = {}
	i = n - 1
	while i > -1:
		dp[i] = {}
		if i == n - 1:
			(dp[i][0], dp[i][1]) = (arr[i], -1 * arr[i])
		else:
			dp[i][0] = max(dp[i + 1][0], max(0, dp[i + 1][1]) + arr[i])
			dp[i][1] = max(dp[i + 1][1], max(dp[i + 1][0], 0) - arr[i])
		i -= 1
	return dp[0][0]
t = int(input())
for i in range(t):
	(n, q) = input().split()
	n = int(n)
	arr = list(map(int, input().split()))
	print(pokemon(n, arr))


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
		continue
	dp = [[0, 0] for i in range(n)]
	dp[0][0] = a[0]
	dp[1][0] = max(a[0], a[1])
	dp[1][1] = max(0, a[0] - a[1])
	for i in range(2, n):
		dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + a[i])
		dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - a[i])
	print(max(dp[-1][0], dp[-1][1]))


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
for _ in range(iinput()):
	(n, q) = rinput()
	a = get_list()
	ans = 0
	cnt = 0
	for i in range(n - 1):
		if cnt == 0 and a[i] > a[i + 1]:
			ans += a[i]
			cnt = 1
		elif cnt == 1 and a[i] < a[i + 1]:
			ans -= a[i]
			cnt = 0
	if cnt == 0:
		ans += a[-1]
	print(ans)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	total = 0
	flip = 0
	curr = a[0]
	for i in range(1, n):
		if flip == 0:
			if a[i] > a[i - 1]:
				curr = a[i]
			else:
				total += a[i - 1]
				curr = a[i]
				flip = 1
		elif a[i - 1] > a[i]:
			curr = a[i]
		else:
			total -= a[i - 1]
			curr = a[i]
			flip = 0
	if flip == 0:
		total += a[-1]
	print(total)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import sys
from sys import stdin, stdout
input = stdin.readline

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
c = 0
a = []
b = []

def f(i):
	if a[i] == 0:
		return 0
	if a[i - 1] < a[i] > a[i + 1]:
		return 1
	if a[i - 1] > a[i] < a[i + 1]:
		return -1
	return 0

def relax(i):
	global c
	c += (f(i) - b[i]) * a[i]
	b[i] = f(i)

def relax1(i):
	global c
	c -= b[i] * a[i]
	b[i] = 0
for _ in range(int(input())):
	c = 0
	(n, q) = map(int, input().split())
	(*a,) = map(int, input().split())
	a = [0] + a + [0]
	b = [0] * (n + 2)
	for i in range(1, n + 1):
		relax(i)
	print(c)
	for i in range(q):
		(l, r) = map(int, input().split())
		relax1(l)
		relax1(l - 1)
		relax1(l + 1)
		relax1(r)
		relax1(r - 1)
		relax1(r + 1)
		(a[l], a[r]) = (a[r], a[l])
		relax(l)
		relax(l - 1)
		relax(l + 1)
		relax(r)
		relax(r - 1)
		relax(r + 1)
		print(c)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	minn = 0
	maxx = a[n - 1]
	for i in range(n - 2, -1, -1):
		maxx = max(a[i] - minn, maxx)
		minn = min(a[i] - maxx, minn)
	print(maxx)


import sys
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from collections import *
from math import *

def findLocalMaximaMinima(n, arr):
	mx = []
	mn = []
	ans = []
	if n <= 2:
		return arr
	if arr[0] > arr[1]:
		mx.append(0)
		ans.append(arr[0])
	elif arr[0] < arr[1]:
		mn.append(0)
		ans.append(arr[0])
	for i in range(1, n - 1):
		if arr[i - 1] > arr[i] < arr[i + 1]:
			mn.append(arr[i])
			ans.append(arr[i])
		elif arr[i - 1] < arr[i] > arr[i + 1]:
			mx.append(i)
			ans.append(arr[i])
	if arr[-1] > arr[-2]:
		mx.append(n - 1)
		ans.append(arr[-1])
	elif arr[-1] < arr[-2]:
		mn.append(n - 1)
		ans.append(arr[-1])
	return ans

def fun(arr, n):
	max_so_far = arr[0]
	curr_max = arr[0]
	for i in range(1, n):
		curr_max = max(arr[i], curr_max + arr[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far
t = ii1()
for _ in range(t):
	(n, q) = iia()
	arr = iia()
	ans = findLocalMaximaMinima(n, arr)
	flag = 0
	if len(ans) >= 2:
		if ans[0] < ans[1]:
			ans.pop(0)
	for i in range(len(ans)):
		if i % 2 == 1:
			ans[i] = -ans[i]
	res = fun(ans, len(ans))
	print(res)


from sys import stdin
from math import log2

def readline():
	return stdin.readline()

def findsum(a):
	sm = 0
	for i in range(len(a)):
		if isMax(a, i):
			sm += a[i]
		elif isMin(a, i):
			sm -= a[i]
	return sm

def isMax(a, i):
	return i != 0 and i != len(a) - 1 and (a[i - 1] < a[i] > a[i + 1])

def isMin(a, i):
	return i != 0 and i != len(a) - 1 and (a[i - 1] > a[i] < a[i + 1])

def change(a, i1, i2, s):
	used = set()
	for j in [i1, i2]:
		for i in range(j - 1, j + 2):
			if i not in used:
				if isMax(a, i):
					s -= a[i]
					used.add(i)
				if isMin(a, i):
					s += a[i]
					used.add(i)
	(a[i1], a[i2]) = (a[i2], a[i1])
	used = set()
	for j in [i1, i2]:
		for i in range(j - 1, j + 2):
			if i not in used:
				if isMax(a, i):
					s += a[i]
					used.add(i)
				if isMin(a, i):
					s -= a[i]
					used.add(i)
	return s
tests = int(readline())
for t in range(0, tests):
	(n, q) = map(int, readline().split())
	a = [0, *list(map(int, readline().rstrip('\n').split(' '))), 0]
	s = findsum(a)
	print(s)
	for i in range(q):
		(i1, i2) = map(int, readline().split())
		s = change(a, i1, i2, s)
		print(s)


from math import *
t = int(input())
for i in range(t):
	(n, q) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	d = [[0, 0] for i in range(n)]
	d[0][0] = a[0]
	d[0][1] = 0
	maxel0 = a[0]
	maxel1 = 0
	for i in range(1, n):
		d[i][0] = maxel1 + a[i]
		d[i][1] = maxel0 - a[i]
		maxel0 = max(maxel0, d[i][0])
		maxel1 = max(maxel1, d[i][1])
	print(max(maxel0, maxel1))


import sys

def get_diff(a, pos):
	return a[pos] * (int(a[pos] > a[pos + 1]) + int(a[pos] > a[pos - 1]) - 1)

def main():
	iter_data = map(int, sys.stdin.read().split())
	t = next(iter_data)
	for _ in range(t):
		(n, q) = (next(iter_data), next(iter_data))
		a = [0] + [next(iter_data) for _ in range(n)] + [0]
		all_ans = [0] * (q + 1)
		cur_ans = sum((get_diff(a, i) for i in range(1, n + 1)))
		all_ans[0] = cur_ans
		for q_it in range(1, q + 1):
			(l, r) = (next(iter_data), next(iter_data))
			if l != r:
				cur_ans -= sum((get_diff(a, x) for x in set((l - 1, l, l + 1, r - 1, r, r + 1)) if 1 <= x <= n))
				(a[l], a[r]) = (a[r], a[l])
				cur_ans += sum((get_diff(a, x) for x in set((l - 1, l, l + 1, r - 1, r, r + 1)) if 1 <= x <= n))
			all_ans[q_it] = cur_ans
		print(*all_ans, sep='\n')
main()


import io
import os
z = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
o = []
y = lambda a, b: a - b if a - b > 0 else 0
for _ in range(int(z())):
	(n, q) = map(int, z().split())
	p = [0] + [*map(int, z().split())]
	s = m = 0
	for i in p:
		if i > m:
			s += i - m
		m = i
	o.append(s)
	for i in range(q):
		(a, b) = map(int, z().split())
		if a == b:
			o.append(s)
			continue
		(va, vb) = (p[a], p[b])
		va1 = p[a - 1]
		c = b < n
		if c:
			vb1 = p[b + 1]
		(p[a], p[b]) = (vb, va)
		lv = nv = 0
		if b - a < 2:
			if vb > va:
				lv += vb - va
			if va > va1:
				lv += va - va1
			if c and vb1 > vb:
				lv += vb1 - vb
			if va > vb:
				nv += va - vb
			if vb > va1:
				nv += vb - va1
			if c and vb1 > va:
				nv += vb1 - va
		else:
			(va2, vb2) = (p[a + 1], p[b - 1])
			if va2 > va:
				lv += va2 - va
			if va > va1:
				lv += va - va1
			if vb > vb2:
				lv += vb - vb2
			if c and vb1 > vb:
				lv += vb1 - vb
			if va2 > vb:
				nv += va2 - vb
			if vb > va1:
				nv += vb - va1
			if va > vb2:
				nv += va - vb2
			if c and vb1 > va:
				nv += vb1 - va
		s += nv - lv
		o.append(s)
print('\n'.join(map(str, o)))


for h in range(int(input())):
	(n, q) = map(int, input().strip().split())
	arr = list(map(int, input().strip().split()))
	dp = [[0, 0]]
	for i in arr:
		temp = [0, 0]
		temp[0] = max(dp[-1][0], dp[-1][1] + i)
		temp[1] = max(dp[-1][1], dp[-1][0] - i)
		dp.append(temp)
	print(max(dp[-1]))


t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	arr.append(0)
	maxima = 0
	minima = 1
	res = 0
	while minima <= n:
		while minima <= n and arr[maxima] <= arr[minima]:
			minima += 1
			maxima += 1
		while minima < n and arr[minima] >= arr[minima + 1]:
			minima += 1
		res += arr[maxima] - arr[minima]
		maxima = minima + 1
		minima = maxima + 1
	print(res)


import os
import heapq
import sys, threading
import math
import operator
from collections import defaultdict, deque
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def inpt():
	return [int(k) for k in input().split()]

def main():
	t = int(input())
	for _ in range(t):
		(n, q) = inpt()
		arr = inpt()
		mx = 1
		ans = 0
		for i in range(1, n):
			if mx == 1:
				if arr[i - 1] < arr[i]:
					continue
				else:
					ans += arr[i - 1]
					mx = 0
			elif arr[i - 1] > arr[i]:
				continue
			else:
				ans -= arr[i - 1]
				mx = 1
		if mx == 1:
			ans += arr[-1]
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys, math
import io, os
data = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write('\n'.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
mod = 998244353
INF = float('inf')
ans = []
for t in range(int(data())):
	(n, q) = mdata()
	a = mdata()
	lis = []
	for i in range(1, n):
		lis.append(max(a[i - 1] - a[i], 0))
	s = sum(lis)
	ans.append(s + a[-1])
	for i in range(q):
		(l, r) = mdata()
		(l, r) = (l - 1, r - 1)
		(a[l], a[r]) = (a[r], a[l])
		if l:
			s -= lis[l - 1]
			lis[l - 1] = max(a[l - 1] - a[l], 0)
			s += lis[l - 1]
		if l < n - 1:
			s -= lis[l]
			lis[l] = max(a[l] - a[l + 1], 0)
			s += lis[l]
		if r:
			s -= lis[r - 1]
			lis[r - 1] = max(a[r - 1] - a[r], 0)
			s += lis[r - 1]
		if r < n - 1:
			s -= lis[r]
			lis[r] = max(a[r] - a[r + 1], 0)
			s += lis[r]
		ans.append(s + a[-1])
outl(ans)


T = int(input())
for _ in range(T):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(q):
		(l, r) = map(int, input().split())
	d1 = -10000000000000000
	d2 = 0
	for i in range(n):
		(tmp1, tmp2) = (d1, d2)
		d1 = max(tmp1, tmp2 + a[i])
		d2 = max(tmp2, tmp1 - a[i])
	print(max(d1, d2))


def maximal_power(l):
	res = 0
	last = 0
	for i in l:
		if i > last:
			res += i - last
		last = i
	return res
t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	print(maximal_power(l))


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from typing import List
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

@lru_cache(None)
def fact(x, mod=0):
	if x < 2:
		return 1
	if not mod:
		return fact(x - 1) * x
	return fact(x - 1, mod) * x % mod

@lru_cache(None)
def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	ans = 1
	for k in range(i - j + 1, i + 1):
		ans = ans * k % mod
	return ans

@lru_cache(None)
def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j) * inverse(fact(j), mod) % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d
t = N()
for i in range(t):
	(n, q) = RL()
	a = RLL()
	(x, y) = (0, 0)
	for i in a:
		(x, y) = (max(y + i, x), max(y, x - i))
	ans = x
	print(ans)


T = int(input())
for case in range(T):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	i = 0
	f = 0
	while True:
		if f == 0:
			while i + 1 < n and a[i] < a[i + 1]:
				i += 1
			ans += a[i]
			f ^= 1
		else:
			while i + 1 < n and a[i] > a[i + 1]:
				i += 1
			if i != n - 1:
				ans -= a[i]
			f ^= 1
		if i == n - 1:
			break
	print(ans)


for o in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	t = 0
	f = 0
	for i in range(n - 1):
		if f == 0:
			if a[i] < a[i + 1]:
				f = 0
			else:
				t = t + a[i]
				f = 1
		elif a[i] > a[i + 1]:
			f = 1
		else:
			t = t - a[i]
			f = 0
	if f == 0:
		t = t + a[n - 1]
	print(t)


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	sm = 0
	b = True
	for j in range(n - 1):
		if b:
			if a[j] > a[j + 1]:
				sm += a[j]
				b = False
		elif a[j] < a[j + 1]:
			sm -= a[j]
			b = True
	if b:
		sm += a[n - 1]
	print(sm)


from math import log2

def main():
	t = int(input())
	for i in range(t):
		(n, q) = map(int, input().split())
		values = list(map(int, input().split()))
		max_0 = 0
		max_1 = 0
		for i in range(n):
			last = max_0
			if max_1 - values[i] > max_0:
				last = max_1 - values[i]
			if max_0 + values[i] > max_1:
				max_1 = max_0 + values[i]
			max_0 = last
		print(max(max_0, max_1))
main()


T = int(input())
for _ in range(T):
	(n, q) = map(int, input().split())
	ar = list(map(int, input().split()))
	dp = [[0 for _ in range(2)] for _ in range(n + 1)]
	for i in range(n):
		dp[i][1] = max(dp[i - 1][0] + ar[i], dp[i - 1][1])
		dp[i][0] = max(dp[i - 1][0], -ar[i] + dp[i - 1][1])
	print(max(dp[n - 1][0], dp[n - 1][1]))


import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
import io
import os
input = sys.stdin.readline
import random
mo = 10 ** 9 + 7

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for _ in range(inin()):
	(n, k) = cin()
	l = ain()
	ans = 0
	if n <= 2:
		ans = max(l)
	else:
		for i in range(n):
			if i == 0:
				if l[i + 1] < l[i]:
					ans += l[0]
			elif i == n - 1:
				if l[i - 1] < l[i]:
					ans += l[i]
			elif l[i - 1] < l[i] and l[i + 1] < l[i]:
				ans += l[i]
			elif l[i - 1] > l[i] and l[i + 1] > l[i]:
				ans -= l[i]
	print(ans)
	for j in range(k):
		(a, b) = cin()
		if n <= 2:
			ans = ans
		else:
			(a, b) = (a - 1, b - 1)
			for i in range(max(a - 1, 0), min(a + 2, n)):
				if i == 0:
					if l[i + 1] < l[i]:
						ans -= l[0]
				elif i == n - 1:
					if l[i - 1] < l[i]:
						ans -= l[i]
				elif l[i - 1] < l[i] and l[i + 1] < l[i]:
					ans -= l[i]
				elif l[i - 1] > l[i] and l[i + 1] > l[i]:
					ans += l[i]
			for i in range(max(b - 1, a + 2), min(b + 2, n)):
				if i == 0:
					if l[i + 1] < l[i]:
						ans -= l[0]
				elif i == n - 1:
					if l[i - 1] < l[i]:
						ans -= l[i]
				elif l[i - 1] < l[i] and l[i + 1] < l[i]:
					ans -= l[i]
				elif l[i - 1] > l[i] and l[i + 1] > l[i]:
					ans += l[i]
			(l[b], l[a]) = (l[a], l[b])
			for i in range(max(a - 1, 0), min(a + 2, n)):
				if i == 0:
					if l[i + 1] < l[i]:
						ans += l[0]
				elif i == n - 1:
					if l[i - 1] < l[i]:
						ans += l[i]
				elif l[i - 1] < l[i] and l[i + 1] < l[i]:
					ans += l[i]
				elif l[i - 1] > l[i] and l[i + 1] > l[i]:
					ans -= l[i]
			for i in range(max(b - 1, a + 2), min(b + 2, n)):
				if i == 0:
					if l[i + 1] < l[i]:
						ans += l[0]
				elif i == n - 1:
					if l[i - 1] < l[i]:
						ans += l[i]
				elif l[i - 1] < l[i] and l[i + 1] < l[i]:
					ans += l[i]
				elif l[i - 1] > l[i] and l[i + 1] > l[i]:
					ans -= l[i]
		print(ans)


def main(lst):
	boolean = True
	res = 0
	for i in lst:
		if boolean:
			pre = i
			res = i
			boolean = False
		elif i >= pre:
			res += i - pre
			pre = i
		else:
			pre = i
	return res
a = int(input())
for i in range(a):
	a = input()
	lst = list(map(int, input().split()))
	print(main(lst))


t = int(input())
while t > 0:
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	max = None
	i = 0
	while i < n:
		j = i + 1
		k = j + 1
		if max == None:
			max = a[i]
			continue
		try:
			if max < a[j]:
				max = a[j]
				i += 1
				continue
			if a[k] - a[j] <= 0:
				j += 1
				k += 1
			temp_max = 0
			while k < n and a[k] - a[j] > temp_max:
				temp_max = a[k] - a[j]
				k += 1
			max += temp_max
			if k != n - 1:
				i = k - 1
			else:
				break
		except:
			i = n
	print(max)
	t -= 1


from sys import stdin

def sums(a):
	summ = 0
	for i in range(len(a)):
		if i % 2 == 0:
			summ += a[i]
		else:
			summ -= a[i]
	return summ
t = int(stdin.readline())
while t > 0:
	(n, q) = map(int, stdin.readline().split())
	a = list(map(int, stdin.readline().strip().split()))
	pos = True
	results = []
	for i in range(n):
		if i + 1 < n:
			if a[i] > a[i + 1] and pos:
				results.append(a[i])
				pos = False
			elif a[i] < a[i + 1] and (not pos):
				results.append(a[i])
				pos = True
		elif pos:
			results.append(a[i])
	print(sums(results))
	t -= 1


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = [int(x) for x in input().split()]
	ans = max(l)
	c = 0
	stack_inc = []
	stack_dec = []
	flag = 0
	for i in range(n):
		if flag == 0:
			if len(stack_inc) == 0:
				stack_inc.append(l[i])
			elif stack_inc[-1] <= l[i]:
				stack_inc.append(l[i])
			else:
				c += stack_inc[-1]
				stack_inc = []
				flag = 1
				stack_dec = [l[i]]
		elif len(stack_dec) == 0:
			stack_dec.append(l[i])
		elif stack_dec[-1] >= l[i]:
			stack_dec.append(l[i])
		else:
			c -= stack_dec[-1]
			stack_dec = []
			flag = 0
			stack_inc = [l[i]]
	if flag == 0:
		c += stack_inc[-1]
	print(max(ans, c))


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def solve(n, lst):
	summ = 0
	turn = 0
	lst.append(-1)
	n += 1
	for i in range(n - 1):
		if turn == 0:
			if lst[i] > lst[i + 1]:
				turn = 1
				summ += lst[i]
		elif lst[i] < lst[i + 1]:
			turn = 0
			summ -= lst[i]
	return summ
for _ in range(int(input())):
	(n, q) = list(map(int, input().split()))
	lst = list(map(int, input().split()))
	print(solve(n, lst))


INF = 10 ** 15
for _ in range(int(input())):
	(n, q) = tuple(map(int, input().split()))
	arr = list(map(int, input().split()))
	a = -INF
	b = 0
	for i in arr:
		if a == -INF:
			c = 0
			d = i
		else:
			c = b - i
			d = a + i
		(a, b) = (max(a, c), max(b, d))
	print(max(a, b))


for _ in range(int(input())):
	(n, q) = map(int, input().split(' '))
	arr = [int(num) for num in input().split(' ')]
	dp_odd = [0 for x in range(n + 1)]
	dp_even = [0 for x in range(n + 1)]
	inf = 10 ** 18
	dp_odd[0] = 0
	dp_even[0] = 0
	for i in range(1, n + 1):
		dp_odd[i] = max(dp_odd[i - 1], dp_even[i - 1] + arr[i - 1])
		dp_even[i] = max(dp_even[i - 1], dp_odd[i - 1] - arr[i - 1])
		ans = max(dp_even[i], dp_odd[i])
	print(ans)


t = int(input())
while t:
	t -= 1
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	ans = max(a)
	total = 0
	res = []
	flag = True
	i = 0
	while i < n:
		if flag:
			while i + 1 < n and a[i] < a[i + 1]:
				i += 1
			res.append(a[min(i, n - 1)])
			i += 1
			flag = False
		else:
			while i + 1 < n and a[i] > a[i + 1]:
				i += 1
			res.append(a[min(i, n - 1)])
			flag = True
			i += 1
	flag = True
	if len(res) % 2 == 0:
		res.pop()
	for x in res:
		if flag:
			flag = False
			total += x
		else:
			total -= x
			flag = True
	print(max(ans, total))


T = int(input())
for _ in range(T):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if n == 1:
		b = [a[0]]
	else:
		b = []
		d = 1
		for i in range(1, n):
			if d == 1 and a[i] < a[i - 1]:
				b.append(a[i - 1])
				d = -1
			elif d == -1 and a[i] > a[i - 1]:
				b.append(a[i - 1])
				d = 1
			if i == n - 1 and d == 1:
				b.append(a[i])
	print(sum([b[i] if i % 2 == 0 else -b[i] for i in range(len(b))]))


for i in range(int(input())):
	(n, q) = map(int, input().split())
	s = map(int, input().split())
	ans = 0
	j = 0
	accum = 0
	for i in s:
		if j == 0:
			if i >= ans:
				ans = i
			else:
				j = 1
				accum += ans
				ans = i
		elif i <= ans:
			ans = i
		else:
			j = 0
			accum -= ans
			ans = i
	if j == 0:
		accum += ans
	print(accum)


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	(n, q) = map(int, stdin.readline().split())
	a = list(map(int, stdin.readline().split()))
	dp = [0, float('-inf')]
	for i in range(n):
		ndp = [dp[0], dp[1]]
		ndp[0] = max(ndp[0], dp[1] - a[i])
		ndp[1] = max(ndp[1], dp[0] + a[i])
		dp = ndp
	print(max(dp))


import sys
import math
mod = 10 ** 9 + 7
inf = float('inf')

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))
t = I()
for _ in range(t):
	(n, q) = LI()
	a = LI()
	ans = 0
	tmp = a[0]
	flag = True
	for i in range(n - 1):
		if a[i] < a[i + 1] and (not flag):
			tmp = a[i + 1]
			flag = True
			ans -= a[i]
		elif a[i] > a[i + 1] and flag:
			ans += a[i]
			flag = False
	if flag:
		ans += a[-1]
	print(ans)


for t in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	m = max(l)
	if n <= 2:
		print(m)
		continue
	f = 1
	s = 0
	k = 0
	b = 0
	for i in range(1, n):
		if f == 1 and l[i - 1] > l[i]:
			k = l[i - 1]
			s += k
			f = 0
		elif f == 0 and l[i - 1] < l[i] and (l[i - 1] < k):
			s -= l[i - 1]
			f = 1
			b = i - 1
	if f == 1:
		s += max(l[b:])
	print(max(s, m))


from functools import cmp_to_key
from collections import deque
from heapq import heappush, heappop
from math import log, ceil
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def ii():
	return int(inp())

def li(lag=0):
	l = list(map(int, inp().split()))
	if lag != 0:
		for i in range(len(l)):
			l[i] += lag
	return l

def mi(lag=0):
	matrix = list()
	for i in range(n):
		matrix.append(li(lag))
	return matrix

def sli():
	return list(map(str, inp().split()))

def print_list(lista, space=' '):
	print(space.join(map(str, lista)))

class UnionFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def bisect_left(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] < x:
			left = mid + 1
		else:
			right = mid
	return left

def bisect_right(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] > x:
			right = mid
		else:
			left = mid + 1
	return left

def bisect_elements(a, x):
	return bisect_right(a, x) - bisect_left(a, x)

def custom_sort(lista):

	def cmp(x, y):
		if x + y > y + x:
			return 1
		else:
			return -1
	return sorted(lista, key=cmp_to_key(cmp))
MOD = 10 ** 9 + 7
maxN = 10 ** 5
FACT = [0] * maxN

def add(x, y):
	return (x + y) % MOD

def multiply(x, y):
	return x * y % MOD

def power(x, y):
	if y == 0:
		return 1
	elif y % 2:
		return multiply(x, power(x, y - 1))
	else:
		a = power(x, y // 2)
		return multiply(a, a)

def inverse(x):
	return power(x, MOD - 2)

def divide(x, y):
	return multiply(x, inverse(y))

def allFactorials():
	FACT[0] = 1
	for i in range(1, maxN):
		FACT[i] = multiply(i, FACT[i - 1])

def coeffBinom(n, k):
	if n < k:
		return 0
	return divide(FACT[n], multiply(FACT[k], FACT[n - k]))

def primes(N):
	smallest_prime = [1] * (N + 1)
	prime = []
	smallest_prime[0] = 0
	smallest_prime[1] = 0
	for i in range(2, N + 1):
		if smallest_prime[i] == 1:
			prime.append(i)
			smallest_prime[i] = i
		j = 0
		while j < len(prime) and i * prime[j] <= N:
			smallest_prime[i * prime[j]] = min(prime[j], smallest_prime[i])
			j += 1
	return (prime, smallest_prime)

def gcd(a, b):
	a = abs(a)
	b = abs(b)
	(s, t, r) = (0, 1, b)
	(old_s, old_t, old_r) = (1, 0, a)
	while r != 0:
		quotient = old_r // r
		(old_r, r) = (r, old_r - quotient * r)
		(old_s, s) = (s, old_s - quotient * s)
		(old_t, t) = (t, old_t - quotient * t)
	return (old_r, old_s, old_t)

def create_graph(n, m, undirected=1, unweighted=1):
	graph = [[] for i in range(n)]
	if unweighted:
		for i in range(m):
			[x, y] = li(lag=-1)
			graph[x].append(y)
			if undirected:
				graph[y].append(x)
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			graph[x].append([y, w])
			if undirected:
				graph[y].append([x, w])
	return graph

def create_tree(n, unweighted=1):
	children = [[] for i in range(n)]
	if unweighted:
		for i in range(n - 1):
			[x, y] = li(lag=-1)
			children[x].append(y)
			children[y].append(x)
	else:
		for i in range(n - 1):
			[x, y, w] = li(lag=-1)
			w += 1
			children[x].append([y, w])
			children[y].append([x, w])
	return children

def create_edges(m, unweighted=0):
	edges = list()
	if unweighted:
		for i in range(m):
			edges.append(li(lag=-1))
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			edges.append([w, x, y])
	return edges

def dist(tree, n, A, B=-1):
	s = [[A, 0]]
	(massimo, massimo_nodo) = (0, 0)
	distanza = -1
	v = [-1] * n
	while s:
		(el, dis) = s.pop()
		if dis > massimo:
			massimo = dis
			massimo_nodo = el
		if el == B:
			distanza = dis
		for child in tree[el]:
			if v[child] == -1:
				v[child] = 1
				s.append([child, dis + 1])
	return (massimo, massimo_nodo, distanza)

def diameter(tree):
	(_, foglia, _) = dist(tree, n, 0)
	(diam, _, _) = dist(tree, n, foglia)
	return diam

def dfs(graph, n, A):
	v = [-1] * n
	s = [[A, 0]]
	v[A] = 0
	while s:
		(el, dis) = s.pop()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def bfs(graph, n, A):
	v = [-1] * n
	s = deque()
	s.append([A, 0])
	v[A] = 0
	while s:
		(el, dis) = s.popleft()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def connected(graph, n):
	v = dfs(graph, n, 0)
	for el in v:
		if el == -1:
			return False
	return True

def topological(graph, n):
	indegree = [0] * n
	for el in range(n):
		for child in graph[el]:
			indegree[child] += 1
	s = deque()
	for el in range(n):
		if indegree[el] == 0:
			s.append(el)
	order = []
	while s:
		el = s.popleft()
		order.append(el)
		for child in graph[el]:
			indegree[child] -= 1
			if indegree[child] == 0:
				s.append(child)
	if n == len(order):
		return (False, order)
	else:
		return (True, [])

def bipartite(graph, n):
	color = [-1] * n
	color[0] = 0
	s = [0]
	while s:
		el = s.pop()
		for child in graph[el]:
			if color[child] == color[el]:
				return False
			if color[child] == -1:
				s.append(child)
			color[child] = 1 - color[el]
	return True

def dijkstra(graph, n, A):
	dist = [float('inf') for i in range(n)]
	prev = [-1 for i in range(n)]
	dist[A] = 0
	pq = []
	heappush(pq, [0, A])
	while pq:
		[d_v, v] = heappop(pq)
		if d_v != dist[v]:
			continue
		for (to, w) in graph[v]:
			if dist[v] + w < dist[to]:
				dist[to] = dist[v] + w
				prev[to] = v
				heappush(pq, [dist[to], to])
	return (dist, prev)

def dijkstra_0_1(graph, n, A):
	dist = [float('inf') for i in range(n)]
	dist[A] = 0
	p = deque()
	p.append(A)
	while p:
		v = p.popleft()
		for (to, w) in graph[v]:
			if dist[v] + w < dist[to]:
				dist[to] = dist[v] + w
			if w == 1:
				q.append(to)
			else:
				q.appendleft(to)
	return dist

def floyd_warshall(graph, n):
	dist = [[float('inf') for _ in range(n)] for _ in range(n)]
	for i in range(n):
		dist[i][i] = 0
		for (child, d) in graph[i]:
			dist[i][child] = d
			dist[child][i] = d
	for k in range(n):
		for i in range(n):
			for j in range(j):
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
	return dist

def minimum_spanning_tree(edges, n):
	edges = sorted(edges)
	union_find = UnionFind(n)
	used_edges = list()
	for (w, x, y) in edges:
		if union_find.find(x) != union_find.find(y):
			union_find.merge(x, y)
			used_edges.append([w, x, y])
	return used_edges

def parents_children_root_unrooted_tree(tree, n, root=0):
	q = deque()
	visited = [0] * n
	parent = [-1] * n
	children = [[] for i in range(n)]
	q.append(root)
	while q:
		all_done = 1
		visited[q[0]] = 1
		for child in tree[q[0]]:
			if not visited[child]:
				all_done = 0
				q.appendleft(child)
		if all_done:
			for child in tree[q[0]]:
				if parent[child] == -1:
					parent[q[0]] = child
					children[child].append(q[0])
			q.popleft()
	return (parent, children)

def all_longest_path_passing_from_node(parent, children, n):
	q = deque()
	visited = [len(children[i]) for i in range(n)]
	downwards = [[0, 0] for i in range(n)]
	upward = [1] * n
	longest_path = [1] * n
	for i in range(n):
		if not visited[i]:
			q.append(i)
			downwards[i] = [1, 0]
	while q:
		node = q.popleft()
		if parent[node] != -1:
			visited[parent[node]] -= 1
			if not visited[parent[node]]:
				q.append(parent[node])
		else:
			root = node
		for child in children[node]:
			downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse=True)[0:2]
	s = [node]
	while s:
		node = s.pop()
		if parent[node] != -1:
			if downwards[parent[node]][0] == downwards[node][0] + 1:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1])
			else:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0])
		longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1
		for child in children[node]:
			s.append(child)
	return longest_path

def finding_ancestors(parent, queries, n):
	steps = int(ceil(log(n, 2)))
	ancestors = [[-1 for i in range(n)] for j in range(steps)]
	ancestors[0] = parent
	for i in range(1, steps):
		for node in range(n):
			if ancestors[i - 1][node] != -1:
				ancestors[i][node] = ancestors[i - 1][ancestors[i - 1][node]]
	result = []
	for (node, k) in queries:
		ans = node
		if k >= n:
			ans = -1
		i = 0
		while k > 0 and ans != -1:
			if k % 2:
				ans = ancestors[i][ans]
			k = k // 2
			i += 1
		result.append(ans)
	return result

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def nearest_from_the_left_smaller_elements(arr):
	n = len(arr)
	res = [-1] * n
	s = []
	for (i, el) in enumerate(arr):
		while s and s[-1] >= el:
			s.pop()
		if s:
			res[i] = s[-1]
		s.append(el)
	return res

def sliding_window_minimum(arr, k):
	res = []
	q = deque()
	for (i, el) in enumerate(arr):
		while q and arr[q[-1]] >= el:
			q.pop()
		q.append(i)
		while q and q[0] <= i - k:
			q.popleft()
		if i >= k - 1:
			res.append(arr[q[0]])
	return res
for test in range(ii()):
	(n, q) = li()
	a = li()
	neg = [-float('inf')] * n
	pos = [-float('inf')] * n
	neg[n - 1] = -a[n - 1]
	pos[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		pos[i] = max(a[i] + neg[i + 1], pos[i + 1], a[i])
		neg[i] = max(-a[i] + pos[i + 1], neg[i + 1], -a[i])
	print(max(pos))


from functools import reduce
import os
import sys
from collections import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
mo = 1000000007
inf = 1e+18
for _ in range(inn()):
	(n, q) = value()
	a = arr()
	b = []
	i = 0
	ans = 0
	while i < n - 1:
		while i < n - 1 and a[i] < a[i + 1]:
			i += 1
		b += [i]
		val = a[i]
		while i < n - 1 and a[i] > a[i + 1]:
			i += 1
		b += [i]
		val -= a[i]
		ans += val
	if len(b) % 2 == 0:
		ans += a[-1]
	print(ans)


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k, modulus=1):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
		res %= modulus
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(n, q) = int_array()
	a = int_array()
	stack = [a.pop(0)]
	for i in a:
		if len(stack) & 1:
			if i > stack[-1]:
				stack.pop()
				stack.append(i)
			else:
				stack.append(i)
		elif i > stack[-1]:
			stack.append(i)
		else:
			stack.pop()
			stack.append(i)
	ans = 0
	if len(stack) % 2 == 0:
		stack.pop()
	for i in range(len(stack)):
		if i & 1:
			ans -= stack[i]
		else:
			ans += stack[i]
	print(ans)


t = int(input())
for tc in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	val = a[0]
	for i in range(n - 1):
		if a[i + 1] - a[i] > 0:
			val += a[i + 1] - a[i]
	print(val)


import sys
import math
from collections import defaultdict, deque
input = sys.stdin.readline

def inar():
	return [int(el) for el in input().split()]

def main():
	t = int(input())
	for _ in range(t):
		(n, q) = inar()
		arr = inar()
		mx = 1
		ans = 0
		for i in range(1, n):
			if mx == 1:
				if arr[i - 1] < arr[i]:
					continue
				else:
					ans += arr[i - 1]
					mx = 0
			elif arr[i - 1] > arr[i]:
				continue
			else:
				ans -= arr[i - 1]
				mx = 1
		if mx == 1:
			ans += arr[-1]
		print(ans)
main()


def isLocalMin(i, a, n):
	if a[i - 1] > a[i] < a[i + 1]:
		return True
	else:
		return False

def isLocalMax(i, a, n):
	if a[i - 1] < a[i] > a[i + 1]:
		return True
	else:
		return False

def main():
	t = int(input())
	ans = []
	for _ in range(t):
		(n, q) = readIntArr()
		a = readIntArr()
		a = [-1] + a + [-1]
		swaps = []
		for __ in range(q):
			swaps.append(readIntArr())
		total = 0
		for i in range(1, n + 1):
			if isLocalMin(i, a, n):
				total -= a[i]
			elif isLocalMax(i, a, n):
				total += a[i]
		ans.append(total)
		for (l, r) in swaps:
			idxes = set()
			for ll in range(l - 1, l + 2):
				if 1 <= ll < n + 1:
					idxes.add(ll)
			for rr in range(r - 1, r + 2):
				if 1 <= rr < n + 1:
					idxes.add(rr)
			for idx in idxes:
				if isLocalMin(idx, a, n):
					total += a[idx]
				elif isLocalMax(idx, a, n):
					total -= a[idx]
			(a[l], a[r]) = (a[r], a[l])
			for idx in idxes:
				if isLocalMin(idx, a, n):
					total -= a[idx]
				elif isLocalMax(idx, a, n):
					total += a[idx]
			ans.append(total)
	multiLineArrayPrint(ans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]
main()


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	res = arr[0]
	for i in range(1, n):
		res += max(0, arr[i] - arr[i - 1])
	print(res)


import os, sys
from io import BytesIO, IOBase

def op(x, a, dp, ans, n):
	if x < 0 or x >= n:
		return ans
	fl = -1 if x and a[x] < a[x - 1] else 1
	fl1 = -1 if x != n - 1 and a[x] < a[x + 1] else 1
	if fl == 1 and fl1 == 1:
		dp[x] = 1
	elif fl == -1 and fl1 == -1:
		dp[x] = -1
	else:
		dp[x] = 0
	if (not x or x == n - 1) and dp[x] == -1:
		dp[x] = 0
	ans += dp[x] * a[x]
	return ans

def main():
	for _ in range(int(input())):
		(n, q) = map(int, input().split())
		a = list(map(int, input().split()))
		if n != 1:
			dp = [1 if a[0] > a[1] else 0] + [0] * (n - 2) + [1 if a[-1] > a[-2] else 0]
			for i in range(1, n - 1):
				if a[i] < a[i + 1] and a[i] < a[i - 1]:
					dp[i] = -1
				elif a[i] > a[i + 1] and a[i] > a[i - 1]:
					dp[i] = 1
		else:
			dp = [1]
		ans = 0
		for i in range(n):
			ans += dp[i] * a[i]
		print(ans)
		for _ in range(q):
			(l, r) = map(lambda xx: int(xx) - 1, input().split())
			y = {l - 1, l, l + 1, r - 1, r, r + 1}
			for i in y:
				if i < 0 or i >= n:
					continue
				ans -= a[i] * dp[i]
			(a[l], a[r]) = (a[r], a[l])
			for x in y:
				ans = op(x, a, dp, ans, n)
			print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


T = int(input())
for _ in range(0, T):
	(n, q) = map(int, input().split())
	s = [int(x) for x in input().split()]
	ps = 0
	ng = 0
	ans = n
	for i in range(0, len(s)):
		pos = ps
		neg = ng
		ps = max(pos, neg + s[i])
		ng = max(neg, pos - s[i])
		ans = max(ans, ps)
		ans = max(ans, ng)
	print(ans)


import io
import os

def solve(N, Q, A, LR):
	deltas = [max(A[i] - A[i + 1], 0) for i in range(N - 1)]
	total = sum(deltas)
	ans = []
	ans.append(str(total + A[-1]).encode())
	for (l, r) in LR:
		l -= 1
		r -= 1
		(A[l], A[r]) = (A[r], A[l])
		for i in [l - 1, l, r - 1, r]:
			if 0 <= i < N - 1:
				delta = max(A[i] - A[i + 1], 0)
				oldDelta = deltas[i]
				if delta != oldDelta:
					deltas[i] = delta
					total += delta - oldDelta
		ans.append(str(total + A[-1]).encode())
	assert len(ans) == Q + 1
	return b'\n'.join(ans) + b'\n'
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N, Q) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	LR = [[int(x) for x in input().split()] for i in range(Q)]
	ans = solve(N, Q, A, LR)
	os.write(1, ans)


def solve():
	(n, _) = [int(v) for v in input().split()]
	arr = [int(v) for v in input().split()]
	odd_ans = arr[0]
	even_ans = 0
	for i in range(1, n):
		mx_even = max(even_ans, odd_ans - arr[i])
		mx_odd = max(odd_ans, even_ans + arr[i])
		(even_ans, odd_ans) = (mx_even, mx_odd)
	print(max(even_ans, odd_ans))
t = int(input())
for _ in range(t):
	solve()


x = int(input())
for i in range(x):
	(n, q) = list(map(int, input().split()))
	y = list(map(int, input().split()))
	prev = y[0]
	flip = 0
	ans = 0
	for k in range(1, n):
		if flip == 0:
			if y[k] < y[k - 1]:
				ans += y[k - 1]
				flip = 1
		elif y[k] > y[k - 1]:
			ans -= y[k - 1]
			flip = 0
	if flip == 0:
		ans += y[n - 1]
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
	return map(int, sys.stdin.readline().split())

def RLL():
	return list(map(int, sys.stdin.readline().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
data = [list(map(int, line.split())) for line in sys.stdin.readlines()]
line = 1
for _ in range(data[0][0]):
	(n, q) = data[line]
	a = [0] + data[line + 1] + [0]
	(low, high) = (set(), set())
	s = 0
	for i in range(1, n + 1):
		if a[i] > a[i - 1] and a[i] > a[i + 1]:
			high.add(a[i])
			s += a[i]
		elif a[i] < a[i - 1] and a[i] < a[i + 1]:
			low.add(a[i])
			s -= a[i]
	print(s)
	for ii in range(line + 2, line + q + 2):
		(x, y) = data[ii]
		(a[x], a[y]) = (a[y], a[x])
		d = {a[x - 1], a[x], a[x + 1], a[y - 1], a[y], a[y + 1]}
		t = low & d
		s += sum(t)
		low -= t
		t = high & d
		s -= sum(t)
		high -= t
		d = {x - 1, x, x + 1, y - 1, y, y + 1}
		for i in d:
			if 0 < i <= n:
				if a[i] > a[i - 1] and a[i] > a[i + 1]:
					high.add(a[i])
					s += a[i]
				elif a[i] < a[i - 1] and a[i] < a[i + 1]:
					low.add(a[i])
					s -= a[i]
		print(s)
	line += q + 2


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	f = 0
	stack = []
	for i in a:
		if not stack:
			stack.append(i)
		elif f == 0:
			if i > stack[-1]:
				stack.pop()
				stack.append(i)
			else:
				stack.append(i)
				f = 1
		elif i < stack[-1]:
			stack.pop()
			stack.append(i)
		else:
			f = 0
			stack.append(i)
	su = 0
	if len(stack) % 2 == 0:
		l = len(stack) - 1
	else:
		l = len(stack)
	for i in range(l):
		if i % 2 == 0:
			su += stack[i]
		else:
			su -= stack[i]
	print(su)


import sys
input = sys.stdin.buffer.readline
INF = 10 ** 18
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = [-INF] + list(map(int, input().split())) + [-INF]
	queries = [list(map(int, input().split())) for i in range(q)]
	ans = 0
	for i in range(1, n + 1):
		if a[i - 1] < a[i] and a[i + 1] < a[i]:
			ans += a[i]
		if a[i] < a[i - 1] and a[i] < a[i + 1]:
			ans -= a[i]
	print(ans)
	for (ll, rr) in queries:
		for l in range(ll - 2, ll + 3):
			if not (0 <= l - 1 and l + 1 <= n + 1):
				continue
			elif a[l - 1] < a[l] and a[l + 1] < a[l]:
				ans -= a[l]
			elif a[l] < a[l - 1] and a[l] < a[l + 1]:
				ans += a[l]
		for r in range(max(rr - 2, ll + 3), rr + 3):
			if not (0 <= r - 1 and r + 1 <= n + 1):
				continue
			elif a[r - 1] < a[r] and a[r + 1] < a[r]:
				ans -= a[r]
			elif a[r] < a[r - 1] and a[r] < a[r + 1]:
				ans += a[r]
		(a[ll], a[rr]) = (a[rr], a[ll])
		for l in range(ll - 2, ll + 3):
			if not (0 <= l - 1 and l + 1 <= n + 1):
				continue
			elif a[l - 1] < a[l] and a[l + 1] < a[l]:
				ans += a[l]
			elif a[l] < a[l - 1] and a[l] < a[l + 1]:
				ans -= a[l]
		for r in range(max(rr - 2, ll + 3), rr + 3):
			if not (0 <= r - 1 and r + 1 <= n + 1):
				continue
			elif a[r - 1] < a[r] and a[r + 1] < a[r]:
				ans += a[r]
			elif a[r] < a[r - 1] and a[r] < a[r + 1]:
				ans -= a[r]
		print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	lst = list(map(int, input().split()))
	curmin = n + 1
	curmax = 0
	res = 0
	flag = 0
	last = 0
	r = []
	for (i, ele) in enumerate(lst):
		if flag == 0 and ele < last:
			r.append(curmax)
			curmax = 0
		if flag == 1 and ele > last:
			r.append(curmin)
			curmin = n + 1
		if ele > last:
			flag = 0
		else:
			flag = 1
		if flag == 0:
			if ele > curmax:
				curmax = ele
		elif ele < curmin:
			curmin = ele
		last = ele
	if curmax != 0:
		r.append(curmax)
	if len(r) % 2 == 1:
		for i in range(len(r)):
			if i % 2 == 0:
				res += r[i]
			else:
				res -= r[i]
	else:
		for i in range(len(r) - 1):
			if i % 2 == 0:
				res += r[i]
			else:
				res -= r[i]
	print(res)


for _ in range(0, int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[0] * 2] * (n + 2)
	dp[1][1] = a[0]
	dp[1][0] = 0
	for i in range(1, len(a)):
		dp[i + 1][1] = max(a[i], dp[i][0] + a[i], dp[i][1])
		dp[i + 1][0] = max(dp[i][1] - a[i], dp[i][0])
	print(max(dp[n][0], dp[n][1]))


import sys
input = sys.stdin.readline

def algo(n, arr):
	res = 0
	cur = 0
	for i in range(n):
		t = arr[i] - cur
		if t > 0:
			res += t
		cur = arr[i]
	return res
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = algo(n, arr)
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().rstrip().split(' ')))
	a = list(map(int, input().rstrip().split(' ')))
	bestAns = 0
	currentAns = 0
	add = True
	dp = [[-10 ** 6, -10 ** 6, -10 ** 6, -10 ** 6] for i in range(n + 1)]
	dp[0][0] = a[0]
	dp[0][1] = 0
	for i in range(n):
		takeAdd = max(dp[i][1], dp[i][2]) + a[i]
		takeSubtract = max(dp[i][0], dp[i][3]) - a[i]
		dp[i + 1][0] = takeAdd
		dp[i + 1][1] = max(dp[i][1], dp[i][2])
		dp[i + 1][2] = takeSubtract
		dp[i + 1][3] = max(dp[i][0], dp[i][3])
	print(max(dp[-1]))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	ar = list(map(int, input().split()))
	prev = ar[0]
	sum1 = []
	dec = 1
	inc = 0
	for i in range(1, n):
		if ar[i] < prev and dec == 1:
			sum1.append(prev)
			dec = 0
		elif ar[i] > prev and dec == 0:
			sum1.append(prev)
			dec = 1
		prev = ar[i]
	if len(sum1) != 0:
		if prev > sum1[-1]:
			sum1.append(prev)
	else:
		sum1.append(ar[-1])
	ans = 0
	for i in range(len(sum1)):
		if i % 2 == 0:
			ans += sum1[i]
		else:
			ans -= sum1[i]
	print(ans)


import sys

def big(i):
	global a
	return a[i] > a[i - 1] and a[i] >= a[i + 1]

def little(i):
	global a
	return a[i] <= a[i - 1] and a[i] < a[i + 1]

def make(a, i, x):
	global s
	if big(i):
		s -= a[i]
	if little(i):
		s += a[i]
	if big(i - 1):
		s -= a[i - 1]
	if little(i - 1):
		s += a[i - 1]
	if big(i + 1):
		s -= a[i + 1]
	if little(i + 1):
		s += a[i + 1]
	a[i] = x
	if big(i):
		s += a[i]
	if little(i):
		s -= a[i]
	if big(i - 1):
		s += a[i - 1]
	if little(i - 1):
		s -= a[i - 1]
	if big(i + 1):
		s += a[i + 1]
	if little(i + 1):
		s -= a[i + 1]
t = int(input())
for I in range(t):
	(n, q) = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	a.append(0)
	a.append(0)
	up = True
	s = 0
	for i in range(n):
		if up:
			if a[i] > a[i - 1]:
				s += a[i] - a[i - 1]
			else:
				up = False
		elif a[i] > a[i - 1]:
			up = True
			s += a[i] - a[i - 1]
	print(s)
	for i in range(q):
		(l, r) = map(int, sys.stdin.readline().split())
		l -= 1
		r -= 1
		ll = a[l]
		rr = a[r]
		make(a, l, rr)
		make(a, r, ll)
		print(s)


def maxendingeven(i):
	if dpe[i] != None:
		return dpe[i]
	else:
		dpe[i] = max(maxendingeven(i - 1), maxendingodd(i - 1) - given[i - 1])
		return dpe[i]

def maxendingodd(i):
	if dpo[i] != None:
		return dpo[i]
	else:
		dpo[i] = max(maxendingeven(i - 1) + given[i - 1], maxendingodd(i - 1))
		return dpo[i]
T = int(input())
for t in range(T):
	waste = input()
	given = list(map(int, input().split()))
	n = len(given)
	dpe = [None] * (n + 1)
	dpo = [None] * (n + 1)
	dpe[1] = 0
	dpo[1] = given[0]
	if n > 999:
		c = 500
		while c < n:
			maxendingodd(c)
			c = c + 500
	print(maxendingodd(n))


from math import inf as _inf
import sys as _sys
_NEGATIVE_INFINITY = -_inf

def main():
	t = int(input())
	for i_t in range(t):
		(n, q) = _read_ints()
		a = tuple(_read_ints())
		swaps = (_read_ints() for i_q in range(q))
		swaps = ((l - 1, r - 1) for (l, r) in swaps)
		results = find_max_army_powers_after_swaps(a, swaps)
		print(*results, sep='\n')

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split(' '))

def find_max_army_powers_after_swaps(units_powers, swaps):
	units_powers = list(units_powers)
	units_powers = 2 * [_NEGATIVE_INFINITY] + units_powers + 2 * [_NEGATIVE_INFINITY]
	result = sum((_find_contribution(x1, x2, x3) for (x1, x2, x3) in zip(units_powers, units_powers[1:], units_powers[2:])))
	yield result
	for (i_a, i_b) in swaps:
		i_a += 2
		i_b += 2
		indices_may_be_affected = {i_a - 1, i_a, i_a + 1, i_b - 1, i_b, i_b + 1}
		contribution_before = 0
		for i in indices_may_be_affected:
			contribution_before += _find_contribution(units_powers[i - 1], units_powers[i], units_powers[i + 1])
		(units_powers[i_a], units_powers[i_b]) = (units_powers[i_b], units_powers[i_a])
		contribution_after = 0
		for i in indices_may_be_affected:
			contribution_after += _find_contribution(units_powers[i - 1], units_powers[i], units_powers[i + 1])
		result -= contribution_before
		result += contribution_after
		yield result

def _find_contribution(x1, x2, x3):
	if x1 < x2 > x3:
		result = x2
	elif x1 > x2 < x3:
		result = -x2
	else:
		result = 0
	return result
main()


for i in range(int(input())):
	(n, q) = [int(x) for x in input().split()]
	data = [int(x) for x in input().split()]
	cnt = data[0]
	uvel = 1
	mi = 0
	for j in range(1, n):
		if uvel:
			if data[j] > data[j - 1]:
				cnt += data[j] - data[j - 1]
			else:
				uvel = 0
				mi = data[j]
		elif data[j] < data[j - 1]:
			mi = data[j]
		else:
			cnt -= mi - data[j]
			mi = 0
			uvel = 1
	print(cnt)


testcases = int(input())
for testcase in range(testcases):
	temparr = input()
	temparr = temparr.split()
	n = int(temparr[0])
	nnnnn = int(temparr[1])
	temparr = input()
	temparr = temparr.split()
	arr = []
	for i in temparr:
		arr.append(int(i))
	temparr = [0 for _ in range(n)]
	dp = [temparr[:] for _ in range(2)]
	for (i, elem) in enumerate(arr):
		if i == 0:
			dp[1][0] = elem
			continue
		dp[0][i] = max(dp[1][i - 1] - elem, 0, dp[0][i - 1])
		dp[1][i] = max(dp[0][i - 1] + elem, dp[1][i - 1])
	firstrow = max(dp[0])
	secondrow = max(dp[1])
	print(max(firstrow, secondrow))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = [*map(int, input().split())]
	dp = [(0, 0) for i in range(n)]
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			dp[i] = (a[i], -a[i])
			continue
		elif i == n - 2:
			dp[i] = (max(a[i], a[i + 1]), max(-a[i] + dp[i + 1][0], dp[i + 1][1]))
			continue
		else:
			dp[i] = (max(a[i], a[i] + dp[i + 1][1], dp[i + 1][0]), max(-a[i], -a[i] + dp[i + 1][0], dp[i + 1][1]))
	print(max(dp[0][0], dp[0][1]))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	(values, dp_ones, dp_twos) = ([int(i) for i in input().split()], [-float('inf')], [0])
	for i in range(n):
		dp_ones.append(max(dp_ones[-1], dp_twos[-1] + values[i]))
		dp_twos.append(max(dp_twos[-1], dp_ones[-1] - values[i]))
	print(max(dp_ones[-1], dp_twos[-1]))


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	(n, q) = neo()
	A = Neo()
	(t, f, Ans) = (A[0], 0, [])
	for i in A[1:]:
		if f == 0:
			if i > t:
				t = i
			else:
				Ans += [t]
				f = 1
				t = i
		elif i < t:
			t = i
		else:
			Ans += [t]
			t = i
			f = 0
	if f == 0:
		Ans += [t]
	A = Ans[0::2]
	B = Ans[1::2]
	print(sum(A) - sum(B))


import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
import sys
letters = ascii_letters[:26]
input = stdin.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	extremums = []
	for i in range(n):
		if (i == 0 or arr[i - 1] > arr[i]) and (i == n - 1 or arr[i] < arr[i + 1]):
			extremums.append(arr[i])
		elif (i == 0 or arr[i - 1] < arr[i]) and (i == n - 1 or arr[i] > arr[i + 1]):
			extremums.append(arr[i])
	res = 0
	turn = True
	m = len(extremums)
	for i in range(m - (1 - m % 2)):
		if turn:
			res += extremums[i]
		else:
			res -= extremums[i]
		turn = not turn
	bf = 0
	turn = True
	for i in range(1, m // 2 * 2):
		if turn:
			bf += extremums[i]
		else:
			bf -= extremums[i]
		turn = not turn
	print(max(res, bf))


t = int(input())
while t > 0:
	(n, q) = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	flip = 0
	ans = 0
	i = 0
	take = 0
	ma = 0
	mi = n + 1
	while i < n:
		if flip == 0 and l[i] > ma:
			ma = l[i]
			if i == n - 1:
				ans += ma - take
			i += 1
		elif flip == 0 and l[i] < ma:
			ans += ma
			ma = 0
			flip = 1
		elif flip == 1 and l[i] < mi:
			mi = l[i]
			i += 1
		else:
			ans -= mi
			mi = n + 1
			flip = 0
			take = mi
	if flip == 0:
		print(ans + take)
	else:
		print(ans)
	t -= 1


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	d = []
	for i in range(1, n - 1):
		if a[i - 1] > a[i] and a[i + 1] > a[i]:
			d.append((a[i], -1))
		elif a[i - 1] < a[i] and a[i + 1] < a[i]:
			d.append((a[i], 1))
	if len(d) == 0:
		d = [(max(a[0], a[-1]), 1)]
	else:
		if d[0][1] == -1:
			d = [(a[0], 1)] + d
		if d[-1][1] == -1:
			d += [(a[n - 1], 1)]
	print(sum([x * (-1) ** i for (i, (x, y)) in enumerate(d)]))


def get_input():
	return input().split(' ')

def get_int_input():
	return list(map(int, get_input()))

def pokemon_army1(n, nums):
	if n == 1:
		return nums[0]
	nums = [-1] + nums + [-1]
	ans = 0
	max_elem = 0
	going = 'up'
	for i in range(n + 1):
		if going == 'up':
			if nums[i] > nums[i + 1]:
				ans += nums[i]
				going = 'down'
		elif going == 'down':
			if nums[i] < nums[i + 1]:
				ans -= nums[i]
				going = 'up'
	return ans

def main():
	T = get_int_input()[0]
	for _ in range(T):
		(n, q) = get_int_input()
		nums = get_int_input()
		print(pokemon_army1(n, nums))
main()


import math
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split(' ')))
	a = list(map(int, input().split(' ')))
	dp1 = [-1000000.0]
	dp2 = [0]
	for i in a:
		dp1.append(max(dp1[-1], dp2[-1] + i))
		dp2.append(max(dp2[-1], dp1[-1] - i))
	print(max(dp1[-1], dp2[-1]))


for i in range(int(input())):
	k = [int(x) for x in input().split()]
	b = []
	a = [int(x) for x in input().split()]
	count = 0
	for i in range(len(a)):
		if i <= len(a) - 2:
			if count % 2 == 0:
				if a[i + 1] < a[i]:
					b.append(a[i])
					count = count + 1
			if count % 2 == 1:
				if a[i + 1] > a[i]:
					b.append(a[i])
					count = count + 1
	if len(b) % 2 == 0:
		b.append(a[-1])
	su = 0
	for i in range(len(b)):
		if i % 2 == 0:
			su = su + b[i]
		else:
			su = su - b[i]
	print(su)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	l = list(map(int, input().split(' ')))
	mi = []
	ma = []
	if n < 3:
		print(max(l))
	else:
		if l[0] > l[1]:
			ma.append(l[0])
		for i in range(1, n - 1):
			if l[i] > l[i - 1] and l[i] > l[i + 1]:
				ma.append(l[i])
			elif l[i] < l[i - 1] and l[i] < l[i + 1]:
				mi.append(l[i])
		if l[-1] > l[-2]:
			ma.append(l[-1])
		if not ma and (not mi):
			print(l[0])
		else:
			print(sum(ma) - sum(mi))


T = int(input())
while T > 0:
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	plus_dp = [0] * n
	minus_dp = [0] * n
	plus_dp[0] = arr[0]
	for i in range(1, n):
		plus_dp[i] = max(plus_dp[i - 1], minus_dp[i - 1] + arr[i])
		minus_dp[i] = max(minus_dp[i - 1], plus_dp[i - 1] - arr[i])
	res = max(minus_dp[n - 1], plus_dp[n - 1])
	print(res)
	T -= 1


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input()

def msi():
	return map(str, input().split())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x * y // gcd(x, y)
mod = 1000000007

def modInverse(b, m):
	g = gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def modu(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m
from math import log, sqrt, factorial, cos, tan, sin, radians, floor, log2
import bisect
from decimal import *
getcontext().prec = 8
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def mss(a):
	cur = 0
	mx = 0
	for i in range(len(a)):
		mx += a[i]
		if mx < 0:
			mx = 0
		elif cur < mx:
			cur = mx
	return cur

def main():
	for _ in range(ii()):
		(n, q) = mi()
		a = li()
		ans = 0
		f = 1
		for i in range(n - 1):
			if f:
				if a[i + 1] < a[i]:
					ans += a[i]
					f ^= 1
			elif a[i + 1] > a[i]:
				ans -= a[i]
				f ^= 1
		if f:
			ans += a[n - 1]
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	val = a[0]
	for i in range(n - 1):
		val += max(0, a[i + 1] - a[i])
	print(val)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	p = 1
	s = 0
	for i in range(n - 1):
		if p == 1 and a[i] > a[i + 1]:
			s += a[i]
			p = 0
		elif p == 0 and a[i] < a[i + 1]:
			s -= a[i]
			l = a[i]
			p = 1
	if p == 1:
		s += a[-1]
	print(s)


import sys
input = sys.stdin.readline

def main():
	(n, q) = map(int, input().split())
	alst = [0, 0] + list(map(int, input().split())) + [0, 0]
	pm = [0 for _ in range(n + 4)]
	ans = 0
	for i in range(2, n + 2):
		if alst[i - 1] < alst[i] and alst[i + 1] <= alst[i]:
			pm[i] = 1
			ans += alst[i]
		elif alst[i - 1] > alst[i] and alst[i + 1] >= alst[i]:
			pm[i] = -1
			ans -= alst[i]
	print(ans)
	for _ in range(q):
		(l, r) = map(int, input().split())
		l += 1
		r += 1
		lr_s = set()
		for lr in [l, r]:
			for i in range(lr - 1, lr + 2):
				lr_s.add(i)
		for i in lr_s:
			ans -= alst[i] * pm[i]
		(alst[l], alst[r]) = (alst[r], alst[l])
		for i in lr_s:
			if alst[i - 1] < alst[i] and alst[i + 1] <= alst[i]:
				pm[i] = 1
				ans += alst[i]
			elif alst[i - 1] > alst[i] and alst[i + 1] >= alst[i]:
				pm[i] = -1
				ans -= alst[i]
			else:
				pm[i] = 0
		print(ans)
for _ in range(int(input())):
	main()


t = int(input())
for i in range(t):
	(n, g) = map(int, input().strip(' ').split(' '))
	a = list(map(int, input().strip(' ').split(' ')))
	ans = 0
	h = 0
	l = 0
	for i in range(n - 1):
		if h == 0:
			if a[i] > a[i + 1]:
				ans += a[i]
				h = 1
		elif a[i] < a[i + 1]:
			ans -= a[i]
			h = 0
	if h == 0:
		ans += a[n - 1]
	print(ans)


for i in range(int(input())):
	(n, q) = map(int, input().split())
	a = [int(x) for x in input().split()]
	dp = [0 for j in range(n + 1)]
	dp12 = [0 for j in range(n + 1)]
	(x, y) = (0, 0)
	for j in range(1, n + 1):
		dp[j] = x + a[j - 1]
		dp12[j] = y - a[j - 1]
		y = max(y, dp[j])
		x = max(dp12[j], x)
	print(max(x, y))


for t in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	a.append(-1)
	a = [-1] + a
	count = 0
	for i in range(1, n + 1):
		if a[i] > a[i + 1] and a[i] >= a[i - 1]:
			count += a[i]
		elif a[i] < a[i + 1] and a[i] <= a[i - 1]:
			count -= a[i]
	print(count)


import math
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ma = -1
	mi = 1000000000.0
	c = 0
	flag = 0
	ans = []
	for i in range(n):
		if flag == 0:
			if ma < a[i]:
				ma = a[i]
			elif ma > a[i]:
				c += ma
				ans.append(ma)
				flag = 1
				mi = a[i]
				ma = -1
		elif mi > a[i]:
			mi = a[i]
		elif mi < a[i]:
			c -= mi
			ans.append(mi)
			flag = 0
			ma = a[i]
			mi = 1000000000.0
	if flag == 0:
		c += a[n - 1]
	print(c)


import sys
import math as m

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def sinp():
	return sys.stdin.readline().strip()

def iinp():
	return int(input())

def ninp():
	return map(int, sys.stdin.readline().strip().split())

def linp():
	return list(map(int, sys.stdin.readline().strip().split()))

def p(a):
	print(a)
for _ in range(iinp()):
	(n, q) = ninp()
	a = linp()
	s = 0
	f = 0
	if n <= 2:
		print(max(a))
	else:
		for i in range(len(a) - 1):
			if a[i] < a[i + 1] and f == 0:
				pass
			elif a[i] > a[i + 1] and f == 0:
				s = s + a[i]
				f = 1
			elif a[i] < a[i + 1] and f == 1:
				s = s - a[i]
				f = 0
			elif a[i] > a[i + 1] and f == 1:
				pass
		if f == 0:
			s = s + a[len(a) - 1]
		print(s)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	(x, k) = (0, 0)
	for i in range(n):
		x += max(a[i] - k, 0)
		(x, k) = (max(x, a[i]), a[i])
	print(x)


from collections import defaultdict
from queue import deque
from math import log2

def arrinp():
	return [*map(int, input().split(' '))]

def mulinp():
	return map(int, input().split(' '))

def intinp():
	return int(input())

def fun(a, n):
	dp1 = [0] * n
	dp2 = [0] * n
	max1 = 0
	max2 = 0
	for i in range(n):
		if i == 0:
			dp1[0] = a[0]
			max1 = a[0]
		else:
			dp1[i] = max2 + a[i]
			dp2[i] = max1 - a[i]
			max1 = max(max1, dp1[i])
			max2 = max(max2, dp2[i])
	return max1

def solution():
	(n, q) = mulinp()
	a = arrinp()
	print(fun(a, n))
	for i in range(q):
		(l, r) = mulinp()
		l -= 1
		r -= 1
		(a[l], a[r]) = (a[r], a[l])
		print(fun(a, n))
testcases = 1
testcases = int(input())
for _ in range(testcases):
	solution()


t = int(input())
for i in range(0, t):
	a = input().split()
	n = int(a[0])
	b = list(map(int, input().split()))
	maxno = 0
	next = 0
	minno = 0
	maxi = 0
	if len(b) == 1:
		print(b[0])
	else:
		for j in range(0, n):
			if j > 0 and j < n - 1:
				if (b[j] >= b[j + 1] and b[j] >= b[j - 1]) and next == 0:
					if b[j] - minno > 0:
						maxno = b[j]
						maxi = maxi + maxno - minno
						next = 1
				elif (b[j] >= b[j + 1] and b[j] >= b[j - 1]) and next == 1:
					if b[j] > maxno:
						maxi = maxi - maxno + b[j]
				elif (b[j] <= b[j + 1] and b[j] <= b[j - 1]) and next == 1:
					minno = b[j]
					next = 0
				elif (b[j] <= b[j + 1] and b[j] <= b[j - 1]) and next == 0:
					if b[j] < minno:
						minno = b[j]
			elif j == 0:
				if b[0] > b[1]:
					maxno = b[0]
					maxi = maxi + maxno
					next = 1
			elif j == n - 1:
				if next == 0 and b[n - 1] > b[n - 2]:
					if b[j] - minno > 0:
						maxi = maxi + b[j] - minno
				elif next == 1 and b[n - 1] > b[n - 2]:
					if b[j] > maxno:
						maxi = maxi - maxno + b[j]
		print(maxi)


from sys import stdin, stdout, setrecursionlimit
import threading

class Tail_Recursion_Optimization:

	def __init__(self, RECURSION_LIMIT, STACK_SIZE):
		setrecursionlimit(RECURSION_LIMIT)
		threading.stack_size(STACK_SIZE)
		return None

class SOLVE:
	a = n = q = None
	cnt = 0

	def DP(self, pos, sym, dp):
		self.cnt += 1
		if pos == self.n:
			return 0
		if dp[pos][sym] != None:
			return dp[pos][sym]
		total1 = total2 = 0
		total1 += [-1, 1][sym] * self.a[pos] + self.DP(pos + 1, sym ^ 1, dp)
		total2 += self.DP(pos + 1, sym, dp)
		dp[pos][sym] = max(total1, total2)
		return dp[pos][sym]

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(R())):
			(self.n, self.q) = [int(x) for x in R().split()]
			self.a = [int(x) for x in R().split()]
			dp = [[None, None] for j in range(self.n)]
			ans.append(str(self.DP(0, 1, dp)))
		W('\n'.join(ans))
		return 0

def main():
	s = SOLVE()
	s.solve()
Tail_Recursion_Optimization(10 ** 7, 80 * 1024 ** 2)
threading.Thread(target=main).start()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	lis = list(map(int, input().split()))
	hi = []
	lo = []
	if n <= 2:
		print(max(lis))
	else:
		if lis[0] > lis[1]:
			hi.append(lis[0])
		for i in range(1, n - 1, 1):
			if lis[i] > lis[i - 1] and lis[i] > lis[i + 1]:
				hi.append(lis[i])
			elif lis[i] < lis[i - 1] and lis[i] < lis[i + 1]:
				lo.append(lis[i])
		if lis[-1] > lis[-2]:
			hi.append(lis[-1])
		print(sum(hi) - sum(lo))


t = int(input())
for u in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	flag = True
	for i in range(n - 1):
		if a[i] > a[i + 1] and flag:
			ans += a[i]
			flag = False
		elif a[i + 1] > a[i] and ans - a[i] > 0 and (not flag):
			ans -= a[i]
			flag = True
	if flag:
		ans += a[-1]
	print(ans)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 9 + 7
LOGN = 30
T = 1
T = int(stdin.readline())
for _ in range(T):
	(n, q) = list(map(int, stdin.readline().rstrip().split()))
	a = list(map(int, stdin.readline().rstrip().split()))
	p1 = []
	p2 = []
	p3 = []
	p4 = []
	p2.append(a[0])
	p4.append(a[0])
	for i in range(1, n - 1):
		if a[i] > a[i - 1] and a[i] > a[i + 1]:
			p1.append(a[i])
			p2.append(a[i])
			p3.append(a[i])
			p4.append(a[i])
		if a[i] < a[i - 1] and a[i] < a[i + 1]:
			p1.append(a[i])
			p2.append(a[i])
			p3.append(a[i])
			p4.append(a[i])
	p3.append(a[-1])
	p4.append(a[-1])
	(ans1, ans2, ans3, ans4) = (0, 0, 0, 0)
	for i in range(len(p1)):
		if i % 2 == 0:
			ans1 += p1[i]
		else:
			ans1 -= p1[i]
	for i in range(len(p2)):
		if i % 2 == 0:
			ans2 += p2[i]
		else:
			ans2 -= p2[i]
	for i in range(len(p3)):
		if i % 2 == 0:
			ans3 += p3[i]
		else:
			ans3 -= p3[i]
	for i in range(len(p4)):
		if i % 2 == 0:
			ans4 += p4[i]
		else:
			ans4 -= p4[i]
	print(max(ans1, ans2, ans3, ans4))


import bisect
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	i = 0
	ans = 0
	c = arr[0]
	i += 1
	if n == 1:
		print(arr[0])
		continue
	while i < n:
		while i < n and c <= arr[i]:
			c = arr[i]
			i += 1
		ans += c
		while i < n and c >= arr[i]:
			c = arr[i]
			i += 1
		if i <= n - 1:
			ans -= c
	print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	i = 0
	c = 0
	k = 1
	for i in range(1, n):
		if k % 2 == 1:
			if l[i] < l[i - 1]:
				c += l[i - 1]
				k += 1
			elif i == n - 1:
				c += l[i]
		elif l[i] > l[i - 1]:
			c -= l[i - 1]
			k += 1
			if i == n - 1:
				c += l[i]
	if len(l) == 1:
		c = l[0]
	print(c)


import math
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split(' ')))
	a = list(map(int, input().split(' ')))
	ans = 0
	if n == 1:
		print(a[0])
	elif n == 2:
		print(max(a[0], a[1]))
	else:
		if a[0] > a[1]:
			ans += a[0]
		for i in range(1, n - 1):
			if a[i] > a[i - 1] and a[i] > a[i + 1]:
				ans += a[i]
			elif a[i] < a[i - 1] and a[i] < a[i + 1]:
				ans -= a[i]
		if a[-1] > a[-2]:
			ans += a[-1]
		print(ans)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	dp1 = [a[0]]
	dp2 = [0]
	for i in range(n):
		dp2.append(max(dp2[-1], dp1[-1] - a[i]))
		dp1.append(max(dp1[-1], dp2[-2] + a[i]))
	print(max(dp1[-1], dp2[-1]))


for tt in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	ptr = 0
	ans = l[0]
	m = True
	fans = l[0]
	for i in range(1, n):
		if m and l[i] >= l[ptr]:
			ans += l[i]
			ans -= l[ptr]
			ptr = i
		elif m and l[i] < l[ptr]:
			ans -= l[i]
			m = False
			ptr = i
		elif l[i] >= l[ptr]:
			ans += l[i]
			m = True
			ptr = i
		else:
			ans -= l[i]
			ans += l[ptr]
			ptr = i
		fans = max(ans, fans)
	print(fans)


from math import inf, log2

class SegmentTree:

	def __init__(self, array, func=max):
		self.n = len(array)
		self.size = 2 ** (int(log2(self.n - 1)) + 1) if self.n != 1 else 1
		self.func = func
		self.default = 0 if self.func != min else inf
		self.data = [self.default] * (2 * self.size)
		self.process(array)

	def process(self, array):
		self.data[self.size:self.size + self.n] = array
		for i in range(self.size - 1, -1, -1):
			self.data[i] = self.func(self.data[2 * i], self.data[2 * i + 1])

	def query(self, alpha, omega):
		if alpha == omega:
			return self.data[alpha + self.size]
		res = self.default
		alpha += self.size
		omega += self.size + 1
		while alpha < omega:
			if alpha & 1:
				res = self.func(res, self.data[alpha])
				alpha += 1
			if omega & 1:
				omega -= 1
				res = self.func(res, self.data[omega])
			alpha >>= 1
			omega >>= 1
		return res

	def update(self, index, value):
		index += self.size
		self.data[index] = value
		index >>= 1
		while index:
			self.data[index] = self.func(self.data[2 * index], self.data[2 * index + 1])
			index >>= 1
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def sign(x):
	if x > 0:
		return 1
	else:
		return -1

def findMaxSum(arr, size):
	max_sum = 0
	i = 0
	while i < size:
		pres = arr[i]
		j = i
		while j < size and sign(arr[i]) == sign(arr[j]):
			pres = max(pres, arr[j])
			j += 1
		max_sum = max_sum + pres
		i = j - 1
		i += 1
	return max_sum
for _ in range(int(input()) if True else 1):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	st = SegmentTree(a, func=min)
	dp = [0] * (n + 69)
	dp2 = [-inf] * (n + 69)
	(c1, c2) = (0, 0)
	for i in range(n):
		dp[i] = max(a[i], c2 + a[i])
		dp2[i] = max(-a[i], c1 - a[i])
		(c1, c2) = (max(c1, dp[i]), max(c2, dp2[i]))
	print(max(dp + dp2))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
from collections import defaultdict as dc
for _ in range(N()):
	(n, q) = RL()
	a = RLL()
	(add, minus) = (a[0], 0)
	for i in range(1, n):
		(add, minus) = (max(add, minus + a[i]), max(minus, add - a[i]))
	print(max(minus, add))


t = int(input())
for _ in range(t):
	[n, q] = list(map(int, input().split()))
	numbers = list(map(int, input().split()))
	add = [0 for i in range(n)]
	sub = [0 for i in range(n)]
	add[0] = numbers[0]
	for i in range(1, n):
		add[i] = max(add[i - 1], sub[i - 1] + numbers[i])
		sub[i] = max(sub[i - 1], add[i - 1] - numbers[i])
	print(add[n - 1])


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(n, q) = list(map(int, stdin.readline().split()))
	a = list(map(int, stdin.readline().split()))
	possum = negsum = p = 0
	while p < n:
		mx = 0
		mn = float('inf')
		while p < n and a[p] > mx:
			mx = a[p]
			p += 1
		possum += mx
		if p >= n:
			break
		mn = a[p]
		while p < n and a[p] < mn:
			mn = a[p]
			p += 1
		negsum += mn
	print(possum - negsum)


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter
from pprint import pprint

def main():
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	pos = 0
	neg = -float('inf')
	for i in range(n):
		pos = max(pos, neg - arr[i])
		neg = max(neg, pos + arr[i])
	print(max(pos, neg))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	main()


from sys import stdin, stdout

class Input:

	def __init__(self):
		self.lines = stdin.read().splitlines()
		self.idx = 0

	def line(self):
		try:
			return self.lines[self.idx]
		finally:
			self.idx += 1

	def array(self, sep=' ', cast=int):
		return list(map(cast, self.line().split(sep=sep)))

	def known_tests(self):
		(num_of_cases,) = self.array()
		for case in range(num_of_cases):
			yield self

	def unknown_tests(self):
		while self.idx < len(self.lines):
			yield self

def problem_solver():
	oo = float('Inf')

	def solver(inpt):
		(n, q) = inpt.array()
		a = inpt.array()
		dp = [-oo, 0]
		ans = 0
		for i in range(n):
			ndp = [max(a[i], dp[0], a[i] + dp[1]), max(dp[1], dp[0] - a[i])]
			ans = max(ans, *ndp)
			dp = ndp
		print(ans)
	return solver
try:
	solver = problem_solver()
	for tc in Input().known_tests():
		solver(tc)
except Exception as e:
	import traceback
	traceback.print_exc(file=stdout)


def solve():
	(n, q) = map(int, input().strip().split())
	a = list(map(int, input().strip().split()))
	dp = [[0 for i in range(2)] for i in range(n)]
	dp[0][0] = a[0]
	dp[0][1] = 0
	max_0_till = a[0]
	max_1_till = 0
	ans = a[0]
	for i in range(1, n):
		best_ans = a[i] + max_1_till
		dp[i][0] = best_ans
		max_0_till = max(max_0_till, dp[i][0])
		best_ans = max_0_till - a[i]
		dp[i][1] = best_ans
		max_1_till = max(max_1_till, dp[i][1])
	print(max_0_till)
t = int(input().strip())
for _ in range(t):
	solve()


import sys

def main():
	iter_data = map(int, sys.stdin.read().split())
	t = next(iter_data)
	for t_it in range(t):
		(n, q) = (next(iter_data), next(iter_data))
		a = [0] + [next(iter_data) for _ in range(n)] + [0]
		all_ans = [0] * (q + 1)
		val = [0] * (n + 1)
		for i in range(n + 1):
			val[i] = max(a[i], a[i + 1])
		cur_ans = sum(val) - sum(a)
		all_ans[0] = cur_ans
		for q_it in range(1, q + 1):
			(l, r) = (next(iter_data), next(iter_data))
			if l != r:
				(a[l], a[r]) = (a[r], a[l])
				cur_ans -= val[l - 1] + val[l] + val[r]
				val[l - 1] = max(a[l - 1], a[l])
				val[l] = max(a[l], a[l + 1])
				val[r] = max(a[r], a[r + 1])
				cur_ans += val[l - 1] + val[l] + val[r]
				if r - l > 1:
					cur_ans -= val[r - 1]
					val[r - 1] = max(a[r - 1], a[r])
					cur_ans += val[r - 1]
			all_ans[q_it] = cur_ans
		print(*all_ans, sep='\n')
main()


import math
import sys

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	(n, q) = Read.list_int()
	a = Read.list_int()
	lr = []
	for i in range(q):
		(l, r) = Read.list_int()
		lr.append({l: l, r: r})
	if n == 1:
		print(a[0])
		return
	tmp = {}
	idx = 0
	tmp[idx] = [a[0]]
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			tmp[idx].append(a[i + 1])
		elif a[i] < a[i + 1]:
			idx = idx + 1
			tmp[idx] = [a[i + 1]]
	res = 0
	lastmx = 0
	for v in tmp.values():
		if len(v) >= 2:
			mn = min(v)
			mx = max(v)
			res = res + mx - mn
			lastmn = mn
			lastmx = 0
		elif v[0] > lastmx:
			lastmx = v[0]
	if lastmx > 0:
		res = res + lastmx
	else:
		res = res + lastmn
	mxa = max(a)
	if res < mxa:
		res = mxa
	print(res)
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = [int(c) for c in input().split()]
	dp1 = [0 for i in range(n + 1)]
	dp2 = [0 for i in range(n + 1)]
	dp1[0] = -1
	for i in range(n):
		dp1[i + 1] = max(dp1[i], dp2[i] + l[i])
		dp2[i + 1] = max(dp2[i], dp1[i] - l[i])
	print(max(dp1[n], dp2[n]))


import math
t = int(input())
for j in range(0, t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	k = 1
	p = 0
	for l in range(0, n - 1):
		if k == 1:
			if a[l] > a[l + 1]:
				p = p + a[l]
				k = -1
		elif a[l] < a[l + 1]:
			p = p - a[l]
			k = 1
	if k == 1:
		p = p + a[-1]
	print(p)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
from itertools import permutations as permu

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = 1
num = int(z())
for _ in range(num):
	(n, m) = zz()
	arr = zz()
	ans = 0
	p = arr[-1]
	c = 0
	for i in range(2, n + 1):
		if p > arr[-i] and c == 0:
			ans += p
			c = 1
		if p < arr[-i] and c == 1:
			ans -= p
			c = 0
		p = arr[-i]
	if c == 0:
		ans += arr[0]
	print(ans)


for _ in ' ' * int(input()):
	r = lambda : map(int, input().split())
	(a, b) = r()
	z = list(r())
	s = z[-1]
	i = 0
	while i < a - 1:
		while i < a - 1 and z[i + 1] >= z[i]:
			i += 1
		n = z[i]
		while i < a - 1 and z[i] > z[i + 1]:
			i += 1
		s += n - z[i]
	print(s)


import collections
import math

def solution(n, q, a):
	if n == 1:
		return a[0]
	(odd, even) = ([0] * n, [0] * n)
	odd[0] = a[0]
	odd[1] = a[1]
	even[1] = a[0] - a[1]
	(me, mo) = (max(even[1], 0), max(odd[0], odd[1]))
	for i in range(2, n):
		odd[i] = me + a[i]
		even[i] = mo - a[i]
		if me < even[i]:
			me = even[i]
		if mo < odd[i]:
			mo = odd[i]
	return max(odd)
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split(' '))
	a = list(map(int, input().split(' ')))
	print(solution(n, q, a))


import sys
import math
import collections
import bisect
import itertools
import decimal
import copy
import heapq
INF = 10 ** 20
MOD = 998244353
ni = lambda : int(sys.stdin.buffer.readline())
ns = lambda : map(int, sys.stdin.buffer.readline().split())
na = lambda : list(map(int, sys.stdin.buffer.readline().split()))
na1 = lambda : list(map(lambda x: int(x) - 1, sys.stdin.buffer.readline().split()))
flush = lambda : sys.stdout.flush()

def main():
	t = ni()
	for _ in range(t):
		(n, q) = ns()
		a = [-INF] + na() + [-INF]
		ans = 0
		for i in range(1, n + 1):
			if a[i - 1] < a[i] and a[i] > a[i + 1]:
				ans += a[i]
			elif a[i - 1] > a[i] and a[i] < a[i + 1]:
				ans -= a[i]
		print(ans)
		for _ in range(q):
			(l, r) = ns()
			for li in range(l - 2, l + 3):
				if 1 <= li <= n:
					if a[li - 1] < a[li] and a[li] > a[li + 1]:
						ans -= a[li]
					elif a[li - 1] > a[li] and a[li] < a[li + 1]:
						ans += a[li]
			for ri in range(max(l + 3, r - 2), r + 3):
				if 1 <= ri <= n:
					if a[ri - 1] < a[ri] and a[ri] > a[ri + 1]:
						ans -= a[ri]
					elif a[ri - 1] > a[ri] and a[ri] < a[ri + 1]:
						ans += a[ri]
			(a[l], a[r]) = (a[r], a[l])
			for li in range(l - 2, l + 3):
				if 1 <= li <= n:
					if a[li - 1] < a[li] and a[li] > a[li + 1]:
						ans += a[li]
					elif a[li - 1] > a[li] and a[li] < a[li + 1]:
						ans -= a[li]
			for ri in range(max(l + 3, r - 2), r + 3):
				if 1 <= ri <= n:
					if a[ri - 1] < a[ri] and a[ri] > a[ri + 1]:
						ans += a[ri]
					elif a[ri - 1] > a[ri] and a[ri] < a[ri + 1]:
						ans -= a[ri]
			print(ans)
main()


import math
for _ in range(int(input())):
	(n, q) = input().split()
	n = int(n)
	a = [int(x) for x in input().split()]
	dp_odd = [None] * (n + 1)
	dp_even = dp_odd[:]
	dp_odd[0] = -math.inf
	dp_even[0] = 0
	for i in range(n):
		dp_odd[i + 1] = max(dp_odd[i], dp_even[i] + a[i])
		dp_even[i + 1] = max(dp_even[i], dp_odd[i] - a[i])
	print(max(dp_odd[n], dp_even[n]))


def solveC(xs):
	best_power = 0
	curr_power_pos = 0
	curr_power_neg = xs[0]
	for x in xs:
		(curr_power_neg, curr_power_pos) = (max(curr_power_neg, curr_power_pos + x), max(curr_power_pos, curr_power_neg - x))
		best_power = max([best_power, curr_power_neg, curr_power_pos])
	return best_power
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	xs = [int(x) for x in input().split()]
	print(solveC(xs))


from math import inf

def main():
	for _ in range(int(input())):
		(n, q) = map(int, input().split())
		a = list(map(int, input().split()))
		dpOdd = [0] * (n + 1)
		dpEven = [0] * (n + 1)
		dpOdd[0] = -inf
		for i in range(n):
			dpOdd[i + 1] = max(dpOdd[i], dpEven[i] + a[i])
			dpEven[i + 1] = max(dpEven[i], dpOdd[i] - a[i])
		print(max(dpOdd[n], dpEven[n]))
main()


n = int(input())
for j in range(n):
	ans = 0
	peak = []
	trough = []
	(length, q) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	if length == 1:
		print(arr[0])
	else:
		if arr[0] > arr[1]:
			peak.append(0)
		for i in range(1, length - 1):
			if arr[i + 1] < arr[i] and arr[i] > arr[i - 1]:
				peak.append(i)
			elif arr[i + 1] > arr[i] and arr[i - 1] > arr[i] and (len(peak) > 0):
				trough.append(i)
		if arr[length - 1] > arr[length - 2]:
			peak.append(len(arr) - 1)
		for p in peak:
			ans = ans + arr[p]
		for t in trough:
			ans = ans - arr[t]
		print(ans)


_ = int(input())
for t in range(_):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	x = []
	f = 0
	a2 = a.copy()
	a2.sort()
	a1 = a.copy()
	a1.sort()
	a1.reverse()
	if a1 == a:
		print(a[0])
	elif a2 == a:
		print(a[n - 1])
	else:
		for i in range(1, n):
			if f == 0:
				if a[i] > a[i - 1]:
					continue
				else:
					x.append(a[i - 1])
					f = 1
			elif a[i] < a[i - 1]:
				continue
			else:
				x.append(a[i - 1])
				f = 0
		if x[len(x) - 1] != a[n - 1]:
			if len(x) > 0:
				if len(x) % 2 == 0:
					x.append(a[n - 1])
		if len(x) == 1:
			print(*x)
		else:
			y = 0
			for i in range(0, len(x) - 1, 2):
				y += x[i] - x[i + 1]
			if len(x) % 2 != 0:
				y += x[len(x) - 1]
			print(y)


for T in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	(s, i) = (0, 1)
	while i < n and a[i - 1] <= a[i]:
		i += 1
	s += a[i - 1]
	while i < n:
		while i < n and a[i - 1] > a[i]:
			i += 1
		s -= a[i - 1]
		while i < n and a[i - 1] <= a[i]:
			i += 1
		s += a[i - 1]
	print(s)


t = int(input())
for p in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	l = []
	var = 1
	for j in range(n - 1):
		if var == 1:
			if a[j + 1] > a[j]:
				pass
			else:
				l.append(a[j])
				var = 0
		elif a[j + 1] < a[j]:
			pass
		else:
			l.append(a[j])
			var = 1
	if var == 1:
		l.append(a[n - 1])
	ans = []
	var = 0
	for j in range(len(l)):
		if j % 2 == 0:
			var += l[j]
		else:
			var -= l[j]
		ans.append(var)
	print(max(ans))


def process(n, li):
	li = [0] + li
	inc = [0]
	flag = 1
	dec = []
	for i in range(1, n + 1):
		if li[i] > li[i - 1] and flag == 1:
			flag = 1
			inc.pop()
			inc.append(li[i])
		elif li[i] > li[i - 1]:
			inc.append(li[i])
			flag = 1
		elif flag == 0 and li[i] < li[i - 1]:
			flag = 0
			dec.pop()
			dec.append(li[i])
		else:
			flag = 0
			dec.append(li[i])
	if len(inc) == len(dec):
		dec.pop()
	return sum(inc) - sum(dec)

def brute(n, li):
	res = 0
	for i in range(0, n):
		for j in range(i + 1, n):
			if li[i] & li[j] >= li[i] ^ li[j]:
				res += 1
	return res
tests = int(input())
for i in range(tests):
	(n, q) = list(map(int, input().split()))
	li = list(map(int, input().split()))
	print(process(n, li))


from sys import stdout, stdin
from collections import defaultdict, deque
import math
t = int(stdin.readline())
for _ in range(t):
	(n, q) = map(int, stdin.readline().split())
	l = list(map(int, stdin.readline().split()))
	l.insert(0, 0)
	l.append(0)
	pos = 0
	neg = 0
	for i in range(1, n + 1):
		if pos > 0:
			if l[i] < l[i - 1] and l[i] < l[i + 1]:
				neg += l[i]
		if neg > 0:
			if l[i] > l[i - 1] and l[i] > l[i + 1]:
				pos += l[i]
		if neg == 0:
			if l[i] > l[i - 1] and l[i] > l[i + 1]:
				if l[i] > pos:
					pos = l[i]
	print(pos - neg)


def countSwaps(A, n):
	f = 0
	ans = 0
	res = 0
	for i in range(1, n):
		if f == 0 and A[i] < A[i - 1]:
			ans = max(ans + A[i - 1], A[i - 1])
			res = max(res, ans)
			f = 1
			continue
		if f == 1 and A[i] > A[i - 1]:
			ans -= A[i - 1]
			res = max(res, ans)
			f = 0
	if f == 0:
		ans = max(ans + A[-1], A[-1])
		res = max(res, ans)
	return res
T = int(input())
while T:
	(N, Q) = map(int, input().split())
	A = list(map(int, input().split()))
	print(countSwaps(A, N))
	T -= 1


import sys
import math
import collections
import bisect
import itertools
import decimal
import copy
import heapq
INF = 10 ** 20
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline().rstrip())
ns = lambda : map(int, sys.stdin.readline().rstrip().split())
na = lambda : list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda : list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))
flush = lambda : sys.stdout.flush()

def main():
	t = ni()
	for _ in range(t):
		(n, q) = ns()
		a = na()
		swap = [na() for _ in range(q)]
		res = []
		a.insert(0, -INF)
		a.append(-INF)
		num = -INF
		status = 1
		for i in range(1, n + 2):
			if a[i] > a[i - 1]:
				if status:
					continue
				else:
					res.append(a[i - 1])
					status ^= 1
			elif status:
				res.append(a[i - 1])
				status ^= 1
		ans = 0
		for (i, ri) in enumerate(res):
			if i % 2:
				ans -= ri
			else:
				ans += ri
		ans = max(max(a), ans)
		print(ans)
main()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = []
	a = arr[0]
	f = 0
	for i in range(1, n):
		if f == 0:
			if arr[i] > a:
				a = arr[i]
			elif arr[i] < a:
				ans.append(a)
				a = arr[i]
				f = 1
		elif arr[i] < a:
			a = arr[i]
		elif arr[i] > a:
			ans.append(a)
			a = arr[i]
			f = 0
	ans.append(a)
	if len(ans) > 0 and len(ans) % 2 == 0:
		ans = ans[:len(ans) - 1]
	p = 0
	for i in range(len(ans)):
		if i % 2 == 0:
			p += ans[i]
		else:
			p -= ans[i]
	print(p)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	b = set(a)
	if len(b) == 1:
		print(a[0])
	else:
		if n > 1:
			if a[0] > a[1]:
				ans += a[0]
			if a[n - 1] > a[n - 2]:
				ans += a[n - 1]
		for i in range(1, n - 1):
			if a[i] > a[i - 1] and a[i] > a[i + 1]:
				ans += a[i]
			elif a[i] < a[i - 1] and a[i] < a[i + 1]:
				ans -= a[i]
		print(ans)


cases = int(input())
for t in range(cases):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	for i in range(q):
		(l, r) = list(map(int, input().split()))
	mv = max(a)
	a.append(0)
	a = [0] + a
	p = []
	for i in range(1, n + 1):
		if a[i] < a[i - 1] and a[i] < a[i + 1] or (a[i] > a[i - 1] and a[i] > a[i + 1]):
			p.append(a[i])
	mv = max(mv, sum(p[::2]) - sum(p[1::2]))
	print(mv)


import collections
for t in range(int(input())):
	(n, q) = map(int, input().split())
	num = list(map(int, input().split()))
	out = 0
	flag = 1
	for i in range(n - 1):
		if flag:
			if num[i] > num[i + 1]:
				out += num[i]
				flag = 0
		elif num[i] < num[i + 1]:
			out -= num[i]
			flag = 1
	if flag:
		out += num[-1]
	print(out)


T = int(input())
for _ in range(T):
	(N, Q) = map(int, input().split())
	A = list(map(int, input().split()))
	j = 0
	ans = 0
	curr = 0
	flag = 0
	if N == 1:
		print(A[0])
	elif N == 2:
		print(max(A))
	else:
		while j < N - 1:
			while A[j] < A[j + 1]:
				j += 1
				if j >= N - 1:
					flag = 1
					break
			curr += A[j]
			ans = max(ans, curr)
			if flag == 1:
				break
			while A[j] > A[j + 1]:
				j += 1
				if j >= N - 1:
					break
			curr = curr - A[j]
			ans = max(ans, curr)
		ans = max(ans, curr)
		print(ans)


t = int(input(''))
for _ in range(t):
	[n, q] = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	sol = 0
	sign = 0
	if n == 1:
		print(arr[0])
		continue
	if arr[0] > arr[1]:
		sol += arr[0]
		sign = 1 - sign
	for i in range(1, n - 1):
		if arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:
			sol += arr[i]
			sign = 1 - sign
		elif arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:
			sol -= arr[i]
			sign = 1 - sign
	if sign == 0:
		if n - 2 >= 0 and n - 1 >= 0:
			if arr[n - 1] > arr[n - 2]:
				sol += arr[n - 1]
	print(sol)


from collections import defaultdict as dd
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	flag = True
	ans = a[0]
	Max = a[0]
	Min = float('inf')
	for i in range(1, n):
		if flag:
			if a[i] > Max:
				(ans, Max) = (ans + a[i] - Max, a[i])
			elif a[i] < Max:
				Max = a[i]
				ans -= Max
				flag = False
		elif a[i] < Max:
			(ans, Max) = (ans + Max - a[i], a[i])
		elif a[i] > Max:
			Max = a[i]
			ans += Max
			flag = True
	if flag == False:
		ans += Max
	print(ans)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	(n, q) = ip()
	x = ip()
	if n == 1:
		print(*x)
		continue
	ans = 0
	if x[0] > x[1]:
		ans += x[0]
	if x[-1] > x[-2]:
		ans += x[-1]
	for i in range(1, n - 1):
		if x[i - 1] < x[i] > x[i + 1]:
			ans += x[i]
		elif x[i - 1] > x[i] < x[i + 1]:
			ans -= x[i]
	print(ans)


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	(n, q) = idata()
	data = [-1] + idata() + [-1]
	ans = 0
	min_max = [0] * (n + 2)
	for i in range(1, n + 1):
		if data[i - 1] < data[i] > data[i + 1]:
			ans += data[i]
			min_max[i] = 1
		if data[i - 1] > data[i] < data[i + 1]:
			ans -= data[i]
			min_max[i] = -1
	print(ans)
	for i in range(q):
		(l, r) = idata()
		sp = [l - 1, l, l + 1, r - 1, r, r + 1]
		sp = sorted(list(set(sp)))
		for j in range(len(sp)):
			ans -= min_max[sp[j]] * data[sp[j]]
		(data[r], data[l]) = (data[l], data[r])
		for j in range(len(sp)):
			if 0 < sp[j] < n + 1:
				if data[sp[j] - 1] < data[sp[j]] > data[sp[j] + 1]:
					min_max[sp[j]] = 1
				elif data[sp[j] - 1] > data[sp[j]] < data[sp[j] + 1]:
					min_max[sp[j]] = -1
				else:
					min_max[sp[j]] = 0
				ans += min_max[sp[j]] * data[sp[j]]
		print(ans)
	return
for t in range(int(ii())):
	solve()


test_cases = int(input())
for test_case in range(test_cases):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	now = 0
	arr = []
	plus = 0
	minus = 0
	for i in range(n - 1):
		diff = a[i] - a[i + 1]
		if now == 0:
			if diff > 0:
				arr.append(a[i])
				plus += a[i]
				now = 1
				continue
		elif diff < 0:
			arr.append(a[i])
			minus += a[i]
			now = 0
	if len(arr) % 2 == 0:
		arr.append(a[-1])
		plus += a[-1]
	print(plus - minus)


def pokemon(n, l):
	inc = False
	last = l[0]
	ans = 0
	if n == 1:
		return l[0]
	if l[1] > l[0]:
		inc = True
		last = l[1]
	else:
		inc = False
		ans += l[0]
		last = l[1]
	for i in range(2, n):
		if inc:
			if l[i] < last:
				ans += last
				last = l[i]
				inc = False
			else:
				last = l[i]
		elif l[i] > last:
			ans -= last
			last = l[i]
			inc = True
		else:
			last = l[i]
	if inc:
		ans += l[-1]
	return ans
for i in range(int(input())):
	[n, p] = list(map(int, input().split()))
	l = list(map(int, input().split()))
	print(pokemon(n, l))


il_testow = int(input())
for w in range(il_testow):
	d = input().split()
	il_pokemonow = int(d[0])
	il_zmian = int(d[1])
	tab = [0] + [int(x) for x in input().split()]
	dp = [[0, 0]] * (il_pokemonow + 1)
	for i in range(1, il_pokemonow + 1):
		dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - tab[i])
		dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + tab[i])
	print(max(dp[i][0], dp[i][1]))


t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	ans = max(a)
	x = 0
	z = 0
	f = 0
	for i in range(n - 1):
		if a[i] >= a[i + 1]:
			x = max(x, a[i])
			f = 1
		elif f:
			z = z + x - a[i]
			f = 0
			x = 0
		else:
			x = max(x, a[i])
	if f == 0:
		x = max(x, a[-1])
	z = z + x
	ans = max(ans, z)
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	i = 0
	ans = []
	f = 1
	while i < n:
		if f:
			mi = -float('inf')
			while i < n and a[i] > mi:
				mi = a[i]
				i += 1
			ans.append(a[i - 1])
			f ^= 1
		else:
			ma = float('inf')
			while i < n and a[i] < ma:
				ma = a[i]
				i += 1
			ans.append(a[i - 1])
			f ^= 1
	fa = 0
	if len(ans) % 2 != 0:
		for (i, j) in enumerate(ans):
			if i % 2 == 0:
				fa += j
			else:
				fa -= j
		print(fa)
	else:
		for (i, j) in enumerate(ans):
			if i % 2 == 0:
				fa += j
			else:
				fa -= j
		print(fa + ans[-1])


import sys
from math import ceil, factorial, gcd
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
MOD = 10 ** 9 + 7
INF = float('inf')
rl = lambda : list(map(int, sys.stdin.readline().split()))
rs = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	(n, q) = rl()
	A = rl()
	ans = A[0]
	for i in range(1, n):
		ans += max(0, A[i] - A[i - 1])
	print(ans)


t = int(input())
for i in range(t):
	(_, _) = map(int, input().split())
	pokemones = [int(i) for i in input().split()]
	if pokemones == []:
		print(0)
		continue
	if len(pokemones) == 1:
		print(pokemones[0])
		continue
	estamos_sumando = True
	s = 0
	for i in range(len(pokemones) - 1):
		if estamos_sumando:
			if pokemones[i] > pokemones[i + 1]:
				s += pokemones[i]
				estamos_sumando = False
		elif pokemones[i] < pokemones[i + 1]:
			s -= pokemones[i]
			estamos_sumando = True
	if estamos_sumando:
		s += pokemones[-1]
	print(s)


n_t = int(input())
for tc in range(n_t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	i = 0
	eles = []
	while i < n:
		while i + 1 < n and arr[i] < arr[i + 1]:
			i += 1
		eles.append(arr[i])
		i += 1
		if i >= n:
			break
		while i + 1 < n and arr[i] > arr[i + 1]:
			i += 1
		eles.append(arr[i])
		i += 1
	if len(eles) % 2 == 0:
		eles.pop()
	(s, c) = (0, 1)
	for i in eles:
		if c:
			s += i
		else:
			s -= i
		c ^= 1
	print(s)


for i in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	k = a[0]
	for i in range(1, n):
		k += max(0, a[i] - a[i - 1])
	print(k)


n = int(input())
for _ in range(n):
	(n, q) = map(int, input().split())
	array = list(map(int, input().split()))
	flip = 0
	max_strength = 0
	current = array[0]
	del array[0]
	for i in array:
		if flip == 0:
			if current > i:
				max_strength += current
				flip = 1
		elif current < i:
			max_strength -= current
			flip = 0
		current = i
	if flip == 0:
		max_strength += current
	print(max_strength)


from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + '\n')
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	(x, y) = (0, 0)
	ans = 0
	curr = 'inc'
	for (j, i) in enumerate(a):
		if curr == 'inc':
			if i >= x:
				x = i
				if j == n - 1:
					ans += x
			else:
				curr = 'dec'
				y = i
				if j == n - 1:
					ans += x
		elif i < y:
			y = i
			if j == n - 1:
				ans += x
		else:
			ans += x - y
			curr = 'inc'
			x = i
			if j == n - 1:
				ans += x
	print(ans)


import os
import sys
from io import BytesIO, IOBase
_str = str
BUFSIZE = 8192

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline()

def mpint():
	return map(int, sys.stdin.readline().split(' '))

def itg():
	return int(sys.stdin.readline())

def solve():
	(n, q) = mpint()
	arr = list(mpint()) + [0]
	ans = 0
	for i in range(n):
		ans += max(0, arr[i] - arr[i - 1])
	print(ans)
	for _ in range(q):
		(lf, rg) = mpint()
		lf -= 1
		rg -= 1
		ans -= max(0, arr[rg + 1] - arr[rg])
		ans -= max(0, arr[rg] - arr[rg - 1])
		if rg != lf + 1:
			ans -= max(0, arr[lf + 1] - arr[lf])
		ans -= max(0, arr[lf] - arr[lf - 1])
		(arr[rg], arr[lf]) = (arr[lf], arr[rg])
		ans += max(0, arr[rg + 1] - arr[rg])
		ans += max(0, arr[rg] - arr[rg - 1])
		if rg != lf + 1:
			ans += max(0, arr[lf + 1] - arr[lf])
		ans += max(0, arr[lf] - arr[lf - 1])
		print(ans)
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
for _ in range(itg()):
	solve()


t = int(input())
while t > 0:
	t = t - 1
	[n, q] = input().split()
	n = int(n)
	q = int(q)
	a = input().split()
	for i in range(0, n):
		a[i] = int(a[i])
	b = [0] * n
	c = [0] * n
	b[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		c[i] = min(c[i + 1], a[i] - b[i + 1])
		b[i] = max(b[i + 1], a[i] - c[i + 1])
	print(b[0])


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	(n, q) = read()
	a = get_list()
	f = 0
	m = max(a)
	ans = 0
	for i in range(n - 1):
		if a[i] > a[i + 1] and f == 0:
			ans += a[i]
			f = 1
		elif a[i] < a[i + 1] and f == 1:
			ans -= a[i]
			f = 0
	if f == 0:
		ans += a[-1]
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(scanner()):
	solve()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	cur = a[0]
	minus = []
	count = 0
	ans = 0
	for i in range(1, len(a)):
		if count == 0:
			if a[i] > a[i - 1]:
				cur = a[i]
			else:
				ans += cur
				count = 1
				cur = a[i]
		if count == 1:
			if a[i] < a[i - 1]:
				cur = a[i]
			else:
				ans -= cur
				cur = a[i]
				count = 0
	if count == 0:
		ans += a[n - 1]
	print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdarr = lambda : map(int, stdstr().split())
mod = 1000000007
for _ in range(stdint()):
	(n, q) = stdarr()
	arr = list(stdarr())
	if n == 1:
		print(arr[0])
		continue
	peaks = []
	val = []
	curr = -99
	for i in range(1, n):
		if i == 1:
			if arr[1] > arr[0]:
				curr = 1
			else:
				curr = -1
				peaks.append(0)
		elif arr[i] < arr[i - 1]:
			if curr == 1:
				peaks.append(i - 1)
			curr = -1
		else:
			if curr == -1:
				val.append(i - 1)
			curr = 1
	if arr[-1] > arr[-2]:
		peaks.append(n - 1)
	else:
		val.append(n - 1)
	while len(val) != 0 and val[0] < peaks[0]:
		del val[0]
	peakm = max(peaks)
	s = 0
	for i in peaks:
		s += arr[i]
	for i in val:
		if i > peakm:
			break
		else:
			s -= arr[i]
	print(s)


from collections import Counter
import math
import sys
from bisect import bisect, bisect_left, bisect_right

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for i in range(INT()):
	(n, q) = MAP()
	arr = LIST()
	if n == 1 or n == 2:
		print(max(arr))
	else:
		i = 0
		ans = []
		while i < n - 1:
			while i < n - 1:
				if arr[i + 1] < arr[i]:
					ans.append(arr[i])
					i += 1
					break
				else:
					i += 1
			while i < n - 1:
				if arr[i + 1] > arr[i]:
					ans.append(arr[i])
					i += 1
					break
				else:
					i += 1
		fans = 0
		if not ans:
			print(n)
		else:
			if len(ans) % 2 != 0:
				for i in range(0, len(ans) - 1, 2):
					fans += ans[i] - ans[i + 1]
				fans += ans[-1]
			else:
				for i in range(0, len(ans) - 1, 2):
					fans += ans[i] - ans[i + 1]
				x = arr.index(ans[-1])
				m = max(arr[x + 1:n + 1])
				fans += m
			print(fans)


import sys
input = sys.stdin.readline
import copy
t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	flg = 0
	ans = 0
	if n != 1:
		if a[0] > a[1]:
			flg += 1
			ans += a[0]
		for j in range(n - 2):
			if a[j] < a[j + 1] > a[j + 2]:
				flg += 1
				ans += a[j + 1]
			if a[j] > a[j + 1] < a[j + 2]:
				flg += 1
				ans -= a[j + 1]
	if flg % 2 == 0:
		ans += a[n - 1]
	print(ans)


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	(n, q) = MI()
	a = LI()
	(s, par) = (0, 0)
	p1 = 0
	m = max(a)
	while True:
		if par:
			while p1 + 1 < n and a[p1] > a[p1 + 1]:
				p1 += 1
		else:
			while p1 + 1 < n and a[p1] < a[p1 + 1]:
				p1 += 1
		if p1 != n - 1:
			if par:
				s -= a[p1]
			else:
				s += a[p1]
			par = 0 if par else 1
		else:
			if not par:
				s += a[-1]
			break
	print(max(m, s))


for i in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	if n == 1:
		print(a[0])
	else:
		if a[0] > a[1]:
			ans += a[0]
		for i in range(1, n - 1):
			if a[i] > a[i - 1] and a[i] > a[i + 1]:
				ans += a[i]
			elif a[i] < a[i - 1] and a[i] < a[i + 1]:
				ans -= a[i]
		if a[n - 1] > a[n - 2]:
			ans += a[n - 1]
		print(ans)


def getint():
	return int(input())

def getints():
	return map(int, input().split())

def getlistints():
	return list(map(int, input().split()))

def solution1(n, nums):
	nums = [-1] + nums + [-1]
	res = 0
	for i in range(1, n + 1):
		if nums[i] < nums[i - 1] and nums[i] < nums[i + 1]:
			res -= nums[i]
		if nums[i] > nums[i - 1] and nums[i] > nums[i + 1]:
			res += nums[i]
	return res
for _ in range(getint()):
	(n, q) = getints()
	nums = getlistints()
	print(solution1(n, nums))


def solution(arr, n, q):
	ranges = []
	ans = 0
	if len(arr) == 1:
		return arr[0]
	if arr[0] >= arr[1]:
		ranges.append(0)
	for i in range(1, len(arr) - 1):
		if arr[i - 1] < arr[i] > arr[i + 1]:
			ranges.append(i)
	if arr[-1] > arr[-2]:
		ranges.append(n - 1)
	if len(ranges) <= 1:
		return max(arr)
	for i in range(1, len(ranges)):
		ans += arr[ranges[i - 1]]
		start = ranges[i - 1] + 1
		end = ranges[i]
		mi = float('inf')
		for j in range(start, end):
			if arr[j] < mi:
				mi = arr[j]
		ans -= mi
	ans += arr[ranges[-1]]
	return ans
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = [int(i) for i in input().split()]
	print(solution(arr, n, q))


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	(n, k) = R()
	(*a,) = R()
	p = [[a[0], 0]]
	flg = False
	for i in a:
		if flg:
			if i > p[-1][1]:
				p.append([i, 0])
				flg = False
			elif i < p[-1][1]:
				p[-1][1] = i
		elif i > p[-1][0]:
			p[-1][0] = i
		elif i < p[-1][0]:
			p[-1][1] = i
			flg = True
	sm = 0
	for i in p[:-1]:
		sm += i[0] - i[1]
	sm += p[-1][0]
	print(sm)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [0] * (n + 1)
	dp1 = [0] * (n + 1)
	dp[0] = -10000000000.0
	dp1[0] = 0
	for i in range(n):
		dp[i + 1] = max(dp[i], dp1[i] + a[i])
		dp1[i + 1] = max(dp1[i], dp[i] - a[i])
	print(max(dp[n], dp1[n]))


for _ in ' ' * int(input()):
	(n, q) = map(int, input().split())
	(a, s, f) = (list(map(int, input().split())), 0, 0)
	for i in range(1, n):
		if f == 0:
			if a[i] < a[i - 1]:
				s += a[i - 1]
				f += 1
		elif a[i] > a[i - 1]:
			s -= a[i - 1]
			f -= 1
	print(s if f else s + a[-1])


import sys
import random
from fractions import Fraction
from math import *

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def finput():
	return float(input())

def tinput():
	return input().split()

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

def fiinput():
	return map(float, tinput())

def rlinput():
	return list(map(int, input().split()))

def trinput():
	return tuple(rinput())

def srlinput():
	return sorted(list(map(int, input().split())))

def NOYES(fl):
	if fl:
		print('NO')
	else:
		print('YES')

def YESNO(fl):
	if fl:
		print('YES')
	else:
		print('NO')

def main():
	(n, m) = rinput()
	q = rlinput()
	w = [[0, 0] for i in range(n + 5)]
	for i in range(1, n + 1):
		w[i][1] = q[i - 1]
		w[i][0] = max(w[i - 1][1] - w[i][1], w[i - 1][0])
		w[i][1] = max(w[i - 1][0] + w[i][1], w[i - 1][1])
	print(max(w[n][0], w[n][1]))
for i in range(iinput()):
	main()


def biggest_sub(l):
	max_now = l[0]
	min_yet = l[0]
	suma = 0
	for i in l[1:]:
		if i > min_yet:
			if max_now - min_yet > 0:
				suma += max_now - min_yet
			max_now = i
			min_yet = max_now
		if i < min_yet:
			min_yet = i
	suma += max_now
	return suma
t = int(input())
while t:
	t -= 1
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	n = biggest_sub(l)
	print(n)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
from collections import Counter, defaultdict, deque

def int1():
	return int(input())

def map1():
	return map(int, input().split())

def list1():
	return list(map(int, input().split()))
mod = pow(10, 9) + 7

def solve():
	(n, q) = map1()
	l1 = list1()
	(sum1, i) = (0, 0)
	flag = 0
	max1 = 0
	while 1:
		if i == n - 1:
			max1 = max(max1, l1[i])
			print(max(n, max1 + sum1))
			return
		if l1[i] < l1[i + 1]:
			i = i + 1
			c1 = l1[i - 1]
			if flag == 1:
				sum1 += c - c1
				max1 = 0
			flag = 0
			max1 = max(max1, l1[i])
		else:
			if flag == 0:
				c = l1[i]
			flag = 1
			i = i + 1
for _ in range(int(input())):
	solve()


import sys

def main():
	all_data = list(map(int, sys.stdin.read().split()))
	idx = 0
	t = all_data[idx]
	idx += 1
	for t_it in range(t):
		(n, q) = all_data[idx:idx + 2]
		idx += 2
		a = [0] + all_data[idx:idx + n] + [0]
		idx += n
		all_ans = [0] * (q + 1)
		val = [0] * (n + 1)
		for i in range(n + 1):
			val[i] = max(a[i], a[i + 1])
		cur_ans = sum(val) - sum(a)
		all_ans[0] = cur_ans
		for q_it in range(1, q + 1):
			(l, r) = all_data[idx:idx + 2]
			idx += 2
			if l != r:
				(a[l], a[r]) = (a[r], a[l])
				for p in set((l - 1, l, r - 1, r)):
					cur_ans -= val[p]
					val[p] = max(a[p], a[p + 1])
					cur_ans += val[p]
			all_ans[q_it] = cur_ans
		print(*all_ans, sep='\n')
main()


def pokemonArmy(n, array):
	x = 1
	prev = 0
	f = 0
	ans = 0
	while x <= n:
		if f == 0:
			if x != n and array[x] > array[prev]:
				pass
			else:
				ans += array[prev]
				f = 1
		elif x == n or array[x] < array[prev]:
			pass
		else:
			ans -= array[prev]
			f = 0
		prev = x
		x += 1
	return ans
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	print(pokemonArmy(n, l))


t = int(input())
for o in range(t):
	ans = 0
	xxx = str.split(input(), ' ')
	n = int(xxx[0])
	l = str.split(input(), ' ')
	for i in range(n):
		l[i] = int(l[i])
	temp = 1
	for i in range(n):
		if temp == 1:
			if i == n - 1:
				ans = ans + l[i]
			elif l[i] > l[i + 1]:
				ans = l[i] + ans
				temp = 0
		elif i != n - 1:
			if l[i + 1] > l[i]:
				temp = 1
				ans = ans - l[i]
	print(ans)


import sys
import heapq
import copy
import math
import decimal
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	s = list(s[:len(s) - 1])
	s = [ord(s[i]) - ord('a') for i in range(len(s))]
	return s

def invr():
	return map(int, input().split())
ans = 0

def pr_list(a):
	print(*a, sep=' ')

def swap(a, b):
	temp = a
	a = b
	b = temp
	return [a, b]

def pri(a, b):
	s = '? ' + str(a) + ' ' + str(b)
	print(s)
	sys.stdout.flush()

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		[n, q] = inlt()
		a = inlt()
		dp = [[0 for i in range(n + 1)] for j in range(2)]
		for i in range(n):
			dp[0][i + 1] = max(dp[0][i], dp[1][i] + a[i])
			dp[1][i + 1] = max(dp[1][i], dp[0][i] - a[i])
		print(max(dp[0][-1], dp[1][-1]))
main()


t = int(input())
while t:
	t -= 1
	(n, q) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	state = 1
	if n == 1:
		print(arr[0])
		continue
	ans = []
	if arr[0] > arr[1]:
		ans.append(arr[0])
		state = 0
		start = 1
	else:
		i = 1
		while i < n - 1:
			if arr[i - 1] < arr[i] > arr[i + 1]:
				ans.append(arr[i])
				start = i + 1
				state = 0
				break
			i += 1
		else:
			ans.append(arr[n - 1])
			start = n
			state = 0
	for i in range(start, n - 1):
		if state == 0:
			if arr[i - 1] > arr[i] and arr[i] < arr[i + 1]:
				ans.append(arr[i])
				state = 1
		elif arr[i - 1] < arr[i] and arr[i] > arr[i + 1]:
			ans.append(arr[i])
			state = 0
	if state == 1:
		ans.append(arr[-1])
	state = 0
	a = 0
	for i in range(len(ans)):
		if state == 0:
			a += ans[i]
			state = 1
		else:
			a -= ans[i]
			state = 0
	print(a)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [1]
	res = 0
	s = set()
	s.add(a[0])
	for x in range(1, n):
		if a[x - 1] < a[x]:
			b.append(1)
		else:
			b.append(-1)
	now = 1
	b.append(-1)
	for x in range(n):
		if b[x] == b[x + 1]:
			continue
		res += a[x] * now
		s.add(res)
		now *= -1
	print(max(s))


from sys import stdin
il_testow = int(stdin.readline())
for w in range(il_testow):
	d = stdin.readline().split()
	il_pokemonow = int(d[0])
	il_zmian = int(d[1])
	tab = [0] + [int(x) for x in stdin.readline().split()]
	dp = [[0, 0]] * (il_pokemonow + 1)
	for i in range(1, il_pokemonow + 1):
		dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - tab[i])
		dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + tab[i])
	print(max(dp[i][0], dp[i][1]))


def main():
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = [1] + [0] * (n - 1)
	(evens, odds) = ([], [])
	odd = True
	for i in range(n - 1):
		if odd:
			if arr[i] > arr[i + 1]:
				odds.append(arr[i])
				odd = False
		elif arr[i] < arr[i + 1]:
			evens.append(arr[i])
			odd = True
	if odd:
		odds.append(arr[n - 1])
	print(sum(odds) - sum(evens))
for _ in range(int(input())):
	main()


import math
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	count = 0
	i = 1
	x = l[0]
	mi = math.inf
	while i < n:
		if l[i] < l[i - 1]:
			i += 1
		else:
			count += x - l[i - 1]
			x = l[i]
			i += 1
	count += x
	print(count)


import sys
input = sys.stdin.readline

class sgTree:

	def __init__(self, L):
		self.len = len(L)
		newL = []
		for i in L:
			newL.append([i, 0, i, 0])
		self.tree = [[0, 0, 0, 0] for i in range(self.len)] + newL
		for i in range(self.len - 1, 0, -1):
			L1 = self.tree[i]
			left = self.tree[2 * i]
			right = self.tree[2 * i + 1]
			L1[0] = max(left[0] - right[3], left[1] + right[0])
			L1[1] = max(left[0] - right[2], left[1] + right[1])
			L1[2] = min(left[2] - right[1], left[3] + right[2])
			L1[3] = min(left[2] - right[0], left[3] + right[3])

	def update(self, i, value):
		self.tree[self.len + i] = [value, 0, value, 0]
		ii = self.len + i
		while ii:
			ii //= 2
			L1 = self.tree[ii]
			left = self.tree[2 * ii]
			right = self.tree[2 * ii + 1]
			L1[0] = max(left[0] - right[3], left[1] + right[0])
			L1[1] = max(left[0] - right[2], left[1] + right[1])
			L1[2] = min(left[2] - right[1], left[3] + right[2])
			L1[3] = min(left[2] - right[0], left[3] + right[3])

	def solve(self):
		return self.tree[1]
for i in ' ' * int(input()):
	(n, q) = map(int, input().split())
	L = list(map(int, input().split()))
	c = 1
	while c < n:
		c *= 2
	L += [0] * (c - n)
	s = sgTree(L)
	print(max(s.solve()[:2]))
	for j in ' ' * q:
		(a, b) = map(int, input().split())
		s.update(a - 1, L[b - 1])
		s.update(b - 1, L[a - 1])
		print(s.solve())


from sys import *
input = stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	sn = 0
	c = 1
	i = 0
	while i < n:
		if i == n - 1 and c == 1:
			sn += a[i]
			break
		elif i == n - 1:
			break
		if c == 1 and a[i] > a[i + 1]:
			sn += a[i]
			c = 0
		elif c == 0 and a[i] < a[i + 1]:
			sn -= a[i]
			c = 1
		i += 1
	stdout.write(str(sn) + '\n')


for a in [*open(0)][2::2]:
	s = k = 0
	for x in map(int, a.split()):
		s += x - k if x > k else 0
		(s, k) = (max(s, x), x)
	print(s)


for _ in range(int(input())):
	input()
	a = [int(i) for i in input().split()]
	if len(a) == 1:
		print(a[0])
		continue
	(plus, s, maybe) = (False, 0, a[0])
	for i in range(1, len(a)):
		if a[i] < maybe and (not plus):
			s += maybe
			plus = True
		elif a[i] > maybe and plus:
			plus = False
			s -= maybe
		maybe = a[i]
	if not plus and a[-1] > a[-2]:
		s += a[-1]
	print(s)


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
for _ in range(int(I1())):
	(n, q) = I()
	a = list(I())
	f = 0
	m1 = []
	if n == 1:
		print(a[0])
		continue
	for i in range(n):
		if f == 0:
			if i == 0 and a[i] > a[i + 1] or (i > 0 and i < n - 1 and (a[i] > a[i - 1]) and (a[i] > a[i + 1])) or (i == n - 1 and a[i] > a[i - 1]):
				m1.append(a[i])
				f = 1
		elif i == n - 1 and a[i] < a[i - 1] or (i > 0 and i < n - 1 and (a[i] < a[i - 1]) and (a[i] < a[i + 1])):
			m1.append(-a[i])
			f = 0
	if m1[-1] < 0:
		m1.pop()
	print(sum(m1))


tt = int(input())
while tt > 0:
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	mx = [0 for i in range(n + 1)]
	mn = [0 for i in range(n + 1)]
	for i in range(n - 1, -1, -1):
		mx[i] = max(mx[i + 1], a[i] - mn[i + 1])
		mn[i] = min(mn[i + 1], a[i] - mx[i + 1])
	print(mx[0])
	tt -= 1


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	result1 = [0] * n
	result1[0] = a[0]
	result2 = [0] * n
	result2[0] = a[0]
	max_result1 = a[0]
	max_result2 = 0
	for i in range(1, n):
		result1[i] = max(max_result2 + a[i], a[i])
		result2[i] = max_result1 - a[i]
		max_result1 = max(max_result1, result1[i])
		max_result2 = max(max_result2, result2[i])
	print(max(max_result1, max_result2))


for _ in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	ss = [int(x) for x in input().split()]
	a = 0
	b = 0
	for i in ss:
		t = a
		a = max(a, b + i)
		b = max(b, t - i)
	print(max(a, b))


import sys, heapq, collections
tc = int(sys.stdin.readline())
for _ in range(tc):
	(n, q) = map(int, sys.stdin.readline().split())
	arr = list(map(int, sys.stdin.readline().split()))
	if n == 1:
		print(arr[0])
	else:
		up = []
		down = []
		for i in range(n):
			if i == 0:
				if arr[i] > arr[i + 1]:
					up.append(i)
			elif i == n - 1:
				if arr[i] > arr[i - 1]:
					up.append(i)
			elif arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
				up.append(i)
		idx = 0
		for i in range(n):
			if i == 0:
				if arr[i] < arr[i + 1]:
					if idx < len(up) - 1 and up[idx] < i < up[idx + 1]:
						down.append(i)
						idx += 1
			elif i == n - 1:
				if arr[i] < arr[i - 1]:
					if idx < len(up) - 1 and up[idx] < i < up[idx + 1]:
						down.append(i)
						idx += 1
			elif arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
				if idx < len(up) - 1 and up[idx] < i < up[idx + 1]:
					down.append(i)
					idx += 1
		a = 0
		for i in up:
			a += arr[i]
		b = 0
		for i in down:
			b += arr[i]
		print(a - b)


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
	(n, q) = input_split()
	arr = input_split()
	for _ in range(q):
		(l, r) = input_split()
		l -= 1
		r -= 1
		swaps.append((l, r))
	if n == 1:
		ans = arr[0]
	else:
		extremes = []
		for i in range(1, n - 1):
			if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
				extremes.append(arr[i])
			elif arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
				extremes.append(arr[i])
		if arr[0] > arr[1]:
			extremes = [arr[0]] + extremes
		if arr[-1] > arr[-2]:
			extremes.append(arr[-1])
		assert len(extremes) % 2 == 1, 'somethign wrong'
		ans = 0
		for i in range(len(extremes)):
			if i % 2 == 0:
				ans += extremes[i]
			else:
				ans += -extremes[i]
	answers.append(ans)
print(*answers, sep='\n')


from sys import stdin, stdout
import math
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

def pr(a):
	return ' '.join(list(map(str, a)))

def solve():
	(n, q) = M()
	a = L()
	ans = 0
	b = 1
	for i in range(1, n):
		if a[i] < a[i - 1]:
			ans += a[i - 1] - a[i]
	print(ans + a[-1])
for _ in range(I()):
	solve()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	d1 = float('-inf')
	d2 = 0
	for i in range(n):
		temp1 = max(d1, d2 + arr[i])
		temp2 = max(d2, d1 - arr[i])
		d1 = temp1
		d2 = temp2
	print(max(d1, d2))


for _ in range(int(input())):
	(n, q) = [int(i) for i in input().split()]
	lst = [int(i) for i in input().split()]
	k = lst[0]
	for i in range(1, n):
		k += max(0, lst[i] - lst[i - 1])
	print(k)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	lis = list(map(int, input().split()))
	big = lis[0]
	small = 0
	sum1 = 0
	i = 0
	while i < len(lis):
		while i < len(lis) and lis[i] >= lis[i - 1]:
			big = lis[i]
			i += 1
		while i < len(lis) and lis[i] < lis[i - 1]:
			small = lis[i]
			i += 1
		if i == len(lis):
			small = 0
		sum1 += big - small
		big = 0
		small = 0
	print(sum1)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	b = []
	maximum = 0
	minimum = n + 1
	i = 0
	while i < n:
		while i < n - 1 and a[i] <= a[i + 1]:
			i += 1
		b.append(a[i])
		if i == n - 1:
			break
		while i < n - 1 and a[i] >= a[i + 1]:
			i += 1
		b.append(a[i])
		if i == n - 1:
			break
	if len(b) % 2 == 0:
		b.pop()
	res = 0
	for i in range(0, len(b), 2):
		res += b[i]
	for i in range(1, len(b), 2):
		res -= b[i]
	print(res)


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		(n, q) = In()
		l = list(In())
		ans = l[0]
		for i in range(1, n):
			ans += max(0, l[i] - l[i - 1])
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	var = 0
	if n == 1:
		print(arr[0])
	else:
		ls = []
		var = 0
		for i in range(n - 1):
			if arr[i] <= arr[i + 1]:
				if len(ls) % 2 != 0:
					ls.append(arr[i])
				if i == n - 2:
					ls.append(arr[i + 1])
			elif len(ls) % 2 == 0:
				ls.append(arr[i])
		ans = 0
		for i in range(len(ls)):
			if (i + 1) % 2 != 0:
				ans += ls[i]
			elif i != len(ls) - 1:
				ans -= ls[i]
		print(ans)


import math

def solve(n, arr):
	arr = [0] + arr
	increasing = None
	if arr[1] > arr[0]:
		increasing = True
	else:
		increasing = False
	ans = 0
	for j in range(1, n + 1):
		if increasing:
			if j == n:
				ans += arr[j]
				break
			if arr[j + 1] - arr[j] < 0:
				ans += arr[j]
				increasing = False
			pass
		else:
			if j == n:
				break
			if arr[j + 1] - arr[j] > 0:
				ans -= arr[j]
				increasing = True
	return ans
	pass
t = int(input())
ans = []
for i in range(t):
	s = input().split()
	n = int(s[0])
	q = int(s[1])
	arr = [int(x) for x in input().split()]
	ans.append(solve(n, arr))
for test in ans:
	print(test)


for test in range(int(input())):
	[n, q] = list(map(int, input().split(' ')))
	a = list(map(int, input().split(' ')))
	i = 0
	plus = True
	res = 0
	while i < n:
		if plus:
			j = 1
			while j + i < n and a[i + j - 1] < a[i + j]:
				j += 1
			res += a[i + j - 1]
			i += j
			plus = False
		else:
			j = 1
			while j + i < n and a[i + j - 1] > a[i + j]:
				j += 1
			if i + j < n:
				res -= a[i + j - 1]
			i += j
			plus = True
	print(str(res))


def PokemonArmyEasy(n, a):
	ans = []
	j = 0
	maxormin = 1
	for i in range(n - 1):
		if maxormin == 1:
			if a[i] > a[i + 1]:
				ans.append(a[i])
				maxormin = 2
		elif maxormin == 2:
			if a[i] < a[i + 1]:
				ans.append(a[i])
				maxormin = 1
	if maxormin == 1:
		ans.append(a[n - 1])
	elif maxormin == 2:
		if a[i] < a[i + 1]:
			ans.append(a[i])
	c = 0
	for i in range(len(ans)):
		if i % 2 == 0:
			c = c + ans[i]
		else:
			c = c - ans[i]
	return c
t = int(input())
for tt in range(t):
	nn = list(map(int, input().rstrip().split()))
	a = list(map(int, input().rstrip().split()))
	ans = PokemonArmyEasy(nn[0], a)
	print(ans)


for _ in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	inc = [0 for i in range(n + 1)]
	dec = [0 for i in range(n + 1)]
	inc[0] = -100000000000
	for i in range(1, n + 1):
		inc[i] = max(inc[i - 1], dec[i - 1] + arr[i - 1])
		dec[i] = max(dec[i - 1], inc[i - 1] - arr[i - 1])
	print(max(inc[-1], dec[-1]))


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	(n, q) = MI()
	aa = [0] + LI()
	ans = 0
	for i in range(n):
		ans += max(0, aa[i + 1] - aa[i])
	print(ans)


t = int(input())
while t > 0:
	t -= 1
	(n, q) = map(int, input().split(' '))
	l1 = [int(i) for i in input().split(' ')]
	flag = 0
	s = 0
	for i in range(len(l1) - 1):
		if l1[i + 1] > l1[i] and flag == 0:
			continue
		if l1[i + 1] < l1[i] and flag == 0:
			s += l1[i]
			flag = 1
			continue
		if l1[i + 1] < l1[i] and flag == 1:
			continue
		if l1[i + 1] > l1[i] and flag == 1:
			flag = 0
			s -= l1[i]
			continue
	if flag == 0:
		s += l1[-1]
	print(s)


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	maxi = 0
	mini = 0
	a = a[::-1]
	maxi = a[0]
	for i in range(1, n):
		maxi = max(maxi, a[i] - mini)
		mini = min(mini, a[i] - maxi)
	print(max(maxi, mini))
	t -= 1


import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))
MOD = 998244353

def ncr(n, r, p=MOD):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def main():
	ans_ = ''
	for _ in range(int(input())):
		(n, q) = read()
		arr = read() + [-10 ** 9]
		ans = 0
		f = 1
		for i in range(n):
			if f and arr[i] > arr[i + 1]:
				ans += arr[i]
				f = 0
			elif f == 0 and arr[i] < arr[i + 1]:
				ans -= arr[i]
				f = 1
		ans_ += str(ans) + '\n'
	print(ans_)
main()


T = int(input())
for I in range(0, T):
	X = input().split()
	Y = input().split()
	M = 0
	N = 0
	O = 0
	for I in Y:
		if O == 0 and int(I) < N:
			M += N
			O = 1
		elif O == 1 and int(I) > N:
			M -= N
			O = 0
		N = int(I)
	if O == 0:
		M += N
	print(M)


t = int(input())
for T in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	flag = 0
	for i in range(1, len(a)):
		if flag == 0:
			if a[i] > a[i - 1]:
				continue
			else:
				ans += a[i - 1]
				flag = 1
		elif a[i] < a[i - 1]:
			continue
		else:
			ans -= a[i - 1]
			flag = 0
	if flag == 0:
		ans += a[len(a) - 1]
	print(ans)


def dp2(arr):
	mat = []
	for i in range(2):
		mat.append([-1] * len(arr))
	mat[0][0] = arr[0]
	mat[1][0] = 0
	for j in range(1, len(mat[0])):
		maxi = -1
		for i in range(2):
			if i == 0:
				op1 = arr[j] + mat[1][j - 1]
			else:
				op1 = -arr[j] + mat[0][j - 1]
			op2 = mat[i][j - 1]
			maxi = max(op1, op2)
			mat[i][j] = maxi
	return max(mat[0][-1], mat[1][-1])
t = int(input())
for i in range(t):
	arr1 = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	print(dp2(arr))


import math
import collections
t = int(input())
for w in range(t):
	(n, q) = (int(i) for i in input().split())
	l = [[int(i)] for i in input().split()]
	if n == 1:
		print(l[0][-1])
	else:
		s = 0
		c = 0
		l1 = []
		hp = []
		lp = []
		m = max(l[0][-1], l[-1][-1])
		for i in range(1, n - 1):
			if l[i][-1] > l[i - 1][-1] and l[i][-1] > l[i + 1][-1]:
				hp.append(i)
				l[i] = ['hp', l[i][-1]]
			if l[i][-1] < l[i - 1][-1] and l[i][-1] < l[i + 1][-1]:
				lp.append(i)
				l[i] = ['lp', l[i][-1]]
			m = max(m, l[i][-1])
		if l[0][-1] > l[1][-1]:
			hp.append(0)
			l[0] = ['hp', l[0][-1]]
		elif l[0][-1] < l[1][-1]:
			lp.append(0)
			l[0] = ['lp', l[0][-1]]
		if l[-1][-1] > l[-2][-1]:
			hp.append(n - 1)
			l[-1] = ['hp', l[-1][-1]]
		elif l[-1][-1] < l[-2][-1]:
			lp.append(n - 1)
			l[-1] = ['lp', l[-1][-1]]
		if len(hp) <= 1:
			print(m)
		else:
			prev = -1
			for i in range(n):
				if c % 2 == 0:
					if l[i][0] == 'hp':
						s += l[i][1]
						prev = i
						c += 1
				elif l[i][0] == 'lp':
					s -= l[i][1]
					prev = i
					c += 1
			if c % 2 == 0:
				s += l[prev][1]
			print(s)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
cases = int(input())
for t in range(cases):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	a = [0] + a + [0]
	track = [0] * (n + 1)
	for i in range(1, n + 1):
		track[i] = max(0, a[i] - a[i + 1])
	out = sum(track)
	print(out)
	swaps = []
	for i in range(q):
		(l, r) = list(map(int, input().split()))
		(al, ar) = (a[l], a[r])
		a[l] = ar
		out -= track[l - 1] + track[l]
		track[l - 1] = max(0, a[l - 1] - a[l])
		track[l] = max(0, a[l] - a[l + 1])
		out += track[l - 1] + track[l]
		a[r] = al
		out -= track[r - 1] + track[r]
		track[r - 1] = max(0, a[r - 1] - a[r])
		track[r] = max(0, a[r] - a[r + 1])
		out += track[r - 1] + track[r]
		print(out)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(*li,) = map(int, input().split())
	a = 1e-05
	b = 0
	for i in li:
		a = max(a, b + i)
		b = max(b, a - i)
	print(max(a, b))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	if n == 1:
		print(arr[0])
		continue
	pos = max(arr[0], arr[1])
	neg = max(arr[0] - arr[1], 0)
	for e in arr[2:]:
		neg = max(neg, pos - e)
		pos = max(pos, neg + e)
	print(max(neg, pos))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	peaks = []
	for x in range(n):
		left = x - 1 < 0 or arr[x - 1] < arr[x]
		right = x + 1 >= n or arr[x + 1] < arr[x]
		if left and right:
			peaks.append(x)
	ans = sum([arr[x] for x in peaks])
	for x in range(1, len(peaks)):
		(left, right) = (peaks[x - 1], peaks[x])
		mn = n + 10
		for i in range(left + 1, right):
			mn = min(mn, arr[i])
		ans -= mn
	print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	curr = arr[0]
	ans = 0
	f = 0
	for i in range(1, n):
		if f == 0:
			if arr[i] > arr[i - 1]:
				curr = arr[i]
			else:
				ans += arr[i - 1]
				curr = arr[i]
				f = 1
		elif arr[i] < arr[i - 1]:
			curr = arr[i]
		else:
			ans -= arr[i - 1]
			curr = arr[i]
			f = 0
	if f == 0:
		ans += arr[n - 1]
	print(ans)


num = int(input())
for _ in range(num):
	(n, q) = map(int, input().split())
	pok = list(map(int, input().split()))
	pok.append(int('0'))
	total = 0
	state = 1
	for i in range(n):
		if state == 1:
			if pok[i] > pok[i + 1]:
				total += pok[i]
				state = 0
			else:
				continue
		elif pok[i] < pok[i + 1]:
			total -= pok[i]
			state = 1
		else:
			continue
	print(total)


def maxTotalStrength(pokemons_Strengths):
	totalStrength = 0
	currStrength = nextStrength = pokemons_Strengths[0]
	for strength in pokemons_Strengths[1:]:
		if strength < nextStrength:
			nextStrength = strength
		elif nextStrength < strength:
			totalStrength += currStrength - nextStrength
			currStrength = nextStrength = strength
	return totalStrength + currStrength

def testCase_1420c1():
	input()
	return tuple(map(int, input().split(' ')))
testCases = (testCase_1420c1() for x in range(int(input())))
tuple((print(maxTotalStrength(testCase)) for testCase in testCases))


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	(n, q) = idata()
	data = idata()
	ans = data[0]
	flag = 1
	mi = 0
	for i in range(1, n):
		if flag:
			if data[i] > data[i - 1]:
				ans += data[i] - data[i - 1]
			else:
				flag = 0
				mi = data[i]
		elif data[i] < data[i - 1]:
			mi = data[i]
		else:
			ans -= mi - data[i]
			mi = 0
			flag = 1
	print(ans)
	return
for t in range(int(ii())):
	solve()


from math import *
from collections import *
from itertools import *
from functools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(100000000)
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	l = [0] + l
	t = []
	for i in range(n + 5):
		t.append([0] * 2)
	for i in range(1, n + 1):
		t[i][1] = max(t[i - 1][0] + l[i], t[i - 1][1])
		t[i][0] = max(t[i - 1][1] - l[i], t[i - 1][0])
		t[i][1] = max(t[i][1], l[i])
	print(max(t[n][1], t[n][0]))


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	s = 0
	pls = True
	for i in range(n):
		if pls and (i == 0 or l[i] >= l[i - 1]) and (i == n - 1 or l[i] > l[i + 1]):
			s += l[i]
			pls = False
		elif not pls and l[i] <= l[i - 1] and (i != n - 1 and l[i] < l[i + 1]):
			s -= l[i]
			pls = True
	print(s)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	MAX = int(1000000.0)
	(pmax, pmin, ans) = (-MAX, MAX, -MAX)
	for i in reversed(l):
		cmax = max(i, i - pmin, pmax)
		cmin = min(i, i - pmax, pmin)
		(pmax, pmin) = (cmax, cmin)
	print(pmax)


t = int(input())
for q in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	if len(a) == 1:
		print(a[0])
		continue
	form = 1
	ans = 0
	x = a[0]
	y = a[0]
	for i in a[1:]:
		if form == 1:
			if i > x:
				x = i
			if i < x:
				y = i
				ans += x
				form = 0
		if form == 0:
			if i < y:
				y = i
			if i > y:
				x = i
				ans -= y
				form = 1
	if form == 1:
		ans += x
	print(ans)


t = int(input())
for i in range(t):
	(n, q) = (int(temp) for temp in input().split())
	nums = [int(temp) for temp in input().split()]
	mx = nums[0]
	le = len(nums)
	cur = nums[0]
	totl = 0
	seg = 0
	if le > 1:
		for k in range(1, le):
			if nums[k] >= nums[k - 1]:
				if seg != 0:
					totl += seg
					seg = 0
				mx = max(totl + nums[k], mx)
				cur = nums[k]
			else:
				seg = max(seg, cur - nums[k])
	if seg != 0:
		totl += seg
		mx = max(totl + nums[k], mx)
	print(mx)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	b = []
	i = 1
	mx = a[0]
	check = False
	mn = max(a)
	while i < n:
		if not check:
			if a[i] > mx:
				mx = a[i]
			else:
				mn = a[i]
				check = True
				b.append(mx)
				mx = 0
		elif a[i] < mn:
			mn = a[i]
		else:
			mx = a[i]
			check = False
			b.append(mn)
			mn = 0
		i += 1
	if len(b) % 2 == 0:
		if mx != 0:
			b.append(mx)
	if len(b) % 2 == 0:
		n1 = len(b) - 1
	else:
		n1 = len(b)
	ans = 0
	for i in range(n1):
		if i % 2 == 0:
			ans += b[i]
		else:
			ans -= b[i]
	print(ans)


import sys
from functools import lru_cache
max_int = 1000000001
min_int = -max_int
t = int(input())
for _t in range(t):
	(n, q) = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split())) + [0] + [0]
	Q = []
	for i in range(q):
		Q.append(sys.stdin.readline().split())
	dp1 = a[0]
	dp2 = min_int
	last_dp1 = a[0]
	last_dp2 = min_int
	for i in range(1, n + 2):
		new_dp1 = max(dp1, dp1 - last_dp1 + a[i], dp2 + a[i])
		new_dp2 = max(dp2, dp2 + last_dp2 - a[i], dp1 - a[i])
		if new_dp1 >= dp1:
			last_dp1 = a[i]
			dp1 = new_dp1
		if new_dp2 >= dp2:
			last_dp2 = a[i]
			dp2 = new_dp2
	print(max(dp1, dp2))


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	l1 = [int(x) for x in input().split()]
	i = 0
	j = 1
	k = 1
	ans = 0
	while i < len(l1):
		if j:
			while i < n - 1 and l1[i] < l1[i + 1]:
				i += 1
			j = 0
			ans += l1[i]
		else:
			if k % 2 == 0:
				while i < n - 1 and l1[i] < l1[i + 1]:
					i += 1
				ans += l1[i]
			else:
				while i < n - 1 and l1[i] > l1[i + 1]:
					i += 1
				if i != n - 1:
					ans -= l1[i]
			k += 1
		i += 1
	print(ans)


from math import inf as _inf
import sys as _sys
ans = []

def input():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def change(i, i1):
	totChange = 0
	for i in range(i - 1, i1 + 2):
		if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			totChange += arr[i]
		elif arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
			totChange -= arr[i]
	return totChange
arr = []
for _ in range(int(input())):
	(n, q) = list(map(int, input().split(' ')))
	arr = [-99, -98] + list(map(int, input().split())) + [-98, -99]
	flag = False
	s = 0
	for i in range(2, n + 2):
		if not flag:
			if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
				s += arr[i]
				flag = not flag
		elif arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			s -= arr[i]
			flag = not flag
	ans += [s]
	for i in range(q):
		(a, b) = map(int, input().split(' '))
		a += 1
		b += 1
		if b - a >= 5:
			sWill = s
			sWill += change(a, a)
			sWill += change(b, b)
			(arr[a], arr[b]) = (arr[b], arr[a])
			sWill -= change(a, a)
			sWill -= change(b, b)
			s = sWill
		else:
			sWill = s
			sWill += change(a, b)
			(arr[a], arr[b]) = (arr[b], arr[a])
			sWill -= change(a, b)
			s = sWill
		ans += [s]
print(*ans, sep='\n')


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(N, q) = map(int, input().split())
	A = list(map(int, input().split()))
	B = [[A[N - 1], 0]]
	j = 0
	for i in range(N - 2, -1, -1):
		val1 = max(A[i], A[i] + B[j][1], B[j][0])
		val2 = max(-A[i], -A[i] + B[j][0], B[j][1])
		B.append([val1, val2])
		j = j + 1
	print(max(B[N - 1]))


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	i = 0
	ans = 0
	mini = 10 ** 18
	while i < n:
		maxi = l[i]
		while i < n:
			if i + 1 < n and l[i] <= l[i + 1]:
				i += 1
				maxi = l[i]
			else:
				maxi = l[i]
				i += 1
				break
		ans += maxi
		if mini != 10 ** 18:
			ans -= mini
		while i < n:
			if i + 1 < n and l[i] > l[i + 1]:
				i += 1
				mini = l[i]
			else:
				mini = l[i]
				i += 1
				break
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	(n, k) = list(map(int, input().split(' ')))
	l = list(map(int, input().split(' ')))
	pos = [0] * n
	neg = [0] * n
	a = -10 ** 30
	b = -10 ** 30
	for i in range(n):
		if i == 0:
			pos[i] = l[i]
			neg[i] = -10 ** 20
			a = max(a, pos[i])
			b = max(b, neg[i])
		else:
			pos[i] = l[i] + max(b, 0)
			neg[i] = -l[i] + max(a, 0)
			a = max(a, pos[i])
			b = max(b, neg[i])
	print(max(max(pos), max(neg)))


import collections
import heapq
import sys
import math
import itertools
import bisect
import os
from io import BytesIO, IOBase
input = sys.stdin.readline

def solve():
	(n, q) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	even = [0]
	odd = [l[0]]
	for i in l[1:]:
		if even[-1] + i > odd[-1]:
			odd.append(even[-1] + i)
		if odd[-1] - i > even[-1]:
			even.append(odd[-1] - i)
	print(max(even[-1], odd[-1]))
for i in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	if n == 1:
		print(arr[0])
	else:
		for i in range(n):
			if i == 0:
				if arr[i + 1] < arr[i]:
					ans += arr[i]
			elif i == n - 1:
				if arr[i - 1] < arr[i]:
					ans += arr[i]
			elif arr[i - 1] < arr[i] and arr[i] > arr[i + 1]:
				ans += arr[i]
			elif arr[i - 1] > arr[i] and arr[i] < arr[i + 1]:
				ans -= arr[i]
		print(ans)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n <= 2:
		print(max(a))
	else:
		sw = 0
		sm = 0
		if a[0] > a[1]:
			sm += a[0]
			sw ^= 1
		for i in range(1, n):
			if sw:
				if i != n - 1:
					if a[i] < a[i - 1] and a[i] < a[i + 1]:
						sm -= a[i]
						sw ^= 1
			elif i == n - 1:
				if a[i] > a[i - 1]:
					sm += a[i]
			elif a[i] > a[i - 1] and a[i] > a[i + 1]:
				sm += a[i]
				sw ^= 1
		print(sm)


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	(n, q) = idata()
	data = [-1] + idata() + [-1]
	ans = 0
	ma = [0] * (n + 2)
	for i in range(1, n + 1):
		if data[i - 1] < data[i] > data[i + 1]:
			ans += data[i]
			ma[i] = 1
		if data[i - 1] > data[i] < data[i + 1]:
			ans -= data[i]
			ma[i] = -1
	print(ans)
	for i in range(q):
		(l, r) = idata()
		(l, r) = ([l - 1, l, l + 1], [r - 1, r, r + 1])
		for j in range(3):
			ans -= ma[l[j]] * data[l[j]]
			if r[j] not in l:
				ans -= ma[r[j]] * data[r[j]]
		(data[r[1]], data[l[1]]) = (data[l[1]], data[r[1]])
		for j in range(3):
			if 0 < l[j] < n + 1:
				if data[l[j] - 1] < data[l[j]] > data[l[j] + 1]:
					ans += data[l[j]]
					ma[l[j]] = 1
				elif data[l[j] - 1] > data[l[j]] < data[l[j] + 1]:
					ans -= data[l[j]]
					ma[l[j]] = -1
				else:
					ma[l[j]] = 0
			if r[j] not in l:
				if 0 < r[j] < n + 1:
					if data[r[j] - 1] < data[r[j]] > data[r[j] + 1]:
						ans += data[r[j]]
						ma[r[j]] = 1
					elif data[r[j] - 1] > data[r[j]] < data[r[j] + 1]:
						ans -= data[r[j]]
						ma[r[j]] = -1
					else:
						ma[r[j]] = 0
		print(ans)
	return
for t in range(int(ii())):
	solve()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	i = 0
	d = 1
	while i < n - 1:
		if d == 1:
			if a[i + 1] < a[i]:
				ans += a[i]
				d *= -1
		elif a[i + 1] > a[i]:
			ans -= a[i]
			d *= -1
		i += 1
	if d == 1:
		ans += a[-1]
	print(ans)


import sys
read = lambda : sys.stdin.readline().strip()
readi = lambda : map(int, read().split())
from collections import *
from math import *
for _ in range(int(read())):
	(n, q) = readi()
	nums = list(readi())
	ans = 0
	temp = [nums[0]]
	for i in range(1, n):
		if temp[-1] < nums[i]:
			if len(temp) > 1:
				ans += temp[0] - temp[-1]
			temp = list()
		temp.append(nums[i])
	print(ans + temp[0])


from sys import stdin, stdout
input = stdin.readline
a = []
ans = 0
n = 0
c = 0
b = []

def f(i):
	if a[i] == 0:
		return 0
	if a[i - 1] < a[i] > a[i + 1]:
		return 1
	if a[i - 1] > a[i] < a[i + 1]:
		return -1
	return 0

def relax(i):
	global c
	c += (f(i) - b[i]) * a[i]
	b[i] = f(i)

def relax1(i):
	global c
	c -= b[i] * a[i]
	b[i] = 0

def up(id):
	global ans
	global n
	if id < 1 or id > n:
		return
	if a[id - 1] < a[id] and a[id + 1] < a[id]:
		ans += a[id]
	if a[id - 1] > a[id] and a[id + 1] > a[id]:
		ans -= a[id]

def erase(id):
	global ans
	global n
	if id < 1 or id > n:
		return
	if a[id - 1] < a[id] and a[id + 1] < a[id]:
		ans -= a[id]
	if a[id - 1] > a[id] and a[id + 1] > a[id]:
		ans += a[id]
for _ in range(int(input())):
	ans = 0
	c = 0
	(n, q) = map(int, input().split())
	(*a,) = map(int, input().split())
	a = [0] + a + [0]
	b = [0] * (n + 2)
	for i in range(1, n + 1):
		relax(i)
	print(c)
	for i in range(q):
		(l, r) = map(int, input().split())
		relax1(l)
		relax1(l - 1)
		relax1(l + 1)
		relax1(r)
		relax1(r - 1)
		relax1(r + 1)
		(a[l], a[r]) = (a[r], a[l])
		relax(l)
		relax(l - 1)
		relax(l + 1)
		relax(r)
		relax(r - 1)
		relax(r + 1)
		print(c)


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	if n == 1:
		print(l[0])
	else:
		(flag, first) = (0, 0)
		ans = l[0] if l[0] > l[1] else 0
		if ans == 0:
			flag = 1
		else:
			first = ans
		for i in range(1, n - 1):
			if l[i - 1] < l[i] > l[i + 1] and flag:
				if first == 0:
					first = l[i]
				ans += l[i]
				flag = 0
			elif l[i - 1] > l[i] < l[i + 1] and flag == 0:
				ans -= l[i]
				flag = 1
		if l[-1] > l[-2]:
			if flag:
				ans += l[-1]
			else:
				ans = max(ans, ans + l[-1] - first)
		if ans == 0:
			ans = max(l)
		print(ans)


import math

def solve():
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	d = [a[0], -math.inf]
	for i in range(1, n):
		d[0] = max(a[i], a[i] + d[1], d[0])
		d[1] = max(-a[i] + d[0], d[1])
	print(max(d))
t = int(input())
i = 0
while i < t:
	solve()
	i += 1


for dsds in range(int(input())):
	(n, sa) = map(int, input().split())
	s = list(map(int, input().split()))
	q = [0 for i in range(n)]
	w = [0 for i in range(n)]
	w[0] = s[0]
	for i in range(1, n):
		q[i] = max(q[i], q[i - 1])
		q[i] = max(q[i], w[i - 1] - s[i])
		w[i] = max(w[i], w[i - 1])
		w[i] = max(w[i], q[i - 1] + s[i])
	print(w[n - 1])


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')

def solve():
	for _ in range(ii()):
		(n, q) = mi()
		a = li()
		n1 = n
		for i in range(q):
			(l, r) = mi()
		p = []
		f = 0
		for i in range(n - 1):
			if f == 0:
				if a[i] > a[i + 1]:
					p.append(a[i])
					f ^= 1
			elif a[i] < a[i + 1]:
				p.append(a[i])
				f ^= 1
		if f == 0:
			p.append(a[-1])
		if len(p) == 0:
			print(n)
		else:
			ans = 0
			n = len(p)
			for i in range(1, n, 2):
				ans += p[i - 1] - p[i]
			if n & 1:
				ans += p[-1]
			print(max(ans, n1))
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
solve()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	if n == 1:
		print(1)
		continue
	ans = 0
	for i in range(n):
		if i == 0:
			if arr[0] > arr[1]:
				ans += arr[0]
		elif i == n - 1:
			if arr[-1] > arr[-2]:
				ans += arr[-1]
		elif arr[i] > max(arr[i - 1], arr[i + 1]):
			ans += arr[i]
		elif ans > 0 and arr[i] < min(arr[i - 1], arr[i + 1]):
			ans -= arr[i]
	print(ans)


from math import ceil, log, floor, sqrt
import math
k = 1

def mod_expo(n, p, m):
	result = 1
	while p != 0:
		if p % 2 == 1:
			result = result * n % m
		p //= 2
		n = n * n % m
	return result

def test():
	for i in range(1, 50):
		for j in range(i, 50):
			if i & j >= i ^ j:
				print(i, j)

def get_mx(n, a):
	d1 = [-1000000000.0]
	d2 = [0]
	for i in range(n):
		d1.append(max(d1[i], d2[i] + a[i]))
		d2.append(max(d2[i], d1[i] - a[i]))
	return max(d1[n], d2[n])
t = 1
t = int(input())
while t:
	t = t - 1
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	count = 0
	print(get_mx(n, arr))


t = int(input())
while t:
	t -= 1
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	z = 0
	s = 0
	(k, zz) = (0, 0)
	m = max(a)
	if n == 2:
		print(m)
		continue
	for i in range(1, n):
		if z == 0 and a[i - 1] > a[i]:
			zz = a[i - 1]
			s += zz
			z = 1
		elif z == 1 and a[i - 1] < a[i] and (zz > a[i - 1]):
			s -= a[i - 1]
			z = 0
			k = i - 1
	if z == 0:
		s += max(a[k:])
	print(max(s, m))


for t in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	log = 0
	ans = 0
	for i in range(1, n):
		if log == 0:
			if a[i] < a[i - 1]:
				log = 1
				ans += a[i - 1]
		elif a[i] > a[i - 1]:
			log = 0
			ans -= a[i - 1]
	if log == 0:
		ans += a[n - 1]
	print(ans)


from collections import Counter
import string
import math
import sys

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(number_of_variables):
	if number_of_variables == 1:
		return int(sys.stdin.readline())
	if number_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
mod = 100000007
import math

def countBits(number):
	return int(math.log(number) / math.log(2) + 1)
for _ in range(vary(1)):
	(n, q) = vary(2)
	num = array_int()
	sumt = 0
	tt = []
	k = 0
	flag = 0
	for i in range(1, n):
		if k % 2 == 0 and num[i] > num[i - 1]:
			flag = i
			continue
		elif k % 2 != 0 and num[i] < num[i - 1]:
			continue
		else:
			k += 1
			tt.append(num[i - 1])
	if len(tt) % 2 == 0:
		tt.append(num[-1])
	n = 1
	for i in range(len(tt)):
		sumt += tt[i] * n
		n = n * -1
	print(sumt)


def func(a):
	(mx, mn) = (a[-1], a[-1])
	for i in range(n - 2, -1, -1):
		(mx, mn) = (max(a[i] - min(mn, 0), mx), min(a[i] - max(mx, 0), mn))
	return mx
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	mx = func(a)
	print(mx)


def sol():
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	a.append(0)
	cur = float('-inf')
	res = 0
	for num in a:
		if num < cur:
			res += cur - num
		cur = num
	print(res)

def main():
	T = int(input())
	for t in range(T):
		sol()
main()


t = int(input())
for _ in range(t):
	(n, q) = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	sign = 0
	s = 0
	i = 0
	while i < n:
		m = l[i]
		if sign == 0:
			while i < n and l[i] >= m:
				m = l[i]
				i += 1
			i -= 1
			s += l[i]
			sign = 1
		else:
			while i < n and l[i] <= m:
				m = l[i]
				i += 1
			i -= 1
			if i != n - 1:
				s -= l[i]
			sign = 0
		i += 1
	print(s)


for _ in range(int(input())):
	(n, q) = tuple(map(int, input().split()))
	arr = list(map(int, input().split()))
	i = 0
	res = 0
	for k in range(n):
		if k + 1 == n:
			if i == 0:
				res += arr[k]
		else:
			if arr[k] > arr[k + 1] and i == 0:
				res += arr[k]
				i = 1
			if arr[k] < arr[k + 1] and i == 1:
				res -= arr[k]
				i = 0
	print(res)


t = int(input())

def solve():
	ans = l[0]
	curr = 0
	(cnt, suma) = (0, 0)
	(f, s) = (0, 0)
	l.insert(0, 0)
	n = len(l)
	msum = 0
	for i in range(1, n):
		if l[i] >= l[i - 1]:
			s = 0
			if f == 0:
				suma -= curr
			curr = l[i]
			f = 1
		else:
			if s == 0:
				suma += curr
				msum = max(msum, suma)
			s = 1
			f = 0
			curr = l[i]
	if s == 0:
		suma += curr
		msum = max(msum, suma)
	return msum
for _ in range(t):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	print(solve())


import sys
import math
from collections import defaultdict, Counter
input = sys.stdin.readline

def print(x):
	sys.stdout.write(str(x) + '\n')
t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	for j in range(q):
		(l, r) = map(int, input().split())
	flag = 0
	mi = a[0]
	dp = [0] * (n + 1)
	dp[1] = a[0]
	mi = [0, 0]
	for j in range(1, n):
		if a[j] > a[j - 1]:
			dp[j + 1] = dp[mi[1]] + a[j] - mi[0]
		else:
			dp[j + 1] = dp[j]
			mi = [a[j], j + 1]
	print(dp[n])


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	maxi = 0
	flag = 1
	for i in range(n - 1):
		if flag == 1 and arr[i] > arr[i + 1]:
			maxi += arr[i]
			flag = 0
		if flag == 0 and arr[i] < arr[i + 1]:
			maxi -= arr[i]
			flag = 1
	if flag == 1:
		maxi += arr[n - 1]
	print(maxi)


for _ in range(int(input())):
	(n, q) = list(map(int, input().split()))
	arr = [-999999999999] + list(map(int, input().split())) + [-99999999999]
	picked = []
	flag = False
	for i in range(1, n + 1):
		if not flag:
			if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
				picked.append(arr[i])
				flag = not flag
		elif arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			picked.append(arr[i])
			flag = not flag
	s = 0
	flag = True
	for i in range(len(picked)):
		if flag:
			s += picked[i]
		else:
			s -= picked[i]
		flag = not flag
	print(s)


def main():
	t = int(input())
	for _ in range(t):
		(n, q) = [int(x) for x in input().split()]
		strengths = [int(y) for y in input().split()]
		answers = [[0, 0]] * (n + 1)
		for i in range(1, n + 1):
			answers[i][0] = max(answers[i - 1][0], answers[i - 1][1] - strengths[i - 1])
			answers[i][1] = max(answers[i - 1][1], answers[i - 1][0] + strengths[i - 1])
		print(max(answers[n][0], answers[n][1]))
main()


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print(arr[0])
		continue
	ans = 0
	boo = 1
	for i in range(n - 1):
		if arr[i + 1] < arr[i]:
			if boo == 1:
				ans += arr[i]
				boo = 0
		elif arr[i + 1] > arr[i]:
			if boo == 0:
				boo = 1
				ans -= arr[i]
	if arr[n - 1] >= arr[n - 2]:
		ans += arr[n - 1]
	print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	if len(l) == 1:
		print(l[0])
	else:
		peak = []
		if l[0] > l[1]:
			peak.append(l[0])
		for i in range(1, n - 1):
			if l[i] > l[i - 1] and l[i] > l[i + 1]:
				peak.append(l[i])
			elif l[i - 1] > l[i] and l[i] < l[i + 1]:
				peak.append(l[i])
		if l[-1] > l[-2]:
			peak.append(l[-1])
		f = 0
		s = 0
		for i in range(len(peak)):
			if f == 0:
				s += peak[i]
				f = 1
			else:
				s -= peak[i]
				f = 0
		print(s)


import sys, math
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 3:
		print(max(max(a), a[0] - a[1] + a[2]))
	elif n == 2:
		print(max(a))
	else:
		c = 1
		ans = 0
		cur = True
		while c < n:
			if cur:
				if a[c] < a[c - 1]:
					ans += a[c - 1]
					cur = False
			elif a[c - 1] < a[c]:
				ans -= a[c - 1]
				cur = True
			c += 1
		if cur:
			ans += a[-1]
		print(ans)


for t in range(int(input())):
	nq = list(map(int, input().split()))
	a = list(map(int, input().split()))
	mx = [0] * nq[0]
	mn = [0] * nq[0]
	mx[nq[0] - 1] = a[nq[0] - 1]
	mn[nq[0] - 1] = a[nq[0] - 1]
	for i in range(nq[0] - 2, -1, -1):
		mx[i] = max(mx[i + 1], a[i] - mn[i + 1], a[i])
		mn[i] = min(mn[i + 1], a[i] - mx[i + 1], a[i])
	print(mx[0])


from sys import stdin
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, stdin.readline().split()))
	prev = a[0]
	up = 1
	cur = 0
	ans = 0
	for i in range(1, n):
		if up and a[i] > prev or (not up and a[i] < prev):
			prev = a[i]
		else:
			cur += -prev if not up else prev
			up ^= 1
			ans = max(ans, cur)
			prev = a[i]
	print(max(ans, cur + (-a[-1] if not up else a[-1])))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = []
	i = 1
	maxx = arr[0]
	minn = -1
	while i < n:
		if arr[i] > maxx:
			maxx = arr[i]
			minn = -1
		else:
			minn = arr[i]
			i += 1
			while i < n and maxx > maxx - minn + arr[i]:
				minn = min(minn, arr[i])
				i += 1
			ans.append(maxx)
			if i != n:
				ans.append(minn)
				maxx = arr[i]
			else:
				maxx = -1
		i += 1
	if maxx != -1:
		ans.append(maxx)
	count = 0
	flag = -1
	for i in range(len(ans)):
		flag = flag * -1
		value = flag * ans[i]
		count += value
	print(count)


import sys

def main():
	T = int(input())
	for ti in range(T):
		(n, q) = map(int, input().split())
		A = [int(a) for a in input().split()]
		A.append(0)
		ans = 0
		for i in range(len(A) - 1):
			s = A[i] - A[i + 1]
			if s > 0:
				ans += s
		print(ans)
main()


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	a.append(0)
	ans = 0
	b = []
	for i in range(n):
		if not a[i] == a[i + 1]:
			b.append(a[i])
	m = len(b)
	b.append(0)
	b.insert(0, 0)
	for i in range(m):
		if b[i] < b[i + 1] and b[i + 1] > b[i + 2]:
			ans += b[i + 1]
		elif b[i] > b[i + 1] and b[i + 1] < b[i + 2]:
			ans -= b[i + 1]
	print(ans)


def solve(n, q, a):
	ans = 0
	up = True
	if n == 1:
		return a[0]
	for i in range(n - 1):
		if a[i + 1] < a[i] and up:
			ans += a[i]
			up = False
		elif a[i + 1] > a[i] and (not up):
			ans -= a[i]
			up = True
	if up:
		ans += a[-1]
	return ans
for t in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	print(solve(n, q, a))


def solve():
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
		return
	inc = 1
	ans = 0
	a.append(0)
	for (i, v) in enumerate(a[:n]):
		if inc == 1 and v > a[i + 1]:
			ans += v
			inc = 0
		elif inc == 0 and v < a[i + 1]:
			ans -= v
			inc = 1
	print(ans)
	return

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()
main()


import io, os
inp = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
from math import log2
_T_ = inin()
for _t_ in range(_T_):
	(n, q) = inar()
	a = [0] + inar() + [0]
	diff = []
	for i in range(n + 1):
		diff.append(max(0, a[i + 1] - a[i]))
	print(sum(diff))
	ans = sum(diff)
	for qq in range(q):
		(l, r) = inar()
		al = a[l]
		ar = a[r]
		a[l] = ar
		ans -= diff[l - 1]
		ans -= diff[l]
		diff[l - 1] = max(0, a[l] - a[l - 1])
		diff[l] = max(0, a[l + 1] - a[l])
		ans += diff[l - 1]
		ans += diff[l]
		a[r] = al
		ans -= diff[r - 1]
		ans -= diff[r]
		diff[r - 1] = max(0, a[r] - a[r - 1])
		diff[r] = max(0, a[r + 1] - a[r])
		ans += diff[r - 1]
		ans += diff[r]
		print(ans)


import sys
input = sys.stdin.readline
ansl = []
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	al = list(map(int, input().split()))
	if n == 1:
		ansl.append(al[0])
		for _ in range(q):
			(l, r) = map(int, input().split())
			ansl.append(al[0])
		continue
	al = [-100] + al + [-100]
	ud = [0] * (n + 2)
	ans = 0
	for i in range(1, n + 1):
		if al[i - 1] > al[i] and al[i] < al[i + 1]:
			ud[i] = -1
			ans -= al[i]
		elif al[i - 1] < al[i] and al[i] > al[i + 1]:
			ud[i] = 1
			ans += al[i]
	ansl.append(ans)
	for _ in range(q):
		(l, r) = map(int, input().split())
		changed = set()
		for i in [-1, 0, 1]:
			ll = l + i
			rr = r + i
			if 0 < ll < n + 1 and ll not in changed:
				ans -= ud[ll] * al[ll]
				changed.add(ll)
			if 0 < rr < n + 1 and rr not in changed:
				ans -= ud[rr] * al[rr]
				changed.add(rr)
		changed = set()
		(al[l], al[r]) = (al[r], al[l])
		for i in [-1, 0, 1]:
			ll = l + i
			if not 0 < ll < n + 1:
				continue
			if al[ll - 1] > al[ll] and al[ll] < al[ll + 1]:
				ud[ll] = -1
				ans -= al[ll]
				changed.add(ll)
			elif al[ll - 1] < al[ll] and al[ll] > al[ll + 1]:
				ud[ll] = 1
				ans += al[ll]
				changed.add(ll)
			else:
				ud[ll] = 0
		for i in [-1, 0, 1]:
			rr = r + i
			if not 0 < rr < n + 1:
				continue
			if rr in changed:
				continue
			if al[rr - 1] > al[rr] and al[rr] < al[rr + 1]:
				ud[rr] = -1
				ans -= al[rr]
			elif al[rr - 1] < al[rr] and al[rr] > al[rr + 1]:
				ud[rr] = 1
				ans += al[rr]
			else:
				ud[rr] = 0
		ansl.append(ans)
for a in ansl:
	print(a)


import os
import sys
from io import BytesIO, IOBase

def main1():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		cnt = 0
		for i in range(1, n):
			if a[i] < a[i - 1]:
				cnt += 1
		if cnt == n - 1:
			print('NO')
		else:
			print('YES')

def main2():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		ans = 0
		cnt = [0] * 100
		for i in range(n):
			c = len(bin(a[i])[2:])
			ans += cnt[c]
			cnt[c] += 1
		print(ans)

def main3():
	for _ in range(int(input())):
		(n, q) = map(int, input().split())
		a = list(map(int, input().split()))
		flag = 0
		ans = 0
		for i in range(1, n):
			if not flag:
				if a[i] < a[i - 1]:
					ans += a[i - 1]
					flag = 1
			elif a[i] > a[i - 1]:
				ans -= a[i - 1]
				flag = 0
		if not flag:
			ans += a[-1]
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main3()


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f():
	(n, q) = [int(s) for s in input().split()]
	A = [int(s) for s in input().split()]
	minusMax = [0] * (n + 1)
	plusMax = [0] * (n + 1)
	for i in range(n):
		m = plusMax[i - 1] - A[i]
		p = minusMax[i - 1] + A[i]
		minusMax[i] = max(minusMax[i - 1], m)
		plusMax[i] = max(plusMax[i - 1], p)
	ans = max(minusMax[n - 1], plusMax[n - 1])
	print(ans)
for t in range(int(input())):
	f()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	(*monsters,) = map(int, input().split())
	i = 0
	s = 0
	powers = []
	while i < n:
		while i + 1 < n and monsters[i] < monsters[i + 1]:
			i += 1
		if i < n:
			powers.append(monsters[i])
		i += 1
		while i + 1 < n and monsters[i] > monsters[i + 1]:
			i += 1
		if i < n:
			powers.append(-monsters[i])
		i += 1
	if len(powers) % 2 == 0:
		powers.pop()
	print(sum(powers))


test = int(input())
for _ in range(test):
	(n, q) = map(int, input().split())
	a = [0]
	kr = [int(u) for u in input().split()]
	a.extend(kr)
	a.append(0)
	ans = max(a)
	ar = []
	for i in range(1, len(a) - 1):
		if a[i + 1] < a[i] > a[i - 1]:
			ar.append(a[i])
		elif a[i + 1] > a[i] < a[i - 1]:
			ar.append(-a[i])
	if len(ar) != 0 and ar[len(ar) - 1] < 0:
		ar[len(ar) - 1] = 0
	print(max(ans, sum(ar)))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = []
	inc = 0
	dec = 0
	if n == 1:
		ans.append(l[0])
	for i in range(1, n):
		if l[i - 1] > l[i] and dec == 0:
			ans.append(l[i - 1])
			inc = 0
			dec = 1
		elif l[i - 1] < l[i] and inc == 0:
			if len(ans) != 0:
				inc = 1
				dec = 0
				ans.append(l[i - 1])
			else:
				dec = 0
				inc = 1
	if inc == 1:
		ans.append(l[-1])
	sum = 0
	le = len(ans)
	for i in range(le):
		if i % 2 == 0:
			sum += ans[i]
		else:
			sum -= ans[i]
	print(sum)


l = []
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	a = [0] + a + [0]
	n = n + 2
	max = a[0]
	min = a[0]
	s = []
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			if a[i] <= max:
				s.append(max)
				max = 0
			min = a[i + 1]
		else:
			if a[i] >= min:
				s.append(min)
				min = 10000000000
			max = a[i + 1]
	sum = 0
	for i in range(len(s)):
		if i % 2 == 0:
			sum = sum + s[i]
		else:
			sum = sum - s[i]
	l.append(abs(sum))
for i in l:
	print(i)


for i in range(int(input())):
	(n, q) = list(map(int, input().split()))
	lst = list(map(int, input().split()))
	oddsum = [0] * (n + 1)
	evensum = [0] * (n + 1)
	oddsum[0] = -1000000007
	evensum[0] = 0
	for i in range(n):
		oddsum[i + 1] = max(oddsum[i], evensum[i] + lst[i])
		evensum[i + 1] = max(evensum[i], oddsum[i] - lst[i])
	print(max(evensum[-1], oddsum[-1]))


from enum import *
from copy import *
from sys import *
from collections import *
from itertools import *
from functools import *
from math import *

def l2i(fr=None):
	return map_type_to_fr(int, fr)[0]

def i2i(fr=None):
	return l2i(fr)

def dbg(*args):
	print(*args, file=stderr)

def dbv(var_name):
	print(f'{var_name} = {eval(var_name)}', file=stderr)

def map_type_to_fr(t, fr):
	global GETLINE_OFFSET
	if fr is None:
		if RUNNING_ON_MY_PC:
			fr = TEST_INP[GETLINE_OFFSET].split()
			GETLINE_OFFSET += 1
		else:
			fr = input().split()
	return list(map(t, fr))

def mis(fr=None):
	return map_type_to_fr(int, fr)

def mbs(fr=None):
	return map_type_to_fr(bool, fr)

def gs(fr=None):
	return map_type_to_fr(str, fr)[0]
import os
import sysconfig
RUNNING_ON_MY_PC = os.name == 'nt' and platform == 'win32' and (sysconfig.get_platform() == 'win-amd64')
GETLINE_OFFSET = 0
TEST_INP = '3\n3 0\n1 3 2\n2 0\n1 2\n7 0\n1 2 5 4 3 6 7\n'.split('\n')

def solve():
	n = i2i()
	a = mis()
	ans = a[0]
	prev = a[0]
	for x in a:
		delta = x - prev
		if delta > 0:
			ans += delta
		prev = x
	return ans
q = i2i()
for i in range(q):
	print(solve())


T = int(input())
res = list()
for i in range(T):
	y = list(map(int, input().split()))
	x = list(map(int, input().split()))
	res.append(x)
for i in range(len(res)):
	sum = 0
	result = [0] + res[i] + [0]
	for j in range(1, len(result) - 1):
		if result[j - 1] < result[j] > result[j + 1]:
			sum += result[j]
		elif result[j - 1] > result[j] < result[j + 1]:
			sum -= result[j]
	print(sum)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict, Counter
from os import path
import bisect as bi
import heapq
mod = 10 ** 9 + 7

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def inp():
		return int(input())

	def minp():
		return map(int, input().split())
else:

	def inp():
		return int(stdin.readline())

	def minp():
		return map(int, stdin.readline().split())
for _ in range(inp()):
	(n, q) = map(int, input().split())
	a = map(int, input().split())
	s = m = 0
	for i in a:
		if i > m:
			s += i - m
		s = max(s, i)
		m = i
	print(s)


T = int(input())
for t in range(T):
	(n, q) = [int(i) for i in input().split()]
	A = [int(i) for i in input().split()]
	pos = [0] * n
	neg = [0] * n
	pos[0] = A[0]
	ans = A[0]
	for i in range(1, n):
		pos[i] = max(pos[i - 1], neg[i - 1] + A[i])
		ans = max(ans, pos[i])
		neg[i] = max(neg[i - 1], pos[i - 1] - A[i])
		ans = max(ans, neg[i])
	print(ans)


from collections import *
for u in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	c = 0
	for i in range(n - 1):
		if l[i] >= l[i + 1]:
			c += l[i] - l[i + 1]
	print(l[-1] + c)


ans = []
n = int(input())

def check(lst):
	cur = [lst[0], 0]
	for i in range(1, len(lst)):
		a = max(cur[0], cur[1] + lst[i])
		b = max(cur[1], cur[0] - lst[i])
		cur = [a, b]
	return max(cur)
for i in range(n):
	(p, q) = map(int, input().split())
	lst = list(map(int, input().split()))
	ans.append(check(lst))
for i in ans:
	print(i)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	m = max(a)
	i = 0
	while i < n:
		j = i
		check1 = 0
		if j == n - 1:
			ans += a[j]
			break
		while a[j] <= a[j + 1]:
			j += 1
			if j == n - 1:
				check1 = 1
				break
		if check1 == 1:
			ans += a[n - 1]
			break
		else:
			ans += a[j]
		i = j + 1
		j = i
		check1 = 0
		if j == n - 1:
			break
		while not (a[j] < a[j - 1] and a[j] < a[j + 1]):
			j += 1
			if j == n - 1:
				check1 = 1
				break
		if check1 == 1:
			break
		else:
			ans -= a[j]
		i = j + 1
	print(max(m, ans))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	ar = list(map(int, input().split()))
	ans = ar[0]
	for i in range(1, n):
		ans += max(0, ar[i] - ar[i - 1])
	print(ans)


t = int(input())
for i in range(t):
	(n, q) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	for j in range(q):
		(l, r) = list(map(int, input().split()))
	ans = arr[0]
	s = 0
	j = 1
	lis = []
	while j < n:
		if s == 0:
			if arr[j] > arr[j - 1]:
				ans = arr[j]
			else:
				lis.append(ans)
				s = 1
				ans = arr[j]
		elif arr[j] < arr[j - 1]:
			ans = arr[j]
		else:
			lis.append(-1 * ans)
			s = 0
			ans = arr[j]
		j += 1
	if s == 0:
		lis.append(ans)
	a = len(lis)
	if a % 2 == 1:
		print(sum(lis))
	else:
		print(sum(lis[:a - 1]))


for w in range(int(input())):
	(n, q) = tuple(map(int, input().split()))
	a = list(map(int, input().split()))
	l = []
	l1 = []
	ans = 0
	if n == 2 or n == 1:
		print(max(a))
	else:
		i = 0
		l1.append(a[0])
		while i < n - 1:
			while i < n - 1 and a[i] < a[i + 1]:
				l1.append(a[i + 1])
				i += 1
			if l1 != []:
				l.append(l1)
				l1 = []
			while i < n - 1 and a[i] > a[i + 1]:
				l1.append(a[i + 1])
				i += 1
			if l1 != []:
				l.append(l1)
				l1 = []
		if len(l) == 1:
			print(max(l[0]))
		else:
			i = 0
			while i < len(l) - 1:
				ma = max(l[i])
				mi = min(l[i + 1])
				if i == len(l) - 2:
					mi = 0
				if ma - mi > 0:
					ans += ma - mi
					i += 2
				else:
					i += 1
			if i == len(l) - 1:
				ans += max(l[-1])
			print(ans)


def main():
	for _ in range(int(input())):
		(n, q) = list(map(int, input().split()))
		a = list(map(int, input().split()))
		op = [list(map(int, input().split())) for i in range(q)]
		w = max(range(n), key=lambda x: a[x])
		w1 = 0
		lm = 0
		l = []
		i = 0
		while i < w:
			if a[i] >= lm:
				lm = a[i]
			elif lm != 0:
				m = a[i]
				while i + 1 < n and m >= a[i + 1]:
					m = a[i + 1]
					i += 1
				l.append([lm, a[i]])
				w1 += lm - m
				lm = 0
			i += 1
		lm = float('inf')
		i = w + 1
		while i < n:
			if lm >= a[i]:
				lm = a[i]
			elif lm != float('inf'):
				m = a[i]
				while i + 1 < n and m <= a[i + 1]:
					m = a[i + 1]
					i += 1
				l.append([lm, a[i]])
				w1 += m - lm
				lm = float('inf')
			i += 1
		print(w1 + a[w])
main()


from sys import stdin
tt = int(stdin.readline())
pls = []
for loop in range(tt):
	(n, q) = map(int, stdin.readline().split())
	a = list(map(int, stdin.readline().split()))
	a = [0] + a + [0]
	lis = [0] * len(a)
	ans = 0
	for i in range(1, len(a) - 1):
		if a[i - 1] < a[i] and a[i] > a[i + 1]:
			lis[i] = 1
			ans += a[i]
		elif a[i - 1] > a[i] and a[i] < a[i + 1]:
			lis[i] = -1
			ans -= a[i]
	pls.append(ans)
	for loop in range(q):
		(l, r) = map(int, stdin.readline().split())
		if l == r:
			pls.append(ans)
			continue
		ch = {}
		ch[l - 1] = 0
		ch[l] = 0
		ch[l + 1] = 0
		ch[r - 1] = 0
		ch[r] = 0
		ch[r + 1] = 0
		for i in ch:
			ans -= a[i] * lis[i]
		(a[l], a[r]) = (a[r], a[l])
		for i in ch:
			if i == 0 or i == len(a) - 1:
				pass
			elif a[i - 1] < a[i] and a[i] > a[i + 1]:
				lis[i] = 1
				ans += a[i]
			elif a[i - 1] > a[i] and a[i] < a[i + 1]:
				lis[i] = -1
				ans -= a[i]
			else:
				lis[i] = 0
		pls.append(ans)
print('\n'.join(map(str, pls)))


from sys import stdin
for _ in ' ' * int(stdin.readline()):
	(a, b) = map(int, stdin.readline().split())
	z = list(map(int, input().split()))
	s = z[-1]
	k = 1
	i = 0
	while i < a - 1:
		while i < a - 1 and z[i + 1] >= z[i]:
			i += 1
		n = z[i]
		while i < a - 1 and z[i] > z[i + 1]:
			i += 1
		s += n - z[i]
	print(s)


def main():
	(aa, bb) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	a = [0] + a
	re = []
	flag = 'u'
	i = 0
	ans = 0
	n = len(a)
	for i in range(1, n):
		if a[i] > a[i - 1]:
			if flag == 'd':
				re.append(a[i - 1])
			flag = 'u'
		elif a[i] < a[i - 1]:
			if flag == 'u':
				re.append(a[i - 1])
			flag = 'd'
		if i == n - 1:
			if flag == 'u':
				re.append(a[i])
	ans = 0
	i = 0
	sign = 1
	while i < len(re):
		ans += re[i] * sign
		sign = -1 * sign
		i += 1
	print(ans)
t = int(input())
for i in range(t):
	main()


t = int(input())
while t:
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	count = 0
	check = 0
	last = -1
	for i in range(0, n):
		if check == 0:
			if i + 1 >= n or a[i + 1] < a[i]:
				if i - 1 < 0 or a[i - 1] < a[i]:
					count = count + a[i]
					check = 1
		elif i + 1 < n and a[i + 1] > a[i]:
			if i - 1 < 0 or a[i - 1] > a[i]:
				count = count - a[i]
				check = 0
	print(count)
	t = t - 1


cases = int(input())
for _ in range(cases):
	(n, q) = map(int, input().split())
	c = list(map(int, input().split()))
	ans = 0
	for i in range(len(c) - 1):
		ans += max(0, c[i] - c[i + 1])
	ans += c[-1]
	print(ans)


for i in range(int(input())):
	A = input().split()
	num = int(A[0])
	B = list(map(int, input().split()))
	c = []
	d = []
	p = 0
	e = 0
	if len(B) == 1 or len(B) == 2:
		print(max(B))
	else:
		if B[0] > B[1]:
			c += [B[0]]
		if B[-1] > B[-2]:
			c += [B[-1]]
		for j in range(1, len(B) - 1):
			if B[j] > B[j - 1] and B[j] > B[j + 1]:
				p = 0
				c += [B[j]]
			elif B[j - 1] > B[j] and B[j] < B[j + 1]:
				p = 1
				d += [B[j]]
				e = B[j]
		print(sum(c) - sum(d))


from sys import stdin
from collections import deque
input = stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	s = list(map(int, input().split()))
	d = deque()
	pb = True
	for i in range(n):
		if pb:
			if i == 0 or s[i] > s[i - 1]:
				if i == n - 1 or s[i] > s[i + 1]:
					d.append(s[i])
					pb = False
		else:
			if i == n - 1:
				break
			if i == 0 or s[i] < s[i - 1]:
				if s[i] < s[i + 1]:
					d.append(s[i])
					pb = True
	res = 0
	dir = 1
	for i in d:
		res += i * dir
		dir *= -1
	print(res)


t = int(input())
for t1 in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	max_n = 0
	curr = a[0]
	plus = True
	for i in range(1, n):
		if plus:
			if curr < a[i]:
				curr = a[i]
			else:
				max_n += curr
				curr = a[i]
				plus = False
		elif curr > a[i]:
			curr = a[i]
		else:
			max_n -= curr
			curr = a[i]
			plus = True
	if plus:
		max_n += a[n - 1]
	print(max_n)


import sys

def solve(n, q, a, u):
	z = []

	def chk(i):
		if a[i - 1] < a[i] > a[i + 1]:
			return a[i]
		if a[i - 1] > a[i] < a[i + 1]:
			return -a[i]
		return 0
	if n == 1:
		z.append(a[0])
		for i in u:
			z.append(a[0])
	else:
		a = [-1] + a + [-1]
		c = 0
		for i in range(1, n + 1):
			c += chk(i)
		z.append(c)
		for (l, r) in u:
			if l == r:
				z.append(c)
			else:
				b = {l, max(1, l - 1), min(n, l + 1), r, max(1, r - 1), min(n, r + 1)}
				for j in b:
					c -= chk(j)
				(a[l], a[r]) = (a[r], a[l])
				for j in b:
					c += chk(j)
				z.append(c)
	return z

def stdin():
	while True:
		yield sys.stdin.readline()
inputs = stdin()
t = int(next(inputs))
z = []
for _ in range(t):
	(n, q) = map(int, next(inputs).split())
	a = list(map(int, next(inputs).split()))
	u = [map(int, next(inputs).split()) for i in range(q)]
	z += solve(n, q, a, u)
print('\n'.join(map(str, z)))


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	cnt = 0
	dp = []
	dp1 = []
	l = list(map(int, input().split()))[:a]
	dp.append(l[0])
	dp1.append(0)
	for j in range(1, a):
		dp.append(max(dp[j - 1], dp1[j - 1] + l[j]))
		dp1.append(max(dp1[j - 1], dp[j - 1] - l[j]))
	print(max(dp[a - 1], dp1[a - 1]))


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))

def localMaxima(arr, d):
	if d == 0:
		if arr[d] > arr[d + 1]:
			return True
	elif d == len(arr) - 1:
		if arr[d] > arr[d - 1]:
			return True
	elif arr[d + 1] < arr[d] > arr[d - 1]:
		return True
	return False

def localMinima(arr, d):
	if d == 0:
		if arr[d] > arr[d + 1]:
			return True
	elif d == len(arr) - 1:
		if arr[d] > arr[d - 1]:
			return True
	elif arr[d + 1] < arr[d] > arr[d - 1]:
		return True
	return False
t = iinput()
while t:
	t -= 1
	(n, q) = minput()
	a = linput()
	if n == 1:
		print(a[0])
	else:
		x = 1
		strength = 0
		prev = 0
		if a[0] > a[1]:
			x = 0
			strength += a[0]
			prev = a[0]
		i = 1
		while i < n:
			if x:
				while i < n:
					if a[i] > a[i - 1]:
						i += 1
					else:
						break
				strength += a[i - 1]
				prev = a[i - 1]
				x ^= 1
			else:
				while i < n:
					if a[i] < a[i - 1]:
						i += 1
					else:
						break
				strength -= a[i - 1]
				prev = a[-1]
				x ^= 1
		if x:
			strength += prev
		print(strength)


import math
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	res1 = [0] * n
	res1[0] = arr[0]
	res2 = [0] * n
	res2[0] = arr[0]
	max_res1 = arr[0]
	max_res2 = 0
	for i in range(1, n):
		res1[i] = max(max_res2 + arr[i], arr[i])
		res2[i] = max_res1 - arr[i]
		max_res1 = max(max_res1, res1[i])
		max_res2 = max(max_res2, res2[i])
	print(max(max_res1, max_res2))


import bisect
import array
import functools
import math
input = __import__('sys').stdin.readline

def solve():
	(n, Q) = map(int, input().split())
	a = array.array('i', map(int, input().split()))

	def jud(x):
		if x < 0 or x >= n:
			return 0
		(p, s) = (0, 0)
		if x != 0:
			p = a[x - 1]
		if x + 1 != n:
			s = a[x + 1]
		if p < a[x] and s <= a[x]:
			return a[x]
		if p > a[x] and s >= a[x]:
			return -a[x]
		return 0
	ans = 0
	for i in range(n):
		ans = ans + jud(i)
	print(ans)
	for i in range(Q):
		(x, y) = map(int, input().split())
		(kx, ky) = (a[x - 1], a[y - 1])
		ans = ans - jud(x - 2) - jud(x - 1) - jud(x)
		a[x - 1] = ky
		ans = ans + jud(x - 2) + jud(x - 1) + jud(x)
		ans = ans - jud(y - 2) - jud(y - 1) - jud(y)
		a[y - 1] = kx
		ans = ans + jud(y - 2) + jud(y - 1) + jud(y)
		print(ans)
T = int(input())
for Tid in range(T):
	solve()


def main():
	iloP = int(input())
	for P in range(iloP):
		(iloL, asdsdasdasd) = list(map(int, input().split()))
		lista = list(map(int, input().split()))
		czy_pocz = True
		wyn = 0
		dodaj = False
		czy_mal = False
		ile = 0
		for i in range(1, iloL):
			if lista[i] > lista[i - 1]:
				if czy_mal:
					wyn -= lista[i - 1]
				czy_mal = False
			elif lista[i] < lista[i - 1]:
				if not czy_mal:
					wyn += lista[i - 1]
				czy_mal = True
		if not czy_mal:
			wyn += lista[-1]
		print(wyn)
main()


import sys
from bisect import bisect
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def solution():
	for _ in range(Int()):
		(n, q) = Mint()
		a = list(Mint())
		ok = 0
		tot = 0
		for i in range(n):
			if i == n - 1:
				if ok == 0:
					tot += a[i]
				break
			if ok == 0:
				if a[i] > a[i + 1]:
					ok = 1
					tot += a[i]
			elif a[i] < a[i + 1]:
				ok = 0
				tot -= a[i]
		print(tot)
solution()


input = __import__('sys').stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	s = list(map(int, input().split()))
	dp = [[0, 0] for i in range(n + 1)]
	for i in range(1, n + 1):
		dp[i][0] = max(dp[i - 1][1] + s[i - 1], dp[i - 1][0])
		dp[i][1] = max(dp[i - 1][0] - s[i - 1], dp[i - 1][1])
	print(max(dp[n]))


def solve(n, q, ar):
	ans = ar[0]
	for i in range(1, n):
		ans += max(0, ar[i] - ar[i - 1])
	print(ans)
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	ar = list(map(int, input().split()))
	solve(n, q, ar)


def ans(n, ar, q):
	i = 0
	flag = True
	summ = 0
	while i < n - 1:
		if flag:
			while i < n - 1 and ar[i + 1] > ar[i]:
				i += 1
			flag = False
			summ += ar[i]
		else:
			while i < n - 1 and ar[i + 1] < ar[i]:
				i += 1
			flag = True
			summ -= ar[i]
	if flag:
		summ += ar[-1]
	return summ
for i in range(int(input())):
	(n, q) = map(int, input().split())
	ar = [int(i) for i in input().split()]
	print(ans(n, ar, q))


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	j = 0
	cnt = 0
	kt = 0
	jt = 0
	while j < n - 1 and a[j + 1] >= a[j]:
		j += 1
	cnt += a[j]
	while j < n - 1:
		jt = 0
		kt = 0
		while j < n - 1 and a[j + 1] <= a[j]:
			j += 1
			jt = 1
		tmp = a[j] + 0
		while j < n - 1 and a[j + 1] >= a[j]:
			j += 1
			kt = 1
		if kt == jt == 1:
			if a[j] - tmp > 0:
				cnt += a[j] - tmp
	print(cnt)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	d1 = [0] * (n + 1)
	d1[0] = -1e+18
	d2 = [0] * (n + 1)
	for i in range(n):
		d1[i + 1] = max(d1[i], d2[i] + l[i])
		d2[i + 1] = max(d2[i], d1[i] - l[i])
	print(max(d1[n], d2[n]))


def findLocalMaximaMinima(n, arr):
	mx = []
	mn = []
	if len(arr) == 1:
		mx.append(0)
		return (mx, mn)
	if arr[0] > arr[1]:
		mx.append(0)
	elif arr[0] < arr[1]:
		mn.append(0)
	for i in range(1, n - 1):
		if arr[i - 1] > arr[i] < arr[i + 1]:
			mn.append(i)
		elif arr[i - 1] < arr[i] > arr[i + 1]:
			mx.append(i)
	if arr[-1] > arr[-2]:
		mx.append(n - 1)
	elif arr[-1] < arr[-2]:
		mn.append(n - 1)
	return (mx, mn)
t = int(input())
while t:
	t -= 1
	(n, q) = [int(_) for _ in input().split(' ')]
	arr = [int(_) for _ in input().split(' ')]
	(mx, mn) = findLocalMaximaMinima(n, arr)
	if mn:
		if mx:
			if mn[0] < mx[0]:
				mn.pop(0)
	if mn:
		if mx:
			if mn[-1] > mx[-1]:
				mn.pop(len(mn) - 1)
	ans = 0
	for i in mx:
		ans += arr[i]
	for j in mn:
		ans -= arr[j]
	print(ans)


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	p = a[0]
	f = 0
	s = 0
	for j in range(1, n):
		if f == 0:
			if a[j] >= p:
				p = a[j]
			else:
				s = s + p
				f = 1
				p = a[j]
		elif a[j] <= p:
			p = a[j]
		else:
			s = s - p
			f = 0
			p = a[j]
	if f == 0:
		s = s + p
	print(s)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	dp_pair = 0
	dp_non = 0
	for i in range(len(arr)):
		dp_pair = max(dp_pair, dp_non - arr[i])
		dp_non = max(dp_non, dp_pair + arr[i])
	print(max(dp_pair, dp_non))


import math

def get_int():
	return int(input())

def get_ints():
	return [int(i) for i in input().split()]
for t in range(0, get_int()):
	(n, q) = get_ints()
	strengths = get_ints()
	largest_after = [0] * n
	largest_after[n - 1] = strengths[n - 1]
	for i in range(n - 2, 0, -1):
		largest_after[i] = max(largest_after[i + 1], strengths[i])
	total = 0
	add = True
	for i in range(1, n):
		if strengths[i - 1] < strengths[i] and (not add):
			total -= strengths[i - 1]
			add = True
		elif strengths[i - 1] > strengths[i] and add == True:
			total += strengths[i - 1]
			add = False
	if add:
		total += strengths[-1]
	print(total)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil, pi, sin, cos, acos, atan
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	(n, q) = inp()
	arr = list(inp())
	maxv = arr[n - 1]
	minv = 0
	for i in range(n - 2, -1, -1):
		maxv = max(arr[i] - minv, maxv)
		minv = min(arr[i] - maxv, minv)
	ans = max(maxv, minv)
	out(ans)


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import math
from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
		continue
	best = 0
	idx = 0
	s = 0
	while idx < n - 1:
		while idx < n - 1 and a[idx + 1] > a[idx]:
			idx += 1
		s += a[idx]
		best = max(best, s)
		while idx < n - 1 and a[idx + 1] < a[idx]:
			idx += 1
		s -= a[idx]
		best = max(best, s)
	print(best)


for _ in range(int(input())):
	inp = input().split()
	n = int(inp[0])
	q = int(inp[0])
	a = list(map(int, input().split()))
	val = 0
	inc = True
	for i in range(1, n):
		if inc:
			if a[i] < a[i - 1]:
				val += a[i - 1]
				inc = False
		elif a[i] > a[i - 1]:
			val -= a[i - 1]
			inc = True
	if inc:
		val += a[-1]
	print(val)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
GANS = []

def print(*args):
	global GANS
	for i in args:
		GANS.append(f'{i}'.encode())
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	li = [-1] + [int(i) for i in input().split()] + [-1]
	if n == 1:
		print(li[1])
		for qi in range(q):
			trash = input()
			print(li[1])
		continue
	if n == 2:
		print(max(li[2], li[1]))
		for qi in range(q):
			trash = input()
			print(max(li[2], li[1]))
		continue
	hi = li[0]
	status = 1
	lo = li[0]
	ans = 0
	for i in li[1:]:
		if status:
			if i > hi:
				hi = i
			else:
				status = 0
				lo = i
		elif i < lo:
			lo = i
		else:
			status = 1
			ans += hi - lo
			hi = i
	ans += hi
	print(ans)
	for qi in range(q):
		(u, v) = map(int, input().split())
		if u == v:
			print(ans)
			continue
		da = max(li[u - 1], li[u + 1])
		xiao = min(li[u - 1], li[u + 1])
		ans -= max(li[u] - da, 0)
		ans -= max(xiao - li[u], 0)
		ans += max(li[v] - da, 0)
		ans += max(xiao - li[v], 0)
		tmp = li[u]
		li[u] = li[v]
		da = max(li[v - 1], li[v + 1])
		xiao = min(li[v - 1], li[v + 1])
		ans -= max(li[v] - da, 0)
		ans -= max(xiao - li[v], 0)
		ans += max(tmp - da, 0)
		ans += max(xiao - tmp, 0)
		li[v] = tmp
		print(ans)
os.write(1, b'\n'.join(GANS) + b'\n')


def finput():
	return float(input())

def iinput():
	return int(input())

def liinput():
	return [int(_) for _ in input().split()]

def lfinput():
	return [float(_) for _ in input().split()]
T = iinput()
for _ in range(T):
	(n, q) = liinput()
	a = liinput()
	if n == 1:
		print(a[0])
		continue
	if n == 2:
		print(max(a))
		continue
	flag = True
	tmp = a[0]
	ans = a[0]
	for i in range(1, len(a)):
		if a[i] >= ans:
			ans = a[i]
		else:
			break
	j = i
	while j < len(a):
		cur1 = a[j]
		while j < len(a) and a[j] <= cur1:
			cur1 = a[j]
			j += 1
		if j < len(a):
			cur2 = a[j]
			while j < len(a) and a[j] >= cur2:
				cur2 = a[j]
				j += 1
			ans = max(ans, ans - cur1 + cur2)
	print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans1 = 0
	cnt1 = 0
	for i in range(1, n - 1):
		if cnt1 % 2 == 0 and a[i] > a[i - 1] and (a[i] > a[i + 1]):
			cnt1 += 1
			ans1 += a[i]
		elif cnt1 % 2 == 1 and a[i] < a[i - 1] and (a[i] < a[i + 1]):
			cnt1 += 1
			ans1 -= a[i]
	if cnt1 % 2 == 0:
		ans1 += a[n - 1]
		cnt1 += 1
	ans2 = a[0]
	cnt2 = 1
	for i in range(1, n - 1):
		if cnt2 % 2 == 0 and a[i] > a[i - 1] and (a[i] > a[i + 1]):
			cnt2 += 1
			ans2 += a[i]
		elif cnt2 % 2 == 1 and a[i] < a[i - 1] and (a[i] < a[i + 1]):
			cnt2 += 1
			ans2 -= a[i]
	if cnt2 % 2 == 0:
		ans2 += a[n - 1]
		cnt2 += 1
	print(max(ans1, ans2))


t = int(input())
while t:
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	dp = [[0, 0] for i in range(n)]
	dp[-1][0] = a[-1]
	dp[-1][1] = 0
	for i in range(n - 2, -1, -1):
		dp[i][0] = max(a[i] + dp[i + 1][1], dp[i + 1][0])
		dp[i][1] = max(dp[i + 1][0] - a[i], dp[i + 1][1])
	if n == 1:
		print(a[0])
	else:
		print(max(dp[0][0], dp[1][0]))
	t -= 1


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	res = []
	findMax = True
	localMax = arr[0]
	localMin = float('inf')
	for i in range(1, len(arr)):
		if findMax:
			if arr[i] >= arr[i - 1]:
				localMax = arr[i]
			else:
				findMax = False
				res.append(localMax)
				localMin = arr[i]
		elif arr[i] <= arr[i - 1]:
			localMin = arr[i]
		else:
			findMax = True
			res.append(-1 * localMin)
			localMax = arr[i]
	if findMax:
		res.append(localMax)
	print(sum(res))


def shares(A):
	n = len(A)
	if n == 1:
		return A[0]
	sum = 0
	cost = A[0]
	sell = A[0]
	for i in range(1, n):
		if A[i] > sell:
			sum += cost - sell
			cost = A[i]
			sell = A[i]
		else:
			sell = min(sell, A[i])
	return sum + cost
t = int(input())
for i in range(t):
	(n, q) = input().split()
	n = int(n)
	A = [int(i) for i in input().split()][:n]
	print(shares(A))


from sys import stdin
from math import log2

def readline():
	return stdin.readline()

def solve(n, a):
	a = [-1, *a, 0]
	sm = 0
	mine = 0
	for i in range(1, n + 1):
		if a[i - 1] < a[i] > a[i + 1]:
			sm += a[i]
			mine = 0
		elif a[i - 1] > a[i] < a[i + 1]:
			sm -= a[i]
			mine = a[i]
	sm += mine
	return sm
tests = int(readline())
for t in range(0, tests):
	(n, q) = map(int, readline().split())
	a = list(map(int, readline().rstrip('\n').split(' ')))
	print(solve(n, a))


import sys
inp = sys.stdin.readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
from math import log2
_T_ = inin()
for _t_ in range(_T_):
	(n, q) = inar()
	a = [0] + inar() + [0]
	diff = []
	for i in range(n + 1):
		diff.append(max(0, a[i + 1] - a[i]))
	print(sum(diff))
	ans = sum(diff)
	for qq in range(q):
		(l, r) = inar()
		al = a[l]
		ar = a[r]
		a[l] = ar
		ans -= diff[l - 1]
		ans -= diff[l]
		diff[l - 1] = max(0, a[l] - a[l - 1])
		diff[l] = max(0, a[l + 1] - a[l])
		ans += diff[l - 1]
		ans += diff[l]
		a[r] = al
		ans -= diff[r - 1]
		ans -= diff[r]
		diff[r - 1] = max(0, a[r] - a[r - 1])
		diff[r] = max(0, a[r + 1] - a[r])
		ans += diff[r - 1]
		ans += diff[r]
		print(ans)


def ans(a, n):
	i = 0
	x = 0
	q = True
	while i < n - 1:
		if q == True:
			if a[i] < a[i + 1]:
				i += 1
			else:
				x += a[i]
				q = False
		elif a[i] > a[i + 1]:
			i += 1
		else:
			x -= a[i]
			q = True
	if q == True:
		x += a[-1]
	return x
m = int(input())
for i in range(m):
	brr = input().split()
	n = int(brr[0])
	arr = input().split()
	a = []
	for i in arr:
		a.append(int(i))
	print(ans(a, n))


import sys
input = sys.stdin.readline

def solve():
	(n, q) = map(int, input().split())
	L = list(map(int, input().split()))
	if len(L) < 3:
		print(max(L))
		return
	i = 0
	ans = []
	while i < n and i + 1 < n:
		while i + 1 < n and L[i] <= L[i + 1]:
			i += 1
		ans.append(L[i])
		while i + 1 < n and L[i] > L[i + 1]:
			i += 1
		ans.append(L[i])
	i += 1
	ans.pop()
	cost = 0
	for i in range(0, len(ans), 2):
		cost += ans[i]
	for i in range(1, len(ans), 2):
		cost -= ans[i]
	print(cost)
for _ in range(int(input())):
	solve()


from collections import defaultdict
for _ in range(int(input())):
	(n, q) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	i = 1
	ans = 0
	if n == 1:
		print(a[0])
	else:
		while i < n:
			while i < n and a[i] > a[i - 1]:
				i += 1
			ans += a[i - 1]
			while i < n and a[i] <= a[i - 1]:
				i += 1
			if i != n:
				ans -= a[i - 1]
		print(ans)


import sys
read = lambda : sys.stdin.readline().strip()
readi = lambda : map(int, read().split())
from collections import *
from math import *
for _ in range(int(read())):
	(n, q) = readi()
	nums = [0] + list(readi()) + [0]
	diff = [0] * (n + 1)
	for i in range(1, n + 1):
		diff[i] = max(0, nums[i] - nums[i + 1])
	ans = sum(diff)
	print(ans)
	for _ in range(q):
		(a, b) = readi()
		(av, bv) = (nums[a], nums[b])
		nums[a] = bv
		ans -= diff[a - 1] + diff[a]
		diff[a - 1] = max(0, nums[a - 1] - nums[a])
		diff[a] = max(0, nums[a] - nums[a + 1])
		ans += diff[a - 1] + diff[a]
		nums[b] = av
		ans -= diff[b - 1] + diff[b]
		diff[b - 1] = max(0, nums[b - 1] - nums[b])
		diff[b] = max(0, nums[b] - nums[b + 1])
		ans += diff[b - 1] + diff[b]
		print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, q) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	if n == 1:
		print(l[0])
		continue
	ans = 0
	f = 0
	if l[1] > l[0]:
		f = 1
	prev = l[0]
	nextt = 0
	for i in range(1, n):
		if l[i] > l[i - 1]:
			if f == 0:
				ans += prev - l[i - 1]
				f = 1
		elif f == 1:
			prev = l[i - 1]
			f = 0
	if f == 0:
		ans += max(prev - l[i], l[-1], prev)
	else:
		ans += l[-1]
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.append(0)
	val = 0
	flag = True
	mmin = 0
	if n == 1:
		print(arr[0])
	else:
		for i in range(0, n):
			if flag:
				if arr[i] > arr[i + 1]:
					val += arr[i]
					flag = False
			elif arr[i] < arr[i + 1]:
				val -= arr[i]
				mmin = arr[i]
				flag = True
		if flag == True:
			val += mmin
		print(val)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = []
	i = 0
	flag = 0
	while i < n:
		j = i + 1
		if flag == 0:
			while j < n and arr[j] > arr[j - 1]:
				j += 1
			ans.append(arr[j - 1])
			flag = abs(flag - 1)
		elif flag == 1:
			while j < n and arr[j] < arr[j - 1]:
				j += 1
			if j != n:
				ans.append(arr[j - 1])
			flag = abs(flag - 1)
		if i != j:
			i = j
		else:
			i += 1
	if len(ans) == 1:
		print(ans[0])
	else:
		fin = 0
		flag = 0
		for i in range(len(ans)):
			if not flag:
				fin += ans[i]
				flag = abs(flag - 1)
			else:
				fin -= ans[i]
				flag = abs(flag - 1)
		print(fin)


import sys
from collections import defaultdict
input = sys.stdin.readline
from math import log2
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	lis = list(map(int, input().split()))
	if lis == sorted(lis):
		print(max(lis))
		continue
	f = 0
	i = 0
	c = 0
	while True:
		try:
			if f == 0:
				if lis[i] < lis[i + 1]:
					i += 1
				else:
					c += lis[i]
					f = 1 - f
			elif lis[i] > lis[i + 1]:
				i += 1
			else:
				c -= lis[i]
				f = 1 - f
		except:
			break
	if f == 0:
		c += lis[-1]
	print(c)


inf = 10 ** 12
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[-inf, -inf] for i in range(n)]
	dp[0] = [-inf, a[0]]
	for i in range(n - 1):
		dp[i + 1][0] = dp[i][0]
		dp[i + 1][1] = dp[i][1]
		dp[i + 1][0] = max(dp[i + 1][0], dp[i][1] - a[i + 1])
		dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + a[i + 1])
		dp[i + 1][1] = max(dp[i + 1][1], a[i + 1])
	print(max(dp[n - 1]))


for i in range(int(input())):
	trash = input()
	s = list(map(int, input().split()))
	power = 0
	turn = 0
	for j in range(len(s) - 1):
		if turn % 2 == 0 and s[j] > s[j + 1]:
			turn += 1
			power += s[j]
		elif turn % 2 and s[j] < s[j + 1]:
			turn += 1
			power -= s[j]
	if len(s) > 1 and turn % 2 == 0 and (s[-1] > s[-2]):
		power += s[-1]
	elif len(s) == 1:
		power = s[0]
	print(power)


import math

def solve():
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	c = 0
	if n == 1:
		print(a[0])
	else:
		for i in range(n):
			if i == 0:
				if a[i] > a[i + 1]:
					c += a[i]
			elif i == n - 1:
				if a[i] > a[i - 1]:
					c += a[i]
			else:
				if a[i - 1] < a[i] > a[i + 1]:
					c += a[i]
				if a[i - 1] > a[i] < a[i + 1]:
					c -= a[i]
		print(c)
t = int(input())
i = 0
while i < t:
	solve()
	i += 1


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	p = map(int, input().split())
	s = m = 0
	for i in p:
		if i > m:
			s += i - m
		s = max(s, i)
		m = i
	print(s)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp1 = [-sys.maxsize]
	dp2 = [0]
	for i in arr:
		dp1.append(max(dp2[-1] + i, dp1[-1]))
		dp2.append(max(dp1[-1] - i, dp2[-1]))
	print(max(dp1[-1], dp2[-1]))


t = int(input())

def ans(a):
	plus = True
	p = 0
	s = 0
	for i in range(n):
		if plus:
			if a[i] > p:
				p = a[i]
			elif a[i] <= p:
				plus = False
				m = a[i]
		elif a[i] >= m:
			plus = True
			s += p - m
			p = a[i]
		elif a[i] < m:
			m = a[i]
		if i == n - 1:
			s += p
	return s
for i in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	print(ans(a))
	for i in range(q):
		(l, r) = map(int, input().split())
		(a[l - 1], a[r - 1]) = (a[r - 1], a[l - 1])
		print(ans(a))


def main(pokemons, n):
	army = []
	current_candidate_value = pokemons[0]
	is_sum = True
	for (index, item) in enumerate(pokemons[1:]):
		if is_sum:
			if item > current_candidate_value:
				current_candidate_value = item
			elif item < current_candidate_value:
				army.append(current_candidate_value)
				current_candidate_value = pokemons[index + 1]
				is_sum = False
		elif item < current_candidate_value:
			current_candidate_value = item
		elif item > current_candidate_value:
			army.append(current_candidate_value)
			current_candidate_value = pokemons[index + 1]
			is_sum = True
	else:
		if is_sum:
			army.append(current_candidate_value)
	result = 0
	is_sum = True
	if len(army) % 2 == 1:
		for item in army:
			if is_sum:
				result += item
				is_sum = False
			else:
				result -= item
				is_sum = True
	else:
		for item in army[:-1]:
			if is_sum:
				result += item
				is_sum = False
			else:
				result -= item
				is_sum = True
	return result
tt = int(input())
results = []
for _ in range(tt):
	(n, q) = map(int, input().split(' '))
	pokemons = list(map(int, input().split(' ')))
	print(main(pokemons, n))


def solve():
	(n, q) = [int(x) for x in input().split(' ')]
	a = [int(x) for x in input().split(' ')]
	if len(a) == 1:
		print(a[0])
	elif len(a) == 2:
		print(max(a[0], a[1]))
	else:
		m1 = []
		m2 = []
		m1.append(a[0])
		m1.append(max(a[0], a[1]))
		m2.append(0)
		m2.append(max(a[0] - a[1], 0))
		for i in range(2, len(a)):
			m1.append(max(m2[i - 1] + a[i], m1[i - 1]))
			m2.append(max(m1[i - 1] - a[i], m2[i - 1]))
		print(max(m1[-1], m2[-1]))
t = int(input())
for i in range(t):
	solve()


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=2 ** 30, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.count = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
				self.temp.count += 1
			if not val:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left
				self.temp.count += 1
		self.temp.data = pre_xor

	def query(self, xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = xor & 1 << i
			if not val:
				if self.temp.left and self.temp.left.count > 0:
					self.temp = self.temp.left
				elif self.temp.right:
					self.temp = self.temp.right
			elif self.temp.right and self.temp.right.count > 0:
				self.temp = self.temp.right
			elif self.temp.left:
				self.temp = self.temp.left
			self.temp.count -= 1
		return xor ^ self.temp.data
for ik in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	dp = [[[0 for i in range(2)] for k in range(2)] for j in range(n)]
	dp[0][0][0] = l[0]
	for i in range(1, n):
		dp[i][0][0] = max(dp[i - 1][0][0], dp[i - 1][1][0])
		dp[i][0][1] = max(dp[i - 1][0][1], dp[i - 1][1][1])
		dp[i][1][1] = max(dp[i - 1][0][0] - l[i], dp[i - 1][1][0] - l[i])
		dp[i][1][0] = max(dp[i - 1][0][1] + l[i], dp[i - 1][1][1] + l[i])
	print(max(max(dp[n - 1][0]), max(dp[n - 1][1])))


for i in range(int(input())):
	(n, q) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	flag = 0
	ans = 0
	i = n - 1
	while i >= 0:
		if flag == 0:
			while i >= 1 and a[i - 1] > a[i]:
				i -= 1
			ans += a[i]
			i -= 1
		else:
			while i >= 1 and a[i - 1] < a[i]:
				i -= 1
			if i != 0:
				ans -= a[i]
			i -= 1
		flag = 1 - flag
	print(ans)


def check(a, n):
	n += 1
	d1 = [0] * n
	d1[0] = float('-inf')
	d2 = [0] * n
	for i in range(1, n):
		d1[i] = max(d1[i - 1], d2[i - 1] + a[i - 1])
		d2[i] = max(d2[i - 1], d1[i - 1] - a[i - 1])
	return max(d1[-1], d2[-1])
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	print(check(a, n))


def solve(arr, n):
	flag = True
	ans = 0
	prev = -999
	for i in range(n - 1):
		if flag:
			if arr[i] > arr[i + 1]:
				ans += arr[i]
				prev = arr[i]
				flag = False
		elif arr[i] < arr[i + 1]:
			ans -= arr[i]
			prev = arr[i]
			flag = True
	if flag:
		if arr[-1] > prev:
			ans += arr[-1]
	return ans
for _ in range(int(input())):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	print(solve(a, n))


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def graph(n, m):
	adj = dict()
	for i in range(1, n + 1):
		adj.setdefault(i, 0)
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a] += 1
		adj[b] += 1
	return adj
import sys
import threading
from math import *
INF = 1000000000.0
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[0, 0]] * (n + 1)
	dp[0][0] = -INF
	for i in range(n):
		dp[i + 1][0] = max(dp[i][0], dp[i][1] + a[i])
		dp[i + 1][1] = max(dp[i][1], dp[i][0] - a[i])
	print(max(dp[-1][0], dp[-1][1]))


import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
	(N, Q) = map(int, input().split())
	A = [int(a) for a in input().split()]
	ans = sum((max(b - a, 0) for (a, b) in zip([0] + A, A)))
	print(ans)


from sys import stdin
from math import ceil, gcd

def func():
	return
for _ in range(int(stdin.readline())):
	(n, q) = map(int, stdin.readline().split())
	arr = list(map(int, stdin.readline().split()))
	dp = [[0, 0] for i in range(n)]
	dp[0][1] = arr[0]
	for i in range(1, n):
		dp[i][0] = max(dp[i - 1][1] - arr[i], dp[i - 1][0])
		dp[i][1] = max(dp[i - 1][0] + arr[i], dp[i - 1][1])
	print(max(dp[n - 1][0], dp[n - 1][1]))


import sys, os.path
from collections import *
from copy import *
import math
mod = 10 ** 9 + 7
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for t in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = a[0]
	for i in range(1, n):
		ans += max(0, a[i] - a[i - 1])
	print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	a = [-1] + a + [-1]
	ans = 0
	for i in range(1, n + 1):
		if a[i] > a[i + 1] and a[i] > a[i - 1]:
			ans += a[i]
		if a[i] < a[i + 1] and a[i] < a[i - 1]:
			ans -= a[i]
	print(ans)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, Q, A, LR):
	chunks = []
	for x in A:
		if chunks and chunks[-1][-1] >= x:
			chunks[-1].append(x)
		else:
			chunks.append([x])
	ans = 0
	for (i, chunk) in enumerate(chunks):
		if i == len(chunks) - 1:
			ans += max(chunk[0], chunk[0] - chunk[-1])
		elif len(chunk) > 1:
			ans += chunk[0] - chunk[-1]
	return ans
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N, Q) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	LR = [[int(x) for x in input().split()] for i in range(Q)]
	ans = solve(N, Q, A, LR)
	print(ans)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	stack = []
	stack.append(arr[0])
	status = True
	for i in range(1, n):
		if status == True:
			if arr[i] > stack[-1]:
				stack.pop()
				stack.append(arr[i])
			elif arr[i] < stack[-1]:
				stack.append(arr[i])
				status = False
		elif arr[i] < stack[-1]:
			stack.pop()
			stack.append(arr[i])
		elif arr[i] > stack[-1]:
			stack.append(arr[i])
			status = True
	size = len(stack)
	if size % 2 == 0 and size != 0:
		stack.pop()
		size -= 1
	ans = 0
	for i in range(size):
		if i % 2 == 0:
			ans += stack[i]
		else:
			ans -= stack[i]
	print(ans)


import sys
import math
import heapq
from collections import deque
YES = 'YES'
NO = 'NO'
modulo = 10 ** 9 + 7

def solve():
	(n, q) = map(int, input().split())
	p = list(map(int, input().split()))
	if len(p) < 2:
		return p[0]
	dp = [0] * n
	dp[0] = p[0]
	dp[1] = max(p[1], p[0])
	for i in range(2, n):
		dp[i] = max(dp[i - 1] + max(0, p[i] - p[i - 1]), p[i] - p[i - 1] + dp[i - 2])
	return dp[n - 1]
for _ in range(int(input())):
	print(solve())


t = int(input())
for test in range(t):
	n = int(input().split(' ')[0])
	arr = [int(i) for i in input().split(' ')]
	if n < 3:
		print(max(arr))
		continue
	total = 0
	low = float('inf')
	high = arr[0]
	mode = 1
	for i in range(1, n):
		if arr[i] > arr[i - 1]:
			if mode == 0:
				total -= low
			mode = 1
			high = arr[i]
		elif arr[i] < arr[i - 1]:
			if mode == 1:
				total += high
			mode = 0
			low = arr[i]
	if mode == 1:
		total += high
	print(total)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	(n, q) = mp()
	arr = lmp()
	d1 = l1d(n + 1)
	d2 = l1d(n + 1)
	d1[0] = -inf
	d2[0] = 0
	for i in range(1, n + 1):
		d1[i] = max(d1[i - 1], d2[i - 1] + arr[i - 1])
		d2[i] = max(d2[i - 1], d1[i - 1] - arr[i - 1])
	print(max(d1[n], d2[n]))


import bisect
import functools
import math
T = int(input())
for Tid in range(T):
	(n, Q) = map(int, input().split())
	a = list(map(int, input().split()))
	f0 = 0
	f1 = -1e+18
	for i in range(n):
		tmp = max(f0, f1 - a[i])
		f1 = max(f1, f0 + a[i])
		f0 = tmp
	print(f1)


from itertools import permutations
from collections import defaultdict
from math import *
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
from bisect import *
import copy
import itertools
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	if n == 1:
		print(arr[0])
	else:
		ans = 0
		bool1 = True
		i = 1
		while i < n:
			if bool1:
				if arr[i] < arr[i - 1]:
					ans += arr[i - 1]
					bool1 = False
				else:
					i += 1
			elif arr[i] > arr[i - 1]:
				ans -= arr[i - 1]
				bool1 = True
			else:
				i += 1
		if bool1:
			ans += arr[-1]
		print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	l = input().split()
	li = [int(i) for i in l]
	xori = 0
	dx = [-1, 1]
	ans = 0
	for i in range(n):
		if xori == 0:
			yes = 1
			for j in dx:
				if i + j >= 0 and i + j < n:
					if li[i] < li[i + j]:
						yes = 0
						break
			if yes:
				ans += li[i]
				xori = 1
		else:
			yes = 1
			for j in dx:
				if i + j >= 0 and i + j < n and (li[i] < li[i + j]):
					continue
				yes = 0
				break
			if yes:
				ans -= li[i]
				xori = 0
	print(ans)


def add(f, i, x):
	while i < len(f):
		f[i] += x
		i |= i + 1

def sum(f, i):
	if i < 0:
		return 0
	else:
		return f[i] + sum(f, (i & i + 1) - 1)
t = int(input())
for I in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	up = True
	s = a[0]
	f = [0 for i in range(n)]
	add(f, 0, 1)
	for i in range(1, n):
		if up:
			if a[i] > a[i - 1]:
				add(f, i - 1, -1)
				add(f, i, 1)
				s += a[i] - a[i - 1]
			else:
				up = False
				add(f, i, 1)
		elif a[i] > a[i - 1]:
			add(f, i - 1, 1)
			add(f, i, 1)
			up = True
			s += a[i] - a[i - 1]
	print(s)
	for i in range(q):
		(l, r) = map(int, input().split())
		l -= 1
		r -= 1
		ll = a[l]
		rr = a[r]
		make(a, l, rr)
		make(a, r, ll)


import sys
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input().strip()

def listStr():
	return list(input().strip())
import collections as col
import math, itertools

def solve():
	(N, Q) = getInts()
	A = getInts()
	if N == 1:
		return A[0]
	dp = [[0, A[0]], [max(0, A[0] - A[1]), max(A[0], A[1])]]
	for j in range(2, N):
		x0 = max(dp[j - 2][0] + A[j - 1] - A[j], dp[j - 1][0], dp[j - 1][1] - A[j])
		x1 = max(dp[j - 2][1] - A[j - 1] + A[j], dp[j - 1][1], dp[j - 1][0] + A[j])
		dp.append([x0, x1])
	return max(dp[N - 1])
for _ in range(getInt()):
	print(solve())


import sys
from bisect import *
from collections import deque
pl = 1
from copy import *
if pl:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('outpt.txt', 'w')

def li():
	return [int(xxx) for xxx in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())
t = fi()
while t > 0:
	t -= 1
	(n, q) = mi()
	a = li()
	dp = [[0, 0, 0, 0] for i in range(2 * n)]
	for i in range(n):
		if i == 0:
			dp[i][0] = a[i]
		else:
			dp[i][0] = max(max(dp[i - 1][1], dp[i - 1][3]) + a[i], dp[i - 1][0])
			dp[i][1] = max(max(dp[i - 1][0], dp[i - 1][2]) - a[i], dp[i - 1][1])
			dp[i][2] = max(dp[i - 1][0], dp[i - 1][2])
			dp[i][3] = max(dp[i - 1][1], dp[i - 1][3])
	print(max(dp[n - 1]))


t = int(input())
from itertools import accumulate
INF = 10 ** 18
for _ in range(t):
	(n, q) = map(int, input().split())
	A = list(map(int, input().split()))
	for i in range(q):
		(l, r) = map(int, input().split())
		(l, r) = (l - 1, r - 1)
	dp = [[-INF] * 2 for i in range(n + 1)]
	dp[0][0] = 0
	dp[0][1] = 0
	A.reverse()
	for i in range(n):
		a = A[i]
		dp[i + 1][0] = max(dp[i][1] + a, dp[i][0])
		dp[i + 1][1] = max(dp[i][0] - a, dp[i][1])
	ans = -INF
	for i in range(n):
		ans = max(ans, dp[i + 1][0])
	print(ans)


from sys import stdin
N = int(stdin.readline())
for case in range(N):
	(len, op) = map(int, stdin.readline().split())
	array = list(map(int, stdin.readline().split()))
	parity = 1
	ans = 0
	for i in range(len - 1):
		if parity:
			if array[i] > array[i + 1]:
				ans += array[i]
				parity = 0
		elif array[i] < array[i + 1]:
			ans -= array[i]
			parity = 1
	if parity:
		ans += array[-1]
	print(ans)


import itertools
import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '3\n3 1\n1 3 2\n1 2\n2 2\n1 2\n1 2\n1 2\n7 5\n1 2 5 4 3 6 7\n1 2\n6 7\n3 4\n1 2\n2 3\n\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield sys.stdin.readline()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]

def solve(a, queries):
	results = []
	res = 0
	a = [-1] + a + [-1]

	def check(res, i, remove=False):
		k = 1
		if remove:
			k = -1
		if a[i] > a[i - 1] and a[i] > a[i + 1]:
			return res + a[i] * k
		elif a[i] < a[i - 1] and a[i] < a[i + 1]:
			return res - a[i] * k
		return res
	for i in range(1, len(a) - 1):
		res = check(res, i)
	results.append(res)
	for (l, r) in queries:
		affected = {l, r, max(1, l - 1), max(1, r - 1), min(len(a) - 2, l + 1), min(len(a) - 2, r + 1)}
		for x in affected:
			res = check(res, x, True)
		tmp = a[l]
		a[l] = a[r]
		a[r] = tmp
		for x in affected:
			res = check(res, x)
		results.append(res)
	return results
t = getType(int)
for _ in range(t):
	(n, q) = getArray(int)
	a = getArray(int)
	queries = [getArray(int) for _ in range(q)]
	res = solve(a, queries)
	print('\n'.join([str(x) for x in res]))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = []
	x = 0
	for i in range(n - 1):
		if ans == []:
			if a[i] > a[i + 1]:
				ans.append(a[i])
		elif len(ans) % 2 != 0:
			if a[i] < a[i + 1]:
				ans.append(-a[i])
		elif len(ans) % 2 == 0:
			if a[i] > a[i + 1]:
				ans.append(a[i])
	if len(ans) % 2 == 0:
		ans.append(a[-1])
	mx = max(max(a), sum(ans))
	print(mx)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
c = 0
a = []
b = []

def f(i):
	if a[i] == 0:
		return 0
	if a[i - 1] < a[i] > a[i + 1]:
		return 1
	if a[i - 1] > a[i] < a[i + 1]:
		return -1
	return 0

def relax(i):
	global c
	c += (f(i) - b[i]) * a[i]
	b[i] = f(i)

def relax1(i):
	global c
	c -= b[i] * a[i]
	b[i] = 0
for _ in range(int(input())):
	c = 0
	(n, q) = map(int, input().split())
	(*a,) = map(int, input().split())
	a = [0] + a + [0]
	b = [0] * (n + 2)
	for i in range(1, n + 1):
		relax(i)
	print(c)
	for i in range(q):
		(l, r) = map(int, input().split())
		relax1(l)
		relax1(l - 1)
		relax1(l + 1)
		relax1(r)
		relax1(r - 1)
		relax1(r + 1)
		(a[l], a[r]) = (a[r], a[l])
		relax(l)
		relax(l - 1)
		relax(l + 1)
		relax(r)
		relax(r - 1)
		relax(r + 1)
		print(c)


import sys
inp = sys.stdin.buffer.readline
inar = lambda : list(map(int, inp().split()))
inin = lambda : int(inp())
inst = lambda : inp().decode().strip()
from math import log2
_T_ = inin()
for _t_ in range(_T_):
	(n, q) = inar()
	a = inar()
	tt = []
	t = []
	prev = 0
	for i in a:
		if i > prev:
			prev = i
			t.append(i)
		else:
			tt.append(t)
			prev = i
			t = [i]
	tt.append(t)
	ss = tt[0][-1]
	for i in range(1, len(tt)):
		if len(tt[i]) <= 1:
			pass
		else:
			ss -= tt[i][0]
			ss += tt[i][-1]
	print(ss)


from collections import Counter
from collections import OrderedDict
from collections import defaultdict

def SOCountR(mylist):
	return OrderedDict(sorted(Counter(mylist).items(), reverse=True))

def SOCount(mylist):
	return OrderedDict(sorted(Counter(mylist).items()))

def freq_count(mylist):
	return Counter(mylist)
import math

def setBitNumber(n):
	k = int(math.log(n, 2))
	return 2 ** k
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	arr = [int(x) for x in input().split()]
	if n == 1:
		print(arr[0])
		continue
	m = True
	arr1 = []
	for i in range(n):
		if m:
			if i == 0:
				if i == 0 and arr[i] > arr[i + 1]:
					arr1.append(arr[i])
					m = False
			elif i == n - 1:
				if i == n - 1 and arr[i] > arr[i - 1]:
					arr1.append(arr[i])
					m = False
			elif arr[i - 1] < arr[i] > arr[i + 1]:
				arr1.append(arr[i])
				m = False
		elif i == 0:
			if i == 0 and arr[i] < arr[i + 1]:
				arr1.append(arr[i])
				m = True
		elif i == n - 1:
			if i == n - 1 and arr[i] < arr[i - 1]:
				arr1.append(arr[i])
				m = True
		elif arr[i - 1] > arr[i] < arr[i + 1]:
			arr1.append(arr[i])
			m = True
	if m:
		arr1.pop()
	count = 0
	for i in range(0, len(arr1), 2):
		count += arr1[i]
	for i in range(1, len(arr1), 2):
		count -= arr1[i]
	print(count)


for t in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	added = arr[0]
	ans = arr[0]
	ele_bet = 10 ** 10
	for i in range(1, n):
		if arr[i] > ele_bet:
			ans += arr[i] - ele_bet
			ele_bet = 10 ** 10
		if arr[i] > ans:
			ans = arr[i]
			ele_bet = 10 ** 10
		ele_bet = min(arr[i], ele_bet)
	print(ans)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[[-1 * float('inf') for i in range(2)] for i in range(2)] for i in range(n)]
	dp[0][0][0] = 0
	dp[0][1][1] = a[0]
	i = 0
	ans = max(dp[i][0][0], dp[i][0][1], dp[i][1][0], dp[i][1][1])
	for i in range(1, n):
		dp[i][0][0] = max(dp[i - 1][0])
		dp[i][1][0] = max(dp[i - 1][1])
		dp[i][0][1] = max(dp[i - 1][1]) - a[i]
		dp[i][1][1] = max(max(dp[i - 1][0]) + a[i], a[i])
		ans = max(ans, dp[i][0][0], dp[i][0][1], dp[i][1][0], dp[i][1][1])
	print(ans)


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	b = [a[0]]
	c = []
	fl = 0
	ans = 0
	for i in range(1, n):
		if fl == 0:
			if b[-1] <= a[i]:
				b.append(a[i])
			else:
				ans += b[-1]
				c.append(a[i])
				b = []
				fl = 1
		elif c[-1] >= a[i]:
			c.append(a[i])
		else:
			ans -= c[-1]
			b.append(a[i])
			c = []
			fl = 0
	if fl == 0:
		ans += b[-1]
	print(ans)


for x in range(int(input())):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	t = 0
	if n == 1:
		print(a[0])
	else:
		v = []
		if a[0] > a[1]:
			v.append(a[0])
			t = 1
		for i in range(1, n - 1):
			if a[i] > a[i - 1] and a[i] > a[i + 1] and (t == 0):
				v.append(a[i])
				t = 1
			if a[i] < a[i - 1] and a[i] < a[i + 1] and (t == 1):
				v.append(a[i])
				t = 0
		if a[n - 1] > a[n - 2] and t == 0:
			v.append(a[n - 1])
		if len(v) % 2 == 0:
			v.pop()
		ans = 0
		for i in range(len(v)):
			if i % 2:
				ans -= v[i]
			else:
				ans += v[i]
		print(ans)


test = int(input())
for i in range(test):
	(n, q) = [int(x) for x in input().split(' ')]
	a = [int(x) for x in input().split(' ')]
	counter = 0
	diff = 0
	su = 0
	for i in range(n - 1):
		if counter == 0:
			if a[i] > a[i + 1]:
				su += a[i]
				counter = 1
		elif a[i] < a[i + 1]:
			diff += a[i]
			counter = 0
	if counter == 0:
		su += a[-1]
		print(su - diff)
	else:
		print(su - diff)


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	nums = list(map(int, input().split()))
	dp = [0, 0]
	for j in nums:
		dp = [max(dp[0], dp[1] + j), max(dp[1], dp[0] - j)]
	print(dp[0])


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	x = 0
	i = 0
	p = []
	while i < n:
		if x == 0:
			while i + 1 < n and a[i] <= a[i + 1]:
				i += 1
			p.append(a[i])
			i += 1
			x += 1
		else:
			while i + 1 < n and a[i] >= a[i + 1]:
				i += 1
			p.append(a[i])
			i += 1
			x -= 1
	if len(p) % 2 == 0:
		p.pop()
	print(sum(p[::2]) - sum(p[1::2]))


import sys, math
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ext = []
	ele = arr[0]
	inc = True
	i = 0
	while i < n:
		if inc:
			if arr[i] > ele:
				ele = arr[i]
			elif arr[i] < ele:
				ext.append(ele)
				ele = arr[i]
				inc = False
		elif arr[i] < ele:
			ele = arr[i]
		else:
			ext.append(ele)
			ele = arr[i]
			inc = True
		i += 1
	ext.append(ele)
	i = 1
	out = ext[0]
	while i < len(ext) - 1:
		if ext[i + 1] - ext[i] > 0:
			out += ext[i + 1] - ext[i]
		i += 2
	print(out)


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
		continue
	inc = -1
	for i in range(1, n):
		if a[i] > a[i - 1]:
			inc = 1
			break
		elif a[i] < a[i - 1]:
			inc = 0
			break
	if inc == -1:
		print(a[0])
		continue
	elif inc:
		ans = 0
	else:
		ans = a[0]
	for i in range(1, n):
		if a[i] == a[i - 1]:
			continue
		if a[i] > a[i - 1] and inc:
			continue
		elif a[i] > a[i - 1]:
			ans -= a[i - 1]
			inc = 1
		elif a[i] < a[i - 1] and inc:
			ans += a[i - 1]
			inc = 0
		else:
			continue
	if inc:
		ans += a[-1]
	print(ans)


from collections import defaultdict
import sys
import math

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def f(n):
	cnt = 0
	while n:
		n = n // 2
		cnt += 1
	return cnt
for _ in range(int(input())):
	(n, q) = get_ints()
	a = get_array()
	ans = 0
	plus = -1
	arr = []
	i = 0
	while i < n:
		if a[i] > plus:
			plus = a[i]
			i += 1
			neg = -1
			while i < n:
				if a[i] > plus:
					break
				if neg == -1:
					neg = a[i]
				if a[i] > neg:
					break
				else:
					neg = a[i]
				i += 1
			if neg != -1 and i != n:
				arr.append(plus)
				arr.append(neg)
				plus = -1
	if plus > -1:
		arr.append(plus)
	ans = 0
	sum = 0
	for i in range(len(arr)):
		if i & 1:
			ans = max(ans, sum)
			sum -= arr[i]
		else:
			sum += arr[i]
	ans = max(ans, sum)
	print(ans)


import sys
from collections import defaultdict as dc
import math
from bisect import bisect, bisect_left, bisect_right
input = sys.stdin.readline
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	f = 0
	a = 0
	b = 0
	i = 0
	while i < n:
		if f == 0:
			t = l[i]
			q = 1
			for j in range(i + 1, n):
				b += 1
				if l[j] < l[j - 1]:
					break
				else:
					t = max(t, l[j])
					q += 1
			a += t
			i += q
			f = 1
		else:
			t = l[i]
			q = 1
			for j in range(i + 1, n):
				b += 1
				if l[j] > l[j - 1]:
					break
				else:
					t = min(t, l[j])
					q += 1
			if i + q == n:
				break
			else:
				a -= t
				i += q
				f = 0
	print(a)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	ls = list(map(int, input().split()))
	i = 0
	ans = 0
	fans = -1000000
	f = 1
	while i < n:
		if f:
			while i < n - 1 and ls[i] < ls[i + 1]:
				i += 1
			if i == n - 1:
				ans += ls[i]
				i += 1
			else:
				ans += ls[i]
				f = 1 - f
		else:
			while i < n - 1 and ls[i] > ls[i + 1]:
				i += 1
			if i == n - 1:
				i += 1
			else:
				ans -= ls[i]
				f = 1 - f
		fans = max(fans, ans)
	print(max(fans, ans))


for _ in range(int(input())):
	(nk, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if nk == 3:
		print(max(max(a), a[0] - a[1] + a[2]))
	elif nk == 2:
		print(max(a))
	else:
		c = 1
		ans = 0
		cu = True
		while c < nk:
			if cu:
				if a[c] < a[c - 1]:
					ans += a[c - 1]
					cu = False
			elif a[c - 1] < a[c]:
				ans -= a[c - 1]
				cu = True
			c += 1
		if cu:
			ans += a[-1]
		print(ans)


from sys import stdin, stdout
from collections import Counter
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def solve():
	(n, q) = mp()
	l = li()
	ans = l[0]
	for i in range(1, n):
		ans += max(0, l[i] - l[i - 1])
	pr(ans)
for _ in range(inp()):
	solve()


for _ in range(int(input())):
	(n, q) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = []
	s = 0
	if n == 1:
		print(a[0])
		continue
	for i in range(len(a) - 1):
		if a[i] > a[i + 1] and s == 0:
			b.append(a[i])
			s += 1
		elif a[i + 1] > a[i] and s == 1:
			b.append(a[i])
			s -= 1
	if len(b) == 0:
		print(max(a))
		continue
	if len(b) % 2 == 0:
		if b[-1] == a[-1]:
			b = b[:-1]
		else:
			b.append(a[-1])
	count = 0
	for i in range(len(b)):
		if i % 2 == 0:
			count += b[i]
		else:
			count -= b[i]
	print(count)


from math import *
from random import *
from bisect import *
from math import *
for t in range(int(input())):
	(n, q) = map(int, input().split())
	mas = list(map(int, input().split()))
	dpmin = [0 for i in range(n)]
	dppls = [0 for i in range(n)]
	if n == 1:
		print(mas[0])
		continue
	dppls[0] = mas[0]
	dppls[1] = max(mas[0], mas[1])
	dpmin[0] = 0
	dpmin[1] = max(0, mas[0] - mas[1])
	for i in range(2, n):
		dppls[i] = max(dppls[i - 1], mas[i] + dpmin[i - 1])
		dpmin[i] = max(0, dpmin[i - 1], dppls[i - 1] - mas[i])
	print(max(dppls[n - 1], dpmin[n - 1]))


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def output(*args):
	sys.stdout.buffer.write(('\n'.join(map(str, args)) + '\n').encode('utf-8'))

def main():
	t = int(input())
	ans_a = []
	for ti in range(t):
		(n, q) = map(int, input().split())
		a = [-1] + list(map(int, input().split())) + [-1]
		is_peak = [0] * (n + 2)
		score = 0
		for i in range(1, n + 1):
			if a[i - 1] < a[i] > a[i + 1]:
				is_peak[i] = 1
				score += a[i]
			elif a[i - 1] > a[i] < a[i + 1]:
				is_peak[i] = 2
				score -= a[i]
		ans_a.append(str(score))
		for (li, ri) in (map(int, input().split()) for _ in range(q)):
			for (i, new_val) in ((li, a[ri]), (ri, a[li])):
				for j in range(i - 1, i + 2):
					if is_peak[j]:
						score += a[j] * (-1 if is_peak[j] == 1 else 1)
				a[i] = new_val
				for j in range(i - 1, i + 2):
					if 1 <= j <= n and a[j - 1] < a[j] > a[j + 1]:
						is_peak[j] = 1
						score += a[j]
					elif 1 <= j <= n and a[j - 1] > a[j] < a[j + 1]:
						is_peak[j] = 2
						score -= a[j]
					else:
						is_peak[j] = 0
			ans_a.append(str(score))
	output(*ans_a)
main()


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	dp = [[0, 0, 0, 0] for j in range(n)]
	c = -10 ** 11
	if n == 1:
		print(l[0])
	elif n == 2:
		print(max(l[0], l[1]))
	else:
		dp[0] = [l[0], c, c, c]
		dp[1] = [l[1], l[0] - l[1], l[0], c]
		for j in range(2, n):
			dp[j][0] = max(l[j], dp[j - 1][1] + l[j], dp[j - 1][3] + l[j])
			dp[j][1] = max(dp[j - 1][0] - l[j], dp[j - 1][2] - l[j])
			dp[j][2] = max(dp[j - 1][0], dp[j - 1][2])
			dp[j][3] = max(dp[j - 1][1], dp[j - 1][3])
		print(max(dp[n - 1]))


from sys import stdin, stdout
import math
import heapq
import collections
input = stdin.readline

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstrings():
	return [x for x in input().split()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	(n, q) = inputnums()
	a = inputlist()
	x = 0
	y = 0
	for i in reversed(range(n)):
		x = min(x, a[i] - y)
		y = max(y, a[i] - x)
	print(y)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().strip().split()))[:n]
	curr = arr[0]
	flip = 0
	total = 0
	for i in range(1, n):
		if flip == 0:
			if arr[i] < arr[i - 1]:
				total += arr[i - 1]
				flip = 1
			curr = arr[i]
		else:
			if arr[i] > arr[i - 1]:
				total -= arr[i - 1]
				flip = 0
			curr = arr[i]
	if flip == 0:
		total += arr[-1]
	print(total)


from math import log2
T = int(input())
for t in range(T):
	(N, q) = map(int, input().split())
	L = list(map(int, input().split()))
	arr = [0] * N
	if N == 1:
		print(L[0])
	else:
		if N > 1:
			if L[0] > L[1]:
				arr[0] = 1
			else:
				arr[0] = -1
			if L[-1] > L[-2]:
				arr[-1] = 1
		for i in range(1, len(L) - 1):
			if L[i] > L[i - 1] and L[i] > L[i + 1]:
				arr[i] = 1
			if L[i] < L[i - 1] and L[i] < L[i + 1]:
				arr[i] = -1
		need = 1
		count = 0
		for i in range(N):
			if arr[i] == need:
				count += need * L[i]
				need = -1 * need
		print(count)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n > 1:
		m = [0 for i in range(n)]
		k = True
		ans = 0
		for i in range(n):
			if i == 0 and a[i] > a[i + 1]:
				ans += a[i]
				m[i] = 1
				k = False
			elif i == n - 1 and a[i] > a[i - 1] and k:
				ans += a[i]
				m[i] = 1
				k = False
			elif i > 0 and i < n - 1:
				if a[i] > a[i - 1] and a[i] > a[i + 1] and k:
					ans += a[i]
					m[i] = 1
					k = False
				elif a[i] < a[i - 1] and a[i] < a[i + 1] and (not k):
					ans -= a[i]
					m[i] = -1
					k = True
		print(ans)
		out = []
		out.append(ans)
		for i in range(q):
			z = []
			(left, right) = map(int, input().split())
			(a[left - 1], a[right - 1]) = (a[right - 1], a[left - 1])
			(m[left - 1], m[right - 1]) = (m[right - 1], m[left - 1])
			z = [left - 1, right - 1, left, right, left - 2, right - 2]
			for ind in z:
				if ind < 0 or ind >= n:
					continue
				elif ind == 0:
					if a[ind] > a[ind + 1]:
						out[0] += abs(1 - m[ind]) * a[ind]
						m[ind] = 1
					else:
						out[0] += (0 - m[ind]) * a[ind]
						m[ind] = 0
				elif ind == n - 1:
					if a[ind] > a[ind - 1]:
						out[0] += abs(1 - m[ind]) * a[ind]
						m[ind] = 1
					else:
						out[0] += (0 - m[ind]) * a[ind]
						m[ind] = 0
				elif a[ind] > a[ind - 1] and a[ind + 1] < a[ind]:
					out[0] += abs(1 - m[ind]) * a[ind]
					m[ind] = 1
				elif a[ind] < a[ind - 1] and a[ind] < a[ind + 1]:
					out[0] -= abs(-1 - m[ind]) * a[ind]
					m[ind] = -1
				else:
					out[0] += (0 - m[ind]) * a[ind]
					m[ind] = 0
			print(out[0])
	else:
		print(a[0])
		for _ in range(q):
			(left, right) = map(int, input().split())
			print(a[0])


t = int(input())
while t:
	t -= 1
	(n, q) = map(int, input().split())
	a = [int(i) for i in input().split()]
	dp = [[0, 0] for i in range(n + 1)]
	for i in range(n):
		dp[i + 1][0] = dp[i][0]
		dp[i + 1][1] = dp[i][1]
		dp[i + 1][1] = max(dp[i + 1][1], a[i] + dp[i][0])
		dp[i + 1][0] = max(dp[i + 1][0], -a[i] + dp[i][1])
	print(max(dp[n]))


tc = int(input())
for _ in range(tc):
	(l, q) = map(int, input().split(' '))
	arr = map(int, input().split(' '))
	d1 = [-float('inf')]
	d2 = [0]
	for x in arr:
		d1.append(max(d1[-1], d2[-1] + x))
		d2.append(max(d2[-1], d1[-1] - x))
	print(max(d1[-1], d2[-1]))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	data = [int(x) for x in input().split()]
	ans = 0
	flag = 1
	for q in range(n - 1):
		if flag:
			if data[q + 1] < data[q]:
				ans += data[q]
				flag = 0
		elif data[q + 1] > data[q]:
			ans -= data[q]
			flag = 1
	if flag:
		ans += data[-1]
	print(ans)


from math import log2

def isPeak(arr, n, num, i, j):
	if i >= 0 and arr[i] > num:
		return False
	if j < n and arr[j] > num:
		return False
	return True

def isTrough(arr, n, num, i, j):
	if i >= 0 and arr[i] < num:
		return False
	if j < n and arr[j] < num:
		return False
	return True

def printPeaksTroughs(arr, n):
	pts = []
	for i in range(n):
		if isPeak(arr, n, arr[i], i - 1, i + 1):
			pts.append(a[i])
		if isTrough(arr, n, arr[i], i - 1, i + 1):
			pts.append(a[i])
	return pts
for t in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	a.append(0)
	pts = printPeaksTroughs(a, n)
	mx = 0
	sm = 0
	if len(a) == 1:
		print(a[0])
	elif pts[0] < pts[1]:
		for i in range(1, len(pts), 2):
			sm += pts[i]
			mx = max(mx, sm)
			if i + 1 < len(pts):
				sm -= pts[i + 1]
			mx = max(mx, sm)
		print(mx)
	else:
		for i in range(0, len(pts), 2):
			sm += pts[i]
			mx = max(mx, sm)
			if i + 1 < len(pts):
				sm -= pts[i + 1]
			mx = max(mx, sm)
		print(mx)


import math
import sys
t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	last_minus = [0]
	last_plus = [0]
	for i in range(n):
		last_plus.append(max(last_plus[-1], last_minus[-1] + a[i]))
		last_minus.append(max(last_plus[-2] - a[i], last_minus[-1]))
	print(last_plus[-1])


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = [*map(int, input().split())]
	ans = max(a)
	res = i = 0
	add = 1
	while i < n:
		if add:
			if i == n - 1 or a[i] > a[i + 1]:
				res += a[i]
				add ^= 1
		elif i != n - 1 and a[i] < a[i + 1]:
			res -= a[i]
			add ^= 1
		i += 1
		ans = max(ans, res)
	print(ans)


_ = int(input())
while _:
	_ -= 1
	(n, q) = list(map(int, input().split()))
	num = list(map(int, input().split()))
	dp = [[0, 0] for i in range(n)]
	maxn = 0
	minn = 0
	for i in range(n - 1, -1, -1):
		dp[i][0] = num[i] - minn
		dp[i][1] = num[i] - maxn
		maxn = max(maxn, dp[i][0])
		minn = min(minn, dp[i][1], 0)
	print(maxn)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = [0] + arr
	answer = 0
	for i in range(n):
		answer += max(arr[i] - ans[i], 0)
	print(answer)


from itertools import combinations
T = int(input())
for _ in range(T):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	my_max = max(arr)
	for i in range(n):
		if arr[i] == my_max:
			break
	ans = 0
	for i in range(n - 1):
		ans += max(arr[i] - arr[i + 1], 0)
	print(ans + arr[-1])


from math import *
from cmath import *
from itertools import *
from decimal import *
from fractions import *
for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = [int(x) for x in input().split()]
	(dp1, dp2) = ([-1000000000 * 1000000000] * (n + 2), [0] * (n + 2))
	for x in range(n):
		dp1[x + 1] = max(dp2[x] + a[x], dp1[x])
		dp2[x + 1] = max(dp2[x], dp1[x] - a[x])
	print(max(dp1[n], dp2[n]))


t = int(input())
for x in range(t):
	(n, _) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [0 for i in range(n)]
	dp[0] = arr[0]
	odd = 0
	even = 0
	for i in range(n):
		odd = max(odd, even - arr[i])
		even = max(even, odd + arr[i])
	print(max(odd, even))


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c for j in range(b)] for i in range(a)]

def list3d(a, b, c, d):
	return [[[d for k in range(c)] for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** (-10)
for _ in range(INT()):
	(N, Q) = MAP()
	A = LIST()
	dp0 = [0] * (N + 1)
	dp1 = [0] * (N + 1)
	for i in range(N):
		dp1[i + 1] = max(dp1[i + 1], dp1[i], dp0[i] + A[i])
		dp0[i + 1] = max(dp0[i + 1], dp0[i], dp1[i] - A[i])
	ans = max(dp0[N], dp1[N])
	print(ans)


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = int(input())
while t:
	t -= 1
	(n, q) = ma()
	a = lis()
	curbg = -1
	cursm = 10000000000000
	fl = 0
	s = 0
	for i in a:
		if fl ^ 1:
			if i > curbg:
				curbg = i
				continue
			s += curbg
			curbg = -1
			cursm = i
			fl = 1
		else:
			if i < cursm:
				cursm = i
				continue
			s -= cursm
			curbg = i
			cursm = 1000000000000
			fl = 0
	if curbg != -1:
		s += curbg
	print(s)


t = int(input())
for e in range(t):
	(n, q) = map(int, input().split())
	A = [*map(int, input().split())]
	for i in range(q):
		(r, l) = map(int, input().split())
	dp = [0, 0]
	for i in range(n):
		a = A[i]
		if i > 0:
			dp[0] = max(dp[0], dp[1] - a)
		dp[1] = max(dp[1], dp[0] + a)
	print(max(dp))


for _ in range(int(input())):
	(n, k) = input().split()
	l = list(map(int, input().split()))
	flag = 1
	ans = 0
	for i in range(1, int(n)):
		if flag == 1:
			if l[i] < l[i - 1]:
				ans += l[i - 1]
				flag = 0
		elif l[i] > l[i - 1]:
			ans -= l[i - 1]
			flag = 1
	if flag == 1:
		ans += l[int(n) - 1]
	print(ans)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	(n, q) = MI()
	a = LI()
	ans = []
	i = 1
	temp = a[0]
	asc = True
	while i < n:
		if asc and a[i] > temp:
			temp = a[i]
		elif asc:
			ans.append(temp)
			temp = a[i]
			asc = False
			i += 1
			continue
		elif asc == False and a[i] < temp:
			temp = a[i]
		else:
			ans.append(temp)
			temp = a[i]
			asc = True
			continue
		i += 1
	if ans == [] or temp > ans[-1]:
		ans.append(temp)
	aa = 0
	for i in range(len(ans)):
		if i % 2 == 0:
			aa += ans[i]
		else:
			aa -= ans[i]
	print(aa)


def find_max(arr, m):
	lis = []
	if arr[0] > arr[1]:
		lis.append(arr[0])
	for i in range(1, m - 1):
		if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
			lis.append(arr[i])
		if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			lis.append(arr[i])
	if arr[m - 1] > arr[m - 2]:
		lis.append(arr[m - 1])
	ans = 0
	for j in range(len(lis)):
		if j % 2 == 0:
			ans += lis[j]
		else:
			ans -= lis[j]
	return ans
n = int(input())
for i in range(n):
	(m, p) = list(map(int, input().split()))
	lis = list(map(int, input().split()))
	if m == 1:
		print(lis[0])
	else:
		print(find_max(lis, m))


for _ in range(int(input())):
	ar = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	n = ar[0]
	ok = 1
	a = []
	a.append(arr[0])
	for i in range(1, n):
		if ok == 1:
			if a[len(a) - 1] < arr[i]:
				a.pop()
				a.append(arr[i])
			else:
				a.append(arr[i])
				ok = 0
		elif a[len(a) - 1] > arr[i]:
			a.pop()
			a.append(arr[i])
		else:
			a.append(arr[i])
			ok = 1
	ok = 1
	ans = 0
	for i in range(len(a) - 1):
		if ok % 2 == 1:
			ans += a[i]
		else:
			ans -= a[i]
		ok += 1
	if ok % 2 == 1:
		ans += a[len(a) - 1]
	print(ans)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	(n, q) = map(int, inputi().split())
	aa = [int(a) for a in inputi().split()]
	mnp = 0
	mnn = 0
	for a in aa:
		(mnp, mnn) = (max(mnp, mnn - a), max(mnn, mnp + a))
	print(max(mnp, mnn))

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


from collections import defaultdict
testCases = int(input())
for _ in range(testCases):
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = []
	maxima = True
	if len(arr) == 1:
		print(arr[0])
		continue
	if arr[0] > arr[1]:
		ans.append(arr[0])
		maxima = False
	for i in range(1, n - 1):
		if maxima:
			if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
				ans.append(arr[i])
				maxima = False
		elif arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			ans.append(arr[i])
			maxima = True
	if maxima:
		ans.append(arr[n - 1])
	anss = 0
	for i in range(len(ans)):
		anss += (-1) ** i * ans[i]
	print(anss)


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	s = 0
	for i in range(n - 1):
		s += max(0, l[i] - l[i + 1])
	s += l[-1]
	print(s)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from heapq import *
from math import gcd, factorial, floor, ceil
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 10 ** 9 + 7

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

@iterative
def rec(a, pos, ans, ll, dp):
	if pos == len(a):
		yield 0
	if dp[pos][ll] != -1:
		yield dp[pos][ll]
	temp = 0
	if ll == 0:
		temp = max((yield rec(a, pos + 1, ans, ll, dp)), a[pos] + (yield rec(a, pos + 1, ans + a[pos], 1 - ll, dp)))
	else:
		temp = max((yield rec(a, pos + 1, ans, ll, dp)), -a[pos] + (yield rec(a, pos + 1, ans - a[pos], 1 - ll, dp)))
	dp[pos][ll] = temp
	yield temp

def solve(case):
	(n, q) = sep()
	a = lis()
	dp = [[-1, -1] for i in range(n)]
	print(rec(a, 0, 0, 0, dp))
testcase(int(inp()))


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	psum = 0
	ans = 0
	p = []
	suffmax = [0] * n
	suffmax[-1] = a[-1]
	for j in range(n - 2, -1, -1):
		suffmax[j] = max(a[j], suffmax[j + 1])
	for i in range(1, n - 1):
		psum += max(a[i - 1] - a[i], 0)
		ans = max(ans, psum + suffmax[i + 1])
	ans = max(ans, max(a))
	print(ans)


def pica(arr):
	n = len(arr)
	ans = 0
	arr = [0] + arr + [0]
	for i in range(1, n + 1):
		if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
			ans += arr[i]
		if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			ans -= arr[i]
	return ans

def main():
	(n, q) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(pica(arr))
for _ in range(int(input())):
	main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lis = list(map(int, input().split()))
	dp = [[0] * 2 for i in range(n)]
	dp[0][1] = lis[0]
	ans = 0
	for i in range(1, n):
		t = max(dp[i - 1][0] + lis[i], dp[i - 1][1])
		p = max(0, dp[i - 1][1] - lis[i], dp[i - 1][0])
		dp[i][0] = p
		dp[i][1] = t
	for i in dp:
		ans = max(ans, i[0], i[1])
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		(n, q) = LI()
		a = LI()
		dp = [0, 0]
		for c in a:
			dp = [max(dp[0], dp[1] + c), max(dp[1], dp[0] - c)]
		rr.append(dp[0])
	return JA(rr, '\n')
print(main())


from math import *
t = int(input())
while t:
	t = t - 1
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	if n == 1:
		print(a[0])
		continue
	i = 0
	out = 0
	flag = 1
	while i < n - 1:
		if flag == 0:
			while i < n - 1 and a[i] >= a[i + 1]:
				i += 1
			out -= a[i]
			flag = 1
		elif flag == 1:
			while i < n - 1 and a[i] <= a[i + 1]:
				i += 1
			out += a[i]
			flag = 0
	if flag:
		out += a[i]
	print(out)


t = int(input())
for _ in range(t):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	maxch = [0 for i in range(n)]
	maxnch = [0 for i in range(n)]
	for i in range(n):
		maxch[i] = max(maxnch[i - 1] - a[i], maxch[i - 1])
		maxnch[i] = max(maxch[i - 1] + a[i], maxnch[i - 1])
	mx = maxch[0]
	for i in range(n):
		if maxch[i] > mx:
			mx = maxch[i]
		if maxnch[i] > mx:
			mx = maxnch[i]
	print(mx)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	k = arr[0]
	for x in range(1, n):
		k += max(0, arr[x] - arr[x - 1])
	print(k)


from os import path
import sys
from collections import defaultdict, Counter
maxx = float('inf')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	I = lambda : int(sys.stdin.buffer.readline())
	tup = lambda : map(int, sys.stdin.buffer.readline().split())
	lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
	S = lambda : sys.stdin.readline().replace('\n', '').strip()

	def grid(r, c):
		return [lint() for i in range(r)]
	stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
	star = lambda x: print(' '.join(map(str, x)))
else:
	I = lambda : int(sys.stdin.buffer.readline())
	tup = lambda : map(int, sys.stdin.buffer.readline().split())
	lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
	S = lambda : sys.stdin.readline().replace('\n', '').strip()

	def grid(r, c):
		return [lint() for i in range(r)]
	stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
	star = lambda x: print(' '.join(map(str, x)))
for _ in range(I()):
	(n, q) = tup()
	ls = lint()
	s = m = 0
	for i in ls:
		if i > m:
			s += i - m
		s = max(s, m)
		m = i
	print(s)


def mxm(i):
	mxm = arr[i]
	while i < n - 1:
		if arr[i + 1] > arr[i]:
			mxm = arr[i + 1]
			i += 1
		else:
			break
	return [mxm, i]

def mnm(i):
	mnm = arr[i]
	while i < n - 1:
		if arr[i + 1] < arr[i]:
			mnm = arr[i + 1]
			i += 1
		else:
			break
	if i == n - 1:
		return [0, i]
	else:
		return [mnm, i]
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	ans = 0
	flag = 1
	i = 0
	if n == 1:
		print(*arr)
		continue
	while i < n - 1:
		if flag == 1:
			(val, i) = mxm(i)
			ans += val
			flag = 0
		else:
			(val, i) = mnm(i)
			ans -= val
			flag = 1
	print(ans)


import sys

def main():
	t = Iint()
	for _ in range(t):
		(n, q) = Imap()
		a = Ilist()
		(odd_minus, even_plus) = (0, 0)
		for ai in a:
			t1 = max(odd_minus - ai, even_plus)
			t2 = max(even_plus + ai, odd_minus)
			even_plus = t1
			odd_minus = t2
		print(max(odd_minus, even_plus))
en = enumerate
FIO = 10

def debug(*args, c=6):
	pass
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=sys.stderr)
BUFSIZE = 8 * 1024

def Istr():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import os

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


def solve():
	(N, Q) = map(int, input().split())
	a = list(map(int, input().split()))
	dp_plus = [0 for i in range(N + 1)]
	dp_minus = [0 for i in range(N + 1)]
	sumdp_plus = [0 for i in range(N + 1)]
	sumdp_minus = [0 for i in range(N + 1)]
	for (i, v) in enumerate(a):
		dp_plus[i] = sumdp_minus[i - 1] + v
		dp_minus[i] = sumdp_plus[i - 1] - v
		sumdp_plus[i] = max(sumdp_plus[i - 1], dp_plus[i])
		sumdp_minus[i] = max(sumdp_minus[i - 1], dp_minus[i])
	ans = max(sumdp_plus[N - 1], sumdp_minus[N - 1])
	print(ans)

def main():
	T = int(input())
	for i in range(T):
		solve()
main()


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	num = []
	f = 0
	for i in range(1, n):
		if l[i] < l[i - 1] and f == 0:
			f = 1
			num.append(l[i - 1])
		elif l[i] > l[i - 1] and f == 1:
			f = 0
			num.append(l[i - 1])
	if num:
		if num[-1] != l[-1]:
			num.append(l[-1])
	else:
		num.append(l[-1])
	su = 0
	if len(num) > 2:
		for i in range(len(num)):
			if i & 1:
				if i != len(num) - 1:
					su -= num[i]
			else:
				su += num[i]
		print(su)
	else:
		print(max(num))


t = int(input())
for i in range(t):
	(n, q) = map(int, input().split())
	b = list(map(int, input().split()))
	for j in range(q):
		(l, r) = map(int, input().split())
	dp = []
	dp.append([b[0], [b[0], 0], 0])
	j = 1
	while j < n:
		p1 = b[j] + max(dp[j - 1][2], dp[j - 1][1][1])
		p2 = -b[j] + max(dp[j - 1][0], dp[j - 1][1][0])
		p3 = max(p1, dp[j - 1][1][0])
		p4 = max(p2, dp[j - 1][1][1])
		dp.append([p1, [p3, p4], p2])
		j += 1
	print(max(dp[n - 1][0], max(dp[n - 1][1]), dp[n - 1][2]))


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
N = 1000000
factorialNumInverse = [None] * (N + 1)
naturalNumInverse = [None] * (N + 1)
fact = [None] * (N + 1)

def InverseofNumber(p):
	naturalNumInverse[0] = naturalNumInverse[1] = 1
	for i in range(2, N + 1, 1):
		naturalNumInverse[i] = naturalNumInverse[p % i] * (p - int(p / i)) % p

def InverseofFactorial(p):
	factorialNumInverse[0] = factorialNumInverse[1] = 1
	for i in range(2, N + 1, 1):
		factorialNumInverse[i] = naturalNumInverse[i] * factorialNumInverse[i - 1] % p

def factorial(p):
	fact[0] = 1
	for i in range(1, N + 1):
		fact[i] = fact[i - 1] * i % p

def Binomial(N, R, p):
	ans = fact[N] * factorialNumInverse[R] % p * factorialNumInverse[N - R] % p
	return ans

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		(n, q) = ria()
		a = ria()
		a = [0] + a + [0]
		ans = 0
		for i in range(1, n + 1):
			if a[i] < min(a[i - 1], a[i + 1]):
				ans -= a[i]
			if a[i] > max(a[i - 1], a[i + 1]):
				ans += a[i]
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


t = int(input())
while t > 0:
	(n, q) = map(int, input().split())
	l = list(map(int, input().split()))
	s = []
	a = 0
	c = 0
	ind = -1
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			a += l[i]
			ind = i
			break
	if ind == -1:
		print(l[-1])
	else:
		mi = l[ind + 1]
		ma = -1
		for i in range(ind + 1, n - 1):
			if l[i] <= l[i + 1]:
				ma = l[i + 1]
			else:
				if ma != -1:
					a += ma - mi
					ma = -1
				mi = l[i + 1]
		if ma != -1:
			a += ma - mi
		print(a)
	t -= 1


for _ in range(int(input())):
	(n, q) = map(int, input().split())
	a = list(map(int, input().split()))
	s = 0
	sp = 0
	sign = 1
	c = 0
	d = 0
	for i in range(1, n):
		if sign:
			if a[i - 1] > a[i]:
				s += a[i - 1]
				sign = 0
				c += 1
		elif a[i - 1] < a[i]:
			sp = max(s, sp)
			s -= a[i - 1]
			sign = 1
			c += 1
	sp = max(sp, s + a[-1] * sign)
	if c:
		print(max(s, sp))
	else:
		print(a[-1])


t = int(input())
for _ in range(t):
	(n, q) = [int(i) for i in input().split(' ')]
	a = [int(i) for i in input().split(' ')]
	f = [0] * (n + 1)
	g = [0] * (n + 1)
	for i in range(n - 1, -1, -1):
		f[i] = max(f[i + 1], a[i] + g[i + 1])
		g[i] = max(g[i + 1], -a[i] + f[i + 1])
	print(f[0])