#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << endl;
      continue;
    }
    if (c - d <= 0) {
      cout << -1 << endl;
      continue;
    } else {
      ll sleep = c - d;
      ll mult = (a - b) / sleep + ((ll)(a - b) % sleep != 0);
      cout << mult * c + b << endl;
    }
  }
}
