import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;

		foreach (i; 0..n)
		{
			if (a[i] > b[i])
			{
				swap (a[i], b[i]);
			}
		}

		auto c = new int [n];
		c[] = b[] - a[];
		auto total = sum (c);

		auto f = new bool [total + 1];
		f[0] = true;
		foreach (i; 0..n)
		{
			foreach_reverse (j; c[i]..total + 1)
			{
				f[j] |= f[j - c[i]];
			}
		}

		auto half = total / 2;
		while (!f[half])
		{
			half -= 1;
		}

		int res = 0;
		res += (sum (a) + half) ^^ 2;
		res += (sum (a) + total - half) ^^ 2;
		foreach (i; 0..n)
		{
			res += (n - 2) * a[i] ^^ 2;
			res += (n - 2) * b[i] ^^ 2;
		}
		writeln (res);
	}
}
