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(arr,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

for _ in range(int(input())):
    n,q = list(map(int,input().split()))
    arr = [-999999999999,-999999999998] + list(map(int,input().split())) + [-99999999998,-99999999999]
    picked = []
    flag = False
    for i in range(2,n+2):

        if not flag:
            if arr[i] > arr[i-1] and arr[i] > arr[i+1]:
                picked.append(arr[i])
                flag = not flag
        else:
            if 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
    ans+=str(s) + "\n"
    for i in range(q):
        a,b = list(map(int,input().split()))
        a+=1
        b+=1
        if (b-a >= 5):
            sWill = s
            sWill += change(arr,a,a)
            sWill += change(arr,b,b)
            arr[a],arr[b] = arr[b],arr[a]
            sWill -= change(arr, a,a)
            sWill -= change(arr, b,b)
            s = sWill
        else:
            # print('here')
            sWill = s
            sWill+=change(arr,a,b)
            # print('this shit:', change(arr,a,b))
            arr[a],arr[b] = arr[b],arr[a]
            sWill-=change(arr,a,b)
            s = sWill
            # print('this shit after:', change(arr,a,b))
        ans+=str(s) + "\n"
print(ans)


from math import inf as _inf
import sys as _sys
from io import StringIO
output = StringIO()
# 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 = [-999999999999,-999999999998] + list(map(int,input().split())) + [-99999999998,-99999999999]
    picked = []
    flag = False
    for i in range(2,n+2):

        if not flag:
            if arr[i] > arr[i-1] and arr[i] > arr[i+1]:
                picked.append(arr[i])
                flag = not flag
        else:
            if 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
    _sys.stdout.write(str(s) + "\n")
    for i in range(q):
        a,b = list(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:
            # print('here')
            sWill = s
            sWill+=change(a,b)
            # print('this shit:', change(arr,a,b))
            arr[a],arr[b] = arr[b],arr[a]
            sWill-=change(a,b)
            s = sWill
        _sys.stdout.write(str(s) + "\n")


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
        else:
            if 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:
            # print('here')
            sWill = s
            sWill+=change(a,b)
            # print('this shit:', change(arr,a,b))
            arr[a],arr[b] = arr[b],arr[a]
            sWill-=change(a,b)
            s = sWill
        ans += [s]
print(*ans, sep="\n")


from math import inf as _inf
import sys as _sys

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

def change(arr,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

for _ in range(int(input())):
    n,q = list(map(int,input().split()))
    arr = [-999999999999,-999999999998] + list(map(int,input().split())) + [-99999999998,-99999999999]
    picked = []
    flag = False
    for i in range(2,n+2):

        if not flag:
            if arr[i] > arr[i-1] and arr[i] > arr[i+1]:
                picked.append(arr[i])
                flag = not flag
        else:
            if 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)
    for i in range(q):
        a,b = list(map(int,input().split()))
        a+=1
        b+=1
        if (b-a >= 5):
            sWill = s
            sWill += change(arr,a,a)
            sWill += change(arr,b,b)
            arr[a],arr[b] = arr[b],arr[a]
            sWill -= change(arr, a,a)
            sWill -= change(arr, b,b)
            s = sWill
        else:
            # print('here')
            sWill = s
            sWill+=change(arr,a,b)
            # print('this shit:', change(arr,a,b))
            arr[a],arr[b] = arr[b],arr[a]
            sWill-=change(arr,a,b)
            s = sWill
            # print('this shit after:', change(arr,a,b))
        print(s)


from math import inf as _inf
import sys as _sys
from io import StringIO
output = StringIO()
# 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 = [-999999999999,-999999999998] + list(map(int,input().split())) + [-99999999998,-99999999999]
    picked = []
    flag = False
    for i in range(2,n+2):

        if not flag:
            if arr[i] > arr[i-1] and arr[i] > arr[i+1]:
                picked.append(arr[i])
                flag = not flag
        else:
            if 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
    output.write(str(s) + "\n")
    for i in range(q):
        a,b = list(map(int,input().split()))
        a+=1
        b+=1
        if (b-a >= 5):
            sWill = s
            sWill += change(arr,a,a)
            sWill += change(arr,b,b)
            arr[a],arr[b] = arr[b],arr[a]
            sWill -= change(arr, a,a)
            sWill -= change(arr, b,b)
            s = sWill
        else:
            # print('here')
            sWill = s
            sWill+=change(arr,a,b)
            # print('this shit:', change(arr,a,b))
            arr[a],arr[b] = arr[b],arr[a]
            sWill-=change(arr,a,b)
            s = sWill
        output.write(str(s) + '\n')
_sys.stdout.write(output.getvalue())


import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192<<3

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 traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    for _ in range(t):
        n,q = map(int,input().split())
        li = [int(i) for i in input().split()]
        if n==1:
            print(li[0])
            for qi in range(q):
                trash = input()
                print(li[0])
            continue
        if n==2:
            print(max(li[0],li[1]))
            for qi in range(q):
                trash = input()
                print(max(li[0],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
            else:
                if 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
            u-=1
            v-=1
            tmp = li[u]
            if u>v:
                u,v = v,u
            if u == 0:
                if li[0]<li[1]:
                    ans+= max(li[v]-li[1],0)
                else:
                    ans += max(li[1],li[v]) - li[u]
            else:
                if li[u-1]>=li[u+1]:
                    if li[u]>li[u-1]:
                        ans += max(li[u-1],li[v]) - li[u] + max(0,li[u+1] - li[v])
                    elif li[u]>=li[u+1]:
                        ans += max(li[v]-li[u-1],0)
                        ans += max(li[u+1]-li[v],0)
                    else:
                        ans += li[u] - min(li[u+1],li[v]) +max(0,li[v] - li[u-1])
                else:
                    if li[u]>li[u+1]:
                        ans += max(li[u+1],li[v]) - li[u] + max(0,li[u-1] - li[v])
                    elif li[u]>=li[u-1]:
                        ans += max(li[v]-li[u+1],0)
                        ans += max(li[u-1]-li[v],0)
                    else:
                        ans += li[u] - min(li[u-1],li[v]) +max(0,li[v] - li[u+1])

            li[u] = li[v]
            if v==n-1:
                if li[-1]<li[-2]:
                    if li[u]>li[-2]:
                        ans += li[u]-li[-2]
                else:
                    ans += max(li[-2],li[u]) - li[v]
            else:
                u,v = v,u
                if li[u-1]>=li[u+1]:
                    if li[u]>li[u-1]:
                        ans += max(li[u-1],tmp) - li[u] + max(0,li[u+1] - tmp)
                    elif li[u]>=li[u+1]:
                        ans += max(tmp-li[u-1],0)
                        ans += max(li[u+1]-tmp,0)
                    else:
                        ans += li[u] - min(li[u+1],tmp) +max(0,tmp - li[u-1])
                else:
                    if li[u]>li[u+1]:
                        ans += max(li[u+1],tmp) - li[u] + max(0,li[u-1] - tmp)
                    elif li[u]>=li[u-1]:
                        ans += max(tmp-li[u+1],0)
                        ans += max(li[u-1]-tmp,0)
                    else:
                        ans += li[u] - min(li[u-1],tmp) +max(0,tmp - li[u+1])
                u,v = v,u
            li[v] = tmp
            print(ans)

    os.write(1,b'\n'.join(GANS)+b'\n')
except:
    os.write(1,traceback.format_exc().encode())


# 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 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 query_range(l,r,ta):
    return treearray_getsum(r, ta) - treearray_getsum(l-1, ta)

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

def verify(l,r,treearray):
    # l+=1
    # r+=1
    if query_range(l,r,treearray):
        return True
    else:
        return False

def bsr(n,r,ta):
    l = n
    mid = (l+r)>>1
    while mid != r-1:
        if query_range(n,r,ta)>=1:
            r = mid
        else:
            l = mid
        mid = (l+r)>>1
    return r

def bsl(n,l,ta):
    r = n
    mid = (l+r)>>1
    while mid != r-1:
        if query_range(n,r,ta)>=1:
            r = mid
        else:
            l = mid
        mid = (l+r)>>1
    return r

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])
        for qi in range(q):
            trash = input()
            print(li[0])
        continue
    if n==2:
        print(max(li[0],li[1]))
        for qi in range(q):
            trash = input()
            print(max(li[0],li[1]))
        continue
    # daguaidian = [False for i in li]
    # xiaoguaidian = [False for i in li]
    # TAB = [0 for i in range(len(li)+1)]
    # TAS = [0 for i in range(len(li)+1)]

    hi = li[0]
    status = 1
    lo = li[0]
    hipos = 0
    ans = 0
    for p,i in enumerate(li[1:]):
        p+=2
        if status:
            if i>hi:
                hi = i
                hipos = p
            else:
                # daguaidian[p] = True

                # treearray_update(p,1,TAB)
                status = 0
                lo = i
        else:
            if i<lo:
                lo = i
            else:
                # xiaoguaidian[p] = True
                # treearray_update(p,1,TAS)
                status=1
                ans += hi-lo
                hi = i
                hipos = p
    # treearray_update(p,1,TAB)
    ans += hi
    print(ans)

    for qi in range(q):
        u,v = map(int,input().split())
        u-=1
        v-=1
        tmp = li[u]
        if u>v:
            u,v = v,u
        if u==0:
            if li[0]<li[1]:
                if li[v]>li[1]:
                    ans+=li[v]-li[1]
            else:
                ans += max(li[1],li[v]) - li[u]
        else:
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if li[v] > li[u-1]:
                    ans += li[v]-li[u-1]
                if li[v] < li[u+1]:
                    ans += li[u+1]-li[v]

            if li[u-1]<=li[u] and li[u]<=li[u+1]:
                if li[v] < li[u-1]:
                    ans += li[u-1]-li[v]
                if li[v] > li[u+1]:
                    ans += li[v]-li[u+1]

            if li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if li[v] <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],li[v])
                else:
                    ans += li[u] - min(li[u-1],li[u+1],li[v]) + li[v] - max(li[u-1],li[u+1])

            if li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if li[v] >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],li[v]) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],li[v]) - li[u] + min(li[u-1],li[u+1]) - li[v]
        li[u] = li[v]
        if v==n-1:
            if li[-1]<li[-2]:
                if li[u]>li[-2]:
                    ans += li[u]-li[-2]
            else:
                ans += max(li[-2],li[u]) - li[v]
        else:
            u,v = v,u
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if tmp > li[u-1]:
                    ans += tmp-li[u-1]
                if tmp < li[u+1]:
                    ans += li[u+1]-tmp

            if li[u-1]<=li[u] and li[u]<=li[u+1]:
                if tmp < li[u-1]:
                    ans += li[u-1]-tmp
                if tmp > li[u+1]:
                    ans += tmp-li[u+1]

            if li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if tmp <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],tmp)
                else:
                    ans += li[u] - min(li[u-1],li[u+1],tmp) + tmp - max(li[u-1],li[u+1])

            if li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if tmp >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],tmp) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],tmp) - li[u] + min(li[u-1],li[u+1]) - tmp
            u,v = v,u
        li[v] = tmp
        print(ans)


