Pashmak has fallen in love with an attractive girl called Parmida since one year ago...Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.

The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.

If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.  Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const a = [], b = [];
    const c = [], e = [];
    let m = 0;
    let d = 0;
    inputLine.split(' ').forEach((number, i) => i > 1 ? a.push(parseInt(number)) : b.push(parseInt(number)));

    m = Math.abs((b[1] - a[1]) / (b[0] - a[0]));
    // console.log("The slope is : ", m)

    if (b[0] - a[0] === 0) {
        d = Math.abs(b[1] - a[1]);
        c.push(a[0] + d, a[1]);
        e.push(b[0] + d, b[1]);
    }
    else if (b[1] - a[1] === 0) {
        d = Math.abs(b[0] - a[0]);
        c.push(b[0], b[1] + d);
        e.push(a[0], a[1] + d);
    } else if (m == 1) {
        d = b[1] - a[1]
        c.push(a[0], a[1] + d);
        e.push(b[0], b[1] - d);
    } else {
        return console.log(-1);
    }
    return console.log(...e, ...c);

});

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const a = [], b = [];
    const c = [], e = [];
    let m = 0;
    let d = 0;
    inputLine.split(' ').forEach((number, i) => i > 1 ? a.push(parseInt(number)) : b.push(parseInt(number)));

    m = Math.abs((b[1] - a[1]) / (b[0] - a[0]));
    // console.log("The slope is : ", m)

    if (m == Infinity) {
        d = Math.abs(b[1] - a[1]);
        c.push(a[0] + d, a[1]);
        e.push(b[0] + d, b[1]);
    }
    else if (m === 0) {
        d = Math.abs(b[0] - a[0]);
        c.push(b[0], b[1] + d);
        e.push(a[0], a[1] + d);
    } else if (m == 1) {
        d = b[1] - a[1]
        c.push(a[0], a[1] + d);
        e.push(b[0], b[1] - d);
    } else {
        return console.log(-1);
    }
    return console.log(...e, ...c);

});

;(function () {

	var l = readline().split(' ');
	print(function(p1, p2) {
		if (p1.x === p2.x) {
			var d = Math.abs(p1.y - p2.y);
			return [p1.x + d, p1.y, p2.x + d, p2.y].join(' ');
		} else if (p1.y === p2.y) {
			var d = Math.abs(p1.x - p2.x);
			return [p1.x, p1.y + d, p2.x, p2.y + d].join(' ');
			return 2;
		} else if (Math.abs(p1.x - p2.x) === Math.abs(p1.y - p2.y) && p1.x !== p2.x && p1.y !== p2.y) {
			return [p1.x, p2.y, p2.x, p1.y].join(' ');
			return 3;
		} else return -1;
	}({x: +l[0], y: +l[1]}, {x: +l[2], y: +l[3]}));

}).call(this);

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function main() {
	var data = tokenizeIntegers(readline()),
		x1 = data[0], y1 = data[1],
		x2 = data[2], y2 = data[3];
	if (x1 == x2) {
		var s = y2 - y1,
			x3 = x1 + s, y3 = y1,
			x4 = x3, y4 = y2;
	} else if (y1 == y2) {
		var s = x2 - x1,
			x3 = x1, y3 = y1 + s,
			x4 = x2, y4 = y3;
	} else {
		var s = Math.abs(x2 - x1);
		if (s == Math.abs(y2 - y1)) {
			var x3 = x1, y3 = y2,
				x4 = x2, y4 = y1;
		} else {
			print(-1);
			return;
		}
	}
	print(x3, y3, x4, y4);
}

main();

print(function(x1, y1, x2, y2) {
    if (x1 != x2 && y1 != y2) {
        if (Math.abs(x1 - x2) != Math.abs(y1 - y2)) {
            return -1;
        } else {
            return [x1, y2, x2, y1].join(" ");
        }
    } else {
        return [
            x1 == x2 ? x1 + Math.abs(y1 - y2) : x1,
            y1 == y2 ? y1 + Math.abs(x1 - x2) : y1,
            x1 == x2 ? x1 + Math.abs(y1 - y2) : x2,
            y1 == y2 ? y1 + Math.abs(x1 - x2) : y2,
        ].join(" ");
    }
}.apply(null, readline().split(" ").map(Number)));

print(function(x1, y1, x2, y2) {
    if (x1 != x2 && y1 != y2) {
        if (Math.abs(x1 - x2) != Math.abs(y1 - y2)) {
            return -1
        } else {
            return [x1, y2, x2, y1].join(" ")
        }
    } else {
        return [
            x1 == x2 ? x1 + Math.abs(y1 - y2) : x1,
            y1 == y2 ? y1 + Math.abs(x1 - x2) : y1,
            x1 == x2 ? x1 + Math.abs(y1 - y2) : x2,
            y1 == y2 ? y1 + Math.abs(x1 - x2) : y2,
        ].join(" ")
    }
}.apply(null, readline().split(" ").map(Number)));

//var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
var numbers = readline().split(' ').map(function(x) { return parseInt(x); });

buildByPoints(numbers);

function buildByPoints(numbers) {
    var x1 = numbers[0],
        y1 = numbers[1],
        x2 = numbers[2],
        y2 = numbers[3];

    var answ = [];
    var lenght = Math.abs(x1 - x2 || y1 - y2);
    if (x1 === x2) {
        answ.push(x1 + lenght, y1, x1 + lenght, y2);
    } else if (y1 === y2) {
        answ.push(x1, y1 + lenght, x2, y2 + lenght);
    } else {
        var d1 = Math.abs(y2 - y1);
        var d2 = Math.abs(x2 - x1);
        if (d1 !== d2) {
            print(-1);
            return;
        } else {
            answ.push(x1, y2, x2, y1);
        }
    }
    print(answ.join(' '));
}

