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 rowNum = integers[0], colNum = integers[1], delta = integers[2];

	var factors = [];
	row = tokenizeIntegers(readline());
	var remainder = row[0]%delta;

	for (var r = 0; r < rowNum; r += 1) {
		if (r != 0) {
			row = tokenizeIntegers(readline());
		}
		for (var c = 0; c < colNum; c += 1) {
			if (row[c]%delta != remainder) {
				print(-1);
				return;
			}
			factors.push(row[c]);
		}
	}

	factors.sort(function(a, b) {
		return a-b;
	});

	var best = factors.length * -factors[0];
	for (var i = 0; i < factors.length; i += 1) {
		best += factors[i];
	}

	var total = best, n = factors.length;
	for (var i = 1; i < n; i += 1) {
		var jump = factors[i] - factors[i-1];
		total += i * jump;
		total -= (n-i) * jump;
		best = Math.min(best, total);
	}

	print(best/delta);
}

main();

print(function(n, m, d) {
	var a = [];
	for (var i = 0; i < n; i++) {
		a.push.apply(a, readline().split(' ').map(Number));
	}
	n = n * m;

	a = a.sort(function(a, b) {
		return a - b;
	}).map(function(v, i, a) {
		return v - a[0];
	});

	return a.some(function(v) {
		return v % d > 0;
	}) ? -1 : a.map(function(v) {
		return v / d;
	}).reduce(function(q, p, i, a) {
		return q + Math.abs(p - a[~~(n / 2)]);
	}, 0);

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

nmd = readline().split(' ').map(Number); n = nmd[0]; m = nmd[1]; d = nmd[2];

function fn() {
    a = readline().split(' ').map(Number);
    x = a[0] % d;
    for (j = 1; j < m; j++) if (x !== a[j] % d) return -1;
    A = [].concat(a);
    for (i = 1; i < n; i++) {
        a = readline().split(' ').map(Number);
        for (j = 0; j < m; j++) if (x !== a[j] % d) return -1;
        A = A.concat(a);
    }
    A = A.map(i => (i - x) / d).sort((a,b) => a-b);
    i=0;j=A.length-1;a=1;b=1;r=0;
    while(i < j) {
        if(a < b) {
            r += a*(A[i+1] - A[i]);
            a++;i++;
        } else {
            r += b*(A[j] - A[j-1]);
            b++;j--;
        }
    }
    return r;
}
write(fn());

nmd = readline().split(' ').map(Number); n = nmd[0]; m = nmd[1]; d = nmd[2];

function fn() {
    a = readline().split(' ').map(Number);
    x = a[0] % d;
    for (j = 1; j < m; j++) if (x !== a[j] % d) return -1;
    A = [].concat(a);
    for (i = 1; i < n; i++) {
        a = readline().split(' ').map(Number);
        for (j = 0; j < m; j++) if (x !== a[j] % d) return -1;
        A = A.concat(a);
    }

    A = A.map(i => (i - x) / d).sort((a,b) => a-b);
    i=0;j=A.length-1;a=1;b=1;r=0;
    while(i < j) r += a < b ? a++*(A[i+1] - A[i++]) : b++*(A[j] - A[--j]);
    return r;
}
write(fn());

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
    A[i] = readline().split(" ");
    for (var j = 0; j < m && tik == 0; j++){
        if (parseInt(A[0][0]) % d != parseInt(A[i][j]) % d){
            write(-1);
            tik = 1;
        }
        B.push(Math.floor(parseInt(A[i][j]) / d));
    }
}
if (tik == 0){
    B = B.sort(function(a, b){return parseInt(a)-parseInt(b)});
    avgi = Math.floor(B.length / 2);
    avg = B[avgi];
    sum = 0;
    for (var i = 0; i < B.length; i++){
        sum += Math.abs(parseInt(B[i]) - parseInt(avg));
    }
    write(sum);
}

print(function(n, m, d) {
	var a = [];
	for (var i = 0; i < n; i++) {
		a.push.apply(a, readline().split(' ').map(Number));
	}

	a = a.sort(function(a, b) {
		return a - b;
	}).map(function(v, i, a) {
		return v - a[0];
	});

	return a.some(function(v) {
		return v % d > 0;
	}) ? -1 : a.map(function(v) {
		return v / d;
	}).reduce(function(q, p, i, a) {
		return q + Math.abs(p - a[~~n / 2]);
	}, 0);

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

print(function(n, m, d) {
	var a = [];
	for (var i = 0; i < n; i++) {
		a.push.apply(a, readline().split(' ').map(Number));
	}
	n = n * m;

	a = a.sort(function(a, b) {
		return a - b;
	}).map(function(v, i, a) {
		return v - a[0];
	});

	return a.some(function(v) {
		return v % d > 0;
	}) ? -1 : a.map(function(v) {
		return v / d;
	}).reduce(function(q, p, i, a) {
		return q + Math.abs(p - a[~~n / 2]);
	}, 0);

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

nmd = readline().split(' ').map(Number); n = nmd[0]; m = nmd[1]; d = nmd[2];

function fn() {
    a = readline().split(' ').map(Number);
    x = a[0] % d;
    for (j = 1; j < m; j++) if (x !== a[j] % d) return -1;
    A = [].concat(a);
    for (i = 1; i < n; i++) {
        a = readline().split(' ').map(Number);
        for (j = 0; j < m; j++) if (x !== a[j] % d) return -1;
        A = A.concat(a);
    }
    A = A.map(i => (i - x) / d).sort((a,b) => a-b);
    i=0;j=5;a=1;b=1;r=0;
    while(i < j) {
        if(a < b) {
            r += a*(A[i+1] - A[i]);
            a++;i++;
        } else {
            r += b*(A[j] - A[j-1]);
            b++;j--;
        }
    }
    return r;
}
write(fn());

nmd = readline().split(' ').map(Number); n = nmd[0]; m = nmd[1]; d = nmd[2];

function fn() {
    a = readline().split(' ').map(Number);
    A = [].concat(a);
    x = a[0] % d;
    for (j = 1; j < m; j++) if (x !== a[j] % d) return -1;
    for (i = n-1; i; --i) {
        a = readline().split(' ').map(Number);
        for (j = 0; j < m; j++) if (x !== a[j] % d) return -1;
        A = A.concat(a);
    }
    var mediana = A.reduce((res, i) => res + i, 0) / (n * m);
    return A.reduce((res, i) => res + Math.abs(i - mediana) / d, 0);
}
write(fn());

nmd = readline().split(' ').map(Number); n = nmd[0]; m = nmd[1]; d = nmd[2];

function fn() {
    a = readline().split(' ').map(Number);
    x = a[0] % d;
    for (j = 1; j < m; j++) if (x !== a[j] % d) return -1;
    A = [].concat(a);
    for (i = 1; i < n; i++) {
        a = readline().split(' ').map(Number);
        for (j = 0; j < m; j++) if (x !== a[j] % d) return -1;
        A = A.concat(a);
    }
    var mediana = A.reduce((res, i) => res + i, 0) / (n * m);
    return A.reduce((res, i) => res + Math.abs(i - mediana) / d, 0);
}
write(fn());

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
prev = 0;
sum = 0;
s1 = 0;
s2 = 0;
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (i == 0 && j == 0){
			prev = parseInt(A[i][j]) % d;
		} else {
			if (prev != parseInt(A[i][j]) % d){
				write(-1);
				tik = 1;
			}
		}
		B.push(parseInt(A[i][j]) / d);
		sum += parseInt(A[i][j]) / d;
	}
}
if (tik == 0){
	avg = Math.floor(sum/B.length);
	avg1 = avg + 1;
	for (var i = 0; i < B.length; i++){
		if (B[i] > avg){
			s1 += (B[i] - avg);
		} else {
			s1 += (avg - B[i]);
		}
		if (B[i] > avg1){
			s2 += (B[i] - avg1);
		} else {
			s2 += (avg1 - B[i]);
		}
	}
	write(Math.min(s1,s2));
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (parseInt(A[0][0]) % d != parseInt(A[i][j]) % d){
			write(-1);
			tik = 1;
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B.length / 2;
	avg = B[avg];
	write(avg);

}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (parseInt(A[0][0]) % d != parseInt(A[i][j]) % d){
			write(-1);
			tik = 1;
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B.length / 2;
	avg = B[avg];
	sum = 0;
	for (var i = 0; i < B.length; i++){
		sum = Math.abs(B[i] - avg);
	}
	write(sum);
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (parseInt(A[0][0]) % d != parseInt(A[i][j]) % d){
			write(-1);
			tik = 1;
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B.length / 2;
	avg = B[avg];
	sum = "";
	for (var i = 0; i < B.length; i++){
		sum += B[i] + " ";
	}
	write(sum);
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (parseInt(A[0][0]) % d != parseInt(A[i][j]) % d){
			write(-1);
			tik = 1;
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B.length / 2;
	write(avg);

}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (parseInt(A[0][0]) % d != parseInt(A[i][j]) % d){
			write(-1);
			tik = 1;
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B.length / 2;
	avg = B[avg];
	sum = "";
	for (var i = 0; i < B.length; i++){
		sum += Math.abs(B[i] - avg) + " ";
	}
	write(sum);
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
prev = 0;
sum = 0;
s1 = 0;
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (i == 0 && j == 0){
			prev = parseInt(A[i][j]) % d;
		} else {
			if (prev != parseInt(A[i][j]) % d){
				write(-1);
				tik = 1;
			}
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B[Math.floor(B.length / 2)];
	for (var i = 0; i < B.length; i++){
		if (B[i] > avg){
			s1 += (B[i] - avg);
		} else {
			s1 += (avg - B[i]);
		}
	}
	write(s1);
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (parseInt(A[0][0]) % d != parseInt(A[i][j]) % d){
			write(-1);
			tik = 1;
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B.length / 2;
	avg = B[avg];
	sum = 0;
	for (var i = 0; i < B.length; i++){
		sum += Math.abs(B[i] - avg);
	}
	write(sum);
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
prev = 0;
sum = 0;
s1 = 0;
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (i == 0 && j == 0){
			prev = parseInt(A[i][j]) % d;
		} else {
			if (prev != parseInt(A[i][j]) % d){
				write(-1);
				tik = 1;
			}
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B[B.length / 2];
	for (var i = 0; i < B.length; i++){
		if (B[i] > avg){
			s1 += (B[i] - avg);
		} else {
			s1 += (avg - B[i]);
		}
	}
	write(s1);
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
prev = 0;
sum = 0;
s1 = 0;
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (i == 0 && j == 0){
			prev = parseInt(A[i][j]) % d;
		} else {
			if (prev != parseInt(A[i][j]) % d){
				write(-1);
				tik = 1;
			}
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	B = B.sort();
	avg = B[parseInt(B.length / 2)];
	for (var i = 0; i < B.length; i++){
		if (B[i] > avg){
			s1 += (B[i] - avg);
		} else {
			s1 += (avg - B[i]);
		}
	}
	write(s1);
}

ip = readline().split(" ");
n = parseInt(ip[0]);
m = parseInt(ip[1]);
d = parseInt(ip[2]);
var A = new Array();
var B = new Array();
prev = 0;
sum = 0;
s1 = 0;
tik = 0;

for (var i = 0; i < n && tik == 0; i++){
	A[i] = readline().split(" ");
	for (var j = 0; j < m && tik == 0; j++){
		if (i == 0 && j == 0){
			prev = parseInt(A[i][j]) % d;
		} else {
			if (prev != parseInt(A[i][j]) % d){
				write(-1);
				tik = 1;
			}
		}
		B.push(parseInt(A[i][j]) / d);
	}
}
if (tik == 0){
	avg = B[parseInt(B.length / 2)];
	for (var i = 0; i < B.length; i++){
		if (B[i] > avg){
			s1 += (B[i] - avg);
		} else {
			s1 += (avg - B[i]);
		}
	}
	write(s1);
}


;(function () {

	print((function (n, str) {

		var m = {A: 0, F: 0, I: 0};
		for (var i = 0; i < n; i++) m[str[i]]++;
		return m.I > 1 ? 0 : (m.I === 1 ? 1 : m.A);

	})(+readline(), readline()));

}).call(this);


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() {
  const len1 = +readLine();
  const arr1 = readLine()
    .split(" ")
    .map((n) => parseInt(n));
  const len2 = +readLine();
  const arr2 = readLine()
    .split(" ")
    .map((n) => parseInt(n));

  let max = Number.MIN_SAFE_INTEGER;
  let count = 0;

  for (let i = 0; i < len2; i++) {
    const a = arr2[i];
    for (let j = 0; j < len1; j++) {
      const b = arr1[j];
      const div = a / b;
      if (Number.isInteger(div)) {
        if (div > max) {
          max = div;
          count = 0;
        }
        if (div === max) count++;
      }
    }
  }
  console.log(count);
}

print(function(n, a, m, b) {
	var i, j,
		max = 0,
		ans = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (b[j] % a[i] === 0) {
				if (b[j] / a[i] > max) {
					max = b[j] / a[i];
					ans = 1;
				} else if (b[j] / a[i] === max) {
					ans++;
				}
			}

	return ans;

}(+readline(), readline().split(' ').map(Number), +readline(), readline().split(' ').map(Number)));

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++];
}

var dp = {};

function fibonacci(n) {
	if (n === 0) return 0;
	if (n === 1) return 1;

	if (dp[n] !== undefined) return dp[n];
	let result = fibonacci(n - 1) + fibonacci(n - 2);
	return (dp[n] = result);
}

function main() {
	let n = readLine() >> 0;
	let a = readLine()
		.split(' ')
		.map(x => x >> 0);
	let m = readLine() >> 0;
	let b = readLine()
		.split(' ')
		.map(x => x >> 0);
	let best = 0;
	let store = {};

	for (let i = 0; i < n; i++) {
		for (let j = m - 1; j >= 0; j--) {
			if (Math.floor(b[j] / a[i]) < best) {
				break;
			}
			if (b[j] % a[i] === 0) {
				best = Math.max(best, b[j] / a[i]);
				store[best] = (store[best] || 0) + 1;
			}
		}
	}
	console.log(store[best]);
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', (inputStd) => {
    inputString += inputStd;
});
process.stdin.on('end', function () {
    inputString = inputString
        .trim()
        .split('\n')
        .map((str) => {
            return str.trim();
        });
    main();
});
function readLine() {
    return inputString[currentLine++];
}

function main() {
    let n = parseInt(readLine());
    let aS = readLine().split(' ').map(Number);
    let m = parseInt(readLine());
    let bS = readLine().split(' ').map(Number);
    let integers = [];
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (Number.isInteger(bS[i] / aS[j])) {
                integers.push(bS[i] / aS[j]);
            }
        }
    }
    integers.sort((a, b) => {
        return b - a;
    });
    let count = 1;
    for (let i = 1; i < integers.length; i++) {
        if (integers[0] === integers[i]) {
            count++;
        }
    }
    console.log(count);
}

;(function () {

	var n = +readline(),
		a = readline().split(' ').map(Number),
		m = +readline(),
		b = readline().split(' ').map(Number),
		c = (function () {
			var t = [], l, max, i, j;
			for (i = 0; i < n; i++)
				for (j = 0; j < m; j++) {
					l = b[j] / a[i];
					if (l === parseInt(l)) {
						t.push(l);
					}
				}
			max = Math.max.apply(null, t);
			return t.filter(function (e) { return e === max; }).length;
		})();

	print(c);

}).call(this);

print(function(n, a, m, b) {
	var i, j, d = new Int8Array(1e4);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (b[j] % a[i] === 0)
				d[b[j] / a[i]]++;

	d[1] = 0;
	return Math.max.apply(0, d);

}(+readline(), readline().split(' ').map(Number),
  +readline(), readline().split(' ').map(Number)));

print(function(n, a, m, b) {
	var i, j, d = new Int8Array(1e4);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (b[j] % a[i] === 0)
				d[b[j] / a[i]]++;

	return Math.max.apply(0, d);

}(+readline(), readline().split(' ').map(Number),
  +readline(), readline().split(' ').map(Number)));


function dormey(n,q,arr){
    let q1=0;
    let ans=[];
    for(let i=n-1;i>=0;i--){
        if(arr[i]<=q1){
           ans[i]=1;
        }
        else{
            if(q1<q){
                q1++;
                ans[i]=1;
            }
            else{
                ans[i]=0;
            }
        }
    }
    return ans.join("");
}
function runProgram(input){
    input = input.trim().split("\n");
    var tc =+input[0];
    var line=1;
    for(var i=0;i<tc;i++){
        var x = input[line++].trim().split(" ").map(Number);
        var n=x[0];
        var q =x[1];
        var arr = input[line++].trim().split(" ").map(Number);
        var res = dormey(n,q,arr);
        console.log(res);
    }

}
if (process.env.USER === "") {
  runProgram(``);
} else {
  process.stdin.resume();
  process.stdin.setEncoding("ascii");
  let read = "";
  process.stdin.on("data", function (input) {
    read += input;
  });
  process.stdin.on("end", function () {
    read = read.replace(/\n$/, "");
    read = read.replace(/\n$/, "");
    runProgram(read);
  });
  process.on("SIGINT", function () {
    read = read.replace(/\n$/, "");
    runProgram(read);
    process.exit(0);
  });
}

const main = () => {

	var t = readInt();
	var allans = [];
	for (var zz = 0; zz < t; zz++) {
		var arr = readIntArr();
		var n, q;
		n = arr[0]; q = arr[1];
		a = readIntArr();

		var ans = [];
		// go from back since ideal to keep v large for as long as possible?
		if (q >= n) {
			for (var i = 0; i < n; i++) ans.push('1');
		} else {
			for (var i = 0; i < n; i++) ans.push('0');
			var v = 0;
			for (var i = n - 1; i > -1; i--) {
				if (a[i] > v) {
					if (v < q) {
						v += 1;
						ans[i] = '1';
					}
				} else {
					ans[i] = '1';
				}
			}
		}
		allans.push(ans.join(''));
	}
	multiLineArrayPrint(allans);

	return;
}

const readInt = () => parseInt(readline());
const readString = () => readline();
const readIntArr = () => readline().split(" ").map(zzzz => parseInt(zzzz));
const parseLong = (string) => { // javascript's largest long is < (1<<62)
	var n = string.length;
	var res = 0;
	for (var i = 0; i < n; i++) {
		res *= 10;
		res += parseInt(string[i]);
	}
	return res;
}
const readLong = () => parseLong(readLong());
const readLongArr = () => readline().split(" ").map(zzzz => parseLong(zzzz));
const oneLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join(' '));
const multiLineArrayPrint = (arr) => print(arr.map(zzzz => zzzz.toString()).join('\n'));
const multiLineArrayOfArraysPrint = (arr) => print(arr.map(yyyy => yyyy.map(zzzz => zzzz.toString()).join(' ')).join('\n'));
const makeArr = (defaultVal, dimensionsArr) => {
	var n = dimensionsArr.length;
	if (n === 1) return Array(dimensionsArr[0]).fill(defaultVal);
	else {
		var temp = [];
		for (var i = 0; i < dimensionsArr[0]; i++)
			temp.push(makeArr(defaultVal, dimensionsArr.slice(1)));
		return temp;
	}
}

for (_ = 0; _ < 1; _++) {
	main();
}

// newline for testing
 //newline for testing
// codeforces js cannot use "let" keyword. use "var" instead
// codeforces js cannot destructure arrays

var tests = parseInt(readline());
    for (var t=0; t < tests; t++) {
        var nq = readline().split(' ').map(x=>parseInt(x));
        var arr = readline().split(' ').map(x=>parseInt(x));
        var iq = 0;
        var ans = Array(nq[0]).fill(0);
        for (var i = nq[0]-1; i >= 0; i--) {
            if (arr[i] <= iq) {
                ans[i] = 1;
            } else {
                if (iq < nq[1]) {
                    iq++;
                    ans[i] = 1;
                }
            }
        }
        print(ans.join(''));
    }

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*/
    const splitLines = lines.map(x => x.split(" ").map(x => parseInt(x)))
    for (let i = 0; i < splitLines[0][0]; i ++)
    {
        const currentArray = splitLines.slice(2 * i + 1, 2 * i + 3);
        const n = currentArray[0][0];
        const q = currentArray[0][1];
        let q0 = 0;
        let str = "";
        for (let i = currentArray[1].length - 1; i >= 0; i --)
        {

            if (q0 >= currentArray[1][i])
            {
                str = 1 + str;
            }
            else if (q0 < q)
            {
                q0 ++;
                str = 1 + str;
            }
            else
            {
                str = 0 + str;
            }
        }

        console.log(str)
    }
})

const fs = require("fs");

const input = fs.readFileSync(0, "utf8").trim().split("\n");

const testCases = [];
let T;

function parseInput() {
  T = parseInt(input[0]);
  let index = 0;

  for (let i = 0; i < T; i++) {
    index++;
    const [n, q] = input[index]
      .trim()
      .split(" ")
      .map((a) => parseInt(a));
    index++;
    const a = input[index]
      .trim()
      .split(" ")
      .map((a) => parseInt(a));
    const testCase = {
      n,
      q,
      a,
    };

    testCases.push(testCase);
  }
}

function solution(testCase, index) {
  const { n, q, a } = testCase;
  let result = "";
  let iq = 0;
  for (let i = n - 1; i >= 0; i--) {
    if (a[i] <= iq) result = "1" + result;
    else if (a[i] > iq && iq < q) {
      iq++;
      result = "1" + result;
    } else result = "0" + result;
  }

  console.log(result);
}

parseInput();

testCases.forEach(solution);

'use strict';

function solve(n, m, a) {
  if (m >= n) return '1'.repeat(n);
  let l = 0,
      r = n - 1;
  const check = s => {
    let t = m;
    for (let i = s; i < n; i++) {
      if (a[i] > t) t--;
      if (t < 0) return false;
    }
    return true;
  };
  while (l < r) {
    const mid = l + r >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  let res = new Array(n).fill(0);
  for (let i = l; i < n; i++) res[i] = 1;
  for (let i = 0; i < l; i++) if (a[i] <= m) res[i] = 1;
  return res.join('');
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, m, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

let input = ''
process.stdin.on('data', chunk => input += chunk)
process.stdin.on('end', () => {
    let array = input.trim().split('\n').slice(1).map(item => item.replace('\r', ''))
    .map(item => item.split(' '));
    array.forEach((item, index, arr) =>{
        if(index % 2 === 0 || index === 0){
            let result = new Array(arr[index][0]*1).fill(0);
            let iq = arr[index][1];
            let test = array[index+1];
            let step = 0;
            for (let i = arr[index][0]-1; i >= 0; i--) {
            if ( test[i] <= step) {
                result[i] = 1;
            } else {
                if (step < iq) {
                    step++;
                    result[i] = 1;
                }
            }
            }
        console.log(result.join(''));
        }
    })
});

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, m, a) {
  const bst = new BinarySearchTree();
  let max = Math.min(n, m),
      t = -1;
  for (let [i, num] of a.entries()) {
    if (num <= m) {
      const cnt = bst.countGreaterThanEq(num + 1);
      let ans = i + 1 - cnt;
      if (cnt <= m - num) {
        ans += cnt;
        ans += Math.min(m - cnt, n - i - 1);
      } else {
        ans += m - num;
        ans += Math.min(num, n - i - 1);
      }
      if (ans > max) {
        max = ans;
        t = i;
      }
      max = Math.max(max, ans);
    }
    bst.insert(num);
  }
  if (t === -1) {
    return '1'.repeat(max).padEnd(n, '0');
  }
  let res = new Array(n).fill(0),
      l = m - a[t],
      r = a[t];
  for (let i = 0; i < n; i++) {
    if (i <= t && a[i] <= a[t]) res[i] = 1;else if (i < t) {
      if (l) {
        res[i] = 1;
        l--;
      }
    } else {
      if (r) {
        res[i] = 1;
        r--;
      }
    }
    if (i === t) r += l;
  }
  return res.join('');
}
class BinarySearchTree {
  constructor(_compare = (a, b) => a - b) {
    _defineProperty(this, "root", null);
    _defineProperty(this, "length", 0);
    _defineProperty(this, "min", null);
    _defineProperty(this, "max", null);
    _defineProperty(this, "minCache", true);
    _defineProperty(this, "maxCache", true);
    this._compare = _compare;
    this.compare = this.compare.bind(this);
  }
  isT(t) {
    return t !== undefined && t !== null;
  }
  compare(a, b) {
    const {
      isT
    } = this;
    if (isT(a) && isT(b)) return this._compare(a, b);
    if (isT(a)) return 1;
    if (isT(b)) return -1;
    return 0;
  }
  isEmpty() {
    return !this.root;
  }
  size() {
    return this.root ? this.root.size : 0;
  }
  getRoot() {
    return this.root;
  }
  getMin() {
    if (this.minCache) {
      return this.min;
    }
    const min = this.searchKth(this.size());
    this.min = min;
    this.minCache = true;
    return min;
  }
  getMax() {
    if (this.maxCache) {
      return this.max;
    }
    const max = this.searchKth(1);
    this.max = max;
    this.maxCache = true;
    return max;
  }
  balance(node) {
    node.height = this.getHeight(node);
    const blance = this.getBalance(node);
    let res;
    if (Math.abs(blance) === 2) {
      if (blance > 0) {
        var _node$left$left$heigh, _node$left, _node$left$left, _node$left$right$heig, _node$left2, _node$left2$right;
        const heightDif = ((_node$left$left$heigh = (_node$left = node.left) === null || _node$left === void 0 ? void 0 : (_node$left$left = _node$left.left) === null || _node$left$left === void 0 ? void 0 : _node$left$left.height) !== null && _node$left$left$heigh !== void 0 ? _node$left$left$heigh : 0) - ((_node$left$right$heig = (_node$left2 = node.left) === null || _node$left2 === void 0 ? void 0 : (_node$left2$right = _node$left2.right) === null || _node$left2$right === void 0 ? void 0 : _node$left2$right.height) !== null && _node$left$right$heig !== void 0 ? _node$left$right$heig : 0);
        if (heightDif > 0) {
          res = this.rotateRight(node);
        } else if (heightDif < 0) {
          res = this.rotateLeftRight(node);
        }
      } else {
        var _node$right$left$heig, _node$right, _node$right$left, _node$right$right$hei, _node$right2, _node$right2$right;
        const heightDif = ((_node$right$left$heig = (_node$right = node.right) === null || _node$right === void 0 ? void 0 : (_node$right$left = _node$right.left) === null || _node$right$left === void 0 ? void 0 : _node$right$left.height) !== null && _node$right$left$heig !== void 0 ? _node$right$left$heig : 0) - ((_node$right$right$hei = (_node$right2 = node.right) === null || _node$right2 === void 0 ? void 0 : (_node$right2$right = _node$right2.right) === null || _node$right2$right === void 0 ? void 0 : _node$right2$right.height) !== null && _node$right$right$hei !== void 0 ? _node$right$right$hei : 0);
        if (heightDif > 0) {
          res = this.rotateRightLeft(node);
        } else if (heightDif < 0) {
          res = this.rotateLeft(node);
        }
      }
    }
    return res ? res : node;
  }
  rotateRight(node) {
    const left = node.left;
    const leftRight = left.right;
    left.right = node;
    node.left = leftRight;
    node.height = this.getHeight(node);
    left.height = this.getHeight(left);
    node.size = this.getSize(node);
    left.size = this.getSize(left);
    return left;
  }
  rotateLeft(node) {
    const right = node.right;
    const rightLeft = right.left;
    right.left = node;
    node.right = rightLeft;
    node.height = this.getHeight(node);
    right.height = this.getHeight(right);
    node.size = this.getSize(node);
    right.size = this.getSize(right);
    return right;
  }
  rotateLeftRight(node) {
    node.left = this.rotateLeft(node.left);
    return this.rotateRight(node);
  }
  rotateRightLeft(node) {
    node.right = this.rotateRight(node.right);
    return this.rotateLeft(node);
  }
  getBalance(node) {
    return this.getHeight(node.left) - this.getHeight(node.right);
  }
  getHeight(node) {
    var _node$left$height, _node$left3, _node$right$height, _node$right3;
    if (!node) return 0;
    return Math.max((_node$left$height = (_node$left3 = node.left) === null || _node$left3 === void 0 ? void 0 : _node$left3.height) !== null && _node$left$height !== void 0 ? _node$left$height : 0, (_node$right$height = (_node$right3 = node.right) === null || _node$right3 === void 0 ? void 0 : _node$right3.height) !== null && _node$right$height !== void 0 ? _node$right$height : 0) + 1;
  }
  getSize(node) {
    var _node$left$size, _node$left4, _node$right$size, _node$right4;
    if (!node) return 0;
    return ((_node$left$size = (_node$left4 = node.left) === null || _node$left4 === void 0 ? void 0 : _node$left4.size) !== null && _node$left$size !== void 0 ? _node$left$size : 0) + ((_node$right$size = (_node$right4 = node.right) === null || _node$right4 === void 0 ? void 0 : _node$right4.size) !== null && _node$right$size !== void 0 ? _node$right$size : 0) + node.count;
  }
  createNode(val) {
    return {
      id: Math.random() * new Date().valueOf(),
      val,
      left: null,
      right: null,
      size: 1,
      height: 1,
      count: 1
    };
  }
  insert(val) {
    let cur = this.createNode(val);
    if (this.isEmpty()) {
      this.root = cur;
      this.length++;
    } else {
      [, cur] = this.insertNode(this.root, cur);
    }
    if (this.min === null || this.compare(this.min, val) > 0) {
      this.min = val;
    }
    if (this.max === null || this.compare(this.max, val) < 0) {
      this.max = val;
    }
  }
  insertNode(node, cur, parent = null) {
    node.size++;
    const compareResult = this.compare(cur.val, node.val);
    let res;
    if (compareResult === 0) {
      node.count++;
      return [false, node];
    } else if (compareResult > 0) {
      if (node.right) {
        res = this.insertNode(node.right, cur, node);
        if (!res[0]) return res;
      } else {
        node.right = cur;
        this.length++;
        res = [true, cur];
      }
    } else {
      if (node.left) {
        res = this.insertNode(node.left, cur, node);
        if (!res[0]) return res;
      } else {
        node.left = cur;
        this.length++;
        res = [true, cur];
      }
    }
    let preHeight = node.height;
    const newNode = this.balance(node);
    if (newNode === node && node.height === preHeight) {
      res = [false, res[1]];
    } else if (newNode !== node) {
      if (parent) {
        parent.left === node ? parent.left = newNode : parent.right = newNode;
      } else {
        this.root = newNode;
      }
      res = [false, res[1]];
    }
    return res;
  }
  delete(val) {
    if (!this.root) return;
    this.deleteNode(val, this.root, null);
  }
  deleteNode(val, node, parent) {
    if (!node) return null;
    let res = this.compare(val, node.val);
    if (res === 0) {
      node.count--;
      node.size--;
      if (node.count > 0) return node;
      if (!node.left || !node.right) {
        if (this.min === val) {
          this.minCache = false;
        }
        if (this.max === val) {
          this.maxCache = false;
        }
        this.length--;
        if (!parent) {
          var _node$left5;
          this.root = (_node$left5 = node.left) !== null && _node$left5 !== void 0 ? _node$left5 : node.right;
          return this.root;
        } else {
          var _node$left6;
          return (_node$left6 = node.left) !== null && _node$left6 !== void 0 ? _node$left6 : node.right;
        }
      } else {
        const selectLeft = node.left.height > node.right.height;
        let replaceNode = selectLeft ? this.pre(node) : this.next(node),
            name = selectLeft ? 'left' : 'right';
        node.val = replaceNode.val;
        node.count = replaceNode.count;
        replaceNode.count = 0;
        node[name] = this.deleteNode(replaceNode.val, node[name], node);
      }
    } else if (res > 0) {
      node.right = this.deleteNode(val, node.right, node);
    } else {
      node.left = this.deleteNode(val, node.left, node);
    }
    node.size = this.getSize(node);
    node.height;
    const newNode = this.balance(node);
    if (parent) {
      parent.left === node ? parent.left = newNode : parent.right = newNode;
    } else {
      this.root = newNode;
    }
    return newNode;
  }
  next(node) {
    let next = node.right;
    while ((_next = next) !== null && _next !== void 0 && _next.left) {
      var _next;
      next = next.left;
    }
    return next;
  }
  pre(node) {
    let pre = node.left;
    while ((_pre = pre) !== null && _pre !== void 0 && _pre.right) {
      var _pre;
      pre = pre.right;
    }
    return pre;
  }
  search(val, compare) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchCeilingNode(this.root, val, compare !== null && compare !== void 0 ? compare : this.compare);
    return node.val;
  }
  searchCeilingNode(node, val, compare, parent = null) {
    const res = compare(val, node.val);
    if (res === 0) {
      return [parent, node];
    } else if (res > 0) {
      if (node.right) {
        return this.searchCeilingNode(node.right, val, compare, node);
      } else {
        return [parent, node];
      }
    } else {
      if (node.left) {
        const [p, value] = this.searchCeilingNode(node.left, val, compare, node);
        if (compare(value.val, val) < 0) {
          return [parent, node];
        } else {
          return [p, value];
        }
      } else {
        return [parent, node];
      }
    }
  }
  ceiling(val) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchCeilingNode(this.root, val, this.compare);
    return this.compare(node.val, val) >= 0 ? node.val : null;
  }
  searchFloorNode(node, val, compare, parent = null) {
    const res = compare(val, node.val);
    if (res === 0) {
      return [parent, node];
    } else if (res > 0) {
      if (node.right) {
        const [p, value] = this.searchFloorNode(node.right, val, compare, node);
        if (compare(value.val, val) > 0) {
          return [parent, node];
        } else {
          return [p, value];
        }
      } else {
        return [parent, node];
      }
    } else {
      if (node.left) {
        return this.searchFloorNode(node.left, val, compare, node);
      } else {
        return [parent, node];
      }
    }
  }
  floor(val) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchFloorNode(this.root, val, this.compare);
    return this.compare(node.val, val) <= 0 ? node.val : null;
  }
  searchKth(k) {
    if (this.isEmpty()) {
      return null;
    }
    if (k <= 0 || k > this.size()) {
      return null;
    }
    const node = this.searchNodeKth(this.root, k);
    return node.val;
  }
  searchNodeKth(node, k) {
    var _node$right$size2, _node$right5, _node$right$size3, _node$right6;
    const rSize = (_node$right$size2 = (_node$right5 = node.right) === null || _node$right5 === void 0 ? void 0 : _node$right5.size) !== null && _node$right$size2 !== void 0 ? _node$right$size2 : 0;
    if (rSize === k - 1 || rSize < k && rSize + node.count >= k) return node;
    if (node.right && rSize > k - 1) return this.searchNodeKth(node.right, k);else return this.searchNodeKth(node.left, k - ((_node$right$size3 = (_node$right6 = node.right) === null || _node$right6 === void 0 ? void 0 : _node$right6.size) !== null && _node$right$size3 !== void 0 ? _node$right$size3 : 0) - node.count);
  }
  countGreaterThanEq(val) {
    if (!this.root) return 0;
    return this.countCompare(val, (a, b) => this._compare(a, b), this.root);
  }
  countCompare(val, compare, node, pre = 0) {
    const res = compare(val, node.val);
    if (res === 0) {
      var _node$right$size4, _node$right7;
      return pre + ((_node$right$size4 = (_node$right7 = node.right) === null || _node$right7 === void 0 ? void 0 : _node$right7.size) !== null && _node$right$size4 !== void 0 ? _node$right$size4 : 0) + node.count;
    } else if (res > 0) {
      if (node.right) {
        return this.countCompare(val, compare, node.right, pre);
      } else {
        return pre;
      }
    } else {
      var _node$right$size5, _node$right8;
      let count = pre + ((_node$right$size5 = (_node$right8 = node.right) === null || _node$right8 === void 0 ? void 0 : _node$right8.size) !== null && _node$right$size5 !== void 0 ? _node$right$size5 : 0) + node.count;
      if (node.left) {
        return this.countCompare(val, compare, node.left, count);
      } else {
        return count;
      }
    }
  }
  toArray() {
    if (!this.root) return [];
    const res = [];
    const dfs = node => {
      if (node.left) dfs(node.left);
      res.push(node.val);
      if (node.right) dfs(node.right);
    };
    dfs(this.root);
    return res;
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, m, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, m, a) {
  const bst = new BinarySearchTree();
  let max = Math.min(n, m),
      t = -1;
  for (let [i, num] of a.entries()) {
    if (num <= m) {
      const cnt = bst.countGreaterThanEq(num + 1);
      let ans = i + 1 - cnt;
      if (cnt <= m - num) {
        ans += cnt;
        ans += Math.min(m - cnt, n - i - 1);
      } else {
        ans += m - num;
        ans += Math.min(num, n - i - 1);
      }
      if (ans > max) {
        max = ans;
        t = i;
      }
      max = Math.max(max, ans);
    }
    bst.insert(num);
  }
  if (t === -1) {
    return '1'.repeat(max).padEnd(n - max, '0');
  }
  let res = new Array(n).fill(0),
      l = m - a[t],
      r = a[t];
  for (let i = 0; i < n; i++) {
    if (i <= t && a[i] <= a[t]) res[i] = 1;else if (i < t) {
      if (l) {
        res[i] = 1;
        l--;
      }
    } else {
      if (r) {
        res[i] = 1;
        r--;
      }
    }
    if (i === t) r += l;
  }
  return res.join('');
}
class BinarySearchTree {
  constructor(_compare = (a, b) => a - b) {
    _defineProperty(this, "root", null);
    _defineProperty(this, "length", 0);
    _defineProperty(this, "min", null);
    _defineProperty(this, "max", null);
    _defineProperty(this, "minCache", true);
    _defineProperty(this, "maxCache", true);
    this._compare = _compare;
    this.compare = this.compare.bind(this);
  }
  isT(t) {
    return t !== undefined && t !== null;
  }
  compare(a, b) {
    const {
      isT
    } = this;
    if (isT(a) && isT(b)) return this._compare(a, b);
    if (isT(a)) return 1;
    if (isT(b)) return -1;
    return 0;
  }
  isEmpty() {
    return !this.root;
  }
  size() {
    return this.root ? this.root.size : 0;
  }
  getRoot() {
    return this.root;
  }
  getMin() {
    if (this.minCache) {
      return this.min;
    }
    const min = this.searchKth(this.size());
    this.min = min;
    this.minCache = true;
    return min;
  }
  getMax() {
    if (this.maxCache) {
      return this.max;
    }
    const max = this.searchKth(1);
    this.max = max;
    this.maxCache = true;
    return max;
  }
  balance(node) {
    node.height = this.getHeight(node);
    const blance = this.getBalance(node);
    let res;
    if (Math.abs(blance) === 2) {
      if (blance > 0) {
        var _node$left$left$heigh, _node$left, _node$left$left, _node$left$right$heig, _node$left2, _node$left2$right;
        const heightDif = ((_node$left$left$heigh = (_node$left = node.left) === null || _node$left === void 0 ? void 0 : (_node$left$left = _node$left.left) === null || _node$left$left === void 0 ? void 0 : _node$left$left.height) !== null && _node$left$left$heigh !== void 0 ? _node$left$left$heigh : 0) - ((_node$left$right$heig = (_node$left2 = node.left) === null || _node$left2 === void 0 ? void 0 : (_node$left2$right = _node$left2.right) === null || _node$left2$right === void 0 ? void 0 : _node$left2$right.height) !== null && _node$left$right$heig !== void 0 ? _node$left$right$heig : 0);
        if (heightDif > 0) {
          res = this.rotateRight(node);
        } else if (heightDif < 0) {
          res = this.rotateLeftRight(node);
        }
      } else {
        var _node$right$left$heig, _node$right, _node$right$left, _node$right$right$hei, _node$right2, _node$right2$right;
        const heightDif = ((_node$right$left$heig = (_node$right = node.right) === null || _node$right === void 0 ? void 0 : (_node$right$left = _node$right.left) === null || _node$right$left === void 0 ? void 0 : _node$right$left.height) !== null && _node$right$left$heig !== void 0 ? _node$right$left$heig : 0) - ((_node$right$right$hei = (_node$right2 = node.right) === null || _node$right2 === void 0 ? void 0 : (_node$right2$right = _node$right2.right) === null || _node$right2$right === void 0 ? void 0 : _node$right2$right.height) !== null && _node$right$right$hei !== void 0 ? _node$right$right$hei : 0);
        if (heightDif > 0) {
          res = this.rotateRightLeft(node);
        } else if (heightDif < 0) {
          res = this.rotateLeft(node);
        }
      }
    }
    return res ? res : node;
  }
  rotateRight(node) {
    const left = node.left;
    const leftRight = left.right;
    left.right = node;
    node.left = leftRight;
    node.height = this.getHeight(node);
    left.height = this.getHeight(left);
    node.size = this.getSize(node);
    left.size = this.getSize(left);
    return left;
  }
  rotateLeft(node) {
    const right = node.right;
    const rightLeft = right.left;
    right.left = node;
    node.right = rightLeft;
    node.height = this.getHeight(node);
    right.height = this.getHeight(right);
    node.size = this.getSize(node);
    right.size = this.getSize(right);
    return right;
  }
  rotateLeftRight(node) {
    node.left = this.rotateLeft(node.left);
    return this.rotateRight(node);
  }
  rotateRightLeft(node) {
    node.right = this.rotateRight(node.right);
    return this.rotateLeft(node);
  }
  getBalance(node) {
    return this.getHeight(node.left) - this.getHeight(node.right);
  }
  getHeight(node) {
    var _node$left$height, _node$left3, _node$right$height, _node$right3;
    if (!node) return 0;
    return Math.max((_node$left$height = (_node$left3 = node.left) === null || _node$left3 === void 0 ? void 0 : _node$left3.height) !== null && _node$left$height !== void 0 ? _node$left$height : 0, (_node$right$height = (_node$right3 = node.right) === null || _node$right3 === void 0 ? void 0 : _node$right3.height) !== null && _node$right$height !== void 0 ? _node$right$height : 0) + 1;
  }
  getSize(node) {
    var _node$left$size, _node$left4, _node$right$size, _node$right4;
    if (!node) return 0;
    return ((_node$left$size = (_node$left4 = node.left) === null || _node$left4 === void 0 ? void 0 : _node$left4.size) !== null && _node$left$size !== void 0 ? _node$left$size : 0) + ((_node$right$size = (_node$right4 = node.right) === null || _node$right4 === void 0 ? void 0 : _node$right4.size) !== null && _node$right$size !== void 0 ? _node$right$size : 0) + node.count;
  }
  createNode(val) {
    return {
      id: Math.random() * new Date().valueOf(),
      val,
      left: null,
      right: null,
      size: 1,
      height: 1,
      count: 1
    };
  }
  insert(val) {
    let cur = this.createNode(val);
    if (this.isEmpty()) {
      this.root = cur;
      this.length++;
    } else {
      [, cur] = this.insertNode(this.root, cur);
    }
    if (this.min === null || this.compare(this.min, val) > 0) {
      this.min = val;
    }
    if (this.max === null || this.compare(this.max, val) < 0) {
      this.max = val;
    }
  }
  insertNode(node, cur, parent = null) {
    node.size++;
    const compareResult = this.compare(cur.val, node.val);
    let res;
    if (compareResult === 0) {
      node.count++;
      return [false, node];
    } else if (compareResult > 0) {
      if (node.right) {
        res = this.insertNode(node.right, cur, node);
        if (!res[0]) return res;
      } else {
        node.right = cur;
        this.length++;
        res = [true, cur];
      }
    } else {
      if (node.left) {
        res = this.insertNode(node.left, cur, node);
        if (!res[0]) return res;
      } else {
        node.left = cur;
        this.length++;
        res = [true, cur];
      }
    }
    let preHeight = node.height;
    const newNode = this.balance(node);
    if (newNode === node && node.height === preHeight) {
      res = [false, res[1]];
    } else if (newNode !== node) {
      if (parent) {
        parent.left === node ? parent.left = newNode : parent.right = newNode;
      } else {
        this.root = newNode;
      }
      res = [false, res[1]];
    }
    return res;
  }
  delete(val) {
    if (!this.root) return;
    this.deleteNode(val, this.root, null);
  }
  deleteNode(val, node, parent) {
    if (!node) return null;
    let res = this.compare(val, node.val);
    if (res === 0) {
      node.count--;
      node.size--;
      if (node.count > 0) return node;
      if (!node.left || !node.right) {
        if (this.min === val) {
          this.minCache = false;
        }
        if (this.max === val) {
          this.maxCache = false;
        }
        this.length--;
        if (!parent) {
          var _node$left5;
          this.root = (_node$left5 = node.left) !== null && _node$left5 !== void 0 ? _node$left5 : node.right;
          return this.root;
        } else {
          var _node$left6;
          return (_node$left6 = node.left) !== null && _node$left6 !== void 0 ? _node$left6 : node.right;
        }
      } else {
        const selectLeft = node.left.height > node.right.height;
        let replaceNode = selectLeft ? this.pre(node) : this.next(node),
            name = selectLeft ? 'left' : 'right';
        node.val = replaceNode.val;
        node.count = replaceNode.count;
        replaceNode.count = 0;
        node[name] = this.deleteNode(replaceNode.val, node[name], node);
      }
    } else if (res > 0) {
      node.right = this.deleteNode(val, node.right, node);
    } else {
      node.left = this.deleteNode(val, node.left, node);
    }
    node.size = this.getSize(node);
    node.height;
    const newNode = this.balance(node);
    if (parent) {
      parent.left === node ? parent.left = newNode : parent.right = newNode;
    } else {
      this.root = newNode;
    }
    return newNode;
  }
  next(node) {
    let next = node.right;
    while ((_next = next) !== null && _next !== void 0 && _next.left) {
      var _next;
      next = next.left;
    }
    return next;
  }
  pre(node) {
    let pre = node.left;
    while ((_pre = pre) !== null && _pre !== void 0 && _pre.right) {
      var _pre;
      pre = pre.right;
    }
    return pre;
  }
  search(val, compare) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchCeilingNode(this.root, val, compare !== null && compare !== void 0 ? compare : this.compare);
    return node.val;
  }
  searchCeilingNode(node, val, compare, parent = null) {
    const res = compare(val, node.val);
    if (res === 0) {
      return [parent, node];
    } else if (res > 0) {
      if (node.right) {
        return this.searchCeilingNode(node.right, val, compare, node);
      } else {
        return [parent, node];
      }
    } else {
      if (node.left) {
        const [p, value] = this.searchCeilingNode(node.left, val, compare, node);
        if (compare(value.val, val) < 0) {
          return [parent, node];
        } else {
          return [p, value];
        }
      } else {
        return [parent, node];
      }
    }
  }
  ceiling(val) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchCeilingNode(this.root, val, this.compare);
    return this.compare(node.val, val) >= 0 ? node.val : null;
  }
  searchFloorNode(node, val, compare, parent = null) {
    const res = compare(val, node.val);
    if (res === 0) {
      return [parent, node];
    } else if (res > 0) {
      if (node.right) {
        const [p, value] = this.searchFloorNode(node.right, val, compare, node);
        if (compare(value.val, val) > 0) {
          return [parent, node];
        } else {
          return [p, value];
        }
      } else {
        return [parent, node];
      }
    } else {
      if (node.left) {
        return this.searchFloorNode(node.left, val, compare, node);
      } else {
        return [parent, node];
      }
    }
  }
  floor(val) {
    if (this.isEmpty()) {
      return null;
    }
    const [, node] = this.searchFloorNode(this.root, val, this.compare);
    return this.compare(node.val, val) <= 0 ? node.val : null;
  }
  searchKth(k) {
    if (this.isEmpty()) {
      return null;
    }
    if (k <= 0 || k > this.size()) {
      return null;
    }
    const node = this.searchNodeKth(this.root, k);
    return node.val;
  }
  searchNodeKth(node, k) {
    var _node$right$size2, _node$right5, _node$right$size3, _node$right6;
    const rSize = (_node$right$size2 = (_node$right5 = node.right) === null || _node$right5 === void 0 ? void 0 : _node$right5.size) !== null && _node$right$size2 !== void 0 ? _node$right$size2 : 0;
    if (rSize === k - 1 || rSize < k && rSize + node.count >= k) return node;
    if (node.right && rSize > k - 1) return this.searchNodeKth(node.right, k);else return this.searchNodeKth(node.left, k - ((_node$right$size3 = (_node$right6 = node.right) === null || _node$right6 === void 0 ? void 0 : _node$right6.size) !== null && _node$right$size3 !== void 0 ? _node$right$size3 : 0) - node.count);
  }
  countGreaterThanEq(val) {
    if (!this.root) return 0;
    return this.countCompare(val, (a, b) => this._compare(a, b), this.root);
  }
  countCompare(val, compare, node, pre = 0) {
    const res = compare(val, node.val);
    if (res === 0) {
      var _node$right$size4, _node$right7;
      return pre + ((_node$right$size4 = (_node$right7 = node.right) === null || _node$right7 === void 0 ? void 0 : _node$right7.size) !== null && _node$right$size4 !== void 0 ? _node$right$size4 : 0) + node.count;
    } else if (res > 0) {
      if (node.right) {
        return this.countCompare(val, compare, node.right, pre);
      } else {
        return pre;
      }
    } else {
      var _node$right$size5, _node$right8;
      let count = pre + ((_node$right$size5 = (_node$right8 = node.right) === null || _node$right8 === void 0 ? void 0 : _node$right8.size) !== null && _node$right$size5 !== void 0 ? _node$right$size5 : 0) + node.count;
      if (node.left) {
        return this.countCompare(val, compare, node.left, count);
      } else {
        return count;
      }
    }
  }
  toArray() {
    if (!this.root) return [];
    const res = [];
    const dfs = node => {
      if (node.left) dfs(node.left);
      res.push(node.val);
      if (node.right) dfs(node.right);
    };
    dfs(this.root);
    return res;
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, m, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});


