#include <bits/stdc++.h>
using namespace std;
long long int i, j, n, t, x, y, a, b, c, d, e, f[100000], g, k, mx, m, z;
string s, p;
char v[100000], fa[10];
int main() {
  cin >> t;
  while (t--) {
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << endl;
    } else if (d >= c) {
      puts("-1");
    } else {
      cout << b + (((a - b) + (c - d) - 1) / (c - d)) * c << endl;
    }
  }
  return 0;
}