function main()	{
			var cor = readline().replace(/\s{1,}/gi, ' ').split(/\s/);

			cor = cor.map(function(val)	{
				return parseInt(val);
			});

			var x1 = cor[0], y1 = cor[1],
				x2 = cor[2], y2 = cor[3];

			var eCors = getExtCor(x1, y1, x2, y2);

			var x3 = eCors[0].x, y3 = eCors[0].y,
				x4 = eCors[1].x, y4 = eCors[1].y;

			if(!isSquare([x1, x2, x3, x4],[y1, y2, y3, y4]))	print(-1);
			else	print(x3 + ' ' + y3 + ' ' + x4 + ' ' + y4 + ' ');
		};

		function isSquare(xArr, yArr)	{
			var width, height;

			for(var i=1; i<4; i++)	{
				width = xArr[0] - xArr[i] == 0 ? width : Math.abs(xArr[0] - xArr[i]);
				height = yArr[0] - yArr[i] == 0 ? height : Math.abs(yArr[0] - yArr[i]);
			}

			return width == height ? true : false;
		};

		function getExtCor(x1, y1, x2, y2)	{
			var eCor1 = { 'x' : 0, 'y' : 0 },
				eCor2 = { 'x' : 0, 'y' : 0 };

			if(Math.abs(x2 - x1) == Math.abs(y2 - y1))	{
				eCor1.x = x1;
				eCor1.y = y2;

				eCor2.x = x2;
				eCor2.y = y1;
			} else	{
				var width = Math.abs(x2 - x1),
					height = Math.abs(y2 - y1);

				if(width == 0)	width = height;
				if(height == 0)	height = width;

				// console.log(width, height);

				if(x2 == x1)	{
					eCor1.x = x1 + width;
					eCor1.y = y1;

					eCor2.x = x2 + width;
					eCor2.y = y2;
				} else if(y2 == y1)	{
					eCor1.x = x1;
					eCor1.y = y1 + height;

					eCor2.x = x2;
					eCor2.y = y2 + height;
				} else	{
					eCor1.x = x1;
					eCor1.y = y2;

					eCor2.x = x2;
					eCor2.y = y1;
				}
			}

			// console.log(JSON.stringify([ eCor1, eCor2 ]));

			return [ eCor1, eCor2 ];
		};

		main();

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const firstTwo = inputLine.split(' ').map(num => parseInt(num));
    const A = firstTwo.slice(0, 2);
    const B = firstTwo.slice(2, 4);
    let d = Math.sqrt(Math.pow(B[0] - A[0], 2) + Math.pow(B[1] - A[1], 2));

    // if the two ponits are placed diagonally
    if (A[0] - B[0] != 0 && A[1] - B[1] != 0)
        d = d / Math.sqrt(2);

    if (A[0] - B[0] == 0)
        return console.log(A[0] + d, A[1], B[0] + d, B[1])
    else if (A[1] - B[1] == 0)
        return console.log(A[0], A[1] + d, B[0], B[1] + d)
    else if ((A[0] - B[0]) / (A[1] - B[1]) == 1)
        return console.log(A[0], A[1] + d, B[0], B[1] - d)
    else if ((A[0] - B[0]) / (A[1] - B[1]) == -1)
        return console.log(A[0], A[1] - d, B[0], B[1] + d)

    return console.log(-1)

});

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const a = [], b = [];
    const c = [], e = [];
    let m = 0;
    let d = 0;
    inputLine.split(' ').forEach((number, i) => i > 1 ? a.push(parseInt(number)) : b.push(parseInt(number)));

    m = (b[1] - a[1]) / (b[0] - a[0]);
    if (m > 1 && m !== Infinity) {
        return console.log(-1);
    } else if (Math.abs(m) == Infinity) {
        d = Math.abs(b[1] - a[1]);
        c.push(a[0] + d, a[1]);
        e.push(b[0] + d, b[1]);
    } else if (m == 0) {
        // console.log("The slop is : ", m)
        d = Math.abs(a[0] - b[0]);
        c.push(b[0], b[1] + d);
        e.push(a[0], a[1] + d);
    } else if (m == 1) {
        // console.log("The slop is : ", m)
        d = Math.abs(a[1] - b[1]);
        c.push(a[0], a[1] - d);
        e.push(b[0], b[1] + d);
    } else {
        // console.log("The slop is : ", m)
        d = Math.abs(a[1] - b[1]);
        c.push(a[0], a[1] + d);
        e.push(b[0], b[1] - d);
    }

    return console.log(...e, ...c);

});

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const a = [], b = [];
    const c = [], e = [];
    let m = 0;
    let d = 0;
    inputLine.split(' ').forEach((number, i) => i > 1 ? a.push(parseInt(number)) : b.push(parseInt(number)));

    m = (b[1] - a[1]) / (b[0] - a[0]);
    if (m > 1 && m !== Infinity) {
        return console.log(-1);
    } else if (Math.abs(m) == Infinity) {
        d = Math.abs(b[1] - a[1]);
        c.push(a[0] + d, a[1]);
        e.push(b[0] + d, b[1]);
    } else if (m === 0) {
        // console.log("The slop is : ", m)
        d = Math.abs(a[0] - b[0]);
        c.push(b[0], b[1] + d);
        e.push(a[0], a[1] + d);
    } else if (Math.abs(m)/1 === 1) {
        // console.log("The slop is : ", m)
        d = Math.abs(a[1] - b[1]);
        c.push(a[0], a[1] + d);
        e.push(b[0], b[1] - d);
    }

    return console.log(...e, ...c);

});

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const a = [], b = [];
    const c = [], e = [];
    let m = 0;
    let d = 0;
    inputLine.split(' ').forEach((number, i) => i > 1 ? a.push(parseInt(number)) : b.push(parseInt(number)));

    m = (b[1] - a[1]) / (b[0] - a[0]);
    if (m > 1 && m !== Infinity) {
        return console.log(-1);
    } else if (Math.abs(m) == Infinity) {
        d = Math.abs(b[1] - a[1]);
        c.push(a[0] + d, a[1]);
        e.push(b[0] + d, b[1]);
    } else if (m === 0) {
        // console.log("The slope is : ", m)
        d = Math.abs(b[0] - a[0]);
        c.push(b[0], b[1] + d);
        e.push(a[0], a[1] + d);
    } else if (m == 1) {
        // console.log("The slope is : ", m)
        d = b[1] - a[1];
        c.push(a[0], a[1] + d);
        e.push(b[0], b[1] - d);
    } else {
        // console.log("The slope is : ", m)
        d = b[1] - a[1]
        c.push(a[0], a[1] + d);
        e.push(b[0], b[1] - d);
    }

    return console.log(...e, ...c);

});

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const a = [], b = [];
    const c = [], e = [];
    let m = 0;
    let d = 0;
    inputLine.split(' ').forEach((number, i) => i > 1 ? a.push(parseInt(number)) : b.push(parseInt(number)));

    m = Math.abs((b[1] - a[1]) / (b[0] - a[0]));
    if (m > 1 && m !== Infinity) {
        return console.log(-1);
    } else if (m == Infinity) {
        d = Math.abs(b[1] - a[1]);
        c.push(a[0] + d, a[1]);
        e.push(b[0] + d, b[1]);
    } else if (m === 0) {
        // console.log("The slop is : ", m)
        d = Math.abs(a[0] - b[0]);
        c.push(b[0], b[1] + d);
        e.push(a[0], a[1] + d);
    } else {
        // console.log("The slop is : ", m)
        d = Math.abs(a[1] - b[1]);
        c.push(a[0], a[1] - d);
        e.push(b[0], b[1] + d);
    }

    return console.log(...e, ...c);

});

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    let firstTwoPoints = inputLine.split(' ').map(num => parseInt(num));
    let length = firstTwoPoints.length;
    let points = [];

    for (let i = 0; i < length / 2; i++) {
        points.push(firstTwoPoints.splice(0, 2));
    }

    const a = points[0];
    const b = points[1];

    const l1 = Math.sqrt(Math.pow(b[0] - a[0], 2) + Math.pow(b[1] - a[1], 2))
    let c, d;

    if (isInt(l1)) {
        d = [b[0] + b[1], b[1]];
        c = [d[0], d[1] - b[1]];
        const l2 = Math.sqrt(Math.pow(d[0] - b[0], 2) + Math.pow(d[1] - b[1], 2))
        if (l2 != l1) {
            return console.log(-1)
        }
    } else {
        d = [a[0] + b[0], a[1]];
        c = [a[0], a[1] + b[1]];
        const l1 = Math.sqrt(Math.pow(b[0] - d[0], 2) + Math.pow(b[1] - d[1], 2))
        const l2 = Math.sqrt(Math.pow(b[0] - c[0], 2) + Math.pow(b[1] - c[1], 2))
        if (l2 != l1) {
            return console.log(-1)
        }
    }

    console.log(...c, ...d)

});

function isInt(n) {
    return (typeof n == 'number' && n % 1 == 0);
}

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const firstTwo = inputLine.split(' ').map(num => parseInt(num));
    const A = firstTwo.slice(0, 2);
    const B = firstTwo.slice(2, 4);
    let d = Math.sqrt(Math.pow(B[0] - A[0], 2) + Math.pow(B[1] - A[1], 2));

    // if the two ponits are placed diagonally
    if (A[0] - B[0] !== 0 && A[1] - B[1] !== 0)
        d = d / Math.sqrt(2);

    if (A[0] - B[0] === 0)
        return console.log(A[0] + d, A[1], B[0] + d, B[1]);
    else if (A[1] - B[1] === 0)
        return console.log(A[0], A[1] + d, B[0], B[1] + d);
    else if ((A[0] - B[0]) / (A[1] - B[1]) == 1)
        return console.log(A[0], A[1] - d, B[0], B[1] + d);
    else if ((A[0] - B[0]) / (A[1] - B[1]) == -1)
        return console.log(A[0], A[1] - d, B[0], B[1] + d);

    return console.log(-1);

});

const readline = require('readline');

let inputs = [];

// Create readline Interface
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// Read Inputs
rl.on('line', inputLine => {
    const a = [], b = [];
    const c = [], e = [];
    let m = 0;
    let d = 0;
    inputLine.split(' ').forEach((number, i) => i > 1 ? a.push(parseInt(number)) : b.push(parseInt(number)));

    m = Math.abs((b[1] - a[1]) / (b[0] - a[0]));
    // console.log("The slope is : ", m)

    if (m == Infinity) {
        d = Math.abs(b[1] - a[1]);
        c.push(a[0] + d, a[1]);
        e.push(b[0] + d, b[1]);
    } else if (m > 1) {
        return console.log(-1);
    }
    else if (m == 0) {
        d = Math.abs(b[0] - a[0]);
        c.push(b[0], b[1] + d);
        e.push(a[0], a[1] + d);
    } else {
        d = b[1] - a[1]
        c.push(a[0], a[1] + d);
        e.push(b[0], b[1] - d);
    }
    return console.log(...e, ...c);

});

var inp = readline().split(" ");
var x1 = parseInt(inp[0]);
var y1 = parseInt(inp[1]);
var x2 = parseInt(inp[2]);
var y2 = parseInt(inp[3]);

var x3,y3,x4,y4,a;

if(x1 == x2)
{
  a = y1-y2;

  x3 = x1+a;
  x4 = x2+a;

  y3 = y1;
  y4 = y2;
}
else if(y1 == y2)
{
  a = x1-x2;

  y3 = y1+a;
  y4 = y2+a;

  x3 = x1;
  x4 = x2;
}

print(x3+" "+y3+" "+x4+" "+y4);

var inp = readline().split(" ");
var x1 = parseInt(inp[0]);
var y1 = parseInt(inp[1]);
var x2 = parseInt(inp[2]);
var y2 = parseInt(inp[3]);

