#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << "\n";
      continue;
    }
    if (d >= c) {
      cout << -1 << "\n";
      continue;
    }
    cout << ((a - b + c - d - 1) / (c - d)) * (c) + b << "\n";
  }
  return 0;
}