var a = 'a'.charCodeAt(0);

;(function () {
	var str = readline(), l = str.length;
	var k = +readline();
	var alphabet = readline().split(' ').map(Number);
	var maxPrice = Math.max.apply(null, alphabet);

	var sum = ((function () {
		for (var i = 0, ret = 0; i < l; i++)
			ret += alphabet[str.charCodeAt(i) - a] * (i + 1);
		return ret;
	})()) + maxPrice * ((function () {
		for (var i = 0, ret = 0; i < k; i++)
			ret += l + i + 1;
		return ret;
	})());

	print(sum);

}).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 s = trim(readline()),
		k = parseInt(readline()),
		values = tokenizeIntegers(readline()),
		maxValue = 0,
		ch2value = {},
		total = 0;
	for (var i = 0; i < 26; ++i) {
		ch2value[String.fromCharCode(97+i)] = values[i];
		maxValue = Math.max(maxValue, values[i]);
	}
	for (var i = 0; i < s.length; ++i) {
		total += (i+1)*ch2value[s.charAt(i)];
	}
	for (var i = 0; i < k; ++i) {
		total += (s.length+i+1)*maxValue;
	}
	print(total);
}

main();

var s = readline()
var k = Number(readline())
var w = readline().split(' ').map(Number)
var l = s.length
var c = Math.max.apply(null, w) * k * (2 * l + k + 1) / 2
for (var i = 1; i <= l; ++i) {
    c += i * w[s.charCodeAt(i - 1) - 'a'.charCodeAt()]
}
print(c)

