#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 100;
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 && d >= c) {
      puts("-1");
      continue;
    }
    if (a <= b) {
      printf("%lld\n", b);
      continue;
    }
    long long x = (a - b) / (c - d) + ((a - b) % (c - d) != 0);
    printf("%lld\n", b + x * c);
  }
  return 0;
}