var x3,y3,x4,y4,a;

if(x1 == x2)
{
  a = y1-y2;

  x3 = x3+a;
  x4 = x4+a;

  y3 = y1;
  y4 = y2;
}
else if(y1 == y2)
{
  a = x1-x2;

  y3 = y3+a;
  y4 = y4+a;

  x3 = x1;
  x4 = x2;
}

print(x3+" "+y3+" "+x4+" "+y4);

var numbers = readline().split(' ').map(function(x) { return parseInt(x); });
var uniq = [];
for (var i = 0; i < 4; ++i) {
  if (uniq.indexOf(numbers[i]) === -1) {
    uniq.push(Math.abs(numbers[i]));
  }
}

if (uniq.length === 2) {
    var x1 = numbers[0],
        y1 = numbers[1],
        x2 = numbers[2],
        y2 = numbers[3];

    var answ = [];
    var lenght = Math.abs(x1 - x2 || y1 - y2);
    if (x1 === y1 && x2 === y2) {
        answ.push(x1,x2,x2,x1);
    } else if (x1 === y1) {
        answ.push(y2, x2, x1 + lenght, x1 + lenght);
    } else if (x2 === y2) {
        answ.push(y1, x1, x2 + lenght, x2 + lenght);
    } else {
        answ.push(x1, x1, y1, y1);
    }
    print(answ.join(' '));
} else {
    print(-1)
}

//var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
var numbers = readline().split(' ').map(function(x) { return parseInt(x); });
var uniq = [];
for (var i = 0; i < 4; ++i) {
  if (uniq.indexOf(numbers[i]) === -1) {
    uniq.push(Math.abs(numbers[i]));
  }
}

if (uniq.length === 2) {
    var x1 = numbers[0],
        y1 = numbers[1],
        x2 = numbers[2],
        y2 = numbers[3];

    var answ = [];
    var lenght = Math.abs(x1 - x2 || y1 - y2);
    var xDif = Math.abs(x1) - Math.abs(x2);
    var yDif = Math.abs(y1) - Math.abs(y2);
    if (x1 === y1 && x2 === y2) {
        answ.push(x1,x2,x2,x1);
    } else if (x1 === y1) {
        answ.push(y2, x2, x1 + lenght, x1 + lenght);
    } else if (x2 === y2) {
        answ.push(y1, x1, x2 + lenght, x2 + lenght);
    } else if (x1 !== 0 && xDif == 0) {
        var sum = Math.abs(x1) + Math.abs(x2);
        answ.push(sum, x1, sum, x2);
    } else if (y1 !== 0 && yDif == 0) {
        var sum = Math.abs(y1) + Math.abs(y2);
        answ.push(sum, y1, sum, y2);
    } else {
        answ.push(x1, x1, y1, y1);
    }
    print(answ.join(' '));
} else {
    print(-1)
}

var numbers = readline().split(' ').map(function(x) { return parseInt(x); });
var uniq = [];
for (var i = 0; i < 4; ++i) {
  if (uniq.indexOf(numbers[i]) === -1) {
    uniq.push(numbers[i]);
  }
}

if (uniq.length === 2) {
    var x1 = numbers[0],
        y1 = numbers[1],
        x2 = numbers[2],
        y2 = numbers[3];

    var answ = [];
    var lenght = Math.abs(x1 - x2 || y1 - y2);
    if (x1 === y1 && x2 === y2) {
        answ.push(x1,x2,x2,x1);
    } else if (x1 === y1) {
        answ.push(y2, x2, x1 + lenght, x1 + lenght);
    } else if (x2 === y2) {
        answ.push(y1, x1, x2 + lenght, x2 + lenght);
    } else {
        answ.push(x1, x1, y1, y1);
    }
    print(answ.join(' '));
} else {
    print(-1)
}

function main()	{
			var cor = readline().replace(/\s{1,}/gi, ' ').split(/\s/);

			cor = cor.map(function(val)	{
				return parseInt(val);
			});

			var x1 = cor[0], y1 = cor[1],
				x2 = cor[2], y2 = cor[3];

			if(!isSquare(x1, y1, x2, y2))	print(-1);
			else	{
				var eCors = getExtCor(x1, y1, x2, y2);

				print(eCors[0].x + ' ' + eCors[0].y + ' ' + eCors[1].x + ' ' + eCors[1].y + ' ');
			}
		};

		function isSquare(x1, y1, x2, y2)	{
			var result = false;

			if(Math.abs(x2 - x1) == Math.abs(y2 - y1))	result = true;
			else if(x2 - x1 == 0 || y2 - y1 == 0)	result = true;

			return result;
		};

		function getExtCor(x1, y1, x2, y2)	{
			var eCor1 = { 'x' : 0, 'y' : 0 },
				eCor2 = { 'x' : 0, 'y' : 0 };

			if(Math.abs(x2 - x1) == Math.abs(y2 - y1))	{
				eCor1.x = x1;
				eCor1.y = y2;

				eCor2.x = x2;
				eCor2.y = y1;
			} else	{
				var size = x2 - x1 == 0 ? Math.abs(y2 - y1) : Math.abs(x2 - x1);

				//console.log(size);

				if(x2 - x1 == 0)	{
					eCor1.x = size;
					eCor1.y = y1;

					eCor2.x = size;
					eCor2.y = y2;
				} else if(y2 - y1 == 0)	{
					eCor1.x = x1;
					eCor1.y = size;

					eCor2.x = x2;
					eCor2.y = size;
				}
			}

			//console.log(JSON.stringify([ eCor1, eCor2 ]));

			return [ eCor1, eCor2 ];
		};

		main();


An array $$$[b_1, b_2, \ldots, b_m]$$$ is a palindrome, if $$$b_i = b_{m+1-i}$$$ for each $$$i$$$ from $$$1$$$ to $$$m$$$. Empty array is also a palindrome.An array is called kalindrome, if the following condition holds: It's possible to select some integer $$$x$$$ and delete some of the elements of the array equal to $$$x$$$, so that the remaining array (after gluing together the remaining parts) is a palindrome. Note that you don't have to delete all elements equal to $$$x$$$, and you don't have to delete at least one element equal to $$$x$$$.For example :   $$$[1, 2, 1]$$$ is kalindrome because you can simply not delete a single element.  $$$[3, 1, 2, 3, 1]$$$ is kalindrome because you can choose $$$x = 3$$$ and delete both elements equal to $$$3$$$, obtaining array $$$[1, 2, 1]$$$, which is a palindrome.  $$$[1, 2, 3]$$$ is not kalindrome. You are given an array $$$[a_1, a_2, \ldots, a_n]$$$. Determine if $$$a$$$ is kalindrome or not.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array. It's guaranteed that the sum of $$$n$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$.

For each test case, print YES if $$$a$$$ is kalindrome and NO otherwise. You can print each letter in any case.

NoteIn the first test case, array $$$[1]$$$ is already a palindrome, so it's a kalindrome as well.In the second test case, we can choose $$$x = 2$$$, delete the second element, and obtain array $$$[1]$$$, which is a palindrome.In the third test case, it's impossible to obtain a palindrome.In the fourth test case, you can choose $$$x = 4$$$ and delete the fifth element, obtaining $$$[1, 4, 4, 1]$$$. You also can choose $$$x = 1$$$, delete the first and the fourth elements, and obtain $$$[4, 4, 4]$$$.

const check = (str,t)=>{
     let res = [],j=0;
     for(let i=0;i<str.length;i++) if(str[i]!==t) res[j++]=str[i];
     for(let i=0;i<res.length;i++) if(res[i]!==res[res.length-1-i]) return false;
     return true;
}
const solve = (n,str)=>{
     for(let i=0;i<n;i++){
          if(str[i]!==str[n-1-i]){
               if(check(str,str[i])===true) console.log("YES");
               else if(check(str,str[n-1-i])===true) console.log("YES");
               else console.log("NO");
               return;
          }
     }
     console.log("YES");
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for(let i=0;i<t;i++){
       let n = parseInt(readline());
       let arr = readline().trim().split(" ").map(cur=>parseInt(cur));
       solve(n,arr);
  }
}
main();

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let t = rn();
	while (t--) {
		let n = rn();
		let a = rna();

		function remove(x) {
			let ar = [];
			for (const elm of a)
				if (elm != x) ar.push(elm);
			return ar;
		}

		function isPal (s) {
			let ok = true;
			for (let i = 0; ok && i < Math.floor(s.length/2); i++)
				ok = s[i] == s[s.length-i-1];
			return ok;
		}

		let ans = true;
		for (let i = 0; i < Math.floor(n/2); i++) {
			if (a[i] != a[n - i - 1]) {
				p = a[i];
				q = a[n - i - 1];
				ans = isPal(remove(p)) || isPal(remove(q));
				break;
			}
		}

		console.log(ans ? 'YES' : 'NO');
	}
}

