#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long mima(long long ak) {
  int largest = 0, smallest = 9;
  long long n = ak;
  while (n) {
    int r = n % 10;
    largest = max(r, largest);
    smallest = min(r, smallest);
    n = n / 10;
  }
  return largest * smallest;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << "\n";
      continue;
    }
    if (c <= d) {
      cout << "-1\n";
      continue;
    }
    double num = (double)(a - b);
    double diff = (double)(c - d);
    long long f = ceil((double)(num / diff));
    long long t = b + f * c;
    cout << t << "\n";
  }
  return 0;
}
