Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least $a$ minutes to feel refreshed.

Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in $b$ minutes.

Every time Polycarp wakes up, he decides if he wants to sleep for some more time or not. If he's slept for less than $a$ minutes in total, then he sets his alarm to go off in $c$ minutes after it is reset and spends $d$ minutes to fall asleep again. Otherwise, he gets out of his bed and proceeds with the day.

If the alarm goes off while Polycarp is falling asleep, then he resets his alarm to go off in another $c$ minutes and tries to fall asleep for $d$ minutes again.

You just want to find out when will Polycarp get out of his bed or report that it will never happen.

Please check out the notes for some explanations of the example.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of testcases.

The only line of each testcase contains four integers $a, b, c, d$ ($1 \le a, b, c, d \le 10^9$) — the time Polycarp has to sleep for to feel refreshed, the time before the first alarm goes off, the time before every succeeding alarm goes off and the time Polycarp spends to fall asleep.


-----Output-----

For each test case print one integer. If Polycarp never gets out of his bed then print -1. Otherwise, print the time it takes for Polycarp to get out of his bed.


-----Example-----
Input
7
10 3 6 4
11 3 6 4
5 9 4 10
6 5 2 3
1 1 1 1
3947465 47342 338129 123123
234123843 13 361451236 361451000

Output
27
27
9
-1
1
6471793
358578060125049



-----Note-----

In the first testcase Polycarp wakes up after $3$ minutes. He only rested for $3$ minutes out of $10$ minutes he needed. So after that he sets his alarm to go off in $6$ minutes and spends $4$ minutes falling asleep. Thus, he rests for $2$ more minutes, totaling in $3+2=5$ minutes of sleep. Then he repeats the procedure three more times and ends up with $11$ minutes of sleep. Finally, he gets out of his bed. He spent $3$ minutes before the first alarm and then reset his alarm four times. The answer is $3+4 \cdot 6 = 27$.

The second example is almost like the first one but Polycarp needs $11$ minutes of sleep instead of $10$. However, that changes nothing because he gets $11$ minutes with these alarm parameters anyway.

In the third testcase Polycarp wakes up rested enough after the first alarm. Thus, the answer is $b=9$.