const check = (str,t)=>{
     let res = "";
     for(let i=0;i<str.length;i++) if(str[i]!==t) res+=str[i];
     for(let i=0;i<res.length;i++) if(res[i]!==res[res.length-1-i]) return false;
     return true;
}
const solve = (n,str)=>{
     for(let i=0;i<n;i++){
          if(str[i]!==str[n-1-i]){
               if(check(str,str[i])===true) console.log("YES");
               else if(check(str,str[n-1-i])===true) console.log("YES");
               else console.log("NO");
               return;
          }
     }
     console.log("YES");
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for(let i=0;i<t;i++){
       let n = parseInt(readline());
       let arr = readline().trim().split(" ").map(cur=>(cur));
       solve(n,arr);
  }
}
main();

const check = (str,t)=>{
     let res = "";
     for(let i=0;i<str.length;i++) if(str[i]!==t) res+=str[i];
     for(let i=0;i<res.length;i++) if(res[i]!==res[res.length-1-i]) return false;
     return true;
}
const solve = (n,str)=>{
     for(let i=0;i<n;i++){
          if(str[i]!==str[n-1-i]){
               if(check(str,str[i])===true) console.log("YES");
               else if(check(str,str[n-1-i])===true) console.log("YES");
               else console.log("NO");
               return;
          }
     }
     console.log("YES");
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for(let i=0;i<t;i++){
       let n = parseInt(readline());
       let arr = readline().trim().split(" ").map(cur=>parseInt(cur));
       solve(n,arr);
  }
}
main();


You're given an array $$$b$$$ of length $$$n$$$. Let's define another array $$$a$$$, also of length $$$n$$$, for which $$$a_i = 2^{b_i}$$$ ($$$1 \leq i \leq n$$$). Valerii says that every two non-intersecting subarrays of $$$a$$$ have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers $$$l_1,r_1,l_2,r_2$$$ that satisfy the following conditions:   $$$1 \leq l_1 \leq r_1 \lt l_2 \leq r_2 \leq n$$$;  $$$a_{l_1}+a_{l_1+1}+\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\ldots+a_{r_2-1}+a_{r_2}$$$. If such four integers exist, you will prove Valerii wrong. Do they exist?An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of every test case contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$). The second line of every test case contains $$$n$$$ integers $$$b_1,b_2,\ldots,b_n$$$ ($$$0 \le b_i \le 10^9$$$).

For every test case, if there exist two non-intersecting subarrays in $$$a$$$ that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line.  Also, note that each letter can be in any case.

NoteIn the first case, $$$a = [16,8,1,2,4,1]$$$. Choosing $$$l_1 = 1$$$, $$$r_1 = 1$$$, $$$l_2 = 2$$$ and $$$r_2 = 6$$$ works because $$$16 = (8+1+2+4+1)$$$.In the second case, you can verify that there is no way to select to such subarrays.

var tc = parseInt(readline());
for(; tc--; ){
    var ok = 0;
    var n = parseInt(readline());
    var ar = readline().split(' ').map(x => +x);
    for(var i=0; i<n; i++){
        for(var j=i+1; j<n; j++){
            if(ar[i] == ar[j]) ok = 1;
        }
    }
    print(ok ? 'YES' : 'NO');
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    const set = new Set();

    for (let n of arr) {
      set.add(n);
    }
    if (set.size === len) console.log(`NO`);
    else console.log(`YES`);
  }
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	var output = new Array(t);
	for(var i = 0; i < t; i++){
		var N = nextInt();
		var list = nextIntArray();
		var used = new Set();
		var isOK = false;
		for(var j = 0; j < N; j++){
			if(used.has(list[j])){
				isOK = true;
			}else{
				used.add(list[j]);
			}
		}
		output[i] = (isOK) ? "YES" : "NO";
	}
	myout(myconv(output, 9));
}

(()=>{"use strict";var t={675:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),Array.numbersMatrix=(t,e,r)=>{let n=[];null==t&&(e=t),null==r&&(r=0);for(let i=0;i<t;++i){let t=[];for(let n=0;n<e;++n)t.push(r);n.push(t)}return n},Array.prototype.sortAsc=function(){return this.sort(((t,e)=>t-e))},Array.prototype.sortDesc=function(){return this.sort(((t,e)=>e-t))},Array.prototype.max=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.max(t,this[e]);return t},Array.prototype.min=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.min(t,this[e]);return t},Array.prototype.sum=function(){let t=0;for(let e=0;e<this.length;++e)t+=this[e];return t},Array.prototype.modSum=function(t){let e=0;for(let r=0;r<this.length;++r)e+=this[r],e%=t;return e},Array.prototype.avg=function(){return this.sum()/this.length},Array.prototype.groupBy=function(t){let e=new Map;for(let r=0;r<this.length;++r){const n=t(this[r]);e.has(n)?e.get(n).push(this[r]):e.set(n,[this[r]])}return e},Array.prototype.deepCopy=function(){let t=[];if(0==this.length)return t;let e=!1;Array.isArray(this[0])&&(e=!0);for(let r=0;r<this.length;++r)e?t.push(this[r].deepCopy()):t.push(this[r]);return t},Array.prototype.transpose=function(){let t=[];if(0==this.length)return[[]];for(let e=0;e<this[0].length;++e){let r=[];for(let t=0;t<this.length;++t)r.push(this[t][e]);t.push(r)}return t},Array.prototype.toString=function(){if(0==this.length)return"";if(!Array.isArray(this[0]))return this.join(" ");let t="";for(let e=0;e<this.length;++e)t+=this[e].join(" "),e!=this.length-1&&(t+="\n");return t}},312:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const i=n(r(747));let s="",o=[],u=0,l="";const p="local"===process.argv[2],a=t=>{if(p){if(o=i.default.readFileSync("input.txt").toString().trim().split("\n").map((t=>t.trim())),t(),i.default.writeFileSync("output.txt",l),console.log(l),i.default.readFileSync("expected.txt").toString().trim()==l.trim()){const{exec:t}=r(129);t("osascript -e 'display notification \"✅ AC!!\"'")}}else process.stdin.resume(),process.stdin.setEncoding("utf-8"),process.stdin.on("data",(t=>{s+=t})),process.stdin.on("end",(e=>{o=s.trim().split("\n").map((t=>t.trim())),t(),console.log(l)}))};function h(){return o[u++]}function f(){return h().split(" ").map((t=>parseFloat(t)))}function c(){return h().split("")}e.default={runMain:a,runEachTest:t=>{a((()=>{let[e]=f();for(;e>0;)t(),e--}))},readline:h,nextNumbers:f,nextBigNumbers:function(){return h().split(" ").map((t=>BigInt(t)))},nextNumbersMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(f());return e},nextCharacters:c,nextCharacterMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(c());return e},nextIntervalArray:function(t){let e=[];for(let r=0;r<t;++r){const[t,r]=f();e.push({a:t,b:r})}return e},put:function(t){l+=t},puts:function(t){l+=t+"\n"},debug:function(t){p&&console.log(t)}}},965:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),r(675);const i=n(r(312));i.default.runEachTest((function(){let[t]=i.default.nextNumbers(),e=[...i.default.nextNumbers().groupBy((t=>t)).values()].find((t=>t.length>1));i.default.puts(null==e?"NO":"YES")}))},129:t=>{t.exports=require("child_process")},747:t=>{t.exports=require("fs")}},e={};!function r(n){if(e[n])return e[n].exports;var i=e[n]={exports:{}};return t[n].call(i.exports,i,i.exports,r),i.exports}(965)})();

// Solution written in typescript and compiled via webpack. Original source code below
// Happy Hacking 😜

// import './array'
//
// import io from './io'
//
// function main() {
//   let [n] = io.nextNumbers()
//
//   let a = io.nextNumbers()
//
//   let m = a.groupBy((x) => x)
//
//   let b = [...m.values()].find((x) => x.length > 1)
//
//   io.puts(b == undefined ? "NO" : "YES")
// }
//
// io.runEachTest(main)
// // io.runMain(main)

let input = ''

process.stdin.on('data', (x) => input += x)

