/** */ #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair P; const int maxn = 1e5+100; int n, m; ll p[100]; int main() { int T; p[0] = 1; for(int i = 1; i <= 40; i++){ p[i] = p[i-1]*2; } cin>>T; while(T--) { int pos; scanf("%d%d",&n,&m); ll ans = 0; for(int i = min(m,34); i >= 1; i--){ if(n>=p[i]){ ans += n/p[i]; n %= p[i]; } } ans += n; printf("%I64d\n",ans); } return 0; }