#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
ll a,b,c,d;
int T;
cin >> T;
while(T--){
cin >> a >> b >> c >> d;
ll t;
if(a>b&&c<=d){cout << -1 << "\n";continue;}
a-=b;
if(a<=0){cout << b << "\n";continue;}
t=a/(c-d);
if(a%(c-d))t++;
ll sum= t*c+b;
cout << sum << "\n";
}
}