#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
ll n, m, r1, r2;
cin >> n >> m >> r1 >> r2;
if(n > m)swap(n, m);
if(r1 > r2)swap(r1, r2);
ll g, h, diff;
g = 2 * r1 + 1 + 1 + 2 * r1 + 1 + (r2 - r1) + (r2 == r1);
g = max(g, 2 * r2 + 1);
h = 2 * r2 + 1 + (r1 == r2);
if(n - h > 0){
if(g - (2 *r2 + 1) > 0){
diff = min(n - h, g - (2 * r2 + 1));
h += diff;
g -= diff;
}
}
if(g <= m && h <= n){
cout << 2 * r1 + 2 + h - r2 + g - r2 << "\n";
}else{
cout << -1 << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// cout << fixed << setprecision(7);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i++){
// cout << "Case #" << i << ": ";
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7CgoKdm9pZCBzb2x2ZSgpewogICAgbGwgbiwgbSwgcjEsIHIyOwogICAgY2luID4+IG4gPj4gbSA+PiByMSA+PiByMjsKCiAgICBpZihuID4gbSlzd2FwKG4sIG0pOwogICAgaWYocjEgPiByMilzd2FwKHIxLCByMik7CiAgICAKICAgIGxsIGcsIGgsIGRpZmY7CiAgICAKICAgIGcgPSAyICogcjEgKyAxICsgMSArIDIgKiByMSArIDEgKyAocjIgLSByMSkgKyAocjIgPT0gcjEpOwogICAgZyA9IG1heChnLCAyICogcjIgKyAxKTsKICAgIGggPSAyICogcjIgKyAxICsgKHIxID09IHIyKTsKICAgIAogICAgaWYobiAtIGggPiAwKXsKICAgIAlpZihnIC0gKDIgKnIyICsgMSkgPiAwKXsKICAgIAkJZGlmZiA9IG1pbihuIC0gaCwgZyAtICgyICogcjIgKyAxKSk7CiAgICAJCWggKz0gZGlmZjsKICAgIAkJZyAtPSBkaWZmOwogICAgCX0KICAgIH0KICAgIGlmKGcgPD0gbSAmJiBoIDw9IG4pewogICAgICAgIGNvdXQgPDwgMiAqIHIxICsgMiArIGggLSByMiArIGcgLSByMiA8PCAiXG4iOyAKICAgIH1lbHNlewogICAgCWNvdXQgPDwgLTEgPDwgIlxuIjsKICAgIH0KICAgIAoKICAgIAogICAgCn0KCmludCBtYWluKCl7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7CgovLyAgY291dCA8PCBmaXhlZCA8PCBzZXRwcmVjaXNpb24oNyk7CiAgICBpbnQgdCA9IDE7CiAgICBjaW4gPj4gdDsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gdDsgaSsrKXsKICAgICAgIC8vIGNvdXQgPDwgIkNhc2UgIyIgPDwgaSA8PCAiOiAiOwogICAgICAgIHNvbHZlKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==