#include <bits/stdc++.h>
using namespace std;
int t;
long long a, b, c, d;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
    if (a <= b) {
      printf("%lld\n", b);
      continue;
    }
    if (c <= d) {
      printf("-1\n");
      continue;
    }
    a -= b;
    if (a % (c - d) == 0)
      a /= (c - d);
    else {
      a /= (c - d);
      a++;
    }
    b += a * c;
    printf("%lld\n", b);
  }
}
