// Arup Guha
// 2/6/2022
// Solution to 20222 Jan USACO Bronze Problem: Drought

import java.util.*;
import java.io.*;

public class drought {

	public static int n;
	public static long[] vals;
	public static long sum;
	
	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		int nC = Integer.parseInt(stdin.readLine());
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
		
			// Set up variables.
			n = Integer.parseInt(stdin.readLine());
			vals = new long[n];
			sum = 0;
			
			// Read in numbers and sum.
			StringTokenizer tok = new StringTokenizer(stdin.readLine());
			for (int i=0; i<n; i++) {
				vals[i] = Long.parseLong(tok.nextToken());
				sum += vals[i];
			}
			
			// Ta da!
			System.out.println(go());
		}
	}
	
	public static long go() {
	
		// Get rid of these easy cases.
		if (n == 1) return 0;
		if (n == 2) {
			if (vals[0] != vals[1]) return -1;
			return 0;
		}
		
		// I aim for a target array equal to the first value.
		// subs[i] is how many times I have to subtract at index i (and i+1).
		long min = 0;
		long[] subs = new long[n-1];
		for (int i=1; i<n-1; i++) {
			long need = vals[i]-vals[0];
			need -= subs[i-1];
			subs[i] = need;
			min = Math.min(min, subs[i]);
		}
		
		// These are our two possible values.
		// I think this has to do with the alternating behavior when doing the system of 
		// equations going from left to right, when we assume a target value.
		long realT1 = Math.min(vals[0], vals[n-1] - subs[n-2]); 
		long realT2 = Math.min(vals[0], vals[n-1] - subs[n-2] + min);
		
		// Just try both.
		long res1 = solve(realT1);
		long res2 = solve(realT2);
		
		// Kind of annoying, but get the best answer out of the two.
		if (res1 == -1 && res2 == -1) return -1;
		if (res1 == -1) return res2;
		if (res2 == -1) return res1;
		return Math.min(res1, res2);

	}
	
	// Returns the answer for target t.
	public static long solve(long t) {
		
		// Not allowed.
		if (t < 0) return -1;
		
		// Current value.
		long cur = vals[0]-t;
		
		// Not allowed.
		if (cur < 0) return -1;
		
		// Force rest of the values.
		for (int i=1; i<n-1; i++) {
			
			// See what else I have to subtract here.
			long next = vals[i]-cur;
			long sub = next-t;
			
			// Can't do it.
			if (sub < 0) return -1;
			cur = sub;
		}
		
		// Last consistency check.
		if (vals[n-1]-cur != t) return -1;
		
		// This is what we had to subtract out.
		return sum - n*t;
	}
}