#include "bits/stdc++.h" using i64 = std::int_fast64_t; using u64 = std::uint_fast64_t; void run() { u64 x, y; std::cin >> x >> y; u64 base = std::__lg(x); u64 ans = 0; if (((x >> y) & 1) == 0) { std::cout << "0\n"; } else if (((x >> y) & 1) == 1 && ((x >> (y + 1)) & 1) == 0) { std::cout << (1ull << (y + 1)) - 1 << "\n"; } else { do { y++; } while ((x >> (y + 1)) & 1); std::cout << (1ull << (y + 1)) - 1 << "\n"; } } int main() { std::cin.tie(nullptr)->sync_with_stdio(false); int t; std::cin >> t; for (int tcase = 0; tcase < t; tcase++) { run(); } return 0; }