process.stdin.on('end', () => {
  const lines = input.trim().split('\n')
  const T = parseInt(lines[0])
  for (let t = 0; t < T; t++) {
    const s = new Set()
    let ans = 'NO'
    lines[2 * t + 2].split(' ').map((x) => parseInt(x)).forEach((x) => {
      if (s.has(x)) {
        ans = 'YES'
      }
      s.add(x)
    })
    console.log(ans)
  }
})

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    const newArr = arr.map((n) => pow(2, n));
    let [found, sum, total] = [false, 0, 0];
    const set = new Set();
    for (let i = 0; i < len; i++) total += arr[i];

    let prevSum = 0;
    for (let i = 0; i < len - 1; i++) {
      let currSum = 0;
      prevSum += arr[i];
      for (let j = i + 1; j < len; j++) {
        currSum += arr[j];
        const nextSum = total - currSum;
        if (currSum === prevSum || currSum === nextSum) {
          found = true;
          console.log(`YES`);
          break;
        }
      }
      if (found) break;
    }
    if (!found) console.log(`NO`);
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    const newArr = arr.map((n) => pow(2, n));
    let [found, sum, total] = [false, 0, 0];
    const set = new Set();
    for (let i = 0; i < len; i++) total += arr[i];

    for (let i = 0; i < len; i++) {
      sum += arr[i];
      const target = total - sum;
      const diff = sum - target;
      if (set.has(diff) || diff === 0) {
        found = true;
        console.log(`YES`);
        break;
      }
      set.add(sum);
    }
    if (!found) console.log(`NO`);
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    const newArr = arr.map((n) => pow(2, n));
    let [found, sum, total] = [false, 0, 0];
    const set = new Set();
    for (let i = 0; i < len; i++) total += newArr[i];

    let prevSum = 0;
    for (let i = 0; i < len - 1; i++) {
      let currSum = 0;
      prevSum += newArr[i];
      for (let j = i + 1; j < len; j++) {
        currSum += newArr[j];
        const nextSum = total - currSum;
        if (currSum === prevSum || currSum === nextSum) {
          found = true;
          console.log(`YES`);
          break;
        }
      }
      if (found) break;
    }
    if (!found) console.log(`NO`);
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    const newArr = arr.map((n) => pow(2, n));
    let [found, sum, total] = [false, 0, 0];
    const set = new Set();
    for (let i = 0; i < len; i++) total += newArr[i];

    for (let i = 0; i < len; i++) {
      sum += newArr[i];
      const target = total - sum;
      const diff = sum - target;
      if (set.has(diff) || diff === 0) {
        found = true;
        console.log(`YES`);
        break;
      }
      set.add(sum);
    }
    if (!found) console.log(`NO`);
  }
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	var output = new Array(t);
	for(var i = 0; i < t; i++){
		var N = nextInt();
		var list = nextIntArray();
		for(var j = 1; j < N; j++){
			list[j] += list[j - 1];
		}
		var isOK = false;
		for(var j = 0; j < N - 1; j++){
			for(var k = j + 1; k < N; k++){
				var L = list[j];
				var R = list[k] - L;
				if(L == R){
					isOK = true;
				}
			}
		}
		output[i] = (isOK) ? "YES" : "NO";
	}
	myout(myconv(output, 9));
}


This is the hard version of the problem. The only difference is that in this version $$$0 \leq k \leq 20$$$.There is an array $$$a_1, a_2, \ldots, a_n$$$ of $$$n$$$ positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.Moreover, it is allowed to do at most $$$k$$$ such operations before the division: choose a number in the array and change its value to any positive integer.What is the minimum number of continuous segments you should use if you will make changes optimally?

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 1000)$$$  — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \leq k \leq 20$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^7$$$). It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print a single integer  — the answer to the problem.

NoteIn the first test case it is possible to change the array this way: $$$[\underline{3}, 6, 2, 4, \underline{5}]$$$ (changed elements are underlined). After that the array does not need to be divided, so the answer is $$$1$$$.In the second test case it is possible to change the array this way: $$$[6, 2, \underline{3}, 8, 9, \underline{5}, 3, 6, \underline{10}, \underline{11}, 7]$$$. After that such division is optimal:   $$$[6, 2, 3]$$$  $$$[8, 9, 5, 3, 6, 10, 11, 7]$$$

// Generated by CoffeeScript 2.4.1
var A, K, N, NK, _, a, cache, dp, get_mask, get_prime, j, k, l, len, m, min, n, nr, nrs, prime, ref, ref1, used,
  indexOf = [].indexOf;

nrs = function() {
  var i, k, len, ref, results;
  ref = readline().split(' ');
  results = [];
  for (k = 0, len = ref.length; k < len; k++) {
    i = ref[k];
    results.push(parseInt(i));
  }
  return results;
};

nr = function() {
  return nrs()[0];
};

min = function(lst) {
  var best, item, k, len;
  best = lst[0];
  for (k = 0, len = lst.length; k < len; k++) {
    item = lst[k];
    best = item < best ? item : best;
  }
  return best;
};

n = 10000000;

get_prime = function(n) {
  var i, is_prime, k, l, len, ref, res, x;
  res = [];
  for (i = k = 2, ref = n; (2 <= ref ? k < ref : k > ref); i = 2 <= ref ? ++k : --k) {
    is_prime = true;
    for (l = 0, len = res.length; l < len; l++) {
      x = res[l];
      if (i % x === 0) {
        is_prime = false;
        break;
      }
    }
    if (is_prime) {
      res.push(i);
    }
  }
  return res;
};

prime = get_prime(3162);

cache = {};

get_mask = function(num) {
  var c, dv, k, key, l, len, len1, p, x;
  key = num;
  if (indexOf.call(cache, key) >= 0) {
    return cache[key];
  }
  dv = [];
  for (k = 0, len = prime.length; k < len; k++) {
    p = prime[k];
    c = 0;
    while (num % p === 0) {
      c += 1;
      num = Math.floor(num / p);
    }
    if (c % 2 === 1) {
      dv.push(p);
    }
    if (num < p * p) {
      break;
    }
  }
  for (l = 0, len1 = dv.length; l < len1; l++) {
    x = dv[l];
    num *= x;
  }
  cache[key] = num;
  return num;
};

for (_ = k = 0, ref = nr(); (0 <= ref ? k < ref : k > ref); _ = 0 <= ref ? ++k : --k) {
  NK = nrs();
  N = NK[0];
  K = NK[1];
  A = nrs();
  dp = (function() {
    var l, ref1, results;
    results = [];
    for (_ = l = 0, ref1 = K + 1; (0 <= ref1 ? l <= ref1 : l >= ref1); _ = 0 <= ref1 ? ++l : --l) {
      results.push(N);
    }
    return results;
  })();
  dp[0] = 1;
  used = (function() {
    var l, ref1, results;
    results = [];
    for (_ = l = 0, ref1 = K + 1; (0 <= ref1 ? l < ref1 : l > ref1); _ = 0 <= ref1 ? ++l : --l) {
      results.push({});
    }
    return results;
  })();
  for (l = 0, len = A.length; l < len; l++) {
    a = A[l];
    a = get_mask(a);
    for (j = m = ref1 = K; (ref1 <= 0 ? m <= 0 : m >= 0); j = ref1 <= 0 ? ++m : --m) {
      if (dp[j] === N) {
        continue;
      }
      if (a in used[j]) {
        if (j < K && dp[j + 1] > dp[j]) {
          dp[j + 1] = dp[j];
          used[j + 1] = used[j];
        }
        dp[j] += 1;
        used[j] = {};
      }
      used[j][a] = 1;
    }
  }
  print(min(dp));
}

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiamltbTg5LmpzIiwic291cmNlUm9vdCI6Ii4uIiwic291cmNlcyI6WyJjb2ZmZWVcXGppbW04OS5jb2ZmZWUiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLElBQUEsQ0FBQSxFQUFBLENBQUEsRUFBQSxDQUFBLEVBQUEsRUFBQSxFQUFBLENBQUEsRUFBQSxDQUFBLEVBQUEsS0FBQSxFQUFBLEVBQUEsRUFBQSxRQUFBLEVBQUEsU0FBQSxFQUFBLENBQUEsRUFBQSxDQUFBLEVBQUEsQ0FBQSxFQUFBLEdBQUEsRUFBQSxDQUFBLEVBQUEsR0FBQSxFQUFBLENBQUEsRUFBQSxFQUFBLE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blx0XHRcdGlmIGEgb2YgdXNlZFtqXVxuXHRcdFx0XHRpZiBqIDwgSyBhbmQgZHBbaiArIDFdID4gZHBbal1cblx0XHRcdFx0XHRkcFtqICsgMV0gPSBkcFtqXVxuXHRcdFx0XHRcdHVzZWRbaiArIDFdID0gdXNlZFtqXVxuXHRcdFx0XHRkcFtqXSArPSAxXG5cdFx0XHRcdHVzZWRbal0gPSB7fVxuXHRcdFx0dXNlZFtqXVthXSA9IDFcblx0cHJpbnQgbWluIGRwXG4iXX0=
//# sourceURL=c:\github\2021\009-CodeForces#708\1497E2\coffee\jimm89.coffee

// Generated by CoffeeScript 2.4.1
var A, K, N, _, a, cache, dp, get_mask, get_prime, j, k, l, len, len1, m, min, n, nr, nrs, prime, ref, ref1, used, xxx,
  indexOf = [].indexOf;

