Problem: 2062C - Cirno and Operations
Language: python

Solution 1 (runtime: 140 ms):
mod = 1e9 + 7


def binpow(a, n):
    res = 1
    while n != 0:
        print(bin(n))
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res


def resheto(n):
    # n = int(input())
    primes = [int(a) for a in range(n + 1)]
    primes[1] = 0

    i = 2
    while i <= n:
        j = i + i
        while j <= n:
            primes[j] = 0
            j += i

        i += 1
    primes = [i for i in primes if i != 0]
    return primes


def delit(x):
    arr = []
    for i in range(2, int(x ** 0.5) + 1):
        if x % i == 0:
            arr.append(i)
            arr.append(x // i)
    return sorted(arr)


# n, k = 100000, 1
# arr = [10000] * 99999
# arr.append(-1)


def xfind(arr, x):
    r = len(arr) - 1
    l = 0
    while l <= r:
        mid = (l + r) // 2

        if arr[mid] == x:
            return True

        elif arr[mid] < x:
            l = mid + 1

        elif arr[mid] > x:
            r = mid - 1

    return False



from math import ceil
import bisect

from decimal import Decimal, getcontext

getcontext().prec = 15
#
# resh = resheto(10**6)

# def s(arr):
#     s = 0
#     for i in range(len(arr)):
#         mn = 1000000
#         for j in range(i, len(arr)):
#             mn = min(mn, arr[j])
#             s += mn
#     print(s)


from bisect import bisect_left, bisect_right

# def bs_l(a, x, lo=0, hi=None):
#     if hi is None: hi = len(a)
#     pos = bisect_left(a, x, lo, hi)
#     return pos if pos != hi and a[pos] == x else -1
#
# def bs_r(a, x, lo=0, hi=None):
#     if hi is None: hi = len(a)
#     pos = bisect_right(a, x, lo, hi)
#     return pos if pos != hi and a[pos] == x else -1

def pref_sum(arr):
    pref_sum_arr = [0] * len(arr)
    pref_sum_arr[0] = arr[0]
    for i in range(1, len(arr)):
        pref_sum_arr[i] = pref_sum_arr[i-1] + arr[i]
    return pref_sum_arr


def fibonacci(n):
    if n < 2:
        return n
    pp = 0
    p = 1
    for i in range(n - 1):
        pp, p = p, pp + p
    return p


import math
def fibonacci_binet(n):
    sqrt5 = math.sqrt(5)
    phi = (1 + sqrt5) / 2
    psi = (1 - sqrt5) / 2
    return round((phi**n - psi**n) / sqrt5)


from math import ceil
from bisect import bisect_left, bisect_right
from math import gcd, factorial
from decimal import Decimal
getcontext().prec = 4


def new_arr(arr):
    x = []
    for i in range(len(arr) - 1):
        x.append(arr[i+1] - arr[i])
    return x

def solve():
    n = int(input())
    arr = [int(a) for a in input().split()]
    ans = sum(arr)
    
    while len(arr) > 1:
        rev_arr = new_arr(arr[::-1])
        arr = new_arr(arr)
        ans = max(ans, sum(arr), sum(rev_arr))
        
    print(ans)


def main():
    t = int(input())
    for _ in range(t):
        # if _ == 152:
        #     n, k = map(int, input().split())
        #     s = input()
        #     print(s)
        # else:
        solve()


main()



Solution 2 (runtime: 281 ms):
import sys
from collections import defaultdict,deque,Counter
from heapq import heappush,heappop,heapify
from math import inf,comb,ceil,log,lcm,gcd,isqrt
from functools import cache
from itertools import accumulate,combinations,permutations
from bisect import bisect_left,bisect_right,insort_left
from random import randint

input=lambda:sys.stdin.readline().rstrip()
inp=lambda:list(map(int,input().split()))
I=lambda:int(input())
fact=[cur:=1]+[cur:=(cur*i)for i in range(1,100)]
def comb(n,k):
    return fact[n]//(fact[n-k]*fact[k])
def f(n,b):
    o=0
    for i in range(n):
        if b:
            if i%2:
                o-=comb(n-1,i)*a[i]
            else:
                o+=comb(n-1,i)*a[i]
        else:
            if i%2:
                o-=comb(n-1,i)*a[~i]
            else:
                o+=comb(n-1,i)*a[~i]

    return o
"""def bf(ox):
    
    n=len(ox)
    o=sum(ox)

    for i in range(1<<(n-1)):
        x=ox.copy()
        for v in range(n-1):
            if (1<<v)&i:
                x=[x[ii]-x[ii+1]for ii in range(len(x)-1)]
            else:
                x=[x[ii+1]-x[ii]for ii in range(len(x)-1)]
            o=max(o,sum(x))
    return o"""

t=I()
for _ in range(t):
    n=I()
    a=inp()
    #n=randint(1,5)
    #a=[randint(-1000,1000)for _ in range(n)]
    o=sum(a)
    for i in range(n-1):
        c1=f(i+1,1)
        c2=f(i+1,0)
        if i==0:
            o=max(o,abs(a[-1]-a[0]))
        else:
            o=max(abs(c1)+abs(c2),o)
    #print(sum(abs(e)for e in a))
    o=max(o,f(n,0),f(n,1))
    print(o)
    #assert o==bf(a),(bf(a),o,a)