#include #include #include #include #include #include #include #include #include #include #include #include #define mem(x,y) memset(x,y,sizeof(x)) #define pb push_back using namespace std; typedef long long ll; typedef pair pii; #define bug puts("==========="); const double pi=(acos(-1.0)); const double eps=1e-8; const ll INF=1e18+10; const int inf=1e9+10; const int maxn=1000+5; const int mod=1e9+7; int main() { int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); m++; int dig; int cnt=0; int ans=0; while(n) { dig=n&1; cnt++; if(cnt==m) { ans+=n; break; } n/=2; if(dig==1)ans++; } printf("%d\n",ans); } }