#include #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; void solve() { int n, m; cin >> n >> m; int ac = 0, wa = n/4+1; while (abs(ac-wa) > 1) { int wj = (ac+wa)/2; auto ok = [&]{ int need = 2*wj-m; int y = 0; if (need > 0) y = (need+3)/4; return 2*y+4*wj <= n; }(); (ok ? ac : wa) = wj; } cout << ac*3 << '\n'; } int main() { cin.tie(nullptr) -> sync_with_stdio(false); int t; cin >> t; while (t--) solve(); return 0; }