//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 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)));

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();


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 = [],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();

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();


(()=>{"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)

//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));
}

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`);
  }
}

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));
}


// 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiamltbTg5LmpzIiwic291cmNlUm9vdCI6Ii4uIiwic291cmNlcyI6WyJjb2ZmZWVcXGppb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C4uLm5yKCldXG5cdE5LID0gbnJzKClcblx0TiA9IE5LWzBdXG5cdEsgPSBOS1sxXVxuXHRBID0gbnJzKClcblx0ZHAgPSAoTiBmb3IgXyBpbiBbMC4uSysxXSlcblx0ZHBbMF0gPSAxXG5cdHVzZWQgPSAoe30gZm9yIF8gaW4gWzAuLi5LKzFdKVxuXHRmb3IgYSBpbiBBXG5cdFx0YSA9IGdldF9tYXNrIGFcblx0XHRmb3IgaiBpbiBbSy4uMF1cblx0XHRcdGlmIGRwW2pdID09IE4gdGhlbiBjb250aW51ZSBcblx0XHRcdGlmIGEgb2YgdXNlZFtqXVxuXHRcdFx0XHRpZiBqIDwgSyBhbmQgZHBbaiArIDFdID4gZHBbal1cblx0XHRcdFx0XHRkcFtqICsgMV0gPSBkcFtqXVxuXHRcdFx0XHRcdHVzZWRbaiArIDFdID0gdXNlZFtqXVxuXHRcdFx0XHRkcFtqXSArPSAxXG5cdFx0XHRcdHVzZWRbal0gPSB7fVxuXHRcdFx0dXNlZFtqXVthXSA9IDFcblx0cHJpbnQgbWluIGRwXG4iXX0=
//# 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]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DwgSyBhbmQgZHBbaiArIDFdID4gZHBbal1cblx0XHRcdFx0XHRkcFtqICsgMV0gPSBkcFtqXVxuXHRcdFx0XHRcdHVzZWRbaiArIDFdID0gdXNlZFtqXVxuXHRcdFx0XHRkcFtqXSArPSAxXG5cdFx0XHRcdHVzZWRbal0gPSB7fVxuXHRcdFx0dXNlZFtqXVthXSA9IDFcblx0cHJpbnQgbWluIGRwXG4iXX0=
//# sourceURL=c:\github\2021\009-CodeForces#708\1497E2\coffee\jimm89.coffee


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(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)
    }
}

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(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)
    }
}


'use strict';

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

let inputString = '';
let inputArray = [];
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const t = parseInt(readline());
  for (let i=0; i<t; i++) {
    const n = parseInt(readline());
    const a = readline().split("").map(x => parseInt(x));

    let good = 0;
    let sum = 0;
    let m = new Map();
    m.set(0,1);
    for (let j=0; j<n; j++) {
      sum += a[j];
      let x = sum - j - 1;
      m.set(x, (m.get(x)|0) + 1);
    }

    for (let [k,v] of m) {
      good += v * (v-1) / 2;
    }
    console.log(good);
  }
}

T = readline();
while (T--) {
  n = readline()
  a = readline()
  m = new Map()
  m.set(0, 1)
  ans = 0
  sum = 0
  for (i = 0; i < n; i++) {
    sum += +a[i]
    x = sum - i - 1
    if (!m.has(x))
      m.set(x, 0)
    v = m.get(x)
    m.set(x, v + 1)
    ans += v
  }
  print(ans)
}

var readline=require('readline');
var rl=readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
var arr=[];
rl.on('line',function(inp){
    arr.push(inp);
    var len=arr.length;
    if(len%2===1&&len>1){
        var n=parseInt(arr[arr.length-2]),
            a=arr[arr.length-1].split(''),
            sum=0,ans=0,obj={};
        for(var i=0;i<n;i++){
            a[i]=parseInt(a[i]);
            sum+=a[i];
            var t=sum-i-1;
            if(sum===i+1) ans++;
            if(!(t in obj)) obj[t]=1;
            else{
                ans+=obj[t];
                obj[t]++;
            }
        }
        console.log(ans);
    }
    if(len===parseInt(arr[0])*2+1) process.exit(0);
});

// process.stdin.resume();
// process.stdin.setEncoding('utf8');

function readDOM() {
    return stdinReadAll();
}

let CONFIG = {
    // getStringFunc: readDOM
};

stdioCPPattern(CONFIG, ({ readNum, readStr, writeLine }) => {
    // your code goes here
    let tc_count = readNum();

    const specVal = Array(1e5 + 9);
    const tracer = Array(1e6).fill(0);
    let n, counter, curPreSum, digitString, digit, sp;

    specVal[0] = 0;

    while (tc_count--) {

        n = readNum();

        const addendum = n;
        counter = 0;
        tracer[addendum] = 1;
        curPreSum = 0;

        digitString = readStr(n);

        for (let i = 1; i <= n; i++) {
            digit = digitString.codePointAt(i - 1) - 48;
            curPreSum += digit;

            sp = specVal[i] = curPreSum - i + addendum;
            counter += tracer[sp];
            tracer[sp]++;
        }
        writeLine(counter);
        // reset tracer
        tracer[addendum] = 0;
        for (let i = 1; i <= n; i++) {
            sp = specVal[i];
            tracer[sp] = 0;
        }
    }
});

/**
 * @author flynn
 */
function stdinReadAll() {
    const fs = require("fs");
    return fs.readFileSync(0).toString();
}

function stdioCPPattern({
    getStringFunc = stdinReadAll
}, callbackfn) {
    const aStr = getStringFunc() + "\n";

    let cur = 0;
    const resultList = [];// Array(numberOfLine);
    const seperator = /\s/;

    callbackfn({
        log: console.log,
        readNum() {
            let x = 0, ch = aStr.charCodeAt(cur) - 48;
            while (ch < 0 || 9 < ch) ch = aStr.charCodeAt(++cur) - 48;
            while (0 <= ch && ch <= 9) {
                x = x * 10 + ch;
                ch = aStr.charCodeAt(++cur) - 48;
            }
            return x;
        },
        writeLine(data) {
            resultList.push(`${data}`);
        },
        readStr(len = 1e6) {
            const chArr = Array(len);
            let ch = aStr[cur], i = 0;
            while (seperator.test(ch)) ch = aStr[++cur];
            while (!seperator.test(ch)) {
                chArr[i++] = ch;
                ch = aStr[++cur];
            }
            return chArr.join("");
        }
    });
    let prompt = resultList.join("\n");
    if (prompt) console.log(prompt);
}

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++];
}
function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine()
      .split("")
      .map((n) => parseInt(n - 1));
    const map = { 0: 1 };
    let [sum, count] = [0, 0];

    arr.forEach((num) => {
      sum += num;
      if (map[sum]) {
        count += map[sum];
        map[sum]++;
      } else {
        map[sum] = 1;
      }
    });
    console.log(count);
  }
}

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();
    });

    main();
});

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

function main() {
  const t = Number(readLine())
  for (let _i = 0; _i < t; _i++) {
	const n = Number(readLine())
    const arr = Array.from(readLine()).map(s => Number(s) - 1)
    const nums = {}
    nums[0] = 1
    let total = 0
    for (let i = 0; i < n; i++) {
      total += arr[i]
      if (!nums[total]) {
        nums[total] = 0
      }
      nums[total] += 1
    }
    console.log(Object.values(nums).reduce((acc, curr) => acc + curr * (curr - 1) / 2, 0))
  }
}

const solution = (string) => {
    const nums = string.split('').map(Number)
    let sum = 0
    const grid = nums.map((n, i) => {
        sum += n
        return sum - (i + 1)
    })
    grid.push(0)
    const freq = {}
    for (const num of grid) {
        freq[num] =
            1 +
            (freq[num] || 0)
    }

    let result = 0
    for (const count of Object.values(freq)) {
        result += count * (count - 1) / 2
    }

    return result
}

// Boilerplate Node.js for codeforces
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*/

    // every other line, without the first line
    for (let i = 2; i < lines.length; i += 2) {
        console.log(solution(lines[i]))
    }
})

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){
        t--;
        let n = pi(readline());
        let a = ti(readline().split(''));
        let s = res = 0;
        let map = {0:1};
        for(let i = 0; i < n; i++){
            s += a[i];
            let x = s-i-1;
            if(map[x] === undefined){
                map[x] = 0;
            }
            map[x] += 1;
            res += map[x]-1;
        }
        console.log(res);
    }
}

T = readline();
while (T--) {
  n = readline()
  a = readline()
  m = new Map()
  m.set(0, 1)
  ans = 0
  sum = 0
  for (i = 0; i < n; i++) {
    sum += +a[i]
    x = sum - i - 1
    if (!m.has(x))
      m.set(x, 0)
    m.set(x, m.get(x) + 1)
    ans += m.get(x) - 1
  }
  print(ans)
}

T = readline()
while (T--) {
  n = readline(); a = readline()
  m = new Map([[0, 1]])
  ans = 0; sum = 0
  for (i = 0; i < n; i++) {
    sum += +a[i]
    x = sum - i - 1
    if (!m.has(x))
      m.set(x, 0)
    v = m.get(x)
    m.set(x, v + 1)
    ans += v
  }
  print(ans)
}

T = readline()
while (T--) {
  n = readline(); a = readline()
  m = new Map([[0, 1]])
  ans = 0; sum = 0
  for (i = 0; i < n; i++) {
    sum += +a[i]
    const x = sum - i - 1
    if (!m.has(x))
      m.set(x, 0)
    const v = m.get(x)
    m.set(x, v + 1)
    ans += v
  }
  print(ans)
}

T = readline()
while (T--) {
  n = readline(); a = readline()
  m = new Map()
  m.set(0, 1)
  ans = 0; sum = 0
  for (i = 0; i < n; i++) {
    sum += +a[i]
    const x = sum - i - 1
    if (!m.has(x))
      m.set(x, 0)
    const v = m.get(x)
    m.set(x, v + 1)
    ans += v
  }
  print(ans)
}

'use strict';

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

let inputString = '';
let inputArray = [];
let currentLine = 0;

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

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

  main();
});

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

function main() {
  const t = parseInt(readline());
  for (let i=0; i<t; i++) {
    const n = parseInt(readline());
    const a = readline().split("").map(x => parseInt(x));

    let good = 0;
    let sum = 0;
    let m = new Map();
    m.set(0,1);
    for (let j=0; j<n; j++) {
      sum += a[j];
      let x = sum - j - 1;
      m.set(x, (m.get(x)|0) + 1);
    }

    for (let [k,v] of m) {
      console.log(k,v);
      good += v * (v-1) / 2;
    }
    console.log(good);
  }
}


print(function(m, n) {
	var max = (m + 1) * 2;
	var min = m - 1;
	if (n < min || n > max)
		return -1;

	var ans=[];
	if(n===min){
		for(var i=0;i<min;i++)
			ans.push('01');
		ans.push('0');
	}else if(n===m){
		for(var i=0;i<m;i++)
			ans.push('01');
	}else if(n===m+1){
		for(var i=0;i<m;i++)
			ans.push('10');
		ans.push('1');
	}else{
		var x=n%(m+1)||max;
		for(var i=0;i<m;i++){
			ans.push(i<x?'11':'1');
			ans.push('0');
		}
		ans.push(n===max?'11':'1');
	}
	return ans.join('');

}.apply(0, readline().split(' ').map(Number)));

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]);
	};
	return tokens;
}

function main() {
	var integers = tokenizeIntegers(readline());
	var zero = integers[0], one = integers[1];

	if (one < zero-1 || one > 2*zero + 2) {
		print(-1);
		return;
	}

	var solution = [];
	function push(s) {
		solution.push(s);
	}

	while (one > 2*zero) {
		push("1");
		one -= 1;
	}
	while (one > zero) {
		push("011");
		one -= 2;
		zero -= 1;
	}
	while (one > 0) {
		push("01");
		one -= 1;
		zero -= 1;
	}
	if (zero == 1) {
		push("0");
	}

	print(solution.join(""));
}

main();

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]);
	};
	return tokens;
}

function main() {
	var integers = tokenizeIntegers(readline());
	var zero = integers[0], one = integers[1];

	var solution = [];
	function push(s, k) {
		if (k === undefined) {
			solution.push(s);
			return;
		}
		for (var i = 0; i < k; ++i) {
			solution.push(s);
		}
	}

	if (2*zero == one) {
		push("011", zero);
	}

	else if (2*zero == one-1) {
		push("1");
		push("011", zero);
	}
	else if (2*zero == one-2) {
		push("11");
		push("011", zero);
	}

	else if (2*(zero-1) == one-1) {
		push("1");
		push("011", zero-1);
		push("0");
	}
	else if (2*(zero-1) == one-2) {
		push("11");
		push("011", zero-1);
		push("0");
	}

	else if (2*(zero-1) == one-3) {
		push("11");
		push("011", zero-1);
		push("01");
	}

	else if (2*(zero-1) == one) {
		push("011", zero-1);
		push("0");
	}

	else {
		print(-1);
		return;
	}

	print(solution.join(""));
}

main();