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