#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
int t;
ll n,k;
ll getsum(ll n)
{
ll sum=0;
sum=n*(n+1)/2;
return sum;
}
void solve()
{
cin>>n>>k;
ll tmp=n/2;
ll ans=getsum(n);
while(k&&tmp)
{
k--;
ans-=getsum(tmp);
tmp/=2;
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
solve();
}
return 0;
}