import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192<<3

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 traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    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[0])
            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
            else:
                if 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')
except:
    os.write(1,traceback.format_exc().encode())


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 traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    for _ in range(t):
        n,q = map(int,input().split())
        li = [int(i) for i in input().split()]
        if n==1:
            print(li[0])
            for qi in range(q):
                trash = input()
                print(li[0])
            continue
        if n==2:
            print(max(li[0],li[1]))
            for qi in range(q):
                trash = input()
                print(max(li[0],li[1]))
            continue

        hi = li[0]
        status = 1
        lo = li[0]
        ans = 0
        for p,i in enumerate(li[1:]):
            p+=2
            if status:
                if i>hi:
                    hi = i
                else:

                    status = 0
                    lo = i
            else:
                if 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())
            u-=1
            v-=1
            tmp = li[u]
            if u>v:
                u,v = v,u
            if u==0:
                if li[0]<li[1]:
                    if li[v]>li[1]:
                        ans+=li[v]-li[1]
                else:
                    ans += max(li[1],li[v]) - li[u]
            else:
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    if li[v] > li[u-1]:
                        ans += li[v]-li[u-1]
                    if li[v] < li[u+1]:
                        ans += li[u+1]-li[v]

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    if li[v] < li[u-1]:
                        ans += li[u-1]-li[v]
                    if li[v] > li[u+1]:
                        ans += li[v]-li[u+1]

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    if li[v] <= max(li[u-1],li[u+1]):
                        ans += li[u] - min(li[u-1],li[u+1],li[v])
                    else:
                        ans += li[u] - min(li[u-1],li[u+1],li[v]) + li[v] - max(li[u-1],li[u+1])

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    if li[v] >= min(li[u-1],li[u+1]):
                        ans += max(li[u-1],li[u+1],li[v]) - li[u]
                    else:
                        ans += max(li[u-1],li[u+1],li[v]) - li[u] + min(li[u-1],li[u+1]) - li[v]
            li[u] = li[v]
            if v==n-1:
                if li[-1]<li[-2]:
                    if li[u]>li[-2]:
                        ans += li[u]-li[-2]
                else:
                    ans += max(li[-2],li[u]) - li[v]
            else:
                u,v = v,u
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    if tmp > li[u-1]:
                        ans += tmp-li[u-1]
                    if tmp < li[u+1]:
                        ans += li[u+1]-tmp

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    if tmp < li[u-1]:
                        ans += li[u-1]-tmp
                    if tmp > li[u+1]:
                        ans += tmp-li[u+1]

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    if tmp <= max(li[u-1],li[u+1]):
                        ans += li[u] - min(li[u-1],li[u+1],tmp)
                    else:
                        ans += li[u] - min(li[u-1],li[u+1],tmp) + tmp - max(li[u-1],li[u+1])

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    if tmp >= min(li[u-1],li[u+1]):
                        ans += max(li[u-1],li[u+1],tmp) - li[u]
                    else:
                        ans += max(li[u-1],li[u+1],tmp) - li[u] + min(li[u-1],li[u+1]) - tmp
                u,v = v,u
            li[v] = tmp
            print(ans)

    os.write(1,b'\n'.join(GANS)+b'\n')
