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 k = readln.splitter.map !(to !(long)).array;
		auto h = readln.splitter.map !(to !(long)).array;

		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				h[i] = max (h[i], h[j] - k[j] + k[i]);
			}
		}
		long res = 0;
		long cur = 0;
		foreach (i; 0..n)
		{
			if (i > 0 && h[i] > k[i] - k[i - 1])
			{
				res += cur * 1L * (k[i] - k[i - 1]);
				res += (k[i] - k[i - 1]) *
				    (k[i] - k[i - 1] + 1L) / 2;
				cur += k[i] - k[i - 1];
			}
			else
			{
				res += h[i] * (h[i] + 1L) / 2;
				cur = h[i];
			}
		}
		writeln (res);
	}
}