nrs = function() {
  var i, k, len, ref, results;
  ref = readline().split(' ');
  results = [];
  for (k = 0, len = ref.length; k < len; k++) {
    i = ref[k];
    results.push(parseInt(i));
  }
  return results;
};

nr = function() {
  return nrs()[0];
};

function range(start, stop, step) {
  if (stop == null) {
    stop = start || 0
    start = 0
  }
  if (!step) step = stop < start ? -1 : 1

  var length = Math.max(Math.ceil((stop - start) / step), 0)
  var range = Array(length)

  for (var idx = 0; idx < length; idx++, start += step) range[idx] = start

  return range;
}
;

min = function(lst) {
  var best, item, k, len;
  best = lst[0];
  for (k = 0, len = lst.length; k < len; k++) {
    item = lst[k];
    best = item < best ? item : best;
  }
  return best;
};

n = 10000000;

get_prime = function(n) {
  var i, is_prime, k, l, len, len1, ref, res, x;
  res = [];
  ref = range(2, n);
  for (k = 0, len = ref.length; k < len; k++) {
    i = ref[k];
    is_prime = true;
    for (l = 0, len1 = res.length; l < len1; l++) {
      x = res[l];
      if (i % x === 0) {
        is_prime = false;
        break;
      }
    }
    if (is_prime) {
      res.push(i);
    }
  }
  return res;
};

prime = get_prime(3162);

cache = {};

get_mask = function(num) {
  var c, dv, k, key, l, len, len1, p, x;
  key = num;
  if (indexOf.call(cache, key) >= 0) {
    return cache[key];
  }
  dv = [];
  for (k = 0, len = prime.length; k < len; k++) {
    p = prime[k];
    c = 0;
    while (num % p === 0) {
      c += 1;
      num = Math.floor(num / p);
    }
    if (c % 2 === 1) {
      dv.push(p);
    }
    if (num < p * p) {
      break;
    }
  }
  for (l = 0, len1 = dv.length; l < len1; l++) {
    x = dv[l];
    num *= x;
  }
  cache[key] = num;
  return num;
};

for (_ = k = 0, ref = nr(); (0 <= ref ? k < ref : k > ref); _ = 0 <= ref ? ++k : --k) {
  xxx = nrs();
  N = xxx[0];
  K = xxx[1];
  A = nrs();
  dp = (function() {
    var l, len, ref1, results;
    ref1 = range(K + 1);
    results = [];
    for (l = 0, len = ref1.length; l < len; l++) {
      _ = ref1[l];
      results.push(N);
    }
    return results;
  })();
  dp[0] = 1;
  used = (function() {
    var l, ref1, results;
    results = [];
    for (_ = l = 0, ref1 = K + 1; (0 <= ref1 ? l < ref1 : l > ref1); _ = 0 <= ref1 ? ++l : --l) {
      results.push({});
    }
    return results;
  })();
  for (l = 0, len = A.length; l < len; l++) {
    a = A[l];
    a = get_mask(a);
    ref1 = range(K, -1, -1);
    for (m = 0, len1 = ref1.length; m < len1; m++) {
      j = ref1[m];
      if (dp[j] === N) {
        continue;
      }
      if (a in used[j]) {
        if (j < K && dp[j + 1] > dp[j]) {
          dp[j + 1] = dp[j];
          used[j + 1] = used[j];
        }
        dp[j] += 1;
        used[j] = {};
      }
      used[j][a] = 1;
    }
  }
  print(min(dp));
}

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiamltbTg5LmpzIiwic291cmNlUm9vdCI6Ii4uIiwic291cmNlcyI6WyJjb2ZmZWVcXGppbW04OS5jb2ZmZWUiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLElBQUEsQ0FBQSxFQUFBLENBQUEsRUFBQSxDQUFBLEVBQUEsQ0FBQSxFQUFBLENBQUEsRUFBQSxLQUFBLEVBQUEsRUFBQ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ayA9IChudW0pIC0+XG5cdGtleSA9IG51bVxuXHRpZiBrZXkgaW4gY2FjaGUgdGhlbiByZXR1cm4gY2FjaGVba2V5XVxuXHRkdiA9IFtdXG5cdGZvciBwIGluIHByaW1lXG5cdFx0YyA9IDBcblx0XHR3aGlsZSBudW0gJSBwID09IDBcblx0XHRcdGMgKz0gMVxuXHRcdFx0bnVtID0gbnVtIC8vIHBcblx0XHRpZiBjICUgMiA9PSAxXG5cdFx0XHRkdi5wdXNoIHBcblx0XHRpZiBudW0gPCBwICogcFxuXHRcdFx0YnJlYWtcblxuXHRmb3IgeCBpbiBkdlxuXHRcdG51bSAqPSB4XG5cblx0Y2FjaGVba2V5XSA9IG51bVxuXHRudW1cblxuZm9yIF8gaW4gWzAuLi5ucigpXVxuXHR4eHggPSBucnMoKVxuXHROID0geHh4WzBdXG5cdEsgPSB4eHhbMV1cblx0QSA9IG5ycygpXG5cdGRwID0gKE4gZm9yIF8gaW4gcmFuZ2UgSysxKVxuXHRkcFswXSA9IDFcblx0dXNlZCA9ICh7fSBmb3IgXyBpbiBbMC4uLksrMV0pXG5cdGZvciBhIGluIEFcblx0XHRhID0gZ2V0X21hc2sgYVxuXHRcdGZvciBqIGluIHJhbmdlIEssLTEsLTFcblx0XHRcdGlmIGRwW2pdID09IE4gdGhlbiBjb250aW51ZSBcblx0XHRcdGlmIGEgb2YgdXNlZFtqXVxuXHRcdFx0XHRpZiBqIDwgSyBhbmQgZHBbaiArIDFdID4gZHBbal1cblx0XHRcdFx0XHRkcFtqICsgMV0gPSBkcFtqXVxuXHRcdFx0XHRcdHVzZWRbaiArIDFdID0gdXNlZFtqXVxuXHRcdFx0XHRkcFtqXSArPSAxXG5cdFx0XHRcdHVzZWRbal0gPSB7fVxuXHRcdFx0dXNlZFtqXVthXSA9IDFcblx0cHJpbnQgbWluIGRwXG4iXX0=
//# sourceURL=c:\github\2021\009-CodeForces#708\1497E2\coffee\jimm89.coffee


You have a multiset containing several integers. Initially, it contains $$$a_1$$$ elements equal to $$$1$$$, $$$a_2$$$ elements equal to $$$2$$$, ..., $$$a_n$$$ elements equal to $$$n$$$.You may apply two types of operations:  choose two integers $$$l$$$ and $$$r$$$ ($$$l \le r$$$), then remove one occurrence of $$$l$$$, one occurrence of $$$l + 1$$$, ..., one occurrence of $$$r$$$ from the multiset. This operation can be applied only if each number from $$$l$$$ to $$$r$$$ occurs at least once in the multiset;  choose two integers $$$i$$$ and $$$x$$$ ($$$x \ge 1$$$), then remove $$$x$$$ occurrences of $$$i$$$ from the multiset. This operation can be applied only if the multiset contains at least $$$x$$$ occurrences of $$$i$$$. What is the minimum number of operations required to delete all elements from the multiset?

The first line contains one integer $$$n$$$ ($$$1 \le n \le 5000$$$). The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$).

Print one integer — the minimum number of operations required to delete all elements from the multiset.

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    probE();
});

function readline() {
    return inputString[currentLine++];
}

function probA(){
    let t = parseInt(readline());
    while(t > 0){
        t--;

        let n = parseInt(readline());
        let s = readline().split('');
        let c = s[n-1];
        let res = '';
        for(let i = 0;  i< n; i++){
            res += c;
        }

        console.log(res);
    }
}

function probB(){
    let t = parseInt(readline());

    while(t > 0){
        t--;
        let [p,f] = readline().split(' ');
        let cap = parseInt(p) + parseInt(f);

        let [cs, cw] = readline().split(' ');
        let [s, w] = readline().split(' ');

        let ava = (parseInt(cs) * parseInt(s)) + (parseInt(cw) * parseInt(w));

        let tot = parseInt(cs) + parseInt(cw);
        if(cap > ava){
            console.log(tot);
            continue;
        }

        let x = Math.min(parseInt(s), parseInt(w));
        if(parseInt(s) === x){
            if(parseInt(cs) * x > cap){
                console.log(Math.floor(cap / parseInt(cs)));
            }else{
                let rem = cap - (x * parseInt(cs));
                console.log(parseInt(cs) + Math.floor(rem/parseInt(w)));
            }
        }else{
            if(parseInt(cw) * x > cap){
                console.log(Math.floor(cap / parseInt(cw)));
            }else{
                let rem = cap - (x * parseInt(cw));
                console.log(parseInt(cw) + Math.floor(rem/parseInt(s)));
            }
        }
    }
}

