annual_day

Abhi loves chocolates! He visits the Binary Bakery to buy some of his favorite chocolates. 
The owner of the bakery, Bobby, is a clever man. He does not want Abhi to finish all his chocolates. Hence, he plays a game.
The Abhi is given N numbers and has to select K of these numbers. For each number that Abhi chooses, he will get as many chocolates as the number of 1's in the Binary representation of the number i.e. the number of bits that are set.
Help Abhi find the maximum number of cakes that he can have.

Input:

The first line of input contains T. T test cases follow.

First line of each test cases contains 2 space-separated integers N and K.

The next line contains N space-separated integers.

Output:

For each test cases, print the answer in a new line.

Constraints:

1 ≤ T ≤ 10

1 ≤ N ≤ 103

0 ≤ K ≤ N

0 ≤ Numbers ≤ 105

SAMPLE INPUT
1
4 2
6 1 2 0

SAMPLE OUTPUT
3

Explanation

He chooses numbers 6 (110) and 1 (001) with 2 and 1 set bits respectively.

Source: hackerearth | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
for _ in range(int(input())):
	n,k = list(map(int,input().strip().split(" ")))
	arr= list(map(int,input().strip().split(" ")))
	counts=[]
	for i in range(len(arr)):
		c=0
		temp=arr[i]
		while(temp>0):
			if(temp%2==1):
				c+=1
			temp/=2
		counts.append(c)
	#print counts
	counts.sort()
	#print counts
	#print counts[0:k]
	sum1=0
	for i in range(n-k,n):
		sum1+=counts[i]
	print(sum1)
		
		


def maxi():
	for _ in range(eval(input())):
		b = []
		N, K = list(map(int, input().split()))
		a = list(map(int, input().split()))
		
		for i in a:
			bin = str('{0:08b}'.format(i))
			x = bin.count('1')
			b.append(x)
			
		b.sort(reverse = True)
		
		sum = 0
		for i in range(K):
			sum += b[i]
		
		print(sum)
		
if __name__ == "__main__":
	maxi()
			



'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
t = int(input())
while t>0:
	l = list(map(int,input().split(" ")))
	n = l[0]
	k = l[1]
	arr = list()
	line  = list(map(int,input().split(" ")))
	for x in range(0,n):
		setbits = 0
		while line[x]>0:
			if line[x]&1:
				setbits+=1
			line[x]/=2
		arr.append(setbits)
#	for x in range(0,n):
#		print arr[x]
	for x in range(0,n-1):
		for y in range(x,n):
			if arr[x]<arr[y]:
				temp = arr[x]
				arr[x] = arr[y]
				arr[y] = temp
	ans = 0
	for x in range(0,k):
		ans+=arr[x]
	print(ans)
		
	t-=1


for _ in range(eval(input())):
	n,s=list(map(int,input().split()))
	lst=list(map(int,input().split()))
	i=0
	def ones(a):
		count=0
		while(a>0):
			a=a&(a-1)
			count=count+1
		return count
	
	olist=[]
	while i<n:
		olist.append(ones(lst[i]))
		i=i+1
		
	oolist=sorted(olist,reverse=True)
	p=0
	flag=0
	while p<s:
		flag=flag+oolist[p]
		p=p+1
	print(flag)
	
	


t=eval(input())
while (t!=0):
	n ,k=list(map(int,input().split()))
	num=list(map(int,input().split()))
	z=0
	b=[]
	add=0
	i=-1
	for x in range(0,n):
		s=num[x]
		d =bin(s)
		sub=str(d)
		c=((sub[2:].count('1')))
		b.append(c)
		a=sorted(b)
	while( k!=0):
		add=add+a[i]
		i=i-1
		k=k-1
		
	print(add)
	t=t-1




from sys import stdin

def main():
	nextint = iter(map(int, stdin.read().split())).__next__
	for _ in range(nextint()):
		n, k = nextint(), nextint()
		print(sum(sorted([bin(nextint()).count('1') for _ in range(n)], reverse=True)[:k]))
		
main()


t=eval(input())
while t:
	n,k=list(map(int,input().split()))
	b=list(map(int,input().split()))
	a=[]
	count = 0
	for i in b:
		while i>0:
    			count = count + i%2
        		i=i/2
    		a.append(count)
   		count = 0
   	a.sort(reverse=True)
   	count=0
   	for i in range(k):
   		count = count + a[i]
   	print(count)
	t=t-1


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
t=eval(input())
while t>0:
	t-=1
	n,k=list(map(int,input().split()))
	l=[int(i) for i in input().split()]
	s=0
	p=[]
	for i in l:
		x=bin(i)
		p.append(x.count("1"))
	p.sort(reverse=True)
	for i in range(k):
		s+=p[i]
	print(s)


x=eval(input());c=0;lis=[];mi=[];pist=[];k=[]
for i in range(int(x)):
	k=[];c=0;mi=[]
	lis = (input().split())
	pist = (input().split())

	for i in range(len(pist)):
		k.append(bin(int(pist[i])).count("1"))
		
	k.sort()
	for i in range(len(k)):
		mi.append(k[i])
	
	mi=mi[::-1]
	
	for i in range(int(lis[1])):
		c=c+mi[i]
	
	
	print (c)