function main(str,k,arr){

    var t = 0;
    for(var i = 1 ; i <= str.length ; i++){
      t += (arr[str.charCodeAt(i-1)-'a'.charCodeAt(0)]*i);

    }
    var max = -Infinity;
    for(var i= 0 ; i < arr.length ; i++)max = Math.max(max,arr[i]);
    for(var i = str.length+1 ; i <= str.length+k ; i++)t += max*i;
    print(t);
}

var str = readline();
var k = parseInt(readline());
var arr = readline().split(" ");
for(var i = 0 ; i < arr.length ; i++)arr[i] = parseInt(arr[i]);

main(str,k,arr);


let buffer = '';
process.stdin.on('data', c => buffer += c);
process.stdin.on('end', () => {
  const { EOL } = require('os');
  const lines = buffer.split(EOL);
  lines.pop();

  main(lines);
});

function main(lines) {
  let T = Number(lines[0]);
  let index = 1;
  let N, K, arr;

  while (T--) {
    [N, K] = lines[index++].split(' ').map(Number);
    arr = lines[index++].split(' ').map(Number);
    solve(N, K, arr);
  }
}

function solve(N, K, input) {
  let result = 0;

  input.sort((a, b) => b - a);

  for (let i = 0; i < K; ++i) {
    result += Number(input[i] === input[i + K]);
  }

  for (let i = 2 * K; i < N; ++i) {
    result += input[i];
  }

  console.log(result);
}

