#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define getmid(l,r) ((l) + ((r) - (l)) / 2) #define MP(a,b) make_pair(a,b) #define PB(a) push_back(a) typedef long long ll; typedef pair pii; const double eps = 1e-8; const int INF = (1 << 30) - 1; const int MAXN = 510; int T; int n,m; int A[100]; int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); int c = 0,tn = n; while(tn){ A[c++] = tn & 1; tn >>= 1; } int ans = 0; for(int i = 0; i < c; ++i) if(A[i]){ if(i <= m) ans++; else ans += (1 << (i - m)); } printf("%d\n",ans); } return 0; }