except:
    traceback.print_exc()


import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192<<3

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 traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    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
            else:
                if 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')
except:
    os.write(1,traceback.format_exc().encode())


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())
    li = [int(i) for i in input().split()]
    if n==1:
        print(li[0])
        for qi in range(q):
            trash = input()
            print(li[0])
        continue
    if n==2:
        print(max(li[0],li[1]))
        for qi in range(q):
            trash = input()
            print(max(li[0],li[1]))
        continue
    # daguaidian = [False for i in li]
    # xiaoguaidian = [False for i in li]
    # TAB = [0 for i in range(len(li)+1)]
    # TAS = [0 for i in range(len(li)+1)]

    hi = li[0]
    status = 1
    lo = li[0]
    hipos = 0
    ans = 0
    for p,i in enumerate(li[1:]):
        p+=2
        if status:
            if i>hi:
                hi = i
                hipos = p
            else:
                # daguaidian[p] = True

                # treearray_update(p,1,TAB)
                status = 0
                lo = i
        else:
            if i<lo:
                lo = i
            else:
                # xiaoguaidian[p] = True
                # treearray_update(p,1,TAS)
                status=1
                ans += hi-lo
                hi = i
                hipos = p
    # treearray_update(p,1,TAB)
    ans += hi
    print(ans)

    for qi in range(q):
        u,v = map(int,input().split())
        u-=1
        v-=1
        tmp = li[u]
        if u>v:
            u,v = v,u
        if u==0:
            if li[0]<li[1]:
                if li[v]>li[1]:
                    ans+=li[v]-li[1]
            else:
                ans += max(li[1],li[v]) - li[u]
        else:
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if li[v] > li[u-1]:
                    ans += li[v]-li[u-1]
                if li[v] < li[u+1]:
                    ans += li[u+1]-li[v]

            if li[u-1]<=li[u] and li[u]<=li[u+1]:
                if li[v] < li[u-1]:
                    ans += li[u-1]-li[v]
                if li[v] > li[u+1]:
                    ans += li[v]-li[u+1]

            if li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if li[v] <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],li[v])
                else:
                    ans += li[u] - min(li[u-1],li[u+1],li[v]) + li[v] - max(li[u-1],li[u+1])

            if li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if li[v] >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],li[v]) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],li[v]) - li[u] + min(li[u-1],li[u+1]) - li[v]
        li[u] = li[v]
        if v==n-1:
            if li[-1]<li[-2]:
                if li[u]>li[-2]:
                    ans += li[u]-li[-2]
            else:
                ans += max(li[-2],li[u]) - li[v]
        else:
            u,v = v,u
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if tmp > li[u-1]:
                    ans += tmp-li[u-1]
                if tmp < li[u+1]:
                    ans += li[u+1]-tmp

            if li[u-1]<=li[u] and li[u]<=li[u+1]:
                if tmp < li[u-1]:
                    ans += li[u-1]-tmp
                if tmp > li[u+1]:
                    ans += tmp-li[u+1]

            if li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if tmp <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],tmp)
                else:
                    ans += li[u] - min(li[u-1],li[u+1],tmp) + tmp - max(li[u-1],li[u+1])

            if li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if tmp >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],tmp) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],tmp) - li[u] + min(li[u-1],li[u+1]) - tmp
            u,v = v,u
        li[v] = tmp
        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")
import traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    for _ in range(t):
        n,q = map(int,input().split())
        li = [int(i) for i in input().split()]
        if n==1:
            print(li[0])
            for qi in range(q):
                trash = input()
                print(li[0])
            continue
        if n==2:
            print(max(li[0],li[1]))
            for qi in range(q):
                trash = input()
                print(max(li[0],li[1]))
            continue

        hi = li[0]
        status = 1
        lo = li[0]
        ans = 0
        for p,i in enumerate(li[1:]):
            p+=2
            if status:
                if i>hi:
                    hi = i
                else:

                    status = 0
                    lo = i
            else:
                if 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
            u-=1
            v-=1
            tmp = li[u]
            if u>v:
                u,v = v,u
            if u==0:
                if li[0]<li[1]:
                    if li[v]>li[1]:
                        ans+=li[v]-li[1]
                else:
                    ans += max(li[1],li[v]) - li[u]
            else:
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    if li[v] > li[u-1]:
                        ans += li[v]-li[u-1]
                    if li[v] < li[u+1]:
                        ans += li[u+1]-li[v]

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    if li[v] < li[u-1]:
                        ans += li[u-1]-li[v]
                    if li[v] > li[u+1]:
                        ans += li[v]-li[u+1]

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    if li[v] <= max(li[u-1],li[u+1]):
                        ans += li[u] - min(li[u-1],li[u+1],li[v])
                    else:
                        ans += li[u] - min(li[u-1],li[u+1],li[v]) + li[v] - max(li[u-1],li[u+1])

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    if li[v] >= min(li[u-1],li[u+1]):
                        ans += max(li[u-1],li[u+1],li[v]) - li[u]
                    else:
                        ans += max(li[u-1],li[u+1],li[v]) - li[u] + min(li[u-1],li[u+1]) - li[v]
            li[u] = li[v]
            if v==n-1:
                if li[-1]<li[-2]:
                    if li[u]>li[-2]:
                        ans += li[u]-li[-2]
                else:
                    ans += max(li[-2],li[u]) - li[v]
            else:
                u,v = v,u
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    if tmp > li[u-1]:
                        ans += tmp-li[u-1]
                    if tmp < li[u+1]:
                        ans += li[u+1]-tmp

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    if tmp < li[u-1]:
                        ans += li[u-1]-tmp
                    if tmp > li[u+1]:
                        ans += tmp-li[u+1]

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    if tmp <= max(li[u-1],li[u+1]):
                        ans += li[u] - min(li[u-1],li[u+1],tmp)
                    else:
                        ans += li[u] - min(li[u-1],li[u+1],tmp) + tmp - max(li[u-1],li[u+1])

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    if tmp >= min(li[u-1],li[u+1]):
                        ans += max(li[u-1],li[u+1],tmp) - li[u]
                    else:
                        ans += max(li[u-1],li[u+1],tmp) - li[u] + min(li[u-1],li[u+1]) - tmp
                u,v = v,u
            li[v] = tmp
            print(ans)

    os.write(1,b'\n'.join(GANS)+b'\n')
