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