In the fourth testcase Polycarp wakes up after $5$ minutes. Unfortunately, he keeps resetting his alarm infinitely being unable to rest for even a single minute :(

Difficulty: interview

Starter code:

Solutions:
import sys

readline = sys.stdin.readline
read = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    a, b, c, d = nm()
    if a <= b:
        print(b)
    else:
        a -= b
        if c <= d:
            print(-1)
        else:
            print(b + c * ((a-1)//(c-d) + 1))
    return


# solve()

T = ni()
for _ in range(T):
    solve()


for _ in range(int(input())):
  a,b,c,d=map(int,input().split())
  if a<=b:print(b);continue
  if c<=d:print(-1);continue
  m=a-b
  t=0--m//(c-d)
  print(b+t*c)

from math import *
from random import *
for t in range(int(input())):
    a, b, c, d = map(int, input().split())
    if b >= a:
        print(b)
        continue
    a -= b
    if d >= c:
        print(-1)
        continue
    otv = b
    cnt = a // (c - d)
    if a % (c - d) != 0:
        cnt += 1
    print(otv + (c * cnt))

import math
for _ in range(int(input())):
    a,b,c,d = list(map(int,input().split()))
    need = a-b
    if (need<=0):
        print(b)
    else:
        if (c-d) <= 0:
            print(-1)
        else:
            print(b+math.ceil((need/(c-d)))*c)

def solve(a, b, c, d):
    if b >= a:
        return b
    if d >= c:
        return -1
    return b + ((a - b + c - d - 1) // (c - d)) * c
    

[print(solve(*map(int, input().split()))) for _ in range(int(input()))]


# cook your dish here
# import sys
# sys.stdin = open('input.txt', 'r') 
# sys.stdout = open('output.txt', 'w')

import math
import collections
from sys import stdin,stdout,setrecursionlimit
import bisect as bs
T = int(stdin.readline())

for _ in range(T):
    # n = int(stdin.readline())
    a,b,c,d = list(map(int,stdin.readline().split()))
    # h = list(map(int,stdin.readline().split()))
    # b = list(map(int,stdin.readline().split()))
    # s = stdin.readline().strip('\n')
    if(b >= a):
        print(b)
        continue
    rem = a-b
    if(c-d <= 0):
        print(-1)
        continue
    ans = b+c*((rem//(c-d))+(1 if rem % (c-d) != 0 else 0))
    print(ans)


n = int(input())

for _ in range(n):
    a,b,c,d = map(int,input().split())
    if a <= b:
        print(b)
        continue
    if c <= d:
        print(-1)
        continue
    print((a - b + c - d - 1) // (c - d) * c + b)

import sys
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7
input = sys.stdin.readline


T = int(input())

for t in range(T):
    a, b, c, d = list(map(int, input().split()))

    if a <= b:
        print(b)
    else:
        if c <= d:
            print(-1)
        else:
            cnt = (a - b - 1) // (c - d) + 1
            print(b + cnt * c)


for _ in range(int(input())):
    a, b, c, d = tuple(map(int, input().split()))

    if a <= b:
        print(b)
    elif c > d:
        a -= b
        t = (a - 1) // (c - d) + 1
        print(b + t * c)
    else:
        print(-1)


t = int(input())
while t > 0 :
	t -= 1
	a, b, c, d = map(int, input().split())
	if b < a and c <= d : 
		print('-1')
	elif b >= a :
		print(b)
	else :
		print(b + (a - b + c - d - 1) // (c - d) * c)

def read_int():
    return int(input())


def read_ints():
    return list(map(int, input().split(' ')))


t = read_int()
for case_num in range(t):
    a, b, c, d = read_ints()
    if a > b and d >= c:
        print(-1)
    elif a <= b:
        print(b)
    else:
        remain = a - b
        extra = ((remain - 1) // (c - d) + 1) * c
        print(b + extra)


t = int(input())
for _ in range(t):
    a, b, c, d = list(map(int, input().split()))
    if b >= a:
        print(b)
    elif d >= c:
        print(-1)
    else:
        k = (a - b + c - d - 1) // (c - d)
        print(b + k * c)



for _ in range(int(input())):
    a, b, c, d = [int(x) for x in input().split()]
    if b >= a:
        print(b)
    elif d >= c:
        print(-1)
    else:
        print(b + c * ((a - b + c - d - 1) // (c - d)))

import math
for nt in range(int(input())):
	a,b,c,d=map(int,input().split())
	if b>=a:
		print (b)
		continue
	if d>=c:
		print (-1)
		continue
	diff=c-d
	t=c
	left=a-b
	print (b+math.ceil(left/diff)*c)

for _ in range(int(input())):
    a, b, c, d = map(int, input().split())
    cur = b
    if cur < a:
        if d >= c:
            print(-1)
            continue
        delta = c - d
        cur += c * ((a - cur + delta - 1) // delta)
    print(cur)

ans = []
for _ in range(int(input())):
    a, b, c, d = list(map(int, input().split()))
    if b >= a:
        ans.append(b)
        continue
    a -= b
    if d >= c:
        ans.append(-1)
        continue
    x = (a - 1) // (c - d) + 1
    ans.append(b + x * c)
print('\n'.join(map(str, ans)))


t = int(input())
for ii in range(t):
    a, b, c, d = map(int, input().split())
    if b >= a:
        print(b)
    else:
        if c - d <= 0:
            print(-1)
        else:
            print(b + c * ((a - b + c - d - 1) // (c - d)))

for _ in range(int(input())):
    a, b, c, d = list(map(int, input().split()))
    if b >= a:
        print(b)
    elif d >= c:
        print('-1')
    else:
        print(b + c * ((a - b - 1) // (c - d) + 1))


t =int(input())
for i in range(t):
    a, b, c, d = list(map(int, input().split()))
    if b >= a:
        print(b)
        continue
    if c <= d:
        print(-1)
        continue
    x = c - d
    y = (a - b + c - d - 1) // x
    print(b + y * c)


#JMD
#Nagendra Jha-4096

 
import sys
import math

#import fractions
#import numpy
 
###File Operations###
fileoperation=0
if(fileoperation):
    orig_stdout = sys.stdout
    orig_stdin = sys.stdin
    inputfile = open('W:/Competitive Programming/input.txt', 'r')
    outputfile = open('W:/Competitive Programming/output.txt', 'w')
    sys.stdin = inputfile
    sys.stdout = outputfile

###Defines...###
mod=1000000007
 
###FUF's...###
def nospace(l):
    ans=''.join(str(i) for i in l)
    return ans
 
 
 
##### Main ####
t=int(input())
for tt in range(t):
    #n=int(input())
    a,b,c,d= map(int, sys.stdin.readline().split(' '))
    if a<=b:
        print(b)
        continue
    diff = c-d

    if diff<=0:
        print(-1)
        continue
    else:
        rem=a-b
        slept=b
        v = (rem//diff)
        if v*diff<rem:
            v+=1
        slept+=(v*c)
        print(slept)
    #a=list(map(int,sys.stdin.readline().split(' ')))
    
    
#####File Operations#####
if(fileoperation):
    sys.stdout = orig_stdout
    sys.stdin = orig_stdin
    inputfile.close()
    outputfile.close()

for _ in range(int(input())):
    a, b, c, d = map(int, input().split())

    if b >= a:
        print(b)
        continue

    if c <= d:
        print(-1)
        continue

    print(b + -(-(a - b) // (c - d)) * c)

for _ in range(int(input())):
    a, b, c, d = list(map(int, input().split()))
    if a > b and d >= c:
        print(-1)
        continue

    if b >= a:
        print(b)
        continue

    diff = a-b
    step = c-d

    howmany = (diff+step-1)//step
    print(b + howmany * c)


T = int(input())
import math
n = [0]*T
m = [0]*T
a = [0]*T
p = [0]*T


for t in range(T):
  #n[t] = int(input())   #,m[t] = [int(i) for i in input().split(' ')]
  a,b,c,d = [int(i) for i in input().split(' ')]
  #p[t] = [int(i) for i in input().split(' ')]
  if b>=a:
    print(b)
  elif c<=d:
    print(-1)
  else:
    print(b+(math.ceil((a-b)/(c-d)))*c)


T = int(input().strip())
for t in range(T):
    a, b,c,d  = list(map(int, input().split()))
    if b>=a:
        print(b)
        continue
    if c>d:
        sleeptime = c-d
        remtime = a-b
        stimes = remtime//sleeptime
        if remtime % sleeptime >0:
            stimes += 1
        print(b + c * stimes)
        continue
    else:
        print(-1)
        continue


for tt in range(int(input())):
	a, b, c, d = map(int,input().split())
	if(a<=b):
		print(b)
		continue
	a = a - b
	if(d>=c):
		print(-1)
		continue
	val = a//(c - d)
	if(a%(c-d)!=0):
		val += 1
	print(b + val*c)

t = int(input())
for _ in range(t):
    a, b, c, d = map(int, input().split())
    nokori = a - b
    if a - b <= 0:
        print(b)
    else:
        diff = c - d
        if diff <= 0:
            print(-1)
        else:
            cnt = -((-nokori) // diff)
            print(b + cnt * c)

import math
t=int(input())
for you in range(t):
    l=input().split()
    a=int(l[0])
    b=int(l[1])
    c=int(l[2])
    d=int(l[3])
    if(b>=a):
        print(b)
    else:
        rem=(a-b)
        time=b
        if(d>=c):
            print(-1)
        else:
            x=math.ceil(rem/(c-d))
            time+=(x*c)
            print(time)


import math
n= int(input())
for i in range(n):
    A, B, C, D = [int(s) for s in input().split()] 
    left = A - B
    if left <1:
        print(B)
    else:
        if C > D:
            gone = C - D
            print(B + (math.ceil(left/gone) * C))
        else:
            print(-1)

t = int(input())
out = ''
while t > 0:
    t -= 1
    a, b, c, d = [int(i) for i in input().split(' ')]
    left = a - b
    if left <= 0:
        out += f'{b}\n'
        continue
    each = c - d
    if each <= 0:
        out += '-1\n'
        continue
    answer = b + c * (left // each)
    if left % each != 0:
        answer += c
    out += f'{answer}\n'
print(out)