var s = parseInt(readline());
  while (s--) {
    var input = readline()
      .split(" ")
      .map((data) => +data);
    var n = input[0];
    var k = input[1];
    var input1 = readline()
      .split(" ")
      .map((data) => +data)
      .sort((a, b) => {
        if (a > b) {
          return 1;
        } else if (a < b) {
          return -1;
        } else {
          return 0;
        }
      });
    var min1 = n - k;
    var min2 = n - (k + k);

    var min = 0;
    for (var i = 0; i < k; i++) {
      min += Math.floor(input1[min2] / input1[min1]);
      input1[min2] = 0;
      input1[min1] = 0;
      min1++;
      min2++;
    }
    for (var i = 0; i < n; i++) {
      min += input1[i];
    }
    print(min);
  }

"use strict";

const nQueues = this.readline().split(" ");

for (let i = 0; i < nQueues; i++) {
  const lll = this.readline().split(" ");
  const n = lll[0]
  const k = lll[1]
  const numbers = this.readline().split(" ").map(function(e){return Number(e)});
  numbers.sort((a, b) => a - b);
  const ys = numbers.slice(n - k, n + 1);
  const xs = numbers.slice(n - k - k, n - k);
  const somar = numbers.slice(0, n - k - k);
  let score = 0;
  for (let j = 0; j < k; j++) {
    score += Math.floor(xs[j] / ys[j]);
  }
  for (let j = 0; j < somar.length; j++) {
    score += somar[j];
  }
  this.print(score);
}

