// #ifndef ONLINE_JUDGE #if __has_include("all.h") #include "all.h" #else #include #include #endif using ll = long long int; template bool chmin(T &x, const T val) { if (x > val) { x = val; return true; } else { return false; } } template bool chmax(T &x, const T val) { if (x < val) { x = val; return true; } else { return false; } } template std::istream &operator>>(std::istream &is, std::pair &p) { return is >> p.first >> p.second; } template std::istream &operator>>(std::istream &is, std::vector &v) { for (T &x : v) is >> x; return is; } template std::istream &operator>>(std::istream &is, std::tuple &tpl) { [&is, &tpl ](std::index_sequence) { (is >> ... >> std::get(tpl)); } (std::make_index_sequence>>{}); return is; } template * = nullptr> std::ostream &operator<<(std::ostream &os, const mint &v) { return os << v.val(); } template std::ostream &operator<<(std::ostream &os, const std::vector &v) { for (int i = 0; i < v.size(); i++) os << v[i] << (i == v.size() - 1 ? "" : " "); return os; } struct Initialization { Initialization() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); } } initialization; constexpr std::pair dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; template using infs = std::numeric_limits; template class factorials { size_t n; std::vector fact, inv_fact; void extend(size_t m) { if (m <= n) return; fact.resize(m + 1); inv_fact.resize(m + 1); for (size_t i = n + 1; i <= m; i++) fact[i] = fact[i - 1] * i; inv_fact[m] = fact[m].inv(); for (size_t i = m - 1; i >= n + 1; i--) inv_fact[i] = inv_fact[i + 1] * i; n = m; } public: factorials() : n(0), fact(1, 1), inv_fact(1, 1) {} factorials(size_t n) : n(n), fact(n + 1), inv_fact(n + 1) { fact[0] = 1; for (size_t i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; inv_fact[n] = fact[n].inv(); for (size_t i = n; i >= 1; i--) inv_fact[i - 1] = inv_fact[i] * i; } T inv(int k) { extend(k); return inv_fact[k]; } T operator()(int k) { extend(k); return fact[k]; } T perm(int n, int k) { if (n < k) return 0; if (k < 0) return 0; extend(n); return fact[n] * inv_fact[n - k]; } T choose(int n, int k) { if (n < k) return 0; if (k < 0) return 0; extend(n); return fact[n] * inv_fact[n - k] * inv_fact[k]; } }; template class fps { std::vector v; public: using value_type = T; using reference = T &; using const_reference = const T &; using iterator = typename std::vector::iterator; using const_iterator = typename std::vector::const_iterator; size_t size() const { return v.size(); } const std::vector &data() const { return v; } explicit fps(int n) : v(n) {} fps(const std::vector &v) : v(v) {} fps(std::vector &&v) : v(v) {} template fps(InputIterator first, InputIterator last) : v(first, last) {} void resize(int n) { v.resize(n); } T &operator[](int i) { return v[i]; } iterator begin() { return v.begin(); } iterator end() { return v.end(); } fps diff() { std::vector res(v.size() - 1); for (int i = 0; i < res.size(); i++) res[i] = v[i + 1] * (i + 1); return fps(res); } fps integral() { std::vector res(v.size() + 1); for (int i = 0; i < v.size(); i++) res[i + 1] = v[i] / (i + 1); return fps(res); } fps inv(int deg = -1) { assert(v[0] != 0); if (deg == -1) deg = size(); std::vector res(deg); res[0] = v[0].inv(); for (int d = 1; d < deg; d <<= 1) { std::vector f(2 * d), g(2 * d); std::copy(v.begin(), v.begin() + std::min(2 * d, (int)v.size()), std::back_inserter(f)); std::copy(res.begin(), res.begin() + d, std::back_inserter(g)); atcoder::internal::butterfly(f); atcoder::internal::butterfly(g); for (int i = 0; i < 2 * d; i++) f[i] *= g[i]; atcoder::internal::butterfly_inv(f); for (int i = 0; i < d; i++) f[i] = 0; atcoder::internal::butterfly(f); for (int i = 0; i < 2 * d; i++) f[i] *= g[i]; atcoder::internal::butterfly_inv(f); for (int i = d; i < std::min(2 * d, deg); i++) res[i] = -f[i]; } res.resize(deg); return res; } fps operator-() { fps res(v.size()); for (int i = 0; i < v.size(); i++) res[i] = -v[i]; return res; } fps &operator+=(const fps &rhs) { if (v.size() < rhs.v.size()) v.resize(rhs.v.size()); for (int i = 0; i < rhs.v.size(); i++) v[i] += rhs.v[i]; return *this; } fps &operator-=(const fps &rhs) { if (v.size() < rhs.v.size()) v.resize(rhs.v.size()); for (int i = 0; i < rhs.v.size(); i++) v[i] -= rhs.v[i]; return *this; } fps &operator*=(const fps &rhs) { return *this = atcoder::convolution(v, rhs.v); } fps &operator+=(const T &rhs) { if (v.size() == 0) v.resize(1); v[0] += rhs; return *this; } fps &operator-=(const T &rhs) { if (v.size() == 0) v.resize(1); v[0] -= rhs; return *this; } fps &operator*=(const T &rhs) { for (int i = 0; i < v.size(); i++) v[i] *= rhs; return *this; } fps &operator/=(const T &rhs) { T rhs_inv = rhs.inv(); for (int i = 0; i < v.size(); i++) v[i] *= rhs_inv; return *this; } friend fps operator+(const fps &lhs, const fps &rhs) { return fps(lhs) += rhs; } friend fps operator-(const fps &lhs, const fps &rhs) { return fps(lhs) -= rhs; } friend fps operator*(const fps &lhs, const fps &rhs) { return fps(lhs) *= rhs; } friend fps operator+(const fps &lhs, const T &rhs) { return fps(lhs) += rhs; } friend fps operator-(const fps &lhs, const T &rhs) { return fps(lhs) -= rhs; } friend fps operator*(const fps &lhs, const T &rhs) { return fps(lhs) *= rhs; } friend fps operator/(const fps &lhs, const T &rhs) { return fps(lhs) /= rhs; } friend fps operator+(const T &lhs, const fps &rhs) { return fps(rhs) += lhs; } friend fps operator-(const T &lhs, const fps &rhs) { return -(fps(rhs) -= lhs); } friend fps operator*(const T &lhs, const fps &rhs) { return fps(rhs) *= lhs; } }; using mint = atcoder::modint998244353; ll solve(ll x, int y) { if (x == 0) return 0; int t = std::__bit_width(x) - 1; ll z = 1LL << t, p = z - (x - z) - 1; if (y >= std::__bit_width(p)) { return z + solve(z - 1, 0); } else { return solve(x - z, y); } } int main() { int T; std::cin >> T; while (T--) { ll x, y; std::cin >> x >> y; std::cout << solve(x, y) << "\n"; } }