except:
    os.write(1,traceback.format_exc().encode())


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 traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    for _ in range(t):
        n,q = map(int,input().split())
        li = [int(i) for i in input().split()]
        if n==1:
            print(li[0])
            for qi in range(q):
                trash = input()
                print(li[0])
            continue
        if n==2:
            print(max(li[0],li[1]))
            for qi in range(q):
                trash = input()
                print(max(li[0],li[1]))
            continue

        hi = li[0]
        status = 1
        lo = li[0]
        ans = 0
        for p,i in enumerate(li[1:]):
            p+=2
            if status:
                if i>hi:
                    hi = i
                else:

                    status = 0
                    lo = i
            else:
                if 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())
            u-=1
            v-=1
            tmp = li[u]
            if u>v:
                u,v = v,u
            if u==0:
                if li[0]<li[1]:
                    if li[v]>li[1]:
                        ans+=li[v]-li[1]
                else:
                    ans += max(li[1],li[v]) - li[u]
            else:
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    if li[v] > li[u-1]:
                        ans += li[v]-li[u-1]
                    if li[v] < li[u+1]:
                        ans += li[u+1]-li[v]

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    if li[v] < li[u-1]:
                        ans += li[u-1]-li[v]
                    if li[v] > li[u+1]:
                        ans += li[v]-li[u+1]

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    if li[v] <= max(li[u-1],li[u+1]):
                        ans += li[u] - min(li[u-1],li[u+1],li[v])
                    else:
                        ans += li[u] - min(li[u-1],li[u+1],li[v]) + li[v] - max(li[u-1],li[u+1])

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    if li[v] >= min(li[u-1],li[u+1]):
                        ans += max(li[u-1],li[u+1],li[v]) - li[u]
                    else:
                        ans += max(li[u-1],li[u+1],li[v]) - li[u] + min(li[u-1],li[u+1]) - li[v]
            li[u] = li[v]
            if v==n-1:
                if li[-1]<li[-2]:
                    if li[u]>li[-2]:
                        ans += li[u]-li[-2]
                else:
                    ans += max(li[-2],li[u]) - li[v]
            else:
                u,v = v,u
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    if tmp > li[u-1]:
                        ans += tmp-li[u-1]
                    if tmp < li[u+1]:
                        ans += li[u+1]-tmp

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    if tmp < li[u-1]:
                        ans += li[u-1]-tmp
                    if tmp > li[u+1]:
                        ans += tmp-li[u+1]

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    if tmp <= max(li[u-1],li[u+1]):
                        ans += li[u] - min(li[u-1],li[u+1],tmp)
                    else:
                        ans += li[u] - min(li[u-1],li[u+1],tmp) + tmp - max(li[u-1],li[u+1])

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    if tmp >= min(li[u-1],li[u+1]):
                        ans += max(li[u-1],li[u+1],tmp) - li[u]
                    else:
                        ans += max(li[u-1],li[u+1],tmp) - li[u] + min(li[u-1],li[u+1]) - tmp
                u,v = v,u
            li[v] = tmp
            print(ans)

    os.write(1,b'\n'.join(GANS)+b'\n')
except:
    os.write(1,traceback.format_exc().encode())


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 = [int(i) for i in input().split()]
    if n==1:
        print(li[0])
        for qi in range(q):
            trash = input()
            print(li[0])
        continue
    if n==2:
        print(max(li[0],li[1]))
        for qi in range(q):
            trash = input()
            print(max(li[0],li[1]))
        continue

    hi = li[0]
    status = 1
    lo = li[0]
    ans = 0
    for p,i in enumerate(li[1:]):
        p+=2
        if status:
            if i>hi:
                hi = i
            else:

                status = 0
                lo = i
        else:
            if 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())
        u-=1
        v-=1
        tmp = li[u]
        if u>v:
            u,v = v,u
        if u==0:
            if li[0]<li[1]:
                if li[v]>li[1]:
                    ans+=li[v]-li[1]
            else:
                ans += max(li[1],li[v]) - li[u]
        else:
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if li[v] > li[u-1]:
                    ans += li[v]-li[u-1]
                if li[v] < li[u+1]:
                    ans += li[u+1]-li[v]

            elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                if li[v] < li[u-1]:
                    ans += li[u-1]-li[v]
                if li[v] > li[u+1]:
                    ans += li[v]-li[u+1]

            elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if li[v] <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],li[v])
                else:
                    ans += li[u] - min(li[u-1],li[u+1],li[v]) + li[v] - max(li[u-1],li[u+1])

            elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if li[v] >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],li[v]) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],li[v]) - li[u] + min(li[u-1],li[u+1]) - li[v]
        li[u] = li[v]
        if v==n-1:
            if li[-1]<li[-2]:
                if li[u]>li[-2]:
                    ans += li[u]-li[-2]
            else:
                ans += max(li[-2],li[u]) - li[v]
        else:
            u,v = v,u
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if tmp > li[u-1]:
                    ans += tmp-li[u-1]
                if tmp < li[u+1]:
                    ans += li[u+1]-tmp

            elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                if tmp < li[u-1]:
                    ans += li[u-1]-tmp
                if tmp > li[u+1]:
                    ans += tmp-li[u+1]

            elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if tmp <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],tmp)
                else:
                    ans += li[u] - min(li[u-1],li[u+1],tmp) + tmp - max(li[u-1],li[u+1])

            elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if tmp >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],tmp) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],tmp) - li[u] + min(li[u-1],li[u+1]) - tmp
            u,v = v,u
        li[v] = tmp
        print(ans)

