#include <bits/stdc++.h>
using namespace std;
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
struct CustomCompare {
  bool operator()(pair<int64_t, pair<int64_t, int64_t> > &a,
                  pair<int64_t, pair<int64_t, int64_t> > &b) {
    if (a.first == b.first) return a.second.first > b.second.second;
    return a.first < b.first;
  }
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int64_t t = 1;
  cin >> t;
  while (t--) {
    int64_t a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << endl;
      continue;
    }
    if (d >= c) {
      cout << -1 << endl;
      continue;
    }
    int64_t ans = b + ceil(1.0 * (a - b) / (c - d)) * c;
    cout << ans << endl;
  }
  return 0;
}