"use strict";

function answer(nums, k) {
    let score = 0;

    nums.sort((a, b) => a - b);

    const l = nums.length;
    for (let i = 0; i < k; i += 1) {
        score += Math.floor(
            nums[l - 1 - k - i] / nums[l - 1 - i]
        );
    }

    let remaining = 0;
    for (let i = 0; i < (nums.length - (k * 2)); i += 1) {
        remaining += nums[i];
    }

    return score + remaining;
}

const nQueues = this.readline().split(' ');

for (let i = 0; i < nQueues; i++) {
    const k = this.readline().split(' ')[1];
    const nums = this.readline().split(' ').map(e => +e);
    const result = answer(nums, k);
    this.print(result.toString());
}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let [n, k] = iInpArr();
    let arr = iInpArr();
    arr.sort((a, b) => b - a);
    let sum = 0;
    for (let i = 2 * k; i < n; i++) sum += arr[i];
    let obj = {},
      cnt = 0;
    for (let i = 0; i < 2 * k; i++) {
      if (obj[arr[i]] === undefined) obj[arr[i]] = 1;
      else obj[arr[i]]++;
    }
    let keys = Object.keys(obj);
    let max = 0;
    for (let i = 0; i < keys.length; i++) max = Math.max(max, obj[keys[i]]);
    if (max <= k) console.log(sum);
    else {
      let rem = max - (2 * k - max);
      rem = rem / 2;
      console.log(rem + sum);
    }
  }
};
solve();