os.write(1,b'\n'.join(GANS)+b'\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")

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
        else:
            if 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 os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192<<3

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 traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    for _ in range(t):
        n,q = map(int,input().split())
        li = [int(i) for i in input().split()]
        if n==1:
            print(li[0])
            for qi in range(q):
                trash = input()
                print(li[0])
            continue
        if n==2:
            print(max(li[0],li[1]))
            for qi in range(q):
                trash = input()
                print(max(li[0],li[1]))
            continue

        hi = li[0]
        status = 1
        lo = li[0]
        ans = 0
        for p,i in enumerate(li[1:]):
            p+=2
            if status:
                if i>hi:
                    hi = i
                else:

                    status = 0
                    lo = i
            else:
                if 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
            u-=1
            v-=1
            tmp = li[u]
            if u>v:
                u,v = v,u
            if u == 0:
                if li[0]<li[1]:
                    ans+= max(li[v]-li[1],0)
                else:
                    ans += max(li[1],li[v]) - li[u]
            else:
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    ans += max(li[v]-li[u-1],0)
                    ans += max(li[u+1]-li[v],0)

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    ans += max(li[u-1]-li[v],0)
                    ans += max(li[v]-li[u+1],0)

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    ans += li[u] - min(li[u-1],li[u+1],li[v]) +max(0,li[v] - max(li[u-1],li[u+1]))

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    ans += max(li[u-1],li[u+1],li[v]) - li[u] + max(0,min(li[u-1],li[u+1]) - li[v])
            li[u] = li[v]
            if v==n-1:
                if li[-1]<li[-2]:
                    if li[u]>li[-2]:
                        ans += li[u]-li[-2]
                else:
                    ans += max(li[-2],li[u]) - li[v]
            else:
                u,v = v,u
                if li[u-1]>=li[u] and li[u]>=li[u+1]:
                    ans += max(tmp-li[u-1],0)
                    ans += max(li[u+1]-tmp,0)

                elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                    ans += max(li[u-1]-tmp,0)
                    ans += max(tmp-li[u+1],0)

                elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                    ans += li[u] - min(li[u-1],li[u+1],tmp) +max(0,tmp - max(li[u-1],li[u+1]))

                elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                    ans += max(li[u-1],li[u+1],tmp) - li[u] + max(0,min(li[u-1],li[u+1]) - tmp)
                u,v = v,u
            li[v] = tmp
            print(ans)

    os.write(1,b'\n'.join(GANS)+b'\n')
except:
    os.write(1,traceback.format_exc().encode())


import os
import sys
# from io import BytesIO, IOBase

# BUFSIZE = 8192<<3

# 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 traceback
GANS = []

def print(*args):
    global GANS
    for i in args:
        GANS.append(f'{i}'.encode())

t = int(input())
try:
    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
            else:
                if 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')
except:
    os.write(1,traceback.format_exc().encode())


import os
import sys
# from io import BytesIO, IOBase
import io

# 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")
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
        else:
            if 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 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())
    li = [int(i) for i in input().split()]
    if n==1:
        print(li[0])
        for qi in range(q):
            trash = input()
            print(li[0])
        continue
    if n==2:
        print(max(li[0],li[1]))
        for qi in range(q):
            trash = input()
            print(max(li[0],li[1]))
        continue

    hi = li[0]
    status = 1
    lo = li[0]
    ans = 0
    for p,i in enumerate(li[1:]):
        p+=2
        if status:
            if i>hi:
                hi = i
            else:

                status = 0
                lo = i
        else:
            if 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())
        u-=1
        v-=1
        tmp = li[u]
        if u>v:
            u,v = v,u
        if u==0:
            if li[0]<li[1]:
                if li[v]>li[1]:
                    ans+=li[v]-li[1]
            else:
                ans += max(li[1],li[v]) - li[u]
        else:
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if li[v] > li[u-1]:
                    ans += li[v]-li[u-1]
                if li[v] < li[u+1]:
                    ans += li[u+1]-li[v]

            elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                if li[v] < li[u-1]:
                    ans += li[u-1]-li[v]
                if li[v] > li[u+1]:
                    ans += li[v]-li[u+1]

            elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if li[v] <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],li[v])
                else:
                    ans += li[u] - min(li[u-1],li[u+1],li[v]) + li[v] - max(li[u-1],li[u+1])

            elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if li[v] >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],li[v]) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],li[v]) - li[u] + min(li[u-1],li[u+1]) - li[v]
        li[u] = li[v]
        if v==n-1:
            if li[-1]<li[-2]:
                if li[u]>li[-2]:
                    ans += li[u]-li[-2]
            else:
                ans += max(li[-2],li[u]) - li[v]
        else:
            u,v = v,u
            if li[u-1]>=li[u] and li[u]>=li[u+1]:
                if tmp > li[u-1]:
                    ans += tmp-li[u-1]
                if tmp < li[u+1]:
                    ans += li[u+1]-tmp

            elif li[u-1]<=li[u] and li[u]<=li[u+1]:
                if tmp < li[u-1]:
                    ans += li[u-1]-tmp
                if tmp > li[u+1]:
                    ans += tmp-li[u+1]

            elif li[u-1] > li[u] and li[u+1] > li[u]: #极小
                if tmp <= max(li[u-1],li[u+1]):
                    ans += li[u] - min(li[u-1],li[u+1],tmp)
                else:
                    ans += li[u] - min(li[u-1],li[u+1],tmp) + tmp - max(li[u-1],li[u+1])

            elif li[u-1] < li[u] and li[u+1] < li[u]: #极大
                if tmp >= min(li[u-1],li[u+1]):
                    ans += max(li[u-1],li[u+1],tmp) - li[u]
                else:
                    ans += max(li[u-1],li[u+1],tmp) - li[u] + min(li[u-1],li[u+1]) - tmp
            u,v = v,u
        li[v] = tmp
        print(ans)


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