function probC(){
    let t = parseInt(readline());
    while(t > 0){
        t--;

        let s = readline().split('');
        let x = parseInt(readline());
        let isPossible = true;

        let w = new Array(s.length);
        for(let i = 0; i < s.length; i++){
            if(s[i] === '0'){
                if(i+x < s.length)
                    w[i+x] = '0';

                if(i-x >= 0)
                    w[i-x] = '0';
            }
        }

        if(!isPossible){
            console.log(-1);
            continue;
        }

        for(let i = 0; i < s.length; i++){
            if(w[i] !== '0')
                w[i] = '1';
        }

        for(let i = 0; i < s.length; i++){
            if(s[i] === '1'){
                if(i+x < s.length && i-x >= 0){
                    if(w[i+x] === '0' && w[i-x] === '0'){
                        isPossible = false;
                        break;
                    }
                }
                else if(i+x < s.length){
                    if(w[i+x] === '0'){
                        isPossible = false;
                        break;
                    }
                }else if(i-x >= 0){
                    if(w[i-x] === '0'){
                        isPossible = false;
                        break;
                    }
                }else{
                    isPossible = false;
                    break;
                }
            }
        }

        if(!isPossible){
            console.log(-1);
            continue;
        }

        console.log(w.join(''));
    }
}

function probD(){
    let t = parseInt(readline());
    while(t > 0){
        t--;
        let n = parseInt(readline());
        let a = readline().split(' ');
        for(let i = 0; i < a.length; i++)
            a[i] = parseInt(a[i]);

        let left = new Array(n+10);
        left.fill(0);
        let ans = 0;
        for(let i = 0; i < n; i++){
            let right = new Array(n+10);
            right.fill(0);
            for(let j = n-1; j > i; j--){
                ans += left[a[j]] * right[a[i]];
                right[a[j]] += 1;
            }
            left[a[i]] += 1;
        }

        console.log(ans);
    }
}

function probE(){
    let n = parseInt(readline());
    let a = readline().split(' ');
    for(let i = 0; i < a.length; i++)
        a[i] = parseInt(a[i]);

    let dp = new Array(n);
    for(let i = 0; i < n; i++)
        dp[i] = new Array(n);

    let solve = (l,r) => {
        if(l > r){
            dp[l][r] = 999999999999;
            return dp[l][r];
        }

        if(l === r){
            dp[l][r] = a[l] === 0 ? 0 : 1;
            return dp[l][r];
        }

        if(dp[l][r])
            return dp[l][r];

        let subs = [];
        let x = l;
        for(let i = l; i <= r; i++){
            if(a[i] === 0){
                subs.push([x,i-1]);
                x = i+1;
            }
        }
        if(subs.length > 0)
            subs.push([x,r]);

        if(subs.length > 0){
            let res = 0;
            for(let i = 0; i < subs.length; i++){
                if(subs[i][0] <= subs[i][1] && subs[i][0] >= 0 && subs[i][1] < a.length)
                    res += solve(subs[i][0], subs[i][1]);
            }

            dp[l][r] = Math.min(res, r-l+1);
            return dp[l][r];
        }else{
            let min = 999999999999;
            for(let i = l; i <= r; i++){
                min = Math.min(min, a[i]);
            }

            for(let i = l; i <= r; i++){
                a[i] -= min;
            }

            dp[l][r] = Math.min(min + solve(l,r), r-l+1);
            return dp[l][r];
        }
    }

    console.log(solve(0,a.length-1));
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    probE();
});

function readline() {
    return inputString[currentLine++];
}

function probA(){
    let t = parseInt(readline());
    while(t > 0){
        t--;

        let n = parseInt(readline());
        let s = readline().split('');
        let c = s[n-1];
        let res = '';
        for(let i = 0;  i< n; i++){
            res += c;
        }

        console.log(res);
    }
}

function probB(){
    let t = parseInt(readline());

    while(t > 0){
        t--;
        let [p,f] = readline().split(' ');
        let cap = parseInt(p) + parseInt(f);

        let [cs, cw] = readline().split(' ');
        let [s, w] = readline().split(' ');

        let ava = (parseInt(cs) * parseInt(s)) + (parseInt(cw) * parseInt(w));

        let tot = parseInt(cs) + parseInt(cw);
        if(cap > ava){
            console.log(tot);
            continue;
        }

        let x = Math.min(parseInt(s), parseInt(w));
        if(parseInt(s) === x){
            if(parseInt(cs) * x > cap){
                console.log(Math.floor(cap / parseInt(cs)));
            }else{
                let rem = cap - (x * parseInt(cs));
                console.log(parseInt(cs) + Math.floor(rem/parseInt(w)));
            }
        }else{
            if(parseInt(cw) * x > cap){
                console.log(Math.floor(cap / parseInt(cw)));
            }else{
                let rem = cap - (x * parseInt(cw));
                console.log(parseInt(cw) + Math.floor(rem/parseInt(s)));
            }
        }
    }
}

function probC(){
    let t = parseInt(readline());
    while(t > 0){
        t--;

        let s = readline().split('');
        let x = parseInt(readline());
        let isPossible = true;

        let w = new Array(s.length);
        for(let i = 0; i < s.length; i++){
            if(s[i] === '0'){
                if(i+x < s.length)
                    w[i+x] = '0';

                if(i-x >= 0)
                    w[i-x] = '0';
            }
        }

        if(!isPossible){
            console.log(-1);
            continue;
        }

        for(let i = 0; i < s.length; i++){
            if(w[i] !== '0')
                w[i] = '1';
        }

        for(let i = 0; i < s.length; i++){
            if(s[i] === '1'){
                if(i+x < s.length && i-x >= 0){
                    if(w[i+x] === '0' && w[i-x] === '0'){
                        isPossible = false;
                        break;
                    }
                }
                else if(i+x < s.length){
                    if(w[i+x] === '0'){
                        isPossible = false;
                        break;
                    }
                }else if(i-x >= 0){
                    if(w[i-x] === '0'){
                        isPossible = false;
                        break;
                    }
                }else{
                    isPossible = false;
                    break;
                }
            }
        }

        if(!isPossible){
            console.log(-1);
            continue;
        }

        console.log(w.join(''));
    }
}

function probD(){
    let t = parseInt(readline());
    while(t > 0){
        t--;
        let n = parseInt(readline());
        let a = readline().split(' ');
        for(let i = 0; i < a.length; i++)
            a[i] = parseInt(a[i]);

        let left = new Array(n+10);
        left.fill(0);
        let ans = 0;
        for(let i = 0; i < n; i++){
            let right = new Array(n+10);
            right.fill(0);
            for(let j = n-1; j > i; j--){
                ans += left[a[j]] * right[a[i]];
                right[a[j]] += 1;
            }
            left[a[i]] += 1;
        }

        console.log(ans);
    }
}

function probE(){
    let n = parseInt(readline());
    let a = readline().split(' ');
    for(let i = 0; i < a.length; i++)
        a[i] = parseInt(a[i]);

    let dp = new Array(n);
    for(let i = 0; i < n; i++)
        dp[i] = new Array(n);

    let solve = (l,r) => {
        if(l > r){
            dp[l][r] = 999999999999;
            return dp[l][r];
        }

        if(l === r){
            dp[l][r] = 1;
            return 1;
        }

        if(dp[l][r])
            return dp[l][r];

        let subs = [];
        let x = l;
        for(let i = l; i <= r; i++){
            if(a[i] === 0){
                subs.push([x,i-1]);
                x = i+1;
            }
        }
        if(subs.length > 0)
            subs.push([x,r]);

        if(subs.length > 0){
            let res = 0;
            for(let i = 0; i < subs.length; i++){
                if(subs[i][0] <= subs[i][1] && subs[i][0] >= 0 && subs[i][1] < a.length)
                    res += solve(subs[i][0], subs[i][1]);
            }

            dp[l][r] = Math.min(res, r-l+1);
            return dp[l][r];
        }else{
            let min = 999999999999;
            for(let i = l; i <= r; i++){
                min = Math.min(min, a[i]);
            }

            for(let i = l; i <= r; i++){
                a[i] -= min;
            }

            dp[l][r] = Math.min(min + solve(l,r), r-l+1);
            return dp[l][r];
        }
    }

    console.log(solve(0,a.length-1));
}


Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought $$$n$$$ integers, now it's time to distribute them between his friends rationally...Lee has $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ in his backpack and he has $$$k$$$ friends. Lee would like to distribute all integers in his backpack between his friends, such that the $$$i$$$-th friend will get exactly $$$w_i$$$ integers and each integer will be handed over to exactly one friend.Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$3t$$$ lines contain test cases — one per three lines. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le n$$$) — the number of integers Lee has and the number of Lee's friends. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the integers Lee has. The third line contains $$$k$$$ integers $$$w_1, w_2, \ldots, w_k$$$ ($$$1 \le w_i \le n$$$; $$$w_1 + w_2 + \ldots + w_k = n$$$) — the number of integers Lee wants to give to each friend.  It's guaranteed that the sum of $$$n$$$ over test cases is less than or equal to $$$2 \cdot 10^5$$$.

