#include #include #include #include #include #define pb push_back #define LL long long using namespace std; int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d%d", &n, &m); int cur = 0, t = 2; int ans = 0; while (n) { if (n & (1 << cur)) { if (cur <= m) ++ans; else { ans += t; } n ^= (1 << cur); } if (cur > m) t <<= 1; ++cur; } printf("%d\n", ans); } return 0; }