/* Common Template Starts */
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++];
}
/* Common Template Ends */

function main() {
    //take input according to the format;
    const t = +(readline());
    for(let i =0; i<t; ++i){
        let[n, k] = readline().split(' ').map(el=>parseInt(el));
        const nums = readline().split(' ').map(el=>parseInt(el));

        nums.sort((a,b)=>a-b);
        if(nums.length == 1){
            console.log(nums[0]);
            continue;
        }

        let p = nums.length - 2*k;
        let q = nums.length - k ;
        let score = 0;
        while(q < nums.length){
            score += Math.floor(nums[p]/nums[q]);
            p++;
            q++;
        }

        for(let i=0; i< nums.length - 2*k; ++i){
            score+= nums[i];
        }
        console.log(score);
    }
}

let buffer = '';
process.stdin.on('data', c => buffer += c);
process.stdin.on('end', () => {
  const { EOL } = require('os');
  const lines = buffer.split(EOL);
  lines.pop();

  main(lines);
});

function main(lines) {
  let T = Number(lines[0]);
  let index = 1;
  let N, K, arr;

  while (T--) {
    [N, K] = lines[index++].split(' ').map(Number);
    arr = lines[index++].split(' ').map(Number);
    solve(N, K, arr);
  }
}

function solve(N, K, input) {
  const arr = new Array(N);
  let sum = 0;
  let bestRemovedSum = 0;
  let removedSum;
  let y, x, step;

  for (let i = 0; i < N; ++i) {
    arr[i] = new Array(N);
    sum += input[i];
  }

  input.sort((a, b) => a - b);

  for (let i = 0; i < N; ++i) {
    for (let j = 0; j < N; ++j) {
      arr[i][j] = input[i] + input[j] - Number(input[i] === input[j]);
    }
  }

  step = 0;

  for (let i = N - 1; i > 0; --i) {
    y = N - 1;
    x = i - 1;
    step++;
    k = K;
    removedSum = 0;
    isFlag = true;

    while (k > 0 && y >= 0 && x >= 0) {
      for (let z = 0; z < step && y >= 0 && x >= 0 && k > 0; ++z) {
        if (isFlag) {
          removedSum += arr[y][x];
          k--;
        }

        y--;
        x--;
      }

      isFlag = !isFlag;
    }

    bestRemovedSum = Math.max(bestRemovedSum, removedSum);
  }

  console.log(sum - bestRemovedSum);
}