For each test case, print a single integer — the maximum sum of happiness Lee can achieve.

NoteIn the first test case, Lee should give the greatest integer to the first friend (his happiness will be $$$17 + 17$$$) and remaining integers to the second friend (his happiness will be $$$13 + 1$$$).In the second test case, Lee should give $$$\{10, 10, 11\}$$$ to the first friend and to the second friend, so the total happiness will be equal to $$$(11 + 10) + (11 + 10)$$$In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.

const { KeyObject } = require("crypto");

var data = '';
process.stdin.setEncoding('utf8');

process.stdin.on('data', (buffer) =>
{
    data += buffer;
});

process.stdin.on('end', () =>
{
    var i = 1;
    var x = data.split(/\s+/).map(Number);
    for (var cas = 0; cas < x[0]; cas++)
    {
        var n = x[i++];
        var k = x[i++];
        var a = [], w = [];
        for (var j = 0; j < n; j++)
            a.push(x[i++]);
        for (var j = 0; j < k; j++)
            w.push(x[i++]);
        a.sort((a, b) => a - b);
        w.sort((a, b) => a - b);

        var res = 0;
        var min = 0, max = n - 1, c = 0;
        for (c = 0; c < k && w[c] == 1; c++)
            res += a[max] + a[max], max--;
        for (var j = k - 1; j >= c; j--)
            res += a[max] + a[min], max--, min += w[j] - 1;
        console.log(res);
    }
});

const processData = (lines) => {
  let acc = 0
  const n = +lines[acc]
  acc++
  for (let i=0; i<n; i++) {
    const [n, k] = lines[acc].split(' ').map(x => +x)
    acc++
    const nums = lines[acc].split(' ').map(x => +x).sort((a, b) => b - a)
    acc++
    const friends = lines[acc].split(' ').map(x => +x).sort((a, b) => a - b)
    acc++

    let sum = 0

    for (let i =0; i<friends.length; i++) {
      sum += nums[i]
      if (friends[i] === 1) {
        sum += nums[i]
      }
    }
    let cur = nums.length - 1
    for (let i = friends.length-1; i >= 0; i--) {
      if (friends[i] === 1) {
        break
      }
      sum += nums[cur]
      cur -= (friends[i] - 1)
    }

    console.log(sum)

    //
    // const zeroPoint = l.indexOf('0')
    // const onePoint = l.indexOf('1')
    // const secondZeroPoint = l.indexOf('0', onePoint)
    //
    // if (zeroPoint === 0) {
    //   if (secondZeroPoint > 0) {
    //     console.log(l.slice(0, onePoint) + '0')
    //   } else {
    //     console.log(l)
    //   }
    // } else {
    //   if (zeroPoint > 0 ) {
    //     console.log('0')
    //   } else {
    //     console.log(l)
    //   }
    // }
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})

function solve() {
   var T = Number(read());
   function funcSort(a, b) {
      if (a < b) {
         return -1;
      }
      if (a > b) {
         return 1;
      }
      return 0;
   }
   for (var t = 0; t < T; t++) {
      var nk = read().split(' ').map(Number);
      var n = nk[0];
      var k = nk[1];
      var a = read().split(' ').map(BigInt);
      var w = read().split(' ').map(Number);
      a.sort(funcSort);
      w.sort(funcSort);
      var li = n - k - 1;
      var ri = n - 1;
      var ans = 0n;
      for (var i = 0; i < k; i++) {
         if (w[i] === 1) {
            ans += a[ri--] * 2n;
         } else {
            li -= w[i] - 1;
            ans += a[ri--] + a[li + 1];
         }
      }
      write('' + ans);
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         solve();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', solve);
   }
} else {
   solve();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    C();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function C(){
    let t = pi(readline());
    while(t > 0){
        t--;
        let [n,k] = ti(readline().split(' '));
        let a = ti(readline().split(' '));
        let w = ti(readline().split(' '));

        a.sort((x,y) => y-x);
        w.sort((x,y) => y-x);

        let res = 0;
        let p1 = 0;
        let p2 = n-1;
        let en = -1;
        for(let i = k-1; i >= 0; i--){
            if(w[i] === 1){
                res += 2*a[p1];
                p1++;
            }else{
                en = i;
                break;
            }
        }

        for(let i = 0; i <= en; i++){
            res += a[p2];
            p2 -= w[i]-1;
            res += a[p1];
            p1++;
        }

        console.log(res)
    }
}

function solve() {
   var T = Number(read());
   function funcSort(a, b) {
      if (a < b) {
         return -1;
      }
      if (a > b) {
         return 1;
      }
      return 0;
   }
   for (var t = 0; t < T; t++) {
      var nk = read().split(' ').map(Number);
      var n = nk[0];
      var k = nk[1];
      var a = read().split(' ').map(Number);
      var w = read().split(' ').map(Number);
      a.sort(funcSort);
      w.sort(funcSort);
      var li = n - k - 1;
      var ri = n - 1;
      var ans = 0;
      for (var i = 0; i < k; i++) {
         if (w[i] === 1) {
            ans += a[ri--] * 2;
         } else {
            li -= w[i] - 1;
            ans += a[ri--] + a[li + 1];
         }
      }
      write('' + ans);
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         solve();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', solve);
   }
} else {
   solve();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

function solve() {
   var T = Number(read());
   function funcSort(a, b) {
      if (a < b) {
         return -1;
      }
      if (a > b) {
         return 1;
      }
      return 0;
   }
   for (var t = 0; t < T; t++) {
      var nk = read().split(' ').map(Number);
      var n = nk[0];
      var k = nk[1];
      var a = read().split(' ').map(BigInt);
      var w = read().split(' ').map(BigInt);
      a.sort(funcSort);
      w.sort(funcSort);
      var li = 0;
      var ri = n - 1;
      var ans = 0n;
      for (var i = 0; i < k; i++) {
         if (w[i] === 1n) {
            ans += a[ri--] * 2n;
         } else {
            ans += a[ri--] + a[li++];
         }
      }
      write('' + ans);
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         solve();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', solve);
   }
} else {
   solve();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

function solve() {
   var T = Number(read());
   function funcSort(a, b) {
      if (a < b) {
         return -1;
      }
      if (a > b) {
         return 1;
      }
      return 0;
   }
   for (var t = 0; t < T; t++) {
      var nk = read().split(' ').map(Number);
      var n = nk[0];
      var k = nk[1];
      var a = read().split(' ').map(BigInt);
      var w = read().split(' ').map(BigInt);
      a.sort(funcSort);
      w.sort(funcSort);
      var li = 0;
      var ri = n - 1;
      var ans = 0n;
      for (var i = 0; i < k; i++) {
         if (w[i] === 1n) {
            ans += a[ri--] * 2n;
         } else {
            ans += a[ri--] + a[li++];
         }
      }
      var strAns = '' + ans;
      write(strAns.substring(0, strAns.length - 1));
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         solve();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', solve);
   }
} else {
   solve();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    C();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function C(){
    let t = pi(readline());
    while(t > 0){
        t--;
        let [n,k] = ti(readline().split(' '));
        let a = ti(readline().split(' '));
        let w = ti(readline().split(' '));

        a.sort((x,y) => y-x);
        w.sort((x,y) => x-y);
        let sum = 0;
        for(let i = 0; i < k; i++)
            sum += w[i];

        let res = 0;
        let p1 = 0;
        let p2 = sum-1;
        for(let i = 0; i < k; i++){
            if(w[i] === 1){
                res += 2*a[p1];
                p1++;
            }else{
                res += a[p1];
                res += a[p2];
                p2 -= w[i]-2;
            }
        }

        console.log(res)
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    C();
});

function readline() {
    return inputString[currentLine++];
}

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function C(){
    let t = pi(readline());
    while(t > 0){
        t--;
        let [n,k] = ti(readline().split(' '));
        let a = ti(readline().split(' '));
        let w = ti(readline().split(' '));

        a.sort((x,y) => y-x);
        w.sort((x,y) => x-y);
        let res = 0;
        let p1 = 0;
        let p2 = n-1;
        for(let i = 0; i < k; i++){
            if(w[i] === 1){
                res += 2*a[p1];
                p1++;
            }else{
                res += a[p1];
                res += a[p2];
                p2 -= w[i]-2;
            }
        }

        console.log(res)
    }
}