import sys
z=sys.stdin.readline
o=[]
y=lambda a,b:max(a-b,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(str(s))
 for i in range(q):
  a,b=map(int,z().split())
  if a==b:o.append(s);continue
  if b-a<2:
   lv=y(p[b],p[a])
   if a:lv+=y(p[a],p[a-1])
   if b<n-1:lv+=y(p[b+1],p[b])
   p[a],p[b]=p[b],p[a]
   nv=y(p[b],p[a])
   if a:nv+=y(p[a],p[a-1])
   if b<n:nv+=y(p[b+1],p[b])
   s+=nv-lv
   o.append(str(s))
   continue
  la=y(p[a+1],p[a])
  if a:la+=y(p[a],p[a-1])
  lb=y(p[b],p[b-1])
  if b<n:lb+=y(p[b+1],p[b])
  p[a],p[b]=p[b],p[a]
  na=y(p[a+1],p[a])
  if a:na+=y(p[a],p[a-1])
  nb=y(p[b],p[b-1])
  if b<n:nb+=y(p[b+1],p[b])
  s+=na+nb-la-lb
  o.append(str(s))
print('\n'.join(o))


import sys
z=sys.stdin.readline
o=[]
y=lambda a,b:max(a-b,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(str(s))
    for i in range(q):
        a,b=map(int,z().split())
        if a==b:o.append(str(s));continue
        if b-a<2:
            lv=y(p[b],p[a])
            lv+=y(p[a],p[a-1])
            if b<n:lv+=y(p[b+1],p[b])
            p[a],p[b]=p[b],p[a]
            nv=y(p[b],p[a])
            nv+=y(p[a],p[a-1])
            if b<n:nv+=y(p[b+1],p[b])
            s+=nv-lv
            o.append(str(s))
            continue
        la=y(p[a+1],p[a])
        la+=y(p[a],p[a-1])
        lb=y(p[b],p[b-1])
        if b<n:lb+=y(p[b+1],p[b])
        p[a],p[b]=p[b],p[a]
        na=y(p[a+1],p[a])
        na+=y(p[a],p[a-1])
        nb=y(p[b],p[b-1])
        if b<n:nb+=y(p[b+1],p[b])
        s+=na+nb-la-lb
        o.append(str(s))
print('\n'.join(o))


import sys,io,os
z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
o=[]
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)))


import sys
z=sys.stdin.readline
o=[]
y=lambda a,b:max(a-b,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(str(s))
 for i in range(q):
  a,b=map(int,z().split())
  if a==b:o.append(s);continue
  if b-a<2:
   lv=y(p[b],p[a])
   if a:lv+=y(p[a],p[a-1])
   if b<n:lv+=y(p[b+1],p[b])
   p[a],p[b]=p[b],p[a]
   nv=y(p[b],p[a])
   if a:nv+=y(p[a],p[a-1])
   if b<n:nv+=y(p[b+1],p[b])
   s+=nv-lv
   o.append(str(s))
   continue
  la=y(p[a+1],p[a])
  if a:la+=y(p[a],p[a-1])
  lb=y(p[b],p[b-1])
  if b<n:lb+=y(p[b+1],p[b])
  p[a],p[b]=p[b],p[a]
  na=y(p[a+1],p[a])
  if a:na+=y(p[a],p[a-1])
  nb=y(p[b],p[b-1])
  if b<n:nb+=y(p[b+1],p[b])
  s+=na+nb-la-lb
  o.append(str(s))
print('\n'.join(o))


import sys,io,os
z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
o=[]
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)))


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:
        s+=y(i,m)
        m=i
    o.append(str(s))
    for i in range(q):
        a,b=map(int,z().split())
        if a==b:o.append(str(s));continue
        if b-a<2:
            lv=y(p[b],p[a])
            lv+=y(p[a],p[a-1])
            if b<n:lv+=y(p[b+1],p[b])
            p[a],p[b]=p[b],p[a]
            nv=y(p[b],p[a])
            nv+=y(p[a],p[a-1])
            if b<n:nv+=y(p[b+1],p[b])
            s+=nv-lv
            o.append(str(s))
            continue
        la=y(p[a+1],p[a])
        la+=y(p[a],p[a-1])
        lb=y(p[b],p[b-1])
        if b<n:lb+=y(p[b+1],p[b])
        p[a],p[b]=p[b],p[a]
        na=y(p[a+1],p[a])
        na+=y(p[a],p[a-1])
        nb=y(p[b],p[b-1])
        if b<n:nb+=y(p[b+1],p[b])
        s+=na+nb-la-lb
        o.append(str(s))
print('\n'.join(o))


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
# from itertools import permutations,combinations
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
# from fractions import Fraction
# sys.setrecursionlimit(100000)
mod = 998244353
INF=float('inf')

def construct(BIT,arr):
    for i in range(len(arr)):
        BIT[i+1]=updateBIT(BIT,i+1,arr[i])

def updateBIT(BIT,v, w):
    while v <= n:
        BIT[v] += w
        v += (v & (-v))

def getvalue(BIT,v):
    ANS = 0
    while v != 0:
        ANS += BIT[v]
        v -= (v & (-v))
    return ANS

def bisect_on_BIT(BIT,x):
    if x <= 0:
        return 0
    ANS = 0
    h = 1 << (n - 1)
    while h > 0:
        if ANS + h <= n and BIT[ANS + h] < x:
            x -= BIT[ANS + h]
            ANS += h
        h //= 2
    return ANS + 1

def update(BIT, l, r, n, val):
    updateBIT(BIT, l, val)
    updateBIT(BIT, r + 1, -val)

for t in range(int(data())):
    n,q=mdata()
    a=mdata()
    BIT = [0] * (n + 1)
    for i in range(1,n):
        updateBIT(BIT,i+1,max(a[i-1]-a[i],0))
    k=0
    out(getvalue(BIT,n)+a[-1])
    for i in range(q):
        l,r=mdata()
        if l==r:
            out(getvalue(BIT, n) + a[-1])
            continue
        if l>1:
            updateBIT(BIT,l,min(a[l-1]-a[l-2],0))
        if l<n:
            updateBIT(BIT, l+1, min(a[l]-a[l-1], 0))
        if r > 1 and r-l>1:
            updateBIT(BIT, r, min(a[r - 1] - a[r - 2], 0))
        if r < n:
            updateBIT(BIT, r+1, min(a[r] - a[r - 1], 0))
        a[l - 1], a[r - 1] = a[r - 1], a[l - 1]
        if l>1:
            updateBIT(BIT, l, max(a[l - 2] - a[l-1], 0))
        if l<n:
            updateBIT(BIT, l+1, max(a[l-1] - a[l], 0))
        if r > 1 and r-l>1:
            updateBIT(BIT, r, max(a[r - 2] - a[r - 1], 0))
        if r < n:
            updateBIT(BIT, r+1, max(a[r - 1] - a[r], 0))

        out(getvalue(BIT,n)+a[-1])


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
# from itertools import permutations,combinations
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
# from fractions import Fraction
# sys.setrecursionlimit(100000)
mod = 998244353
INF=float('inf')

def updateBIT(BIT,v, w):
    while v <= n:
        BIT[v] += w
        v += (v & (-v))

def getvalue(BIT,v):
    ANS = 0
    while v != 0:
        ANS += BIT[v]
        v -= (v & (-v))
    return ANS

ans=[]
for t in range(int(data())):
    n,q=mdata()
    a=mdata()
    BIT = [0] * (n + 1)
    for i in range(1,n):
        updateBIT(BIT,i+1,max(a[i-1]-a[i],0))
    ans.append(getvalue(BIT,n)+a[-1])
    for i in range(q):
        l,r=mdata()
        if l==r:
            ans.append(ans[-1])
            continue
        if l>1:
            updateBIT(BIT,l,min(a[l-1]-a[l-2],0))
        if l<n:
            updateBIT(BIT, l+1, min(a[l]-a[l-1], 0))
        if r > 1 and r-l>1:
            updateBIT(BIT, r, min(a[r - 1] - a[r - 2], 0))
        if r < n:
            updateBIT(BIT, r+1, min(a[r] - a[r - 1], 0))
        a[l - 1], a[r - 1] = a[r - 1], a[l - 1]
        if l>1:
            updateBIT(BIT, l, max(a[l - 2] - a[l-1], 0))
        if l<n:
            updateBIT(BIT, l+1, max(a[l-1] - a[l], 0))
        if r > 1 and r-l>1:
            updateBIT(BIT, r, max(a[r - 2] - a[r - 1], 0))
        if r < n:
            updateBIT(BIT, r+1, max(a[r - 1] - a[r], 0))

        ans.append(getvalue(BIT,n)+a[-1])
outl(ans)


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
# from itertools import permutations,combinations
#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
# from fractions import Fraction
# sys.setrecursionlimit(100000)
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)


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)


# Author : -pratyay-
import sys
inp=sys.stdin.buffer.readline
inar=lambda: list(map(int,inp().split()))
inin=lambda: int(inp())
inst=lambda: inp().decode().strip()
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]))
    print(sum(diff))
    ans=sum(diff)
    for qq in range(q):
        l,r=inar();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        print(ans)


# Author : -pratyay-
import sys
inp=sys.stdin.buffer.readline
inar=lambda: list(map(int,inp().split()))
inin=lambda: int(inp())
inst=lambda: inp().decode().strip()
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();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        wrl(ans)


# Author : -pratyay-
import sys
inp=input
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=[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();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        print(ans)


# Author : -pratyay-
import sys
inp=sys.stdin.buffer.readline
inar=lambda: list(map(int,inp().split()))
inin=lambda: int(inp())
inst=lambda: inp().decode().strip()
def pr(*args,end='\n'):
	for _arg in args:
		sys.stdout.write(str(_arg)+' ')
	sys.stdout.write(end)

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]))
    pr(sum(diff))
    ans=sum(diff)
    for qq in range(q):
        l,r=inar();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        pr(ans)


# Author : -pratyay-
import sys
inp=sys.stdin.buffer.readline
inar=lambda: list(map(int,inp().split()))
inin=lambda: int(inp())
inst=lambda: inp().decode().strip()
def pr(*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]))
    pr(sum(diff))
    ans=sum(diff)
    for qq in range(q):
        l,r=inar();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        pr(ans)


# Author : -pratyay-
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();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        print(ans)


# Author : -pratyay-
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();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        wrl(ans)


# Author : -pratyay-
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=[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();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        print(ans)


# Author : -pratyay-
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();
        #print("Before: ",a,diff,ans,"l=",l,"r=",r)
        al=a[l]
        ar=a[r]

        #after changing a[l] to ar
        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]

        #after changing a[r] to a[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("After: ",a,diff,ans)
        print(ans)


import sys
input = lambda: sys.stdin.readline().rstrip()

def calc(l, r):
    if l == r: return 0
    s = max(A[l] - A[l-1], 0) if l else A[l]
    if l < N - 1: s += max(A[l+1] - A[l], 0)
    if r > l + 1: s += max(A[r] - A[r-1], 0)
    if r < N - 1: s += max(A[r+1] - A[r], 0)
    return s

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)
    for _ in range(Q):
        l, r = map(int, input().split())
        l, r = l-1, r-1
        ans -= calc(l, r)
        A[l], A[r] = A[r], A[l]
        ans += calc(l, r)
        print(ans)