import java.math.BigInteger;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
long[] f = new long[30];
f[0]=1;
int t=cin.nextInt();
for(int i=1;i<30;i++)
f[i]=2*f[i-1];
while(t--!=0)
{
BigInteger n=cin.nextBigInteger();
int k=cin.nextInt();
BigInteger ans=BigInteger.ZERO;
n=n.divide(BigInteger.valueOf(f[k-1]));
while(n.compareTo(BigInteger.ZERO)==1)
{
if(n.compareTo(BigInteger.ZERO)==0) break;
ans=ans.add((n.add(BigInteger.ONE)).divide(BigInteger.valueOf(2)));
n=n.divide(BigInteger.valueOf(f[k]));
}
System.out.println(ans);
}
}
}