#include <bits/stdc++.h>
using namespace std;
int T;
signed long long A, B, C, D;
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> T;
  while (T--) {
    cin >> A >> B >> C >> D;
    if (A <= B) {
      cout << B << endl;
    } else {
      if (C <= D) {
        cout << -1 << endl;
      } else {
        signed long long a = (D + (C - 1)) / C * C;
        signed long long step = a - D;
        signed long long lef = A - B;
        signed long long step2 = (lef + step - 1) / step;
        cout << B + step2 * a << endl;
      }
    }
  }
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
