#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
const long long INF = 1e9 + 7;
const long long maxn = 2e5 + 7;
using namespace std;
void solve() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  if (b >= a) {
    cout << b << '\n';
    return;
  }
  if (d >= c) {
    cout << -1 << '\n';
    return;
  }
  long long nAl = ceil((a - b) * 1.0 / (c - d) * 1.0);
  long long totalTime = c * nAl;
  cout << b + totalTime << '\n';
}
int main(int argc, char** argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