'use strict';

function solve(n, m, a) {
  var _sort$0$, _sort, _sort$;
  const N = n - 2 * m;
  a.sort((a, b) => a - b);
  let res = a.slice(0, N).reduce((a, b) => a + b, 0);
  const map = new Map();
  for (let num of a.slice(N)) {
    var _map$get;
    map.set(num, ((_map$get = map.get(num)) !== null && _map$get !== void 0 ? _map$get : 0) + 1);
  }
  const max = (_sort$0$ = (_sort = [...map.entries()].sort((a, b) => b[1] - a[1])) === null || _sort === void 0 ? void 0 : (_sort$ = _sort[0]) === null || _sort$ === void 0 ? void 0 : _sort$[1]) !== null && _sort$0$ !== void 0 ? _sort$0$ : 0;
  res += Math.max(0, (max - (2 * m - max)) / 2);
  return res;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const [n, m] = (await read()).split(' ').map(Number);
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, m, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

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--) {
		const [n, k] = rna();
		const a = rna();

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

		let ans = 0;
		for (let i = 2*k; i < n; i++)
			ans += a[i];

		function count (x) {
			let cnt = 0, cntl = 0, cntm = 0;
			for (let i = 0; i < 2*k; i++) {
				cnt  += a[i] == x;
				cntl += a[i] <  x;
				cntm += a[i] > x;
			}
			return [cnt, cntl, cntm];
		}

		ar = [...new Set(a.slice(0, 2*k+1))];
		for (const elm of ar) {
			 const [c, cl, cm] = count(elm);
			 ans += Math.max(0, (c-cl-cm)/2);
			 //console.log(elm, {ans, c, cl, cm});
		}

		console.log(ans);
	}
}

'use strict';
const readline = require('readline');
const util = require('util');
const rl = readline.createInterface({
    input: process.stdin,
});
let pos = 0;
const words = [];
rl.on('line', (line) => {
    words.push.apply(words, line.split(' ').filter(s => s != ''));
});
rl.on('close', main);
function check(b) {
    if (!b) {
        process.stderr.write(`${new Error().stack}\n`);
        process.exit(-1);
    }
}
function printf(...args) {
    process.stdout.write(util.format(...args));
}
function nextInt() {
    return Math.trunc(Number(words[pos++]));
}
function nextStr() {
    return words[pos++];
}
function af(l) {
    return Array.from({ length: l });
}
const INIT = -1;
function main() {
    let tc = nextInt();
    while (tc-- > 0) {
        let n = nextInt();
        let k = nextInt();
        const a = af(n);
        for (let i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        a.sort((x, y) => x - y);
        let ans = 0;
        for (let i = 0; i < n - 2 * k; i++) {
            ans += a[i];
        }
        for (let j = n - 2 * k; j + k < n; j++) {
            ans += Math.floor(a[j] / a[j + k]);
        }
        printf("%d\n", ans);
    }
}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let [n, k] = iInpArr();
    let arr = iInpArr();
    arr.sort((a, b) => b - a);
    let sum = 0;
    for (let i = 2 * k; i < n; i++) sum += arr[i];
    let obj = {},
      cnt = 0;
    for (let i = 0; i < 2 * k; i++) {
      if (obj[arr[i]] === undefined) obj[arr[i]] = 1;
      else obj[arr[i]]++;
    }
    for (let i of Object.keys(obj)) {
      if (obj[i] === 1) cnt++;
    }
    if (cnt >= k) console.log(sum);
    else console.log(k - cnt + sum);
  }
};
solve();

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let [n, k] = iInpArr();
    let arr = iInpArr();
    arr.sort((a, b) => b - a);
    let sum = 0;
    for (let i = 2 * k; i < n; i++) sum += arr[i];
    let obj = {},
      cnt = 0;
    for (let i = 0; i < 2 * k; i++) {
      if (obj[arr[i]] === undefined) obj[arr[i]] = 1;
      else obj[arr[i]]++;
    }
    for (let i of Object.keys(obj)) {
      if (obj[i] === 1) cnt++;
    }
    console.log(k - cnt + sum);
  }
};
solve();

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let [n, k] = iInpArr();
    let arr = iInpArr();
    let sum = arr.reduce((i, j) => i + j, 0);
    arr.sort((a, b) => a - b);
    let i = 0,
      j = n - 1;
    while (k > 0) {
      let s1 = arr[i] + arr[j];
      let s2 = arr[j] + arr[j - 1];
      s1 += Math.floor(arr[i] / arr[j]);
      s2 += Math.floor(arr[j - 1] / arr[j]);
      if (s2 > s1) {
        sum -= arr[j - 1] + arr[j];
        sum += Math.floor(arr[j - 1] / arr[j]);
        j = j - 2;
      } else {
        sum -= arr[i] + arr[j];
        sum += Math.floor(arr[i] / arr[j]);
        i++;
      }
      k--;
    }
    console.log(sum);
  }
};
solve();

/* Common Template Starts */
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++];
}
/* Common Template Ends */

function main() {
    //take input according to the format;
    const t = +(readline());
    for(let i =0; i<t; ++i){
        let[n, k] = readline().split(' ').map(el=>parseInt(el));
        const nums = readline().split(' ').map(el=>parseInt(el));

        nums.sort((a,b)=>a-b);
        if(nums.length == 1){
            console.log(nums[0]);
            continue;
        }
        let p = nums.length - 2*k, q = nums.length - 1 ;
        let score = 0;
        while(p<q){
            score += Math.floor(nums[p]/nums[q]);
            p++;
            q--;
        }

        for(let i=0; i< nums.length - 2*k; ++i){
            score+= nums[i];
        }
        console.log(score);
    }
}

 var t = parseInt(readline())

  while (t--) {
    var b = readline()
      .split(" ")
      .map((data) => +data)
    var n = b[0]
    var k = b[1]
    var a = readline()
      .split(" ")
      .map((data) => +data).sort((a, b) => {
        if (a > b) {
          return 1;
        } else if (a < b) {
          return -1;
        } else {
          return 0;
        }
      })

    var b = [].concat(a)
    var min = 0;
    var min1 = 0;
    for (var i = 0; i < k; i++) {
      var last1 = a.pop()
      var last2 = a.pop()
      min = min + Math.floor(last2 / last1)
    }
    for (var i = 0; i < a.length; i++) {
      min += a[i]
    }

    for (var i = 0; i < k; i++) {
      var last1 = b.shift()
      var last2 = b.pop()
      min1 = min1 + Math.floor(last1 / last2)
    }
    for (var i = 0; i < a.length; i++) {
      min1 += b[i]
    }

    var ans = Math.min(min1, min)
    print(ans)
  }

var t = parseInt(readline())

  while (t--) {
    var b = readline()
      .split(" ")
      .map((data) => +data)
    var n = b[0]
    var k = b[1]
    var a = readline()
      .split(" ")
      .map((data) => +data).sort((a, b) => {
        if (a > b) {
          return 1;
        } else if (a < b) {
          return -1;
        } else {
          return 0;
        }
      })
    var min = 0;
    for (var i = 0; i < k; i++) {
      var last1 = a.pop()
      var last2 = a.pop()
      min = min + Math.floor(last2 / last1)
    }
    for (var i = 0; i < a.length; i++) {
      min += a[i]
    }
    print(min)
  }


//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = BigInt(next());
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
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';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//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();
	while(hasNext()){
		var N = nextInt();
		var list = nextIntArray(N);
		var odd = [];
		var even = [];
		for(var i = 0; i < N; i++){
			if(list[i] % 2 == 0){
				even.push(list[i]);
			}else{
				odd.push(list[i]);
			}
		}
		list = even.concat(odd);
		var output = 0;
		for(var i = 0; i < N - 1; i++){
			for(var j = i + 1; j < N; j++){
				if(gcd(list[i], 2 * list[j]) > 1){
					output++;
				}
			}
		}
		myout(output);
	}
}
function gcd(m, n) {return n != 0 ? gcd(n, m % n) : m;}

var timer = 0;
function dfs(v, prev) {
   var h = {};
   v.__h = h;
   h.tin = ++timer;
   h.up = [];
   var i = 0;
   var next = prev;
   while (next) {
      h.up[i] = next;
      next = next.__h.up[i++];
   }
   var children = v.childNodes;
   for (i = 0; i < children.length; i++) {
      dfs(children[i], v);
   }
   h.tout = ++timer;
}

function gcd(x, y) {
   if (y === 0) {
      return x;
   }
   return gcd(y, x % y);
}

function solve() {
   var n = Number(read());
   var a = readArray(Number);
   a.sort((x, y) => {
      var x1 = x & 1;
      var y1 = y & 1;
      if (x1 !== y1) {
         return x1 - y1;
      }
      return y - x;
   });
   var ans = 0;
   for (var i = 0; i < n; i++) {
      for (j = i + 1; j < n; j++) {
         if (gcd(a[i], 2 * a[j]) > 1) {
            ans++;
         }
      }
   }
   write(ans);
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

function sortF(a, b) {
   if (a < b) {
      return -1;
   }
   if (a > b) {
      return 1;
   }
   return 0;
}

function readArray(f) {
   return read().split(' ').map(f);
}

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;

   var needWriteFile = false;

   var writeResult = '';
   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');
         init();
         if (needWriteFile) {
            fs.writeFile(path.join(__dirname, outTextName), writeResult, (err) => {
               err ? console.error(err) : console.log('good')
            });
         }
      });
   } else {
      MEMORY = fs.readFileSync(0, 'utf-8').split('\r\n');
      init();
   }
} else {
   init();
}

function write(value) {
   if (needWriteFile) {
      writeResult += value + '\n';
      return;
   }
   isNode ? console.log('' + value) : print('' + value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}
function readAll() {
   return isNode && MEMORY;
}

let readline = require("readline");

let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];
rl.on("line", function (line) {
  input.push(line);

  if (input.length === +input[0] * 2 + 1) {
    // call your function here
    solve(input);
    rl.close();
  }
});

let solve = (input) => {
  for (let testCase = 1; testCase < input.length; testCase += 2) {
    let numbers = input[testCase + 1].split(" ");
    numbers.sort(function (x, y) {
      if (+x % 2 === 0) {
        return -1;
      }
      if (+y % 2 === 0) {
        return 1;
      }
      return 0;
    });

    let i = 0;
    let count = 0;
    let size = numbers.length - 1;
    while (+numbers[i] % 2 === 0) {
      count += size;
      size--;
      i++;
      continue;
    }

    for (let j = i; j < numbers.length - 1; j++) {
      for (let k = j + 1; k < numbers.length; k++) {
        if (findGCD(+numbers[j], +numbers[k] * 2) > 1) count++;
      }
    }

    console.log(count);
  }
};

const findGCD = (num1, num2) => {
  let a = Math.abs(num1);
  let b = Math.abs(num2);
  while (a && b && a !== b) {
    if (a > b) {
      [a, b] = [a % b, b];
    } else {
      [a, b] = [a, b % a];
    }
  }
  return a || b;
};

process.stdin.setEncoding('utf-8')
const readline = require('readline')

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

let count = -1
let inputArr = []

rl.on('line', data => {
    if (count === -1) {
        count = +data * 2
    } else if (count !== 0) {
        inputArr.push(data.trim())
        count--
    }
    // max number 2^(53) - 1 ~~ 10^(15)
    if (count === 0) {
        inputArr = inputArr.map(i => i.split(' ').map(j => +j))
        main()
        process.exit(0)
    }
})

function foo(n1, n2) {
    if (n1 == 1) return 0
    if (n2 == 0) return n1
    return foo(n2, n1 % n2)
}

function solve([n], arr) {
    let sum = 0
    for (let i = 0; i < n - 1; ++i) {
        for (let j = i + 1; j < n; ++j) {
            if (arr[i] % 2 == 0) {
                sum++
            } else if (arr[j] % 2 == 0) {
                sum++
            } else {
                if (foo(arr[i], arr[j]) > 1 || foo(arr[j], 2 * arr[i] > 1)) sum++
            }
        }
    }
    console.log(sum)
}

function main() {
    for (let i = 0; i < inputArr.length; i += 2) solve(inputArr[i], inputArr[i + 1])
}

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 06/04/21 morning
 * https://codeforces.com/contest/1535/problem/B
 */

const solve = (n, A) => {
    let even = [];
    let odd = [];
    for (const e of A) e & 1 ? odd.push(e) : even.push(e);
    let a = even.concat(odd);
    let res = 0;
    for (let i = 0; i < n; i++) {
        if (a[i] % 2 == 0) {
            res += n - 1 - i;
            continue;
        }
        for (let j = i + 1; j < n; j++) {
            if (gcd(a[i], 2 * a[j]) > 1) {
                res++;
            }
        }
    }
    pr(res);
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        let t = input[0][0];
        let i = 1;
        while (t--) {
            solve(input[i][0], input[i + 1]);
            i += 2;
        }
    });
};

main()

var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var lines = [];
rl.on('line', function(input) {
    lines.push(input);
});
rl.on('close', function() {
    var t = parseInt(lines[0]);
    var l = 1;
    for (var i = 0; i < t; i++) {
        var n = +lines[l++]
        var nums = lines[l++].split(' ').map(Number)
        console.log(solve(n, nums));
    }
});

function solve(n, nums) {
    nums.sort((a, b) => {
        const i = a & 1
        const j = b & 1
        if (i === j) {
            return 0
        } else if (i) {
            return 1
        } else {
            return -1
        }
    })

    let count = 0
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            if (gcd(nums[i], 2 * nums[j]) > 1) count++
        }
    }
    return count
}

function gcd(a, b) {
    if (a === 0) return b
    if (b === 0) return a

    while (a) {
        const r = b % a
        b = a
        a = r
    }
    return b
}

var timer = 0;
function dfs(v, prev) {
   var h = {};
   v.__h = h;
   h.tin = ++timer;
   h.up = [];
   var i = 0;
   var next = prev;
   while (next) {
      h.up[i] = next;
      next = next.__h.up[i++];
   }
   var children = v.childNodes;
   for (i = 0; i < children.length; i++) {
      dfs(children[i], v);
   }
   h.tout = ++timer;
}

function gcd(x, y) {
   if (y === 0) {
      return x;
   }
   return gcd(y, x % y);
}

function solve() {
   var n = Number(read());
   var a = readArray(Number);
   a.sort((x, y) => y - x);
   var ans = 0;
   for (var i = 0; i < n; i++) {
      for (j = i + 1; j < n; j++) {
         if (gcd(a[i], 2 * a[j]) > 1) {
            ans++;
         }
      }
   }
   write(ans);
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

function sortF(a, b) {
   if (a < b) {
      return -1;
   }
   if (a > b) {
      return 1;
   }
   return 0;
}

function readArray(f) {
   return read().split(' ').map(f);
}

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;

   var needWriteFile = false;

   var writeResult = '';
   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');
         init();
         if (needWriteFile) {
            fs.writeFile(path.join(__dirname, outTextName), writeResult, (err) => {
               err ? console.error(err) : console.log('good')
            });
         }
      });
   } else {
      MEMORY = fs.readFileSync(0, 'utf-8').split('\r\n');
      init();
   }
} else {
   init();
}

function write(value) {
   if (needWriteFile) {
      writeResult += value + '\n';
      return;
   }
   isNode ? console.log('' + value) : print('' + value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}
function readAll() {
   return isNode && MEMORY;
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = BigInt(next());
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
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';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//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();
	while(hasNext()){
		var N = nextInt();
		var list = nextIntArray(N);
		list.sort(function(a,b){
			return b - a;
		});
		var output = 0;
		for(var i = 0; i < N - 1; i++){
			for(var j = i + 1; j < N; j++){
				if(gcd(list[i], 2 * list[j]) > 1){
					output++;
				}
			}
		}
		myout(output);
	}
}
function gcd(m, n) {return n != 0 ? gcd(n, m % n) : m;}


const readline = require('readline');

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

const getOutput = n => {
  let output = [];
  while(n>0) {
    if(n%7 === 0) {
      n = n-7;
      output.push(7);
    } else if(n>=4 && n!=7) {
      n = n-4;
      output.push(4);
    } else {
      return -1;
    }
  }

  return output.join("");
}

rl.on('line', n => {

  console.log(getOutput(n));

  process.exit();
});

;(function () {

	print(function (n) {

		for (var i = 1, _i = 25e4; i < _i; i++) {
			if (i * 4 > n) return -1;

			var t = (n - i * 4) / 3;
			if ((t%1) === 0 && t <= i) {
				var result = '';
				while (t--) result += '7';
				while (result.length < i) result = '4' + result;
				return result;
			}
		}

	}(+readline()));

}.call(this));

const readline = require('readline');

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

const getOutput = n => {
  let output = [];
  while(n>0) {
    if(n==7) {
      n = n-7;
      output.push(7);
    } else if(n>=4 && n!=7) {
      n = n-4;
      output.push(4);
    } else {
      return -1;
    }
    console.log(output);
  }

  return output.join("");
}

rl.on('line', n => {

  console.log(getOutput(n));

  process.exit();
});

const readline = require('readline');

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

const getOutput = n => {
  let output = [];
  while(n>0) {
    if(n==7) {
      n = n-7;
      output.push(7);
    } else if(n>=4 && n!=7) {
      n = n-4;
      output.push(4);
    } else {
      return -1;
    }
  }

  return output.join("");
}

rl.on('line', n => {

  console.log(getOutput(n));

  process.exit();
});

;(function () {

	print(function (n) {

		for (var i = 1, _i = 25e4; i < _i; i++) {
			var k = i * 4;

			if (k > n) return -1;

			var t = (n - k) / 3;
			if ((t%1) !== 0) continue;

			if (t > i + 1) continue;

			var result = '';
			while (t--) result += '7';
			while (result.length < i) result = '4' + result;
			return result;
		}

	}(+readline()));

}.call(this));