import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class d_gk {
    private static final long MOD = (long) (1e9 + 7);

    public static void main(final String[] args) {
        final Scanner in = new Scanner();
        final PrintStream out = System.out;
        final int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            new d_gk().solve(in, out);
        }
    }

    private void solve(final Scanner in, final PrintStream out) {
        final int n = in.nextInt();
        final int[] a = IntStream.generate(in::nextInt).limit(n).toArray();
        final long[] k = LongStream.iterate(1L, kk -> madd(kk, -2, 0)).limit(n + 1).toArray();
        k[n] = inverse(1 - k[n]);
        
        long originalSum = 0;
        for (int i = 0; i < n; i++) {
            originalSum += a[i];
        }
        final long constOriginalSum = originalSum;
        
        final long[] b = new long[n + 1];
        final long[] x = new long[n];

        out.println(LongStream.range(0, Math.min(Arrays.stream(a).min().getAsInt(), 2) + 1)
                .flatMap(r -> {
                    for (int i = 0; i < n; i++) {
                        b[i + 1] = madd(b[i], -2, a[i] - r);
                    }
                    x[n - 1] = madd(k[n], b[n], 0);
                    for (int i = 0; i < n - 1; i++) {
                        x[i] = madd(k[i + 1], x[n - 1], b[i + 1]);
                    }
                    return IntStream.range(0, n).allMatch(i -> a[i] - x[i] - 2 * x[(n + i - 1) % n] == r)
                            ? LongStream.of((constOriginalSum - 3 * (Arrays.stream(x).sum() - Arrays.stream(x).min().getAsLong() * n)) / n)
                            : LongStream.of();
                }).max().orElse(0));
    }

    private static long inverse(final long val) {
        return BigInteger.valueOf(val).modInverse(BigInteger.valueOf(MOD)).longValue();
    }

    private static long madd(final long a, final long b, final long c) {
        return ((a * b + c) % MOD + MOD) % MOD;
    }


    static class Scanner {
        final BufferedReader reader;
        StringTokenizer tokenizer = new StringTokenizer("");

        Scanner() {
            this(new BufferedReader(new InputStreamReader(System.in)));
        }

        Scanner(final Reader reader) {
            this.reader = new BufferedReader(reader);
        }

        Scanner(final String input) {
            this(new StringReader(input));
        }


        int nextInt() {
            return Integer.parseInt(next());
        }

        String next() {
            while (!tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (final IOException e) {
                    throw new AssertionError();
                }
            }
            return tokenizer.nextToken();
        }
    }
}
