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: EASY

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


import math

def main():
	(a, b, c, d) = map(int, input().split())
	sleep = c - d
	if b >= a:
		print(b)
		return
	if sleep <= 0 and b < a:
		print(-1)
		return
	ans = math.ceil((a - b) / sleep)
	final = b + c * int(ans)
	print(final)
t = int(input())
while t:
	main()
	t -= 1


import sys
agGK = int(input())
for _ in range(agGK):
	(a, b, c, d) = map(int, input().split())
	e = c - d
	if b >= a:
		print(b)
		continue
	if e <= 0:
		print('-1')
		continue
	t = b
	a -= b
	y = a // e
	a = a - y * e
	t += y * c
	if a > 0:
		t += c
	print(t)


from sys import stdin, stdout
import heapq as hq
from collections import defaultdict
from math import ceil
t = int(input())
for tc in range(t):
	(a, b, c, d) = list(map(int, stdin.readline().split()))
	slept = 0
	time = 0
	if b >= a:
		res = b
	elif c <= d:
		res = -1
	else:
		a -= b
		number = ceil(a / (c - d))
		res = b + number * c
	stdout.write(str(res) + '\n')


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


import math
import sys
from collections import defaultdict, Counter, deque, OrderedDict
import bisect
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
from itertools import accumulate, groupby
for _ in range(int(input())):
	(a, b, c, d) = ilele()
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = a - b
		y = math.ceil(x / (c - d))
		print(b + y * c)


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


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


from collections import defaultdict, deque
import heapq, bisect, math, itertools, string, queue, copy, time
import sys
input = sys.stdin.readline
INF = float('inf')
mod = 10 ** 9 + 7
eps = 10 ** (-7)

def inp():
	return int(input())

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

def inpl_str():
	return list(input().split())
t = inp()
ans = []
for _ in range(t):
	(a, b, c, d) = inpl()
	if a <= b:
		ans.append(b)
		continue
	a -= b
	if c <= d:
		ans.append(-1)
		continue
	tmp = b + math.ceil(a / (c - d)) * c
	ans.append(tmp)
print('\n'.join([str(i) for i in ans]))


from math import ceil
t = int(input())
for test in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = a - b
		x = ceil(x / (c - d))
		print(b + x * c)


import sys
input = sys.stdin.readline
num_tests = int(input().strip())
answers = [None] * num_tests
for test in range(num_tests):
	line = input().strip().split(' ')
	numbers = [int(x) for x in line]
	minutes_needed = numbers[0]
	first_alarm = numbers[1]
	snooze = numbers[2]
	t_to_sleep = numbers[3]
	if minutes_needed <= first_alarm:
		answers[test] = first_alarm
	elif snooze <= t_to_sleep:
		answers[test] = -1
	else:
		minutes_needed = minutes_needed - first_alarm
		actual_sleep_time = snooze - t_to_sleep
		remaining_times = minutes_needed // actual_sleep_time
		if minutes_needed % actual_sleep_time != 0:
			remaining_times += 1
		answers[test] = remaining_times * snooze + first_alarm
for answer in answers:
	print(answer)


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)
	elif c <= d:
		print(-1)
	else:
		cnt = (a - b - 1) // (c - d) + 1
		print(b + cnt * c)


t = int(input())
for i in range(t):
	(r, s, n, fs) = map(int, input().split())
	rs = r - s
	ans = s
	if rs <= 0:
		print(s)
	elif n - fs <= 0:
		print('-1')
	else:
		p = n - fs
		pp = rs // p
		rr = rs % p
		if rr == 0:
			ans = s + pp * n
		else:
			ans = s + (pp + 1) * n
		print(ans)


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		if (a - b) % (c - d) == 0:
			n = (a - b) // (c - d)
		else:
			n = (a - b) // (c - d) + 1
		print(b + c * n)


import math
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	a = a - b
	if a <= 0:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	ev = c - d
	co = math.ceil(a / ev)
	print(co * ev + co * d + b)


import io, os
R = lambda : map(int, input().split())
for _ in range(int(input())):
	(a, b, c, d) = R()
	a -= b
	if a <= 0:
		print(b)
	else:
		if d >= c:
			print(-1)
			continue
		ans = b
		ans += (a + c - d - 1) // (c - d) * c
		print(ans)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
from io import BytesIO
import threading
import bisect
import heapq
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = sys.stdin.readline
for t in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a > b and c <= d:
		print(-1)
		continue
	elif a <= b:
		print(b)
		continue
	r = (a - b) // (c - d)
	r += 1 if (a - b) % (c - d) != 0 else 0
	print(r * c + b)


from sys import stdin, stdout
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		a -= b
		if d >= c:
			print(-1)
		else:
			print(b + ((a - 1) // (c - d) + 1) * c)


from math import ceil
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	time = 0
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	else:
		val1 = a - b
		val2 = c - d
		val = ceil(val1 / val2)
		time = b + val * c
		print(time)


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	elif d >= c:
		print(-1)
	else:
		ans = b + ceil((a - b) / (c - d)) * c
		print(ans)


import sys
import math

def main():
	input_list = []
	for line in sys.stdin:
		input_list.append(line.rstrip('\n'))
	for line in input_list[1:]:
		print(alarm(line.split(' ')))

def alarm(input_list):
	input_list = [int(x) for x in input_list]
	a = input_list[0]
	b = input_list[1]
	c = input_list[2]
	d = input_list[3]
	if b >= a:
		return b
	if c - d == 0:
		return -1
	x = (a - b) / (c - d)
	if x < 0:
		return -1
	return math.ceil(x) * c + b
main()


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


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


t = int(input())
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	total = 0
	if a > b:
		a = a - b
		total += b
		if d >= c:
			total = -1
		else:
			import math
			x = math.ceil(a / (c - d))
			total += x * c
	else:
		total = b
	print(total)


for s in [*open(0)][1:]:
	(a, b, c, d) = map(int, s.split())
	print(b + (a > b) * (c > d and 0 - (b - a) // (c - d) * c or -b - 1))


from math import ceil
for i in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	if a > b and c <= d:
		print(-1)
	elif a <= b:
		print(b)
	else:
		t = b
		a -= b
		t += ceil(a / (c - d)) * c
		print(t)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def solve(t):
	(a, b, c, d) = t
	time = b
	sleep = b
	if sleep >= a:
		print(b)
		return
	sleep_added = c - d
	if sleep_added <= 0:
		print(-1)
		return
	cycles = (a - b) // sleep_added
	if (a - b) % sleep_added > 0:
		cycles += 1
	time += cycles * c
	print(time)
T = inp()
tests = []
for _ in range(T):
	tests.append(inlt())
for t in tests:
	solve(t)


from sys import setrecursionlimit
from collections import deque
import math
import heapq
import functools
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	a -= b
	if a <= 0:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b
		ans += c * math.ceil(a / (c - d))
		print(ans)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c - d <= 0:
		print('-1')
	else:
		ans = b + int(math.ceil((a - b) / (c - d))) * c
		print(ans)


import math
t = int(input())
for i in range(t):
	ii = input().split(' ')
	ii = [int(iii) for iii in ii]
	a = ii[0]
	b = ii[1]
	c = ii[2]
	d = ii[3]
	if a <= b:
		print(b)
		continue
	if d >= c:
		print('-1')
		continue
	time_taken = b + c * math.ceil((a - b) / (c - d))
	print(time_taken)


import sys
input = iter(sys.stdin.read().splitlines()).__next__
sys.setrecursionlimit(10000)

def solve(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	rest = c - d
	rem = a - b
	return b + (rem + rest - 1) // rest * c
TC = int(input())
for tc in range(TC):
	(a, b, c, d) = map(int, input().split())
	res = solve(a, b, c, d)
	print(res)


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
T = ni()
for _ in range(T):
	solve()


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()
from math import ceil

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


t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	res = b
	a -= b
	if a <= 0:
		print(b)
	elif c <= d:
		print(-1)
	else:
		diff = c - d
		(dd, rr) = divmod(a, diff)
		res += dd * c + bool(rr) * c
		print(res)


import math as m
import itertools as itr
import sys
from math import ceil

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


def solution(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	if (a - b) % (c - d) == 0:
		time_slept = (a - b) // (c - d)
	else:
		time_slept = (a - b) // (c - d) + 1
	sum = b + time_slept * c
	return sum
tc = int(input())
for i in range(tc):
	lst = list(map(int, input().split()))
	print(solution(lst[0], lst[1], lst[2], lst[3]))


for _ in range(int(input())):
	a = list(map(int, input().split()))
	if a[0] <= a[1]:
		print(a[1])
		continue
	if a[2] <= a[3]:
		print(-1)
		continue
	m = a[0] - a[1]
	t = 0 - -m // (a[2] - a[3])
	print(a[1] + t * a[2])


from math import ceil

def main():
	t = int(input())
	for __ in range(t):
		(a, b, c, d) = map(int, input().split())
		if a <= b:
			print(b)
		elif c > d:
			x = ceil((a - b) / (c - d))
			print(b + x * c)
		else:
			print(-1)
main()


import math

def func(a, b, c, d):
	if a <= b:
		return b
	elif c <= d:
		return -1
	else:
		k = math.ceil((a - b) / (c - d))
		return int(k * c + b)
n = int(input())
while n:
	(a, b, c, d) = map(int, input().split())
	print(func(a, b, c, d))
	n -= 1


from math import *

def alarm():
	t = int(input())
	for _ in range(t):
		(a, b, c, d) = map(int, input().split())
		if b >= a:
			print(b)
		else:
			x = c - d
			y = a - b
			if x > 0:
				rep = ceil(y / x)
				ans = rep * c + b
				print(ans)
			else:
				print(-1)
alarm()


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


import sys
input = sys.stdin.readline
for f in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	time = b
	a -= b
	if a <= 0:
		print(time)
	elif d >= c:
		print(-1)
	else:
		foo = a // (c - d)
		a -= foo * (c - d)
		time += foo * c
		if a > 0:
			time += c
		print(time)


import sys
import math

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	(a, b, c, d) = get_ints()
	if b >= a:
		print(b)
		continue
	elif d >= c:
		print(-1)
		continue
	else:
		m = a - b
		n = c - d
		p = math.ceil(m / n)
		ans = b + p * c
		print(ans)


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	ans = 0
	a -= b
	ans += b
	if a > 0 and c > d:
		x = math.ceil(a / (c - d))
		ans += c * x
		print(ans)
	elif a <= 0:
		print(ans)
	elif a > 0 and c <= d:
		print(-1)


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
t = 1
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, stdin.readline().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b
		rem = a - b
		cycleval = c - d
		number = math.ceil(rem / cycleval)
		ans += number * c
		print(ans)


t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	elif d >= c:
		print(-1)
		continue
	else:
		to_sleep = a - b
		per_cycle = c - d
		cycles = to_sleep // per_cycle
		to_sleep = to_sleep - cycles * (c - d)
		if to_sleep != 0:
			final = b + (cycles + 1) * c
		else:
			final = b + cycles * c
	print(final)


import math

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n
for _ in range(ii()):
	(a, b, c, d) = mi()
	j = 0
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	else:
		j = math.ceil((a - b) / (c - d))
		print(b + j * c)


from sys import stdin, stdout
from sys import maxsize
from math import ceil

def solve():
	pass
test = 1
test = int(input())
for t in range(0, test):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
	else:
		rest = a - b
		try:
			n = ceil(rest / (c - d))
		except:
			n = -1
		if n <= 0:
			print(-1)
		else:
			print(b + n * c)
	ans = solve()


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


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


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


import math

def calculate_time(values):
	(a, b, c, d) = values
	if b >= a:
		return b
	else:
		left_time = a - b
		if d >= c:
			return -1
		else:
			sleep_time = c - d
			return c * math.ceil(left_time / sleep_time) + b
t = int(input())
for i in range(t):
	values = map(int, input().split(' '))
	ret = calculate_time(values)
	print(ret)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(a, b, c, d) = map(int, stdin.readline().split())
	if b >= a:
		stdout.write(str(b) + '\n')
	elif c <= d:
		stdout.write('-1\n')
	else:
		ans = b
		rem = a - b
		if rem % (c - d) == 0:
			ans += c * (rem // (c - d))
		else:
			ans += c * (rem // (c - d) + 1)
		stdout.write(str(ans) + '\n')


from math import ceil
for n in range(int(input())):
	(x, y, a, b) = map(int, input().split())
	if x <= y:
		print(y)
		continue
	if a <= b:
		print(-1)
		continue
	req = ceil((x - y) / (a - b))
	print(y + req * a)


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


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	dt = max(c - d, 0)
	if b >= a:
		print(b)
	elif b < a and dt == 0:
		print(-1)
	else:
		dt = 0 if dt == 0 else math.ceil((a - b) / dt)
		print(b + c * dt)


import math
t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		left = a - b
		gap = c - d
		e = math.ceil(left / gap)
		print(b + e * c)


import math
t = int(input())
while t > 0:
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		time = c - d
		if time <= 0:
			print('-1')
		else:
			left = a - b
			fac = math.ceil(left / time) * c
			print(fac + b)
	t -= 1


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ans = 0
	if a > b and d >= c:
		ans = -1
	elif a > b:
		ans += b
		a -= b
		temp1 = a % (c - d)
		if temp1 > 0:
			temp1 = c - d - temp1
		temp1 = (a + temp1) // (c - d)
		ans += temp1 * c
	else:
		ans = b
	print(ans)


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:
		a = a - b
		if a % (c - d) == 0:
			x = a // (c - d)
		else:
			x = a // (c - d) + 1
		print(b + x * c)


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


t = int(input())
while t != 0:
	(a, b, c, d) = map(int, input().split())
	if a - b < 0:
		print(b)
	if a - b > 0 and c - d < 0:
		print(-1)
	if a - b == 0:
		print(b)
	if a - b > 0 and c - d == 0:
		print(-1)
	if a - b > 0 and c - d > 0:
		x = a - b
		y = c - d
		count = 0
		z = int(x / y)
		if x % y > 0:
			z = z + 1
		count = count + z
		print(b + count * c)
	t = t - 1


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	a -= b
	e = c - d
	if e <= 0 and a > 0:
		print(-1)
		continue
	if a % e == 0:
		s = a // e
	else:
		s = a // e + 1
	print(b + c * s)


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


import math

def sol(a, b, c, d):
	if b < a and d >= c:
		return -1
	elif a <= b:
		return b
	else:
		s = c - d
		r = a - b
		n = int(math.ceil(r / s))
		return b + n * c
t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	v = sol(a, b, c, d)
	print(v)


import sys
import math
from collections import defaultdict, deque
import heapq
t = int(sys.stdin.readline())
for _ in range(t):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	if a <= b:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	time = a - b
	delta = c - d
	ans = b + math.ceil(time / delta) * c
	print(ans)


import math

def main():
	t = int(input())
	for _ in range(t):
		print(case())

def case():
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		return b
	if c <= d:
		return -1
	if (a - b) % (c - d):
		return b + (a - b) // (c - d) * c + c
	else:
		return b + (a - b) // (c - d) * c
main()


import math

def solve(a, b, c, d):
	if a <= b:
		return b
	elif c <= d:
		return -1
	else:
		sleep_needed = a - b
		inter_alarm_sleep = c - d
		inter_alarm_segments_needed = math.ceil(float(sleep_needed) / float(inter_alarm_sleep))
		return int(inter_alarm_segments_needed * c) + b
t = int(input())
for _ in range(0, t):
	(a, b, c, d) = map(int, input().split(' '))
	print(solve(a, b, c, d))


import math
W = int(input())
for i in range(W):
	Z = input()
	U = Z.split()
	for o in range(4):
		U[o] = int(U[o])
	a = U[0]
	b = U[1]
	c = U[2]
	d = U[3]
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		k = (a - b) / (c - d)
		Y = math.ceil(k)
		X = b + Y * c
		print(X)


import math

def sol():
	t = int(input())
	for _ in range(t):
		(a, b, c, d) = map(int, input().split())
		if b >= a:
			print(b)
			continue
		slp = a - b
		p = c - d
		if p <= 0:
			print(-1)
			continue
		ans = math.ceil(slp / p) * c + b
		print(ans)
sol()


import math

def shrey():
	(a, b, c, d) = map(int, input().split())
	x = a - b
	y = c - d
	if x <= 0:
		print(b)
	elif y <= 0:
		print(-1)
	else:
		p = math.ceil(x / y)
		ans = p * c + b
		print(ans)
for _ in range(int(input())):
	shrey()


def getTime(a, b, c, d):
	if b >= a:
		return b
	if b < a and c <= d:
		return -1
	req = a - b
	eachStep = c - d
	k = req // eachStep
	if req % eachStep != 0:
		k = k + 1
	return b + k * c
n = int(input())
for i in range(0, n):
	(a, b, c, d) = [int(x) for x in input().split()]
	print(getTime(a, b, c, d))


import sys

def solve(a, b, c, d):
	rem = a - b
	if rem < 0:
		return b
	if d >= c:
		if b < a:
			return -1
		else:
			return b
	else:
		each = c - d
		nsleeps = rem // each + (1 if rem % each > 0 else 0)
		t_elapsed = b + nsleeps * c
		return t_elapsed
IN = [x.strip() for x in sys.stdin.readlines()]
T = int(IN[0])
cur = 1
for ti in range(T):
	(a, b, c, d) = [int(x) for x in IN[cur].split(' ')]
	result = solve(a, b, c, d)
	print(result)
	cur += 1


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

def parse():
	v = list(map(int, input().split()))
	return (v[0], v[1], v[2], v[3])

def calc(a, b, c, d):
	if a <= b:
		return b
	if c - d <= 0:
		return -1
	x = math.ceil((a - b) / (c - d))
	return b + x * c
T = int(input())
for i in range(0, T):
	(a, b, c, d) = parse()
	print(calc(a, b, c, d))


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	(a, b, c, d) = MAP()
	need = a
	need -= b
	if need <= 0:
		print(b)
		continue
	each = c - d
	if each <= 0:
		print(-1)
		continue
	cnt = ceil(need, each)
	ans = b + c * cnt
	print(ans)


t = int(input())
while t:
	(total, sleep, c, d) = map(int, input().split())
	ans = 0
	ans += sleep
	if total > sleep and d >= c:
		print(-1)
	elif sleep >= total:
		print(sleep)
	else:
		total -= sleep
		ans += (total - 1 + (c - d)) // (c - d) * c
		print(ans)
	t -= 1


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


t = int(input())
for i in range(0, t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if c <= d:
		print('-1')
		continue
	n = a - b
	if c > d:
		if n % (c - d) != 0:
			alarm_times = int(n / (c - d))
			print(c * alarm_times + c + b)
		else:
			alarm_times = int(n / (c - d))
			print(c * alarm_times + b)


from sys import stdin
import math

def readline():
	return stdin.readline()
tests = int(readline())

def solve(a, b, c, d):
	if b >= a:
		return b
	if c == d:
		m = 0
	else:
		m = math.ceil((a - b) / (c - d))
	answer = b + c * m
	if answer < a:
		return -1
	return answer
for t in range(0, tests):
	[a, b, c, d] = list(map(int, readline().split(' ')))
	print(solve(a, b, c, d))


def proba(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	s = b
	if (a - b) // (c - d) == (a - b) / (c - d):
		return s + (a - b) // (c - d) * c
	return s + ((a - b) // (c - d) + 1) * c
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	print(proba(a, b, c, d))


import math

def solve():
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		return
	if c <= d:
		print('-1')
		return
	val = c - d
	dv = (a - b) // val
	if (a - b) % val != 0:
		dv += 1
	ans = b + dv * c
	print(ans)
	return
t = 1
t = int(input())
while t > 0:
	t -= 1
	solve()


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


import math
for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	if c <= d and a > b:
		print(-1)
	elif b >= a:
		print(b)
	elif b < a:
		value = a - b
		mul = int(math.ceil(value / (c - d)))
		ans = b + mul * c
		print(ans)


def main():
	import sys
	sys.setrecursionlimit(10 ** 6)
	input = sys.stdin.readline
	T = int(input())
	for t in range(T):
		(a, b, c, d) = [int(x) for x in input().strip().split()]
		a -= b
		if a <= 0:
			print(b)
			continue
		if c <= d:
			print(-1)
			continue
		print(b + a // (c - d) * c + c * (a % (c - d) > 0))
main()


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


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if b < a and c <= d:
		print(-1)
		continue
	res = b
	wait = math.ceil((a - b) / (c - d))
	res += wait * c
	print(res)


import math
import sys
from os import path
if path.exists('C:/Users/prana/Desktop/sublime/input.txt'):
	sys.stdin = open('C:/Users/prana/Desktop/sublime/input.txt', 'r')
	sys.stdout = open('C:/Users/prana/Desktop/sublime/output.txt', 'w')

def solve():
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if a > b and c <= d:
		print(-1)
	elif a <= b:
		print(b)
	else:
		temp = math.ceil((a - b) / (c - d))
		print(temp, file=sys.stderr)
		print(b + temp * c)
T = int(input())
for _ in range(1, T + 1):
	print('Case #', _, file=sys.stderr)
	solve()


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	sleep = a - b
	netsleep = c - d
	count = 0
	if a <= b:
		print(b)
		continue
	elif d >= c:
		print(-1)
		continue
	else:
		r = math.ceil(sleep / netsleep)
		print(b + r * c)


from math import *
t = int(input())
for i in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
	elif a > b and d - c >= 0:
		print(-1)
	else:
		INITIAL = b
		DELTA = c - d
		FINAL = a
		TOTAL = a - b
		x = ceil(TOTAL / DELTA)
		print(b + x * c)


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	sr = a - b
	se = c - d
	if sr < 1:
		print(b)
	elif se < 1:
		print(-1)
	else:
		t = ceil(sr / se)
		print(b + t * c)


import math

def sol():
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		return b
	if d >= c:
		return -1
	return b + math.ceil((a - b) / (c - d)) * c
T = int(input())
for t in range(1, T + 1):
	print(sol())


import math
for _ in range(int(input())):
	L = list(map(int, input().split()))
	if L[0] > L[1] and L[3] >= L[2]:
		print('-1')
	elif L[1] >= L[0]:
		print(L[1])
	else:
		print(L[1] + math.ceil((L[0] - L[1]) / (L[2] - L[3])) * L[2])


import math

def leaveBed(a, b, c, d):
	if b >= a:
		return b
	else:
		if d >= c:
			return -1
		inc = c - d
		chunks = math.ceil((a - b) / inc)
		return chunks * c + b

def main():
	cases = int(input())
	for i in range(cases):
		(a, b, c, d) = map(int, input().split())
		print(leaveBed(a, b, c, d))
main()


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c > d:
		ans = b
		x = a - b
		y = c - d
		ans += c * (x // y)
		if x % y != 0:
			ans += c
		print(ans)
	else:
		print(-1)


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


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)


for _ in range(int(input())):
	(a, b, c, d) = 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())
k = t
ae = list()
ans = list()
while k > 0:
	str1 = input()
	ae = str1.split()
	a = int(ae[0])
	b = int(ae[1])
	c = int(ae[2])
	d = int(ae[3])
	if a <= b:
		ans.append(b)
	elif d >= c:
		ans.append(-1)
	else:
		a = a - b
		q = c - d
		if a % q == 0:
			r = int(a / q)
		else:
			r = int(a / q) + 1
		ans.append(b + r * c)
	k = k - 1
for y in ans:
	print(y)


import math
for tt in range(int(input())):
	(a, b, c, d) = input().split()
	(a, b, c, d) = (int(a), int(b), int(c), int(d))
	if b > a or a == b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		print(b + c * math.ceil((a - b) / (c - d)))


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(a, b, c, d) = iia()
	if a > b and d >= c:
		print(-1)
	elif b >= a:
		print(b)
	else:
		res = b + c * ceil((a - b) / (c - d))
		print(res)


tc = int(input())
for _ in range(tc):
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		sleep_left = a - b
		diff = c - d
		multiple = sleep_left // diff if sleep_left % diff == 0 else sleep_left // diff + 1
		total = b + multiple * c
		print(total)


kl = int(input())
for l in range(kl):
	(a, b, c, d) = map(int, input().split())
	rz = 0
	for i in range(1):
		if b >= a:
			rz = b
			break
		if d >= c:
			rz = -1
			break
		t = (a - b + c - d - 1) // (c - d)
		rz = b + t * c
	print(rz)


t = int(input())
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	if a <= b:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	print(b + c * ((a - b - 1) // (c - d) + 1))


t = int(input())

def ceiled(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1
while t > 0:
	(a, b, c, d) = [int(x) for x in input().split()]
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = ceiled(a - b, c - d)
		s = x * c + b
		print(s)
	t = t - 1


import sys
input = sys.stdin.buffer.readline

def solution():
	for _ in range(int(input())):
		(a, b, c, d) = map(int, input().split())
		if a <= b:
			print(b)
		elif d >= c:
			print(-1)
		else:
			x = a - b
			z = x // (c - d)
			if x % (c - d) != 0:
				z += 1
			print(b + c * z)
solution()


import math

def funk(a, b, c, d):
	if b >= a:
		return b
	elif d >= c:
		return -1
	else:
		return math.ceil((a - b) / (c - d)) * c + b
count = int(input())
input_l = []
for i in range(count):
	tmp = input()
	input_l.append([int(x) for x in tmp.split()])
for i in input_l:
	print(funk(*i))


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))
for _ in range(I()):
	(a, b, c, d) = M()
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		x = a - b
		if x % (c - d) == 0:
			print(b + c * (x // (c - d)))
		else:
			print(b + c * (x // (c - d) + 1))


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


from sys import stdin
t = int(stdin.readline())
while t > 0:
	(a, b, c, d) = [int(x) for x in stdin.readline().split()]
	ans = b
	a1 = a
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		a1 -= b
		diff = c - d
		q = a1 // diff
		if a1 % diff == 0:
			print(q * c + ans)
		else:
			print(q * c + ans + c)
	t -= 1


t = int(input())
mul = 0
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	req = a - b
	eff = c - d
	if req == 0 or req < 0:
		print(b)
	elif eff == 0 or eff < 0:
		print(-1)
	else:
		q = req // eff
		remi = req % eff
		if remi == 0:
			mul = q
			print(b + mul * c)
		else:
			q = q + 1
			mul = q
			print(b + mul * c)
	mul = 0


t = int(input())
import math

def get_time_rec(a, b, c, d):
	if b < 0:
		return -1
	if a <= b:
		return b
	if c - d < 0:
		return -1
	return d + b + get_time_rec(a - b, c - d, c, d)
for i in range(t):
	(a, b, c, d) = tuple(map(int, input().split()))
	time_spent = b
	a = a - b
	b = c - d
	if a > 0:
		if b <= 0:
			time_spent = -1
		else:
			time_spent += c * math.ceil(a / b)
	print(time_spent)


def solve():
	(a, b, c, d) = [int(s) for s in input().split()]
	if b >= a:
		print(b)
		return
	if c <= d:
		print(-1)
		return
	diff = c - d
	addition = 1
	if (a - b) % diff == 0:
		addition = 0
	time = b + ((a - b) // diff + addition) * c
	print(time)

def main():
	t = int(input())
	for i in range(t):
		solve()
main()


import sys
import math
import operator as op
from collections import defaultdict
from functools import reduce

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def read_float_line():
	return [float(v) for v in sys.stdin.readline().split()]

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer / denom

def rad(x):
	return math.pi * x / 180
t = read_int()
for i in range(t):
	(a, b, c, d) = read_int_line()
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		t = (a - b) // (c - d)
		r = (a - b) % (c - d)
		if r != 0:
			t += 1
		ans = b + c * t
		print(ans)


testCase = int(input())
for i in range(testCase):
	(a, b, c, d) = input().split()
	res = int(b)
	timeLeft = int(a) - int(b)
	if timeLeft <= 0:
		print(res)
	elif int(d) >= int(c) and int(b) < int(a):
		print(-1)
	else:
		if c != d:
			val = timeLeft // (int(c) - int(d))
			timeLeft -= val * (int(c) - int(d))
			res += val * int(c)
		while timeLeft > 0:
			timeLeft -= int(c) - int(d)
			res += int(c)
		print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b + math.ceil((a - b) / (c - d)) * c
		print(ans)


def findWakeTime(a, b, c, d):
	curr = b
	left = a - b
	if left <= 0:
		return curr
	elif d >= c:
		return -1
	sleepAfterSnooze = c - d
	snoozeNeeded = left // sleepAfterSnooze
	if left % sleepAfterSnooze != 0:
		snoozeNeeded += 1
	return curr + snoozeNeeded * c
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	print(findWakeTime(a, b, c, d))


n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		f = (a - b) % (c - d)
		if f != 0:
			f = 1
		else:
			f = 0
		g = b + (int((a - b) / (c - d)) + f) * c
		print(g)


import math
ans = []
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		ans += [b]
	else:
		time = a - b
		if c <= d:
			ans += [-1]
		else:
			t = math.ceil((a - b) / (c - d))
			ans += [t * c + b]
for an in ans:
	print(an)


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


from sys import stdin, stdout
import math

def main():
	t = int(input())
	for i in range(t):
		(a, b, c, d) = [int(i) for i in input().split()]
		rest = a - b
		if rest <= 0:
			print(b)
			continue
		if d >= c:
			print(-1)
			continue
		milti = math.ceil(rest / (c - d))
		ans = milti * c
		print(ans + b)
main()


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ans = -1
	if a <= b:
		ans = b
	elif d >= c:
		ans = -1
	else:
		ans = b
		t = c - d
		tt = math.ceil((a - b) / t)
		ans = ans + c * tt
	print(ans)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	else:
		e = c - d
		f = a - b
		g = math.ceil(f / e)
		print(b + g * c)


import math
for _ in [0] * int(input()):
	(a, b, c, d) = map(int, input().split())
	total = b
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		total = b + math.ceil((a - b) / (c - d)) * c
		print(total)


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


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)


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	ntest = int(stdin.readline())
	for testcase in range(ntest):
		(a, b, c, d) = [int(x) for x in stdin.readline().split()]
		res = -1
		e = c - d
		if b >= a:
			res = b
		elif e <= 0:
			res = -1
		else:
			need = (a - b + (e - 1)) // e
			res = b + need * max(c, d)
		stdout.write('%d\n' % res)
	return 0
main()


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


from sys import stdin

def main():
	input = lambda : stdin.readline()[:-1]
	T = int(input())
	for _ in [0] * T:
		(A, B, C, D) = map(int, input().split())
		if A <= B:
			print(B)
			continue
		elif C <= D:
			print(-1)
			continue
		A -= B
		x = C - D
		n = (A + x - 1) // x
		print(B + C * n)
main()


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


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


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	rest = b
	total = b
	gap = c - d
	if b >= a:
		print(b)
	elif gap <= 0:
		print(-1)
	else:
		i = math.ceil((a - b) / gap)
		total = total + i * c
		print(total)


t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	sleep = 0
	rem = a
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		sleep += b
		rem = a - sleep
		each = c - d
		times = int(rem / each)
		if rem % each != 0:
			times += 1
		sleep += times * c
		print(sleep)


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


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	a -= b
	now = b
	if c <= d:
		print(-1)
		continue
	q = a // (c - d)
	now += q * c
	a %= c - d
	if a == 0:
		print(now)
	else:
		print(now + c)


t = int(input())
for i in range(t):
	arr = list(map(int, input().split()))
	if arr[0] <= arr[1]:
		print(arr[1])
	else:
		diff = arr[0] - arr[1]
		if arr[2] <= arr[3]:
			print('-1')
		else:
			diff2 = arr[2] - arr[3]
			if diff % diff2 == 0:
				times = diff // diff2
			else:
				times = diff // diff2 + 1
			print(arr[1] + arr[2] * times)


ans = []
for _ in range(int(input())):
	(a, b, c, d) = 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)))


import math
t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	if a <= b:
		print(b)
	elif c <= d:
		print('-1')
	else:
		x = b + c * math.ceil((a - b) / (c - d))
		print(x)


import sys

def i():
	return sys.stdin.readline()[:-1]
cases = int(i())
for x in range(cases):
	(a, b, c, d) = map(int, i().split())
	ans = b
	if a <= b:
		print(ans)
	else:
		if c <= d:
			ans = -1
		else:
			ans += c * ((a - b + c - d - 1) // (c - d))
		print(ans)


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


import math
for t in range(int(input())):
	[a, b, c, d] = list(map(int, input().split()))
	if a - b <= 0:
		print(b)
	else:
		sleep_needed = a - b
		sleep_received_per = c - d
		if c - d <= 0:
			print(-1)
		else:
			num_loop = int(math.ceil((a - b) / (c - d)))
			print(b + c * num_loop)


a = int(input())
for i in range(a):
	t = 0
	b = list(map(int, input().split()))
	delta = b[2] - b[3]
	if delta <= 0:
		if b[1] >= b[0]:
			print(b[1])
		else:
			print(-1)
	elif b[1] >= b[0]:
		print(b[1])
	else:
		t += b[1]
		b[0] -= b[1]
		c = b[0] // delta
		if delta * c < b[0]:
			c += 1
		t += c * b[2]
		print(t)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	(a, b, c, d) = sp()
	answer = b
	if answer >= a:
		out(answer)
		continue
	if c <= d:
		out(-1)
		continue
	temp = ceil((a - answer) / abs(c - d))
	out(answer + temp * c)


import math
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif b < a and c > d:
		temp = c - d
		ans = int(math.ceil((a - b) / temp))
		print(b + ans * c)
	elif b < a and c <= d:
		print(-1)


import sys, math, bisect
from collections import Counter, defaultdict as dd

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def l():
	return list(sp())

def strl():
	return list(strsp())

def sp():
	return map(int, data().split())

def strsp():
	return map(str, data().split())
for _ in range(int(data())):
	(a, b, c, d) = l()
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		g = math.ceil((a - b) / (c - d))
		print(g * c + b)


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


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if b < a:
		if d >= c:
			print(-1)
		else:
			som = b
			x = a - b
			y = abs(c - d)
			z = math.ceil(x / y)
			som = som + z * c
			print(som)


import math
t = int(input())
while t:
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		print(b)
	elif a > b and d >= c:
		print('-1')
	else:
		ans = b
		rest = a - b
		e = int(math.ceil(rest / (c - d)))
		print(ans + c * e)
	t -= 1


import math
import functools

def s():
	(a, b, c, d) = list(map(int, input().split(' ')))
	leftover = a - b
	if leftover <= 0:
		print(b)
	elif c <= d:
		print(-1)
	else:
		ans = b
		temp = c - d
		multiplier = int(math.ceil(leftover / temp))
		ans += multiplier * c
		print(ans)
t = int(input())
for i in range(t):
	s()


import math
answers = list()
t = int(input(''))
for i in range(t):
	(a, b, c, d) = list(map(int, input('').split(' ')))
	a_min_b = a - b
	c_min_d = c - d
	if b >= a:
		answers.append(b)
	elif c_min_d <= 0:
		answers.append(-1)
	else:
		answers.append(b + c * math.ceil(a_min_b / c_min_d))
for a in answers:
	print(a)


t = int(input())
while t > 0:
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		t -= 1
		continue
	if c <= d:
		print(-1)
		t -= 1
		continue
	left = a - b
	tib = b
	if left % (c - d) != 0:
		tib += (int(left / (c - d)) + 1) * c
	else:
		tib += int(left / (c - d)) * c
	print(tib)
	t -= 1


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	elif c <= d:
		print(-1)
		continue
	else:
		times = math.ceil((a - b) / (c - d))
		print(b + times * c)
		continue


import sys
import heapq

def main():
	t = int(input())
	for _ in range(t):
		(a, b, c, d) = map(int, sys.stdin.readline().split())
		a -= b
		ans = 0
		ans += b
		if a > 0 and c <= d:
			print(-1)
			continue
		if a > 0:
			if a % (c - d):
				ans += c * (a // (c - d) + 1)
			else:
				ans += c * (a // (c - d))
		print(ans)
	return
main()


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


import math as m
try:
	t = int(input())
	for i in range(t):
		a1 = input().split()
		a = int(a1[0])
		b = int(a1[1])
		c = int(a1[2])
		d = int(a1[3])
		if a <= b:
			print(b)
		elif d >= c:
			print(-1)
		else:
			diff1 = a - b
			diff2 = c - d
			s = int(c * m.ceil(diff1 / diff2))
			print(s + b)
except EOFError:
	exit(0)


from math import ceil
t = int(input())
for i in range(t):
	a_b_c_d = input()
	a_b_c_d = [int(i) for i in a_b_c_d.split()]
	(a, b, c, d) = a_b_c_d
	if b < a and c <= d:
		print(-1)
	else:
		if c != d:
			time_total = b + ceil((a - b) / (c - d)) * c * (a > b)
		else:
			time_total = b
		print(time_total)


for i in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split()]
	time_left = a - b
	if time_left <= 0:
		print(b)
		continue
	pos_sleep = c - d
	if pos_sleep <= 0:
		print(-1)
		continue
	factor = time_left // pos_sleep
	if pos_sleep * factor < time_left:
		factor += 1
	total_time = b + c * factor
	print(total_time)


from math import *
for u in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = a - b
		y = ceil(x / abs(c - d))
		print(b + y * c)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7
from bisect import bisect_left

def solve():
	(A, B, C, D) = getInts()
	if A <= B:
		return B
	if D >= C:
		return -1
	return B + C * math.ceil((A - B) / (C - D))
for _ in range(getInt()):
	print(solve())


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif a > b and d >= c:
		print(-1)
	else:
		x = a - b
		y = c - d
		if y >= x:
			print(b + c)
		else:
			z = x // y
			if x % y == 0:
				print(z * c + b)
			else:
				j = z + 1
				print(b + j * c)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	if b >= a:
		print(b)
	else:
		extra_time = a - b
		if c <= d:
			print(-1)
		else:
			unit_sleep = c - d
			print(b + c * math.ceil(extra_time / unit_sleep))


import math
total_case = int(input())
for i in range(total_case):
	aline = input().split()
	(a, b, c, d) = [int(x) for x in aline]
	if b >= a:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		rate = c - d
		n = int(math.ceil((a - b) / rate))
		print(b + n * c)


import math

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

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

def solve():
	(a, b, c, d) = read_ints()
	if b >= a:
		return b
	a -= b
	if c <= d:
		return -1
	return b + math.ceil(a / (c - d)) * c
T = read_int()
for _ in range(T):
	print(solve())


import math

def solve():
	(a, b, c, d) = list(map(int, input().split()))
	ans = 0
	if b >= a:
		ans = b
	else:
		more = a - b
		if d >= c:
			ans = -1
		else:
			every_time = c - d
			needed = math.ceil(more / every_time)
			ans = b + needed * c
	return ans
t = int(input())
while t:
	t -= 1
	print(solve())


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		res = a - b
		x = a - b
		y = c - d
		z = (a - b) // (c - d)
		if (a - b) % (c - d) != 0:
			z += 1
		res = b + z * c
		print(res)


def solve():
	(a, b, c, d) = map(int, input().split())
	ans = b
	a -= b
	if a <= 0:
		print(b)
		return
	k = c - d
	if k <= 0:
		print(-1)
		return
	print(b + (a + k - 1) // k * c)
for i in range(int(input())):
	solve()


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


from math import ceil
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b < a and c <= d:
		print(-1)
	elif b >= a:
		print(b)
	else:
		time = b
		am = a - b
		time += ceil(am / (c - d)) * c
		print(time)


n = int(input())
for ti in range(0, n):
	(l, a, b, c) = map(int, input().split())
	if a < l and c >= b:
		print('-1')
	elif a >= l:
		print(a)
	else:
		sum = a
		l -= a
		if l % (b - c) == 0:
			sum += l // (b - c) * b
		else:
			sum += (l // (b - c) + 1) * b
		print(sum)


t = int(input())
while t > 0:
	(a, b, c, d) = [int(x) for x in input().split()]
	rem = a - b
	ct = min(a, b)
	if rem > 0:
		if d >= c:
			ct = -1
		else:
			dd = rem // (c - d)
			ct += dd * c
			ct += 0 if rem % (c - d) == 0 else c
	else:
		ct = b
	print(ct)
	t -= 1


import math

def fun(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	s = math.ceil((a - b) / (c - d))
	return b + c * s
t = int(input())
for _ in range(t):
	l = [int(x) for x in input().split()]
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	ans = fun(a, b, c, d)
	print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		(a, b, c, d) = MI()
		if a - b <= 0:
			print(b)
			continue
		if c - d <= 0:
			print(-1)
			continue
		t = (a - b + c - d - 1) // (c - d)
		print(b + t * c)
main()


t = int(input())
answer = []
for i in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	if b >= a:
		answer.append(b)
		continue
	if c <= d:
		answer.append(-1)
		continue
	time = b
	a -= b
	sleepTime = c - d
	time += c * (a // sleepTime)
	if a % sleepTime != 0:
		time += c
	answer.append(time)
for a in answer:
	print(a)


MOD = 10 ** 9 + 7
RLIMIT = 1000
DEBUG = 1

def main():
	for _ in inputt():
		(a, b, c, d) = inputi()
		if b >= a:
			print(b)
			continue
		if c <= d:
			print(-1)
			continue
		print(ceil((a - b) / (c - d)) * c + b)
from math import *
from heapq import *
from itertools import *
from functools import reduce, lru_cache, partial
from collections import Counter, defaultdict, deque
import re, copy, operator, cmath
import sys, io, os, builtins
sys.setrecursionlimit(RLIMIT)
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(io.IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	if args:
		sys.stdout.write(str(args[0]))
		split = kwargs.pop('split', ' ')
		for arg in args[1:]:
			sys.stdout.write(split)
			sys.stdout.write(str(arg))
	sys.stdout.write(kwargs.pop('end', '\n'))

def debug(*args, **kwargs):
	if DEBUG and (not __debug__):
		print('debug', *args, **kwargs)
		sys.stdout.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip()
inputt = lambda t=0: range(t) if t else range(int(input()))
inputs = lambda : input().split()
inputi = lambda k=int: map(k, inputs())
inputl = lambda t=0, k=lambda : list(inputi()): [k() for _ in range(t)] if t else list(k())

def len(a):
	if isinstance(a, range):
		return -((a.start - a.stop) // a.step)
	return builtins.len(a)

def bisect_left(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if key(a[mid]) < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def bisect_right(a, x, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	while lo < hi:
		mid = (lo + hi) // 2
		if x < key(a[mid]):
			hi = mid
		else:
			lo = mid + 1
	return lo

def insort_left(a, x, key=None, lo=0, hi=None):
	lo = bisect_left(a, x, key, lo, hi)
	a.insert(lo, x)

def insort_right(a, x, key=None, lo=0, hi=None):
	lo = bisect_right(a, x, key, lo, hi)
	a.insert(lo, x)
do_nothing = lambda x: x
bisect = bisect_right
insort = insort_right

def index(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	if key == None:
		key = do_nothing
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi and key(a[i]) == x:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_lt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_le(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo < i <= hi:
		return a[i - 1]
	if default != None:
		return default
	raise ValueError

def find_gt(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_right(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError

def find_ge(a, x, default=None, key=None, lo=0, hi=None):
	if lo < 0:
		lo = 0
	if hi == None:
		hi = len(a)
	i = bisect_left(a, x, key, lo, hi)
	if lo <= i < hi:
		return a[i]
	if default != None:
		return default
	raise ValueError

class Tree:

	def __init__(n):
		self._n = n
		self._conn = [[] for _ in range(n)]
		self._list = [0] * n

	def connect(a, b):
		pass

class Sieve:

	def __init__(self):
		self._n = 6
		self._list = [2, 3, 5, 7, 11, 13]

	def extend(self, n):
		if n <= self._list[-1]:
			return
		maxbase = int(n ** 0.5) + 1
		self.extend(maxbase)
		begin = self._list[-1] + 1
		newsieve = [i for i in range(begin, n + 1)]
		for p in self.primerange(2, maxbase):
			for i in range(-begin % p, len(newsieve), p):
				newsieve[i] = 0
		self._list.extend([x for x in newsieve if x])

	def extend_to_no(self, i):
		while len(self._list) < i:
			self.extend(int(self._list[-1] * 1.5))

	def primerange(self, a, b):
		a = max(2, a)
		if a >= b:
			return
		self.extend(b)
		i = self.search(a)[1]
		maxi = len(self._list) + 1
		while i < maxi:
			p = self._list[i - 1]
			if p < b:
				yield p
				i += 1
			else:
				return

	def search(self, n):
		if n < 2:
			raise ValueError
		if n > self._list[-1]:
			self.extend(n)
		b = bisect(self._list, n)
		if self._list[b - 1] == n:
			return (b, b)
		else:
			return (b, b + 1)

	def __contains__(self, n):
		if n < 2:
			raise ValueError
		if not n % 2:
			return n == 2
		(a, b) = self.search(n)
		return a == b

	def __getitem__(self, n):
		if isinstance(n, slice):
			if n.stop:
				self.extend_to_no(n.stop + 1)
				return self._list[n.start:n.stop:n.step]
			return islice(self, n.start, n.stop, n.step)
		else:
			self.extend_to_no(n + 1)
			return self._list[n]
sieve = Sieve()

def isprime(n):
	if n <= sieve._list[-1]:
		return n in sieve
	for i in sieve:
		if not n % i:
			return False
		if n < i * i:
			return True
prime = sieve.__getitem__
primerange = lambda a, b=0: sieve.primerange(a, b) if b else sieve.primerange(2, a)

def factorint(n):
	factors = []
	for i in sieve:
		if n < i * i:
			break
		while not n % i:
			factors.append(i)
			n //= i
	if n != 1:
		factors.append(n)
	return factors
factordict = lambda n: Counter(factorint(n))
main()


def foo(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	s = (a - b + (c - d) - 1) // (c - d)
	return b + s * c
t = int(input())
for gf in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	print(foo(a, b, c, d))


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


from sys import stdin, stdout
from math import ceil, floor
t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, stdin.readline().strip().split()))
	tottime = 0
	if b >= a:
		print(b)
	else:
		a = a - b
		tottime += b
		if d >= c:
			print(-1)
		else:
			tottime += ceil(a / (c - d)) * c
			print(tottime)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = list(input().split(' '))
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	else:
		n = math.ceil((a - b) / (c - d))
		print(b + n * c)


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


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	answer = 0
	if b >= a:
		print(b)
	elif b < a:
		a -= b
		if d >= c:
			print(-1)
		else:
			answer += b
			answer = answer + a // (c - d) * c
			if a % (c - d) != 0:
				answer += c
			print(answer)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ans = b
	if b >= a:
		print(b)
		continue
	if c <= d:
		if a > b:
			print(-1)
		elif a == b:
			print(a)
	else:
		if (a - b) % (c - d) == 0:
			div = (a - b) // (c - d)
		else:
			div = (a - b) // (c - d) + 1
		ans += div * c
		print(ans)


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


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


import sys
input = sys.stdin.readline
import heapq
import copy
import math

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def pr_list(a):
	print(*a, sep=' ')

def find_permut(hashm, l):
	ans = 1
	total = math.factorial(l)
	for i in hashm:
		if i != 0:
			ans = ans * math.factorial(i)
	return int(total / ans)

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		ans = 0
		[a, b, c, d] = inlt()
		if b >= a:
			ans = b
		else:
			sleep_l = a - b
			if d >= c:
				ans = -1
			else:
				ex_sl = c - d
				times = math.ceil(sleep_l / ex_sl)
				ans = c * times + b
		print(ans)
main()


t = int(input())
respostas = []
for x in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		respostas.append(b)
	elif c <= d:
		respostas.append(-1)
	else:
		tempo = b + (a - b) // (c - d) * c
		if (a - b) % (c - d) != 0:
			tempo = b + ((a - b) // (c - d) + 1) * c
			respostas.append(tempo)
		else:
			respostas.append(tempo)
for x in respostas:
	print(x)


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(A, B, C, D) = map(int, input().split())
	if A <= B:
		print(B)
	elif C <= D:
		print(-1)
	else:
		ans = B
		left = A - B
		ans += math.ceil(left / (C - D)) * C
		print(ans)


import math
t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	inc = c - d
	ans = 0
	res = a
	if a <= b:
		print(b)
	else:
		ans += b
		res -= b
		if inc <= 0:
			print(-1)
		elif res % inc == 0:
			print(ans + res // inc * c)
		else:
			print(ans + res // inc * c + c)


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


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


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


import math as mt

def time(nk):
	if nk[0] <= nk[1]:
		return nk[1]
	elif nk[0] > nk[1] and nk[2] <= nk[3]:
		return -1
	elif nk[0] > nk[1] and nk[2] > nk[3]:
		p = (nk[0] - nk[1]) / (nk[2] - nk[3])
		p = mt.ceil(p)
		return nk[1] + p * nk[2]
t = int(input())
ans = []
for i in range(t):
	nk = list(map(int, input().rstrip().split()))
	r = time(nk)
	ans.append(r)
for i in ans:
	print(i)


def solve(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	a -= b
	v = c - d
	t = (a + v - 1) // v
	return b + t * c
tests = int(input())
for test in range(tests):
	(a, b, c, d) = map(int, input().split())
	print(solve(a, b, c, d))


mod = int(1000000000.0) + 7

def ip():
	return int(input())

def Ip():
	return map(int, input().split())
for _ in range(0, int(input())):
	(a, b, c, d) = Ip()
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		print(b + ((a - b) // (c - d) + ((a - b) % (c - d) != 0)) * c)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		sleep_need = a - b
		can_sleep = c - d
		rep_times = math.ceil(sleep_need / can_sleep)
		total = b + rep_times * c
		print(total)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	sl = a - b
	k = 0
	reset = 0
	sleep = b
	if a <= b:
		print(b)
		continue
	elif a > b:
		if c > d:
			t_sleep = c - d
			turn = sl // t_sleep
			g = sl % t_sleep
			if g != 0:
				turn = turn + 1
		else:
			print(-1)
			continue
	print(b + c * turn)


def test(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	return (a - b + c - d - 1) // (c - d) * c + b
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	print(test(a, b, c, d))


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


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	(a, b, c, d) = get_tuple()
	if a <= b:
		print(b)
	elif a > b and c <= d:
		print(-1)
	else:
		times = math.ceil((a - b) / (c - d))
		print(b + c * times)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


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


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		co = b
		nu = 0
		if d >= c:
			print(-1)
		else:
			q = (a - b) // (c - d)
			r = (a - b) % (c - d)
			if r == 0:
				nu = q
			else:
				nu = q + 1
			ans = b + nu * c
			print(ans)


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	ans = b
	rem_t = a - b
	cycle_cnt = math.ceil(rem_t / (c - d))
	ans += cycle_cnt * c
	print(ans)


t = int(input())
for _ in range(t):
	(a, b, c, d) = [int(ipt) for ipt in input().split()]
	if a <= b:
		ret = b
	elif c <= d:
		ret = -1
	else:
		k = (a - b) // (c - d) + ((a - b) % (c - d) != 0)
		ret = b + k * c
	print(ret)


from collections import Counter
import math
t = int(input())
while t > 0:
	t = t - 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		print(math.ceil((a - b) / (c - d)) * c + b)


from math import ceil
from sys import stdin
input = iter(stdin.readlines()).__next__
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c > d:
		print(b + ceil((a - b) / (c - d)) * c)
	else:
		print(-1)


def solve():
	t = int(input())
	for i in range(t):
		(a, b, c, d) = list(map(int, input().split()))
		res = b
		ans = b
		if b >= a:
			print(b)
		elif b < a and c <= d:
			print(-1)
		else:
			tt = a - res
			if tt % (c - d) == 0:
				ans += c * (tt // (c - d))
			else:
				ans += c * (tt // (c - d) + 1)
			print(ans)
solve()


import sys
import math
input = sys.stdin.readline
for test in range(int(input())):
	(a, b, c, d) = [int(j) for j in input().split()]
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		result = b
		netsleep = c - d
		sleepleft = a - b
		timeleft = math.ceil(sleepleft / netsleep)
		result += timeleft * c
		print(result)


from math import ceil

def solve():
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		a -= b
		if d >= c:
			print(-1)
		else:
			c -= d
			r = ceil(a / c)
			print(b + r * (c + d))
t = int(input())
for test in range(t):
	solve()


import sys
import math
for _ in range(int(sys.stdin.readline())):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	s = b
	count = 0
	if b >= a:
		print(b)
	elif c <= d:
		print('-1')
	else:
		count = math.ceil((a - b) / (c - d))
		ans = b + count * c
		sys.stdout.write(str(ans))
		print()


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	e = a - b
	if b >= a:
		print(b)
	elif c < d:
		print(-1)
	elif c == d:
		print(-1)
	else:
		x = math.ceil(e / abs(c - d))
		if b + x * c >= a:
			print(b + x * c)
		else:
			print(-1)


from math import ceil

def printv(a, sep=' '):
	print(sep.join(map(str, a)))

def solve(a, b, c, d):
	cur = a - b
	if cur <= 0:
		return b
	p = c - d
	if p <= 0:
		return -1
	return ceil(cur / p) * c + b

def main():
	t = int(input())
	ans = [0] * t
	for i in range(t):
		(a, b, c, d) = [int(i) for i in input().split()]
		ans[i] = solve(a, b, c, d)
	printv(ans, '\n')
main()


n = int(input())
i = 0
while i < n:
	(a_totaltimeneeded, b_firstalarm, c_restalarm, d_tofeelasleep) = map(int, input().split())
	if a_totaltimeneeded <= b_firstalarm:
		print(b_firstalarm)
		i += 1
	else:
		e_timeconlai = a_totaltimeneeded - b_firstalarm
		x_nguthem = c_restalarm - d_tofeelasleep
		if x_nguthem < 0:
			print(-1)
			i += 1
		elif x_nguthem == 0:
			print(-1)
			i += 1
		elif x_nguthem > 0:
			if e_timeconlai % x_nguthem == 0:
				f_solan = e_timeconlai // x_nguthem
			else:
				f_solan = e_timeconlai // x_nguthem + 1
			print(b_firstalarm + f_solan * c_restalarm)
			i += 1


import math
t = int(input())
while t > 0:
	t = t - 1
	ans = 0
	abcd = input().split()
	a = int(abcd[0])
	b = int(abcd[1])
	c = int(abcd[2])
	d = int(abcd[3])
	if a <= b:
		print(b)
		continue
	ans = ans + b
	rem = a - b
	if c <= d:
		print('-1')
		continue
	sleeptime = c - d
	sleepiter = math.ceil(rem / sleeptime)
	ans = ans + sleepiter * c
	print(ans)


x = int(input())
for i in range(x):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		left = b - a
		if d >= c:
			print(-1)
			continue
		else:
			left = a - b
			total = c + d
			time = c - d
			if left % time == 0:
				sete = left // time
				print(sete * c + b)
			else:
				sete = left // time + 1
				print(sete * c + b)


p = int(input())
for q in range(p):
	n = input()
	n = n.split()
	a = int(n[0])
	b = int(n[1])
	c = int(n[2])
	d = int(n[3])
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		x = a - b
		g = c - d
		i = x / g
		if i == int(i):
			print(int(i) * c + b)
		else:
			v = int(i + 1)
			print(b + v * c)


import math

def alarm(a, b, c, d):
	if a <= b:
		return b
	elif c > d:
		sleepr = a - b
		cnt = math.ceil(sleepr / (c - d))
		return b + cnt * c
	return -1
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	res = alarm(a, b, c, d)
	print(res)


t = int(input())
while t > 0:
	(a, b, c, d) = [int(x) for x in input().split()]
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		n = b
		p = (a - b + c - d - 1) // (c - d)
		n += c * p
		print(n)
	t -= 1


import sys, math, os
from io import BytesIO, IOBase
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1000000000.0) + 7

def main():
	for t in range(int(data())):
		(a, b, c, d) = mdata()
		ans = 0
		if b < a:
			ans += b
			if c > d:
				ans += math.ceil((a - b) / (c - d)) * c
			elif d >= c:
				print(-1)
				continue
		else:
			ans = b
		print(ans)
main()


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


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a == 1 and b == 1 and (c == 1) and (d == 1):
		print(1)
	elif a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		effctive_sleep = c - d
		total_sleep_req = a - b
		p = int(math.ceil(total_sleep_req / effctive_sleep))
		total_time = b + p * c
		print(total_time)


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


from math import ceil
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	su = b
	mi = b
	if c <= d:
		if b >= a:
			print(mi)
		else:
			print(-1)
	else:
		print(b + max(ceil((a - b) / (c - d)) * c, 0))


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


import sys
input = sys.stdin.readline

def solve(a, b, c, d):
	if a <= b:
		return b
	if d >= c:
		return -1
	x = (a - b) // (c - d)
	if (a - b) % (c - d) > 0:
		x += 1
	return b + x * c
t = int(input())
for _ in range(t):
	[a, b, c, d] = map(int, input().split())
	print(solve(a, b, c, d))


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations

def li():
	return list(map(int, sys.stdin.readline().split()))

def num():
	return map(int, sys.stdin.readline().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007

def solve():
	t = nu()
	for tt in range(t):
		(a, b, c, d) = num()
		if b < a and c <= d:
			print(-1)
		elif b >= a:
			print(b)
		else:
			rr = a - b
			kk = c - d
			cc = rr // kk
			if rr % kk:
				cc += 1
			print(b + cc * c)
solve()


from functools import reduce
import sys
import math

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(delimiter=' '):
		return input().split(delimiter)

	@staticmethod
	def list_int(delimiter=' '):
		return list(map(int, input().split(delimiter)))

def main():
	t = Read.int()
	for _ in range(t):
		(a, b, c, d) = Read.list_int()
		if b >= a:
			print(b)
		elif d >= c:
			print(-1)
		else:
			print(b + math.ceil((a - b) / (c - d)) * c)
main()


import math
T = int(input())
for t in range(T):
	(a, b, c, d) = [int(x) for x in input().split(' ')]
	if b >= a:
		print(b)
		continue
	if d >= c:
		print('-1')
		continue
	remaining = a - b
	perUnit = c - d
	units = math.ceil(remaining / perUnit)
	print(b + c * units)


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	if a > b and d >= c:
		print(-1)
		continue
	rest = c - d
	to_take = a - b
	per = math.ceil(to_take / rest)
	print(b + per * c)


import math
from collections import defaultdict as dd
from bisect import bisect as b
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		diff = a - b
		if c <= d:
			print(-1)
		else:
			d2 = c - d
			ans = b + c * (diff // d2)
			if diff % d2 != 0:
				ans += c
			print(ans)


def main():
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif a > b and c <= d:
		print(-1)
	else:
		x = c - d
		y = a - b
		ans = y // x
		if b + x * ans < a:
			ans += 1
		print(b + c * ans)
t = int(input())
for i in range(t):
	main()


import math
n = int(input())
while n > 0:
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b + c * math.ceil((a - b) / (c - d))
		print(ans)
	n -= 1


import sys
readline = sys.stdin.readline

def solve():
	(A, B, C, D) = map(int, readline().split())
	ans = 0
	A = A - B
	ans += B
	if A <= 0:
		print(ans)
		return
	if C <= D:
		print(-1)
		return
	k = C - D
	n = (A + k - 1) // k
	ans += C * n
	print(ans)
T = int(readline())
for i in range(T):
	solve()


z = lambda : map(int, input().split())
z1 = lambda : int(input())
z2 = lambda : input()
from math import ceil as f
for _ in ' ' * z1():
	(a, b, c, d) = z()
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		print(b + f((a - b) / (c - d)) * c)


count = int(input())
ans = []
for n in range(0, count):
	(a, b, c, d) = map(int, input().split())
	ost = a - b
	if ost <= 0:
		ans.append(b)
	else:
		dalshe = c - d
		if dalshe <= 0:
			ans.append(-1)
		elif ost // dalshe - ost / dalshe < 0:
			ans.append(b + int(ost / dalshe + 1) * c)
		else:
			ans.append(b + int(ost / dalshe) * c)
for n in ans:
	print(n)


from math import ceil as ce
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print('-1')
	else:
		e = a - b
		f = c - d
		n = ce(e / f)
		print(b + n * c)


import math
t = int(input())
lista = []
for i in range(t):
	time = 0
	(a, b, c, d) = [int(q) for q in input().split()]
	if d >= c and b < a:
		lista.append(-1)
		continue
	a -= b
	time += b
	if a > 0:
		x = math.ceil(a / (c - d))
		time += c * x
	lista.append(time)
for i in lista:
	print(i)


import math
T = int(input())
L = []
for i in range(T):
	l = input().split()
	L.append(l)
for l in L:
	(a, b, c, d) = (int(l[0]), int(l[1]), int(l[2]), int(l[3]))
	if a <= b:
		print(b)
	elif c <= d:
		print('-1')
	else:
		res = b + c * math.ceil((a - b) / (c - d))
		print(int(res))


def ProcessInputs():
	(a, b, c, d) = map(int, input().strip().split())
	if b >= a:
		return b
	Rem = a - b
	if c > d:
		n = Rem / (c - d)
	else:
		return -1
	if n < 1:
		n = 1
	elif n % int(n) != 0:
		n = int(n)
		n += 1
	else:
		n = int(n)
	return int(b + c * n)
T = int(input())
for _ in range(T):
	print(ProcessInputs())


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


t = int(input())
for i in range(t):
	inp = list(map(int, input().split()))
	a = inp[0]
	b = inp[1]
	c = inp[2]
	d = inp[3]
	alt = max(a - b, 0)
	ans = b
	if c > d:
		ans += (alt // (c - d) + bool(alt % (c - d))) * c
	elif a > b:
		ans = -1
	print(ans)


def answer(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	initial_sleep = b
	each_cycle = c - d
	sleep_req_rem = a - initial_sleep
	if sleep_req_rem % each_cycle == 0:
		num_cycles = sleep_req_rem // each_cycle
	else:
		num_cycles = sleep_req_rem // each_cycle + 1
	return initial_sleep + c * num_cycles

def main():
	t = int(input())
	a = [0] * t
	b = [0] * t
	c = [0] * t
	d = [0] * t
	for j in range(t):
		(a[j], b[j], c[j], d[j]) = [int(i) for i in input().split()]
	for j in range(t):
		print(answer(a[j], b[j], c[j], d[j]))
main()


from sys import stdin, stdout
from math import floor, ceil

def find(A, B, C, D):
	A -= B
	if A < 1:
		return B
	if D >= C:
		return -1
	K = B
	Z = ceil(A / (C - D))
	return B + Z * C

def main():
	for _ in range(int(stdin.readline())):
		(A, B, C, D) = list(map(int, stdin.readline().split()))
		print(find(A, B, C, D))
main()


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


testcases = int(input())
t = 0
time = []
while t < testcases:
	abcd = input()
	abcd = abcd.split()
	a = int(abcd[0])
	b = int(abcd[1])
	c = int(abcd[2])
	d = int(abcd[3])
	if a <= b:
		total_slept = b
	elif c <= d:
		total_slept = -1
	else:
		temp = (a - b) / (c - d)
		if (a - b) / (c - d) > int((a - b) / (c - d)):
			temp = temp + 1
		total_slept = b + int(temp) * c
	time.append(total_slept)
	t += 1
for i in time:
	print(i)


import sys
import math
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	a -= b
	time = c - d
	if time <= 0:
		print(-1)
		continue
	print(math.ceil(a / time) * c + b)


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


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


from math import ceil
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		r = b
	elif c - d < 1:
		r = -1
	else:
		x = ceil((a - b) / (c - d))
		r = b + x * c
	print(r)


mod = 1000000007
eps = 10 ** (-9)

def main():
	import sys
	input = sys.stdin.readline
	for _ in range(int(input())):
		(a, b, c, d) = map(int, input().split())
		if a <= b:
			print(b)
		else:
			rest = a - b
			if c <= d:
				print(-1)
			else:
				k = (rest - 1) // (c - d) + 1
				print(b + c * k)
main()


t = int(input())
for tc in range(t):
	s = input().split()
	(a, b, c, d) = (int(s[0]), int(s[1]), int(s[2]), int(s[3]))
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		v1 = a - b
		v2 = c - d
		if v1 % v2 == 0:
			ca = v1 // v2
			print(b + c * ca)
		else:
			ca = v1 // v2 + 1
			print(b + c * ca)


import sys
import math
from collections import defaultdict

def solve(test):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		rem = a - b
		quo = c - d
		res = math.ceil(rem / quo)
		print(b + res * c)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


t = int(input())
l = [0] * t
for i in range(0, t):
	(a, b, c, d) = map(int, input().split())
	k = b
	if a <= b:
		l[i] = b
	if a > b:
		if c <= d:
			l[i] = -1
		else:
			g = (a - b) // (c - d)
			while a - b > c - d:
				b = b + g * (c - d)
				k = k + g * c
			if a - b > 0:
				k = k + c
			l[i] = k
for i in range(0, t):
	print(l[i])


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		z = c - d
		print(b + c * ceil((a - b) / (c - d)))


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


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


from math import ceil
tc = int(input())
for _ in range(tc):
	(a, b, c, d) = map(int, input().split(' '))
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		k = (a - b) / (c - d)
		k = ceil(k)
		print(b + k * c)


import math as m
t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
	else:
		e = a - b
		if d >= c:
			print(-1)
		else:
			f = c - d
			print(b + m.ceil(e / f) * c)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	time = b
	sleep = b
	if sleep >= a:
		print(time)
	else:
		rest = a - sleep
		if d >= c:
			print(-1)
		else:
			req = abs(int(-rest // (c - d)))
			time += req * c
			print(time)


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


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)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	import math
	time_left = b - a
	if b - a >= 0:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		ans = abs(c - d)
		nans = abs(a - b)
		print(b + int(math.ceil(nans / ans)) * c)


import math

def time_to_get_out(a, b, c, d):
	if a - b <= 0:
		return b
	elif c - d <= 0:
		return -1
	else:
		return b + c * math.ceil((a - b) / (c - d))
t = int(input())
while t != 0:
	t = t - 1
	(a, b, c, d) = map(int, input().split())
	ans = time_to_get_out(a, b, c, d)
	print(ans)


from math import ceil
t = int(input())
for T in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	if c <= d and a > b:
		print(-1)
		continue
	if b >= a:
		print(b)
		continue
	rem = a - b
	ans = b + c * ceil(rem / (c - d))
	print(ans)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if d >= c and b < a:
		print(-1)
	elif b >= a:
		print(b)
	else:
		awalTidur = a - b
		kesempatanTidur = c - d
		banyakTidur = math.ceil(awalTidur / kesempatanTidur)
		lamaTidur = banyakTidur * c + b
		print(lamaTidur)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		count = 0
		res = b
		diff = a - b
		mult = diff // (c - d)
		if diff % (c - d):
			mult += 1
		res += mult * c
		print(res)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	e = c - d
	if e < 1 and a > b:
		print(-1)
	elif a <= b:
		print(b)
	else:
		print(b + ((a - b - 1) // e + 1) * c)


import queue
t = int(input())
while t:
	line = input().split()
	(a, b, c, d) = (int(line[0]), int(line[1]), int(line[2]), int(line[3]))
	if a <= b:
		print(b)
	elif d >= c:
		print('-1')
	else:
		x = (a - b) // (c - d) + ((a - b) % (c - d) > 0)
		print(x * c + b)
	t -= 1


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


from math import ceil
t = int(input())
for x in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	elif c <= d:
		print(-1)
		continue
	r = c - d
	e = a - b
	n = e / r
	print(b + ceil(n) * c)


import math
n = int(input())

def get_result(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	diff = c - d
	times = math.ceil((a - b) / diff)
	return times * c + b
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	print(get_result(a, b, c, d))


from math import ceil

def solve(a, b, c, d):
	if b >= a:
		return b
	if c <= d:
		return -1
	return b + c * ceil((a - b) / (c - d))
tc = int(input())
for i in range(1, tc + 1):
	(a, b, c, d) = map(int, input().split())
	print(solve(a, b, c, d))


import sys
import math
try:
	sys.stdin = open('Input.txt', 'r')
	sys.stdout = open('Output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for testCases in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b < a:
		if d < c:
			need = math.ceil((a - b) / (c - d))
			print(b + need * c)
		else:
			print(-1)
	else:
		print(b)


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


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


t = int(input())
for _ in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		k = abs(a - b)
		l = abs(c - d)
		if k % l == 0:
			print(b + k // l * c)
		else:
			print(b + (k // l + 1) * c)


from sys import stdin, stdout, setrecursionlimit
from math import ceil
input = stdin.readline
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	x = 0
	if b < a and d >= c:
		print(-1)
	elif b < a:
		x = a - b
		b += c * ceil(x / (c - d))
		print(b)
	else:
		print(b)


n = int(input())

def sol(s):
	if int(s[1]) >= int(s[0]):
		return int(s[1])
	else:
		t = int(s[0]) - int(s[1])
		c = 0
		a = int(s[2]) - int(s[3])
		if a < 1:
			return -1
		else:
			c = t // a
			if t - a * c <= 0:
				return int(s[1]) + c * int(s[2])
			else:
				c = c + 1
				return int(s[1]) + c * int(s[2])
for i in range(0, n):
	s = input()
	s = s.split(' ')
	r = sol(s)
	print(r)


import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	(a, b, c, d) = sep()
	if a - b <= 0:
		print(b)
	else:
		rem = a - b
		if d >= c:
			print(-1)
		else:
			l = c - d
			temp = rem // l
			if rem % l != 0:
				temp += 1
			print(b + temp * c)
testcase(int(inp()))


t = int(input())
import math
for T in range(t):
	(a, b, c, d) = map(int, input().split())
	a = a - b
	if a <= 0:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	else:
		m = c - d
		if a % m != 0:
			ans = b + (a // m + 1) * c
		else:
			ans = b + a // m * c
		print(ans)


t = int(input())
for i in range(t):
	inp = input().split(' ')
	a = int(inp[0])
	b = int(inp[1])
	c = int(inp[2])
	d = int(inp[3])
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		print(b + ((a - b - 1) // (c - d) + 1) * c)


from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		rem = a - b
		s = c - d
		print(b + ceil(rem / s) * c)


t = int(input())

def check(a, b, c, d):
	if d - c > 0 and a > b:
		return -1
	elif b >= a:
		return b
	elif c - d <= 0:
		return -1
	else:
		m = (a - b) // (c - d)
		if m * (c - d) != a - b:
			m += 1
		return m * c + b
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	print(check(a, b, c, d))


a = int(input())
for _ in range(a):
	b = input().split()
	c = int(b[0])
	d = int(b[1])
	e = int(b[2])
	f = int(b[3])
	if c > d and e <= f:
		print(-1)
	elif d >= c:
		print(d)
	elif (c - d) / (e - f) % 1 == 0:
		print((c - d) // (e - f) * e + d)
	else:
		print(((c - d) // (e - f) + 1) * e + d)


from collections import Counter
import math
import sys

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for _ in range(INT()):
	(a, b, c, d) = MAP()
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b
		s = a - b
		x = math.ceil(s / (c - d))
		ans += x * c
		print(ans)


import math
from collections import defaultdict
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	slept = 0
	time = 0
	slept += b
	time += b
	change = c - d
	if slept >= a:
		print(time)
	elif c - d > 0:
		totchange = math.ceil((a - b) / (c - d))
		time += totchange * c
		print(time)
	else:
		print('-1')


import math
N = int(input())
for i in range(0, N):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print('-1')
	else:
		m = a - b
		s = c - d
		v = math.ceil(m / s)
		print(v * c + b)


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		r = c - d
		sleep = a - b
		m = ceil(sleep / r)
		time = b + m * c
		print(time)


n = int(input())
g = 0
list1 = []
for g in range(0, n):
	t = 0
	str1 = [int(c) for c in input().split()]
	goal = int(str1[0]) - int(str1[1])
	x = int(str1[2]) - int(str1[3])
	if x <= 0 and goal > 0:
		list1.append(-1)
	elif goal <= 0:
		list1.append(str1[1])
	elif x > 0 and goal > 0:
		f = goal // x
		if goal % x == 0:
			t = int(str1[1]) + f * int(str1[2])
		else:
			t = int(str1[1]) + (f + 1) * int(str1[2])
		list1.append(t)
i = 0
for i in range(0, n):
	print(list1[i])


num = int(input())
for _ in range(num):
	(a, b, c, d) = map(int, input().split())
	rest = a - b
	total = b
	if c - d > 0 and rest > 0:
		if rest % (c - d) == 0:
			t = int(rest / (c - d))
		else:
			t = int(rest / (c - d)) + 1
		total += c * t
		rest -= t * (c - d)
	if rest > 0:
		print('-1')
	else:
		print(total)


import math
t = int(input())
for j in range(t):
	(a, b, c, d) = map(int, input().split(' '))
	left = a - b
	if left <= 0:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		mul = math.ceil(left / (c - d))
		print(b + mul * c)


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


import sys
sys.setrecursionlimit(10000)
from math import ceil

class Memoize:

	def __init__(self, f):
		self.f = f
		self.memo = {}

	def __call__(self, *args):
		if not args in self.memo:
			self.memo[args] = self.f(*args)
		return self.memo[args]

class Recurse(Exception):

	def __init__(self, *args, **kwargs):
		self.args = args
		self.kwargs = kwargs

def recurse(*args, **kwargs):
	raise Recurse(*args, **kwargs)

def tail_recursive(f):

	def decorated(*args, **kwargs):
		while True:
			try:
				return f(*args, **kwargs)
			except Recurse as r:
				args = r.args
				kwargs = r.kwargs
				continue
	return decorated
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	ans = b
	a -= b
	if a > 0:
		if d >= c:
			ans = -1
		else:
			ans += int(ceil(a / (c - d))) * c
	print(ans)


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


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


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	totalSleep = b
	totalTime = b
	if a <= b:
		print(b)
		continue
	elif 0 >= c - d:
		print(-1)
		continue
	rep = math.ceil((a - b) / (c - d))
	totalTime = rep * c + b
	print(totalTime)


from math import ceil
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print('-1')
	else:
		o1 = c - d
		y = a - b
		y1 = ceil(y / o1)
		print(y1 * c + b)


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


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
	tmp = (m + c - d - 1) // (c - d)
	print(b + tmp * c)


import sys
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
for t in range(int(input())):
	(a, b, c, d) = intInp()
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		x = (a - b) // (c - d)
		if (a - b) % (c - d) != 0:
			x += 1
		print(x * c + b)


import math

def main(nums):
	L = []
	for (a, b, c, d) in nums:
		if b >= a:
			L.append(b)
			continue
		r = a - b
		ans = b
		if d >= c:
			L.append(-1)
			continue
		ans += math.ceil(r / (c - d)) * c
		L.append(ans)
	for ans in L:
		print(ans)
n = int(input())
nums = [list(map(int, input().split(' '))) for _ in range(n)]
main(nums)


import math
t = int(input())

def budi(s):
	(a, b, c, d) = map(int, s.split())
	if a <= b:
		return b
	if c <= d:
		return -1
	res = b
	a = a - b
	cd = c - d
	mult = math.ceil(a / cd)
	res += mult * c
	return res
for i in range(t):
	print(budi(input()))


import math
t = int(input())
for i in range(t):
	listt = list(map(int, input().split()))
	a = listt[0]
	b = listt[1]
	c = listt[2]
	d = listt[3]
	if b >= a:
		print(b)
		continue
	if c <= d:
		print('-1')
		continue
	times = math.ceil((a - b) / (c - d))
	print(b + times * c)


import math
t = int(input(''))
for i in range(0, t):
	x = input('').split()
	x = [int(j) for j in x]
	a = x[0]
	b = x[1]
	c = x[2]
	d = x[3]
	count = 0
	var = b
	if a <= b:
		var = b
	elif d >= c:
		var = -1
	else:
		foo = a - b
		count = math.ceil(foo / (c - d))
	print(var + count * c)


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


import math
T = int(input())
for _ in range(T):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	cur = b
	a -= b
	if c <= d:
		print(-1)
		continue
	sleepPerAlarm = c - d
	resetTimes = math.ceil(a / sleepPerAlarm)
	cur += c * resetTimes
	print(cur)


q = int(input())
for i in range(0, q):
	m = list(map(int, input().split()))[:4]
	s = m[1]
	f = m[1]
	if f < m[0]:
		if m[3] >= m[2]:
			s = -1
		else:
			k = m[2] - m[3]
			g = (m[0] - m[1]) // k
			if (m[0] - m[1]) % k == 0:
				s = s + g * m[2]
			else:
				s = s + (g + 1) * m[2]
	print(s)


import sys, random
from bisect import bisect_left, bisect_right
input = sys.stdin.readline

def main():
	inf = 10 ** 20
	t = int(input())
	for _ in range(1, t + 1):
		(a, b, c, d) = map(int, input().split())
		if b >= a:
			print(b)
			continue
		total = b
		a -= b
		if d >= c:
			print(-1)
			continue
		total += a // (c - d) * c
		if a % (c - d):
			total += c
		print(total)
main()


from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
t = sInt()
for _ in range(t):
	(a, b, c, d) = mInt()
	ans = b
	if c > d and b < a:
		n = ceil((a - b) / (c - d))
		ans = b + n * c
		print(ans)
	elif b >= a:
		print(ans)
	else:
		print(-1)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	ans = b
	if a > b:
		if d >= c:
			ans = -1
		else:
			r = a - b
			s = c - d
			if r % s == 0:
				n = r // s
			else:
				n = r // s + 1
			ans = ans + n * c
	print(ans)


from math import *
from sys import stdin, stdout
input = stdin.buffer.readline
t = int(input())
while t > 0:
	ip = [int(x) for x in input().split()]
	a = ip[0]
	b = ip[1]
	c = ip[2]
	d = ip[3]
	if c <= d:
		if b >= a:
			print(b)
		else:
			print(-1)
	elif b >= a:
		print(b)
	else:
		x = ceil((a - b) / (c - d))
		ans = b + x * c
		if ans >= a:
			print(ans)
		else:
			print(-1)
	t -= 1


from math import factorial as fac
from collections import defaultdict
from copy import deepcopy
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m
from collections import deque, defaultdict
import heapq

def bfs(V, E):
	s = set()
	num_connected_components = 0
	for v in V:
		if v in s:
			continue
		q = deque([])
		s.add(v)
		q.append(v)
		while len(q) > 0:
			x = q.popleft()
			for e in E[x]:
				if e not in s:
					s.add(e)
					q.append(e)
		num_connected_components += 1
	return num_connected_components

def bfs_get_neig(V, get_neig):
	s = set()
	num_connected_components = 0
	for v in V:
		if v in s:
			continue
		q = deque([])
		s.add(v)
		q.append(v)
		while len(q) > 0:
			x = q.popleft()
			for e in get_neig(*x):
				if e not in s:
					s.add(e)
					q.append(e)
		num_connected_components += 1
	return num_connected_components

def binary_count_smaller(L, x):
	l = 0
	if x < L[0]:
		return 0
	r = len(L)
	while r - l > 1:
		mid = (l + r) // 2
		if L[mid] < x:
			l = mid
		else:
			r = mid
	return l + 1

def binary_count_larger(L, x):
	l = 0
	if x > L[-1]:
		return 0
	r = len(L)
	while r - l > 1:
		mid = (l + r) // 2
		if L[mid] <= x:
			l = mid
		else:
			r = mid
	return len(L) - (r - 1)

def regular_bs(L, x, l=None, r=None):
	l = 0 if l == None else l
	r = len(L) if r == None else r
	while r - l > 1:
		m = (r + l) // 2
		l = m if L[m] <= x else l
		r = m if L[m] > x else r
	cnt = 0
	return l
from itertools import product

def preprocess():
	nums = dict(zip(sequences, range(10)))
	changes = defaultdict(dict)
	for comb in map(lambda x: ''.join(x), product(*[['0', '1']] * 7)):
		for seq in sequences:
			a = int(comb, 2)
			b = int(seq, 2)
			num_changes = bin(a ^ b).count('1')
			if num_changes == seq.count('1') - comb.count('1'):
				changes[comb][num_changes] = nums[seq]
	return changes
from collections import deque
from itertools import product

def bi(x):
	return bin(x)[2:]

def check_no_ghg_row(matrix, n, m):
	for i in range(n):
		black = False
		white = False
		for x in matrix[i]:
			if x == '.' and (not black or white):
				continue
			if x == '#' and black and white:
				return False
			if x == '#' and black:
				continue
			if x == '.' and black:
				white = True
			if x == '#':
				black = True
	return True

def check_no_ghg_col(matrix, n, m):
	for j in range(m):
		black = False
		white = False
		for i in range(n):
			x = matrix[i][j]
			if x == '.' and (not black or white):
				continue
			if x == '#' and black and white:
				return False
			if x == '#' and black:
				continue
			if x == '.' and black:
				white = True
			if x == '#':
				black = True
	return True

def empty_col(matrix, n, m):
	for j in range(m):
		empty = True
		for i in range(n):
			if matrix[i][j] == '#':
				empty = False
				break
		if empty:
			return True
	return False

def empty_row(matrix, n, m):
	for i in range(n):
		empty = True
		for j in range(m):
			if matrix[i][j] == '#':
				empty = False
				break
		if empty:
			return True
	return False

def find_lcm(num1, num2):
	if num1 > num2:
		num = num1
		den = num2
	else:
		num = num2
		den = num1
	rem = num % den
	while rem != 0:
		num = den
		den = rem
		rem = num % den
	gcd = den
	lcm = int(int(num1 * num2) / int(gcd))
	return lcm

def find_lcm_array(A):
	if len(A) == 1:
		return A[0]
	lcm = find_lcm(A[0], A[1])
	for i in range(2, len(A)):
		lcm = find_lcm(lcm, A[i])
	return lcm

def solution(a, b, c, d):
	if b >= a:
		return b
	reminaing = a - b
	if d >= c:
		return -1
	sleep_in_attempt = c - d
	attempts = int(math.ceil(reminaing / sleep_in_attempt))
	return attempts * c + b

def main():
	T = read_int()
	for i in range(T):
		(a, b, c, d) = read_int_array()
		x = solution(a, b, c, d)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


import math
import sys

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
test_cases = int(input())
for _ in range(test_cases):
	(time_needed, first_alarm, interval, falling_asleep) = map(int, input().split())
	if first_alarm >= time_needed:
		print(first_alarm)
	elif falling_asleep >= interval:
		print(-1)
	else:
		x = math.ceil((time_needed - first_alarm) / (interval - falling_asleep))
		print(first_alarm + interval * x)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		z = b
	elif c <= d:
		z = -1
	else:
		(s, t) = divmod(a - b, c - d)
		z = b + c * s + c * (t != 0)
	print(z)


for l in range(int(input())):
	(n, m, p, q) = map(int, input().split())
	n -= m
	if n <= 0:
		print(m)
	elif p - q <= 0:
		print(-1)
	else:
		x = n % (p - q)
		if x > 0:
			k = 1
		else:
			k = 0
		m += (n // (p - q) + k) * p
		print(m)


T = int(input())
for _ in range(0, T):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		tt = (a - b) // (c - d)
		if (a - b) % (c - d) != 0:
			tt += 1
		ans = b + tt * c
		print(ans)


def solve(a, b, c, d):
	if a <= b:
		return b
	if d >= c:
		return -1
	if (a - b) / (c - d) != (a - b) // (c - d):
		return b + (a - b) // (c - d) * c + c
	else:
		return b + (a - b) // (c - d) * c
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	print(solve(a, b, c, d))


from math import ceil
test_case = int(input())
my_list = []
for i in range(test_case):
	my_list.append([int(j) for j in input().split()])
	(a, b, c, d) = my_list[i]
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		print(b + ceil((a - b) / (c - d)) * c)


import math
import sys
input = sys.stdin.readline
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
	a -= b
	t = c - d
	if a % t:
		z = a // t + 1
	else:
		z = a // t
	print(b + z * c)


import math
t = int(input())
while t > 0:
	a = list(map(float, input().split()))
	if a[1] >= a[0]:
		print(int(a[1]))
	elif a[2] - a[3] <= 0:
		print('-1')
	else:
		df = a[2] - a[3]
		if df == 1:
			a[0] = int(a[0])
			a[1] = int(a[1])
			a[2] = int(a[2])
			a[3] = int(a[3])
			ts = (a[0] - a[1]) * a[2]
			ts = int(ts + a[1])
			print(int(ts))
		else:
			a[0] = a[0] - a[1]
			ts = math.ceil(a[0] / df) * a[2] + a[1]
			print(int(ts))
	t = t - 1


import math
a = int(input())
for i in range(a):
	n = [int(x) for x in input().split()]
	if n[1] >= n[0]:
		print(n[1])
	elif n[2] - n[3] > 0:
		print(n[1] + n[2] * math.ceil((n[0] - n[1]) / (n[2] - n[3])))
	else:
		print(-1)


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


import sys, math
input = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	else:
		i = math.ceil((a - b) / (c - d))
		print(b + i * c)


import sys
import math
input = sys.stdin.readline
from collections import deque
from queue import LifoQueue

def binary_search(a, n):
	L = 0
	R = len(a) - 1
	while L <= R:
		mid = L + (R - L) // 2
		if a[mid] == n:
			return mid
		elif a[mid] < n:
			L = mid + 1
		else:
			R = mid - 1
	return -1
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		need = a - b
		if c - d > 0:
			if need % (c - d) == 0:
				print(b + need // (c - d) * c)
			else:
				print(b + (need // (c - d) + 1) * c)
		else:
			print(-1)


import math
t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print('-1')
	else:
		temp = b
		i = math.ceil((a - b) / (c - d))
		ans = b + i * c
		print(ans)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	elif c <= d:
		print('-1')
		continue
	else:
		sum = 0
		r = 0
		sum = sum + b
		r = r + b
		m = a - sum
		e = c - d
		if m % e == 0:
			z = int(m / e)
		else:
			z = int(m / e) + 1
		r = r + z * c
		sum = sum + z * e
		if sum >= a:
			print(r)
		else:
			print('-1')


import math
T = int(input())
for i in range(T):
	(a, b, c, d) = map(int, input().split())
	if b < a and c <= d:
		print('-1')
	else:
		ans = 0
		if b >= a:
			print(b)
		else:
			ans = b
			a = a - b
			dif = c - d
			z = math.ceil(a / dif)
			ans = ans + z * c
			print(ans)


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


n = int(input())
for _ in range(n):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		x = c - d
		if x > 0:
			co = (a - b) / x
			import math
			k = math.floor(co)
			ca = co - k
			if ca > 0:
				an = k + 1
			else:
				an = k
			pr = an * c + b
			print(pr)
		else:
			print('-1')


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


import math
n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif a > b and d >= c:
		print(-1)
	else:
		x = math.ceil((a - b) / (c - d))
		print(b + x * c)


def solve(a, b, c, d):
	if b >= a:
		return b
	if c <= d:
		return -1
	x = (a - b) // (c - d)
	if (a - b) % (c - d) != 0:
		x += 1
	return b + x * c
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	print(solve(a, b, c, d))


import math
t = int(input())
for _ in range(t):
	arr = input().split()
	a = int(arr[0])
	b = int(arr[1])
	c = int(arr[2])
	d = int(arr[3])
	if b < a:
		if d < c:
			remaining = a - b
			m = math.ceil(remaining / (c - d))
			print(b + m * c)
		else:
			print(-1)
	else:
		print(b)


t = int(input())
while t:
	(a, b, c, d) = map(int, input().split())
	count = 0
	res = 0
	k = 0
	if a <= b:
		print(b)
		t -= 1
		continue
	if d >= c:
		print(-1)
		t -= 1
		continue
	q = c - d
	p = a - b
	s = p // q
	if p % q == 0:
		s = s
	else:
		s = s + 1
	print(s * c + b)
	t -= 1


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


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


Q = int(input())
list = []
while Q > 0:
	Q -= 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		list.append(str(b))
		continue
	a -= b
	if c <= d:
		list.append('-1')
		continue
	nr = int(a / (c - d))
	ans = b + nr * c
	a = a - nr * (c - d)
	if a:
		ans += c
	list.append(str(ans))
for i in list:
	print(i)


import math
t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		netrest = c - d
		c1 = math.ceil((a - b) / netrest)
		ne = c1 * c
		print(b + ne)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
	else:
		cont = b
		a -= b
		if d >= c:
			print(-1)
		else:
			aux = math.ceil(a / (c - d))
			cont += c * aux
			print(cont)


from math import *
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		k = c - d
		rem = a - b
		ans = ceil(rem / k) * c
		print(ans + b)


s = int(input())
for i in range(s):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	if a > b and d >= c:
		print(-1)
		continue
	m = b
	a = a - b
	f = a // (c - d)
	if a == (c - d) * f:
		m += c * f
	else:
		m += c * (f + 1)
	print(m)


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


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 i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		left_time = a - b
		one_bud = c - d
		buds = (left_time + one_bud - 1) // one_bud
		print(b + c * buds)


from math import ceil
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	res = 0
	if a <= b:
		res = b
	elif c - d <= 0:
		res = -1
	else:
		x = ceil((a - b) / (c - d))
		res = b + c * x
	print(res)


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split()]
	rem = a - b
	if rem > 0:
		if c <= d:
			print(-1)
		else:
			mul = ceil(rem / (c - d))
			print(b + mul * c)
	else:
		print(b)


import math
T = int(input())
for i in range(1, T + 1):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		total = b
		a = a - b
		total = total + c * int(math.ceil(a / (c - d)))
		print(total)


import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for (A, B, C, D) in Query:
	if B >= A:
		print(B)
	else:
		more = A - B
		if C <= D:
			print(-1)
		else:
			print(B + (more + C - D - 1) // (C - D) * C)


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


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		restRequired = a - b
		restGained = c - d
		temp = math.ceil(restRequired / restGained)
		print(c * temp + b)


def findWakeTime(a, b, c, d):
	if b >= a:
		print(b)
		return
	if d >= c:
		print(-1)
		return
	sEachc = c - d
	tLeft = a - b
	turns = 0
	if tLeft % sEachc == 0:
		turns = tLeft // sEachc
	else:
		turns = tLeft // sEachc
		turns += 1
	print(c * turns + b)
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	findWakeTime(a, b, c, d)


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


import sys, math, cmath, time
start_time = time.time()
tt = 0

def inp():
	return int(input())

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

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def solve():
	(a, b, c, d) = invr()
	if a - b > 0 and d >= c:
		print(-1)
	elif a - b <= 0:
		print(b)
	else:
		print(int(b + math.ceil((a - b) / (c - d)) * c))

def main():
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
	sys.stdout.close()

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


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


import math
for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		df = c - d
		s = a - b
		if s % df == 0:
			div = s // df
		else:
			div = s // df + 1
		ans = b + (df + d) * div
		print(ans)


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	if b >= a:
		print(b)
	elif d >= c:
		if b < a:
			print(-1)
	else:
		mins = c - d
		start = b
		rem = a - b
		more = int(math.ceil(rem / mins))
		ans = start + more * c
		print(ans)


def ceil(a, b):
	return a // b + (a % b != 0)
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b + ceil(a - b, c - d) * c
		print(ans)


t = int(input())
while t > 0:
	t -= 1
	dum = str(input())
	dum1 = dum.split()
	a = int(dum1[0])
	b = int(dum1[1])
	c = int(dum1[2])
	d = int(dum1[3])
	ans = 0
	if a <= b:
		print(b)
	elif c <= d and a > b:
		print(-1)
	else:
		ar = 0
		sr = a
		sr -= b
		sg = c - d
		x = sr % sg
		if x == 0:
			ar = int(sr / sg)
		else:
			ar = int(sr / sg + 1)
		dum2 = b + ar * c
		ans += dum2
		print(ans)


import math

def main():
	(a, b, c, d) = [int(i) for i in input().split()]
	if b >= a:
		return b
	elif c <= d:
		return -1
	else:
		e = math.ceil((a - b) / (c - d))
		return b + c * e
for i in range(int(input())):
	print(main())


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


count = int(input())
for _ in range(count):
	list_1 = input().split()
	(a, b, c, d) = [int(i) for i in list_1]
	list_2 = [int(i) for i in list_1]
	if b >= a:
		print(b)
	elif a > b and d >= c:
		print('-1')
	else:
		e = a - b
		f = c - d
		g = e // f
		if e > g * f:
			g += 1
		print(b + g * c)


for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	inc_sleep = c - d
	if b >= a:
		print(b)
		continue
	if inc_sleep <= 0:
		print(-1)
		continue
	a -= b
	tot = b
	no = a // inc_sleep
	tot += no * c
	rem = a % inc_sleep
	if rem:
		tot += c
	print(tot)


for i in range(int(input())):
	(A, B, C, D) = map(int, input().split())
	if A <= B:
		print(B)
		continue
	if C <= D:
		print(-1)
		continue
	print(B - (B - A) // (C - D) * C)


import math
import sys
import itertools
import itertools

def sa(Type=int):
	return [Type(x) for x in input().split()]

def solve(t):
	(a, b, c, d) = sa()
	sleepNeeded = a
	alarmAt = b
	snoozeTime = c
	resumeSleepTime = d
	if sleepNeeded <= alarmAt:
		print(alarmAt)
		return
	if resumeSleepTime >= snoozeTime:
		print(-1)
		return
	sleepNeeded -= alarmAt
	sleepPerSnooze = snoozeTime - resumeSleepTime
	reps = math.ceil(sleepNeeded / sleepPerSnooze)
	print(alarmAt + reps * snoozeTime)
t = int(input())
for i in range(t):
	solve(i + 1)


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif b < a and c <= d:
		print('-1')
	else:
		rem = a - b
		r = c - d
		d = math.ceil(rem / r)
		total = c * d + b
		print(total)


def alarm_clk(arr):
	a = arr[0]
	b = arr[1]
	c = arr[2]
	d = arr[3]
	if a <= b:
		return b
	elif c <= d:
		return -1
	else:
		time = b
		a -= b
		rem = a // (c - d)
		if a % (c - d):
			rem += 1
		time += rem * c
		return time
t = int(input())
for _ in range(t):
	arr = list(map(int, input().split()))
	print(alarm_clk(arr))


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
import bisect as bi
import heapq

def I():
	return int(stdin.readline())

def In():
	return map(int, stdin.readline().split())

def main():
	try:
		(a, b, c, d) = In()
		if a <= b:
			print(b)
		else:
			ans = b
			a -= b
			flag = 0
			if c <= d:
				print(-1)
			else:
				t = math.ceil(a / (c - d))
				ans += c * t
				print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	t = b
	count = 0
	diff = c - d
	if t >= a:
		print(t)
	elif diff <= 0 and a > t:
		print(-1)
	else:
		ans = b + (a - t) // diff * c
		if (a - t) % diff != 0:
			ans = ans + c
		print(ans)


import math
b = int(input())
for k in range(b):
	a = list(map(int, input().split()))
	if a[0] > a[1] and a[2] <= a[3]:
		print(-1)
	elif a[0] <= a[1]:
		print(a[1])
	else:
		print(math.ceil((a[0] - a[1]) / (a[2] - a[3])) * a[2] + a[1])


import math
t = int(input())
for x in range(t):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if a <= b:
		ans = b
		print(ans)
	elif a > b and c <= d:
		ans = -1
		print(ans)
	elif a > b and c > d:
		n = a - b
		m = c - d
		r = math.ceil(n / m)
		ans = b + r * c
		print(ans)


t = int(input())
for _ in range(t):
	(x, y, z, w) = map(int, input().split())
	ans = 0
	if y >= x:
		print(y)
	else:
		x = x - y
		ans += y
		if w >= z:
			print(-1)
		else:
			k = 0
			if x // (z - w) < x / (z - w):
				k = x // (z - w) + 1
			else:
				k = x // (z - w)
			ans += k * z
			print(ans)


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


t = int(input())
for i in range(t):
	(a, b, c, d) = [int(q) for q in input().split(' ')]
	answer = lastAns = 0
	if b >= a:
		lastAns = b
		print(lastAns)
		continue
	elif d > c:
		lastAns = -1
		print(lastAns)
		continue
	elif c - d != 0:
		answer = int((a - b) / (c - d))
	else:
		lastAns = -1
		print(lastAns)
		continue
	if answer * (c - d) + b < a:
		answer = answer + 1
	lastAns = b + answer * c
	print(lastAns)


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		(a, b, c, d) = map(int, input().split())
		if a > b and d >= c:
			print(-1)
		elif a <= b:
			print(b)
		else:
			x = (a - b - 1) // (c - d) + 1
			print(b + c * x)
	q = int(input())
	for _ in range(q):
		solve()
main()


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


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


n = int(input())
for i in range(n):
	l = list(map(int, input().split()))
	if l[0] <= l[1]:
		print(l[1])
	else:
		hrs = l[1]
		rem = l[0] - l[1]
		sleep = l[2] - l[3]
		if sleep <= 0:
			print(-1)
		else:
			x = rem / sleep
			y = 0
			if x.is_integer():
				y = int(x)
			else:
				y = int(x) + 1
			print(hrs + l[2] * y)


import math
t = int(input())
while t != 0:
	(a, b, c, d) = [int(x) for x in input().split()]
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		a -= b
		value = math.ceil(a / (c - d))
		a = b + c * value
		print(a)
	t -= 1


t = int(input())
for j in range(t):
	l = list(map(int, input().split()))
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		e = b
		f = a - b
		g = c - d
		if f % g == 0:
			h = f // g
		else:
			h = f // g + 1
		e += h * c
		print(e)


import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	total = a - b
	time = b
	canSleep = c - d
	time += math.ceil(total / canSleep) * c
	print(time)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		res = b
		cd = c - d
		ab = a - b
		if ab / cd == int(ab / cd):
			cnt = ab // cd
		else:
			cnt = ab // cd + 1
		print(b + cnt * c)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial, ceil
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')

def main():
	for _ in range(N()):
		(a, b, c, d) = RL()
		if a <= b:
			print(b)
		elif d >= c:
			print(-1)
		else:
			dif = a - b
			need = ceil(dif / (c - d))
			print(b + need * c)
main()


t = int(input(''))
u = 0
while u < t:
	tot = 0
	(a, b, c, d) = [int(a) for a in input().split()]
	if a <= b:
		tot = b
	elif d >= c:
		tot = -1
	elif (a - b) % (c - d) != 0:
		tot = b + c * ((a - b) // (c - d) + 1)
	else:
		tot = b + c * ((a - b) // (c - d))
	print(tot)
	u += 1


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		l = 1
		r = 10 ** 9
		while l != r:
			mid = (l + r) // 2
			if b + (c - d) * mid >= a:
				r = mid
			else:
				l = mid + 1
		print(b + l * c)


t = int(input())
while t:
	t = t - 1
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	a = a - b
	ti = b
	if c <= d:
		print(-1)
		continue
	x = c - d
	if a % x == 0:
		ti += c * (a // x)
	else:
		ti += c * (a // x)
		ti += c
	print(ti)


from math import ceil, floor
t = int(input())
for cnt in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		print(b + c * ceil((a - b) / (c - d)))


from sys import stdin
import math

def solve(a, b, c, d):
	if b >= a:
		ans = b
	elif d >= c:
		ans = -1
	else:
		ans = b + int(math.ceil((a - b) / (c - d))) * c
	print(ans)
	return

def main():
	inp = stdin.readline
	for _ in range(int(inp())):
		(a, b, c, d) = map(int, inp().split())
		solve(a, b, c, d)
main()


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)


t = int(input())
for i in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	sl = b
	chk = True
	tot = b
	if sl >= a:
		print(tot)
		continue
	if c <= d:
		print(-1)
		continue
	else:
		if (a - tot) % abs(c - d) == 0:
			ct = (a - tot) // abs(c - d)
		else:
			ct = (a - tot) // abs(c - d) + 1
		print(tot + ct * c)


T = int(input())
for t in range(T):
	(A, B, C, D) = list(map(int, input().split()))
	if B >= A:
		print(B)
	elif D >= C:
		print(-1)
	else:
		minutes_slept = C - D
		abis = A - B
		q = abis // minutes_slept
		r = abis % minutes_slept
		if r != 0:
			q += 1
		print(B + q * C)


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	k = c - d
	x = a - b
	if k <= 0:
		print(-1)
		continue
	temp = math.ceil(x / k)
	addmin = c * temp
	print(b + addmin)


from sys import stdin
import math
t = int(stdin.readline())
for _ in range(t):
	(a, b, c, d) = map(int, stdin.readline().split())
	if d >= c and a > b:
		print(-1)
		continue
	elif b >= a:
		print(b)
	else:
		print(b + c * math.ceil((a - b) / (c - d)))


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


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


import math
import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		a -= b
		x = math.ceil(a / (c - d))
		ans = x * c
		ans += b
		print(ans)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	a -= b
	f = False
	ans = b
	if a <= 0:
		f = True
		print(b)
	elif c <= d:
		print('-1')
	else:
		if a % (c - d) == 0:
			ans += a // (c - d) * c
		else:
			ans += (a // (c - d) + 1) * c
		print(ans)


import math
n = int(input())
for t in range(n):
	(a, b, c, d) = (int(i) for i in input().split())
	if b >= a:
		print(b)
		continue
	a -= b
	ans = b
	if d >= c:
		print(-1)
		continue
	else:
		ans += math.ceil(a / (c - d)) * c
	print(ans)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		t1 = a - b
		t2 = c - d
		if t2 <= 0:
			print('-1')
			continue
		if t1 % t2 == 0:
			t3 = t1 // t2
		else:
			t3 = t1 // t2 + 1
		print(b + c * t3)


import sys, math
input = lambda : sys.stdin.readline().rstrip()
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	a -= b
	ans = b
	if a > 0:
		if c - d <= 0:
			print(-1)
		else:
			print(b + math.ceil(a / (c - d)) * c)
	else:
		print(b)


import math as m
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	ans = b + c * m.ceil((a - b) / (c - d))
	print(ans)


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


N = int(input())
answers = []
for i in range(N):
	(a, b, c, d) = map(int, input().split())
	if c <= d and b < a:
		answers.append(-1)
	elif a <= b:
		answers.append(b)
	else:
		if (a - b) % (c - d):
			m = 1
		else:
			m = 0
		result = b + c * int((a - b) // (c - d) + m)
		answers.append(result)
for i in answers:
	print(i)


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
	f = iter(open('A.txt').readlines())

	def input():
		return next(f)
else:
	input = lambda : sys.stdin.readline().strip()
fprint = lambda *args: print(*args, flush=True)

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


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ans = 0
	ans += b
	a = a - b
	if c <= d and a > 0:
		print(-1)
		continue
	if a > 0:
		x = ceil(a / (c - d))
		ans += c * x
	print(ans)


t = int(input())
while t > 0:
	t -= 1
	inp = input().split(' ')
	a = int(inp[0])
	b = int(inp[1])
	c = int(inp[2])
	d = int(inp[3])
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		eff = c - d
		now = b
		coun = 0
		left = (a - b) // eff
		rem = (a - b) % eff
		if rem == 0:
			print(b + left * c)
		else:
			print(b + (1 + left) * c)


import math

def compute(a, b, c, d):
	if b >= a:
		return b
	elif d >= c:
		return -1
	else:
		rem = a - b
		x = math.ceil(rem / abs(c - d))
		return b + x * c
q = int(input())
for _ in range(q):
	(a, b, c, d) = map(int, input().split())
	print(compute(a, b, c, d))


a = int(input())
for _ in range(a):
	(q, w, e, r) = map(int, input().split())
	if w >= q:
		print(w)
		continue
	if e <= r:
		print(-1)
		continue
	print(w + (int((q - w) / (e - r)) + ((q - w) % (e - r) != 0)) * e)


import math
tstr = input()
t = int(tstr)
ans = list()
nums = [0, 0, 0, 0]
for i in range(t):
	tstr = input().split()
	for i in range(len(tstr)):
		nums[i] = int(tstr[i])
	ttmp = nums[2] - nums[3]
	nums[3] = nums[1]
	if nums[3] >= nums[0]:
		ans.append(nums[3])
		continue
	if ttmp <= 0 and nums[0] > nums[3]:
		ans.append(-1)
		continue
	ans.append(nums[1] + math.ceil((nums[0] - nums[3]) / ttmp) * nums[2])
for i in range(t):
	print(ans[i])


import sys
import math
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	sleep = b
	diff = c - d
	if diff <= 0:
		if b < a:
			print(-1)
			continue
	req = a - sleep
	if req <= 0:
		print(sleep)
	else:
		inc = math.ceil(req / diff)
		sleep += inc * max(c, d)
		print(sleep)


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	a -= b
	s = c - d
	x = int(ceil(a / s))
	print(b + c * x)


def f(a, b, c, d):
	if a <= b:
		return b
	elif c <= d:
		return -1
	else:
		t = (a - b) // (c - d)
		if t * (c - d) == a - b:
			return b + t * c
		else:
			return b + c + t * c
t = int(input())
for i in range(t):
	[a, b, c, d] = input().split(' ')
	print(f(int(a), int(b), int(c), int(d)))


from __future__ import division, print_function
import itertools
import sys
import os
from io import BytesIO, IOBase
import math
if sys.version_info[0] < 3:
	input = raw_input
	range = xrange
	filter = itertools.ifilter
	map = itertools.imap
	zip = itertools.izip

def is_it_local():
	script_dir = str(os.getcwd()).split('/')
	username = 'dipta007'
	return username in script_dir

def READ(fileName):
	if is_it_local():
		sys.stdin = open(f'./{fileName}', 'r')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if not is_it_local():
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')

def input1(type=int):
	return type(input())

def input2(type=int):
	[a, b] = list(map(type, input().split()))
	return (a, b)

def input3(type=int):
	[a, b, c] = list(map(type, input().split()))
	return (a, b, c)

def input_array(type=int):
	return list(map(type, input().split()))

def input_string():
	s = input()
	return list(s)

def main():
	t = input1()
	for ci in range(t):
		[a, b, c, d] = input_array()
		t = b
		slept = b
		alarm = c
		inf = 0
		while True:
			if slept < a:
				if d >= c:
					inf = 1
					break
				else:
					rem = a - slept
					nw = math.ceil(rem / (c - d))
					t += c * nw
					slept += nw * (c - d)
			else:
				break
		if inf == 1:
			print(-1)
		else:
			print(t)
	pass
READ('in.txt')
main()


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


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


from math import ceil
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		tot = b
		rem = a - b
		tot += int(c * ceil(rem / (c - d)))
		print(tot)


import sys
import os
import heapq
import math
try:
	path = './file/input.txt'
	if os.path.exists(path):
		sys.stdin = open(path, 'r')
except:
	pass
t = int(input())

def printd(value):
	pass

def case():
	arr = list(map(int, input().split(' ')))
	(a, b, c, d) = (arr[0], arr[1], arr[2], arr[3])
	if a > 0:
		a -= b
	if a > 0:
		if d >= c:
			print(-1)
		else:
			count = a // (c - d)
			if a % (c - d) != 0:
				count += 1
			print(b + count * c)
	else:
		print(b)
for _ in range(t):
	case()


import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
t = int(stdin.readline())
for _ in range(t):
	(a, b, c, d) = list(map(int, stdin.readline().rstrip().split()))
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		a -= b
		ans = b
		if a % (c - d) == 0:
			x = a // (c - d)
		else:
			x = a // (c - d) + 1
		ans += x * c
		print(ans)


import sys
import math
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
t = int(input())
for case in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c > d:
		print(b + math.ceil((a - b) / (c - d)) * c)
	else:
		print(-1)


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


from sys import stdin
import math
a = int(stdin.readline())
for b in range(0, a):
	A = stdin.readline().split()
	B = int(A[0])
	C = int(A[1])
	D = int(A[2])
	F = int(A[3])
	if C >= B:
		print(C)
		continue
	count = B - C
	if F >= D:
		print(-1)
		continue
	divide = D - F
	ans = int(D * math.ceil(count / divide)) + C
	print(ans)


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		e = (a - b + c - d - 1) // (c - d)
		print(b + e * c)


from sys import stdout, stdin
(_input, _print) = (stdin.buffer.readline, stdout.write)
(_range, _int) = (range, int)

def solution():
	for _ in _range(_int(_input())):
		(a, b, c, d) = [_int(i) for i in _input().split()]
		a -= b
		ans = 0
		if a > 0:
			ans += b
			if d >= c:
				print(-1)
				continue
			else:
				t = a // (c - d) if a % (c - d) == 0 else a // (c - d) + 1
				print(ans + t * c)
		else:
			print(b)
solution()


t = int(input())
for i in range(t):
	ABCD = list(map(int, input().split()))
	a = ABCD[0]
	b = ABCD[1]
	c = ABCD[2]
	d = ABCD[3]
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		n = int((a - b) / (c - d))
		if (a - b) % (c - d) == 0:
			print(b + c * n)
		else:
			print(b + c * (n + 1))


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


import math
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
	left = math.ceil((a - b) / (c - d))
	print(b + left * c)


t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		num = c - d
		val = (a - b) // num
		if (a - b) % num != 0:
			val = val + 1
		print(b + val * c)


def solve(a, b, c, d):
	if a <= b:
		return b
	elif d >= c:
		return -1
	else:
		n = int((a - b) / (c - d))
		if (a - b) % (c - d) != 0:
			n += 1
		return b + n * c
t = int(input())
while t > 0:
	(a, b, c, d) = [int(x) for x in input().split()]
	ans = solve(a, b, c, d)
	print(ans)
	t -= 1


from sys import stdin, stdout
import collections
import heapq
import itertools
import functools
import math

def read_line():
	return stdin.readline().strip()

def read_int():
	return int(read_line())

def read_arr():
	return read_line().split(' ')

def read_int_arr():
	return [int(x) for x in read_arr()]

def query_interactive(s):
	print(s)
	stdout.flush()

def main():
	T = read_int()
	for _ in range(T):
		(a, b, c, d) = read_int_arr()
		if b >= a:
			print(b)
			continue
		elif d >= c:
			print(-1)
			continue
		else:
			net = c - d
			need = a - b
			cycles = math.ceil(need / net)
			print(b + cycles * c)
			continue
main()


answer = []
for i in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split()]
	if a <= b:
		time = b
	elif c <= d:
		time = -1
	else:
		time = b + c * ((a - b) // (c - d))
		if (a - b) % (c - d):
			time += c
	answer.append(str(time))
print('\n'.join(answer))


import copy
import fractions
import heapq
import math
import random
import sys
T = int(input())
for t in range(T):
	(A, B, C, D) = tuple(map(int, input().split()))
	if A <= B:
		print(B)
	elif C <= D:
		print(-1)
	else:
		print(B + C * math.ceil((A - B) / (C - D)))


import sys
import math
lines = []
for line in sys.stdin:
	if 'exit' == line.rstrip():
		break
	lines.append(line)
t = int(lines[0])
for case in range(1, t + 1):
	l = [int(x) for x in lines[case].split(' ')]
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	time_slept = b
	ans = b
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	gap = c - d
	temp = a - b
	times = math.ceil(temp / gap)
	print(b + times * c)


T_ON = 1
DEBUG_ON = 1
MOD = 998244353

def solve():
	(a, b, c, d) = read_ints()
	if a <= b:
		print(b)
		return
	a -= b
	if c <= d:
		print(-1)
		return
	n = math.ceil(a / (c - d))
	print(b + n * c)

def main():
	T = read_int() if T_ON else 1
	for i in range(T):
		solve()

def debug(*xargs):
	if DEBUG_ON:
		print(*xargs)
from collections import *
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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

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

def print_nums(nums):
	print(' '.join(map(str, nums)))

def YES():
	print('YES')

def Yes():
	print('Yes')

def NO():
	print('NO')

def No():
	print('No')

def First():
	print('First')

def Second():
	print('Second')

def fib(n):
	(a, b) = (0, 1)
	for _ in range(n):
		(a, b) = (b, a + b)
	return a

def fib_ns(n):
	assert n >= 1
	f = [0 for _ in range(n + 1)]
	f[0] = 0
	f[1] = 1
	for i in range(2, n + 1):
		f[i] = f[i - 1] + f[i - 2]
	return f

def fib_to_n(n):
	f = []
	(a, b) = (0, 1)
	while a <= n:
		f.append(a)
		(a, b) = (b, a + b)
	return f

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def xgcd(a, b):
	(x0, x1, y0, y1) = (0, 1, 1, 0)
	while a != 0:
		((q, a), b) = (divmod(b, a), a)
		(y0, y1) = (y1, y0 - q * y1)
		(x0, x1) = (x1, x0 - q * x1)
	return (b, x0, y0)

def lcm(a, b):
	d = gcd(a, b)
	return a * b // d

def is_even(x):
	return x % 2 == 0

def is_odd(x):
	return x % 2 == 1

def modinv(a, m):
	(g, x, _) = xgcd(a, m)
	if g != 1:
		raise Exception('gcd(a, m) != 1')
	return x % m

def mod_add(x, y):
	x += y
	while x >= MOD:
		x -= MOD
	while x < 0:
		x += MOD
	return x

def mod_mul(x, y):
	return x * y % MOD

def mod_pow(x, y):
	if y == 0:
		return 1
	if y % 2:
		return mod_mul(x, mod_pow(x, y - 1))
	p = mod_pow(x, y // 2)
	return mod_mul(p, p)

def mod_inv(y):
	return mod_pow(y, MOD - 2)

def mod_div(x, y):
	return mod_mul(x, mod_inv(y))

def is_prime(n):
	if n == 1:
		return False
	for i in range(2, int(n ** 0.5) + 1):
		if n % i:
			return False
	return True

def gen_primes(n):
	P = [True for _ in range(n + 1)]
	P[0] = P[1] = False
	for i in range(int(n ** 0.5) + 1):
		if P[i]:
			for j in range(2 * i, n + 1, i):
				P[j] = False
	return P

def is_lucky(n):
	return set(list(str(n))).issubset({'4', '7'})
main()


t = int(input())
for k in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	q = 0
	s = 0
	if a > b and c <= d:
		print('-1')
	elif a <= b:
		print(b)
	elif a > b and a - b == (c - d) * d:
		print(b + c * d)
	else:
		s = a - b
		q = s // (c - d)
		if s % (c - d) == 0:
			q = q
		else:
			q = q + 1
		print(b + q * c)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(A, B, C, D) = [int(x) for x in input().split()]
		if A > B and C <= D:
			print(-1)
			continue
		if A <= B:
			print(B)
			continue
		ans = B
		ans += -(-(A - B) // (C - D)) * C
		print(ans)
main()


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


from math import ceil

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

def solve():
	(a, b, c, d) = inp()
	if b >= a:
		return b
	if d >= c:
		return -1
	k = (a - b) / (c - d)
	return b + ceil(k) * c
t = int(input())
for _ in range(t):
	print(solve())


from math import *
t = int(input())
A = [list(map(int, input().split())) for i in range(t)]
for i in A:
	(a, b, c, d) = (i[0], i[1], i[2], i[3])
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		k = ceil((a - b) / (c - d))
		print(b + k * c)


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)


from math import ceil

def unso(a, b, c, d):
	if a - b > 0 and c <= d:
		print(-1)
	elif a - b <= 0:
		print(b)
	else:
		missing_time = a - b
		total_times = ceil(missing_time / (c - d))
		print(b + total_times * c)
t = int(input())
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	ans = unso(a, b, c, d)
	ans


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


tes = int(input())
for q in range(tes):
	a = input().split()
	(a1, b, c, d) = (int(a[0]), int(a[1]), int(a[2]), int(a[3]))
	need = a1 - b
	if need <= 0:
		print(b)
	else:
		more = c - d
		if more <= 0:
			print(-1)
		else:
			q = need % more
			if q == 0:
				print(b + need // more * c)
			else:
				print(b + (need // more + 1) * c)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		res = c - d
		sm = a - b
		div = sm // res
		if sm % res != 0:
			div += 1
		ans = b + c * div
		print(ans)


def answer(a, b, c, d):
	import math
	if b >= a:
		return b
	elif d >= c:
		return -1
	else:
		n = (a - b) / (c - d)
		x = math.ceil(n)
		return b + x * c
t = int(input())
lst = list()
for i in range(t):
	lst.append(input().split())
for i in range(t):
	for j in range(4):
		lst[i][j] = int(lst[i][j])
for i in range(t):
	print(answer(lst[i][0], lst[i][1], lst[i][2], lst[i][3]))


from math import ceil
for t in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		rem = a - b
		if c <= d:
			print(-1)
		else:
			timeDiff = c - d
			ans = ceil(rem / timeDiff) * c + b
			print(ans)


import collections
import operator
for _ in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split(' ')]
	if a <= b:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	res = (a - b) // (c - d)
	if (a - b) % (c - d) != 0:
		res += 1
	total = b + res * c
	print(total)


import math
query = int(input())
for i in range(query):
	(a, b, c, d) = map(int, input().split())
	count = 0
	if a > b:
		a -= b
		if c <= d:
			print(-1)
			continue
		elif a % (c - d) == 0:
			count = a // (c - d)
		else:
			count = math.ceil(a / (c - d))
	print(b + count * c)


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


import math
t = int(input())
for test in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		a = a - b
		delta = c - d
		temp = math.ceil(a / delta)
		print(temp * c + b)


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

def main():
	T = int(input())
	for i in range(1, T + 1):
		(a, b, c, d) = map(int, input().strip().split())
		print(solve(a, b, c, d))
main()


t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		res = c - d
		sm = a - b
		div = sm // res
		if sm % res != 0:
			div += 1
		ans = b + c * div
		print(ans)


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


from math import gcd, ceil
from collections import defaultdict as dd

def lcm(a, b):
	return a * b // gcd(a, b)

def inin():
	return int(input())

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

def ar(element, size):
	return [element for i in range(size)]

def digitsum(num):
	su = 0
	while num:
		su += num % 10
		num //= 10
	return su

def yes():
	print('YES')

def no():
	print('NO')
Testcase = inin()
for n in range(Testcase):
	(a, b, c, d) = inar()
	if c > d:
		if b >= a:
			print(b)
		else:
			cnt = 0
			diff = c - d
			ac = a - b
			cnt = int(ac / diff + 0.9999)
			print(b + cnt * c)
	elif b >= a:
		print(b)
	else:
		print(-1)


TT = int(input())
for _ in range(TT):
	(a, b, c, d) = map(int, input().split())
	rem = a - b
	if rem <= 0:
		print(b)
		continue
	gain = c - d
	if gain <= 0:
		print(-1)
		continue
	import math
	nn = math.ceil(rem / gain)
	ans = b + c * nn
	print(ans)


def sleep(total, req, c, d):
	if c <= d:
		print(-1)
	elif c >= d + req:
		print(total + c)
	else:
		x = req // (c - d)
		if req % (c - d) != 0:
			x += 1
		print(int(total + c * x))
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		req = a - b
		sleep(b, req, c, d)


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]
spf_sieve()

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(a, b, c, d) = int_array()
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		a -= b
		k = ceil(a / (c - d)) * c
		print(b + k)


t = int(input())
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	z = 0
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		dis = c - d
		if (a - b) % (c - d) == 0:
			k = 0
		else:
			k = 1
		rep = (a - b) // (c - d) + k
		print(b + rep * c)


import math
t = int(input())
while t > 0:
	l = list(map(int, input().split()))
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	if b >= a:
		print(b)
	elif a > b and c <= d:
		print(-1)
	else:
		x = math.ceil((a - b) / (c - d))
		print(b + x * c)
	t -= 1


import math
n = int(input())
res = []
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	if b < a and c <= d:
		res.append(-1)
	elif b < a:
		total = b
		e = math.ceil((a - b) / (c - d))
		total = total + e * c
		res.append(total)
	else:
		res.append(b)
for i in res:
	print(i)


def solution():
	(a, b) = input().split()
	b = int(b) - 1
	while b:
		b -= 1
		temp = a
		mi = 9
		ma = 0
		for i in temp:
			mi = min(mi, int(i))
			ma = max(ma, int(i))
		if mi == 0:
			return a
		a = str(int(a) + mi * ma)
	return a
from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b + ceil((a - b) / (c - d)) * c
		print(ans)


import math as mt
t = int(input())
while t > 0:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		print(b + mt.ceil((a - b) / (c - d)) * c)


import sys
from math import ceil
TEST = 0
if TEST == 1:
	input_file = open('inputA.txt', 'r')
	input_from_file = input_file.readlines()

def read_int():
	if TEST:
		return int(input_from_file.pop(0))
	else:
		return int(sys.stdin.readline())

def read_ints():
	if TEST:
		return list(map(int, input_from_file.pop(0).rstrip().split()))
	else:
		return list(map(int, sys.stdin.readline().split()))
t = read_int()
for j in range(t):
	(a, b, c, d) = read_ints()
	if b >= a:
		ans = b
	elif d >= c:
		ans = -1
	else:
		rest = a - b
		N = ceil(rest / (c - d))
		ans = b + N * c
	print(ans)


from math import ceil
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	else:
		ts = c - d
		print(int(b + c * ceil((a - b) / ts)))


def ans(a, b, c, d):
	if b >= a:
		return b
	elif d >= c:
		return -1
	else:
		ans = b
		more = a - b
		one = c - d
		ans += (more + one - 1) // one * c
		return ans
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	print(ans(a, b, c, d))


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		ans = b
	else:
		isleep = b - a
		if d >= c:
			ans = -1
		else:
			cycles = math.ceil((a - b) / (c - d))
			ans = b + cycles * c
	print(ans)


def solve(tot, ini, sets, fall):
	if tot <= ini:
		print(ini)
		return
	if sets <= fall:
		print(-1)
		return
	ans = ini
	if (tot - ini) % (sets - fall) == 0:
		ans += sets * ((tot - ini) // (sets - fall))
	else:
		ans += sets * ((tot - ini) // (sets - fall)) + sets
	print(ans)
t = int(input())
for _ in range(t):
	(tot, ini, sets, fall) = map(int, input().split())
	solve(tot, ini, sets, fall)


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
prime = []

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	if n not in d:
		return v
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			v = dfs(i, d, v, c + 1)
	return v

def main():
	t = inin()
	for _ in range(t):
		(a, b, c, d) = cin()
		if a <= b:
			print(b)
		else:
			a = a - b
			x = c - d
			if x <= 0:
				print(-1)
			else:
				ans = math.ceil(a / x) * c
				print(ans + b)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'R'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'R'[0]:
		A.append(sign * numb)
	return A
main()


for t in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	temp = a - b
	result = temp // (c - d)
	v1 = b + result * (c - d)
	r1 = b + result * c
	v2 = b + (result + 1) * (c - d)
	r2 = b + (result + 1) * c
	if v1 >= a:
		print(r1)
	elif v2 >= a:
		print(r2)


import sys
import math
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	(a, b, c, d) = [int(c) for c in input().split()]
	if b < a and c <= d:
		print(-1)
	elif a <= b:
		print(b)
	else:
		left = a - b
		each = c - d
		times = int(math.ceil(left / each))
		ans = b + times * c
		print(ans)


for x in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a - b > 0 and d >= c:
		print('-1')
	elif a == 1 or a <= b:
		print(b)
	else:
		a = a - b
		if a % (c - d) != 0:
			v = a // (c - d) + 1
		else:
			v = a // (c - d)
		print(b + v * c)


from sys import stdin
from math import ceil

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
T = iinput()
while T:
	T -= 1
	(a, b, c, d) = minput()
	if b >= a:
		time = b
	elif c <= d:
		time = -1
	else:
		t = ceil((a - b) / (c - d))
		time = c * t + b
	print(time)


from sys import stdin
input = stdin.readline
R = lambda : map(int, input().split())
for _ in range(int(input())):
	(a, b, c, d) = R()
	ans = b
	d = c - d
	if d < 1 and a > b:
		print(-1)
	elif b >= a:
		print(b)
	else:
		print(b + (a - b + d - 1) // d * c)


import sys
import math as mt
input = sys.stdin.buffer.readline
t = int(input())
for __ in range(t):
	(a, b, c, d) = map(int, input().split())
	ans = b
	if c - d > 0:
		t1 = c - d
		cnt = mt.ceil((a - b) / t1)
		cnt = max(0, cnt)
		print(b + cnt * c)
	elif ans >= a:
		print(b)
	else:
		print(-1)


import math
from sys import stdin
input = stdin.readline
INF = 10 ** 9 + 7
MAX = 10 ** 7 + 7
MOD = 10 ** 9 + 7
for Ti in range(int(input().strip())):
	(a, b, c, d) = [int(x) for x in input().strip().split()]
	if b >= a:
		print(b)
		continue
	a -= b
	if c <= d:
		print('-1')
		continue
	ans = b + c * math.ceil(a / (c - d))
	print(ans)


import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
from itertools import islice, cycle

def go():
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		return b
	if d >= c:
		return -1
	a = a - b
	cyc = a // (c - d)
	result = b + cyc * c
	a -= cyc * (c - d)
	if a:
		result += c
	return result
t = int(input())
ans = []
for _ in range(t):
	ans.append(str(go()))
print('\n'.join(ans))


def alarm(a, b, c, d):
	n = b
	s = c - d
	count = 0
	while n < a and s > 1:
		n = n + s
		count += 1
	if n < a and s <= 0:
		return '-1'
	elif n < a and s == 1:
		return b + c * (a - b)
	else:
		return b + count * c
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a == b:
		print(b)
	else:
		out = alarm(a, b, c, d)
		print(out)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		y = c - d
		z = math.ceil((a - b) / y)
		print(b + z * c)


def ceil_div(x, y):
	ret = x // y
	if x % y != 0:
		ret += 1
	return ret
t = int(input())
for kase in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	print(b + ceil_div(a - b, c - d) * c)


from math import *
from collections import *
from itertools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(1000000)
d = defaultdict(lambda : 0, {})

def io():
	return map(int, input().split())

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

def o():
	return int(input())

def r(x):
	return range(len(x))

def mk(a, b):
	if a > b:
		return (b, a)
	else:
		return (a, b)

def kl(con, x=0):
	if x == 0:
		print('Yes') if con else print('No')
	elif x == 1:
		print('yes') if con else print('no')
	elif x == 2:
		print('YES') if con else print('NO')
MOD = 1000000007
MAX = float('inf')
MIN = -float('inf')
p = input
for _ in range(o()):
	(a, b, c, d) = io()
	if a <= b:
		print(b)
		continue
	if a > b:
		if c <= d:
			print(-1)
		else:
			x = ceil((a - b) / (c - d))
			print(b + c * x)


t = int(input())
for i1 in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	elif b < a and d >= c:
		print(-1)
		continue
	else:
		t1 = (a - b) / (c - d)
		t2 = (a - b) // (c - d)
		if t1 == t2:
			print(b + c * t2)
		else:
			print(b + c * (t2 + 1))


n = int(input())
for _ in range(n):
	ll = input()
	ll = ll.split()
	l = [int(i) for i in ll]
	res = l[0] - l[1]
	if res <= 0:
		print(l[1])
	else:
		t = l[2] - l[3]
		if t <= 0:
			print('-1')
		else:
			print(l[1] + l[2] * -(-res // t))


import math
t = int(input())
while t > 0:
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print('-1')
	else:
		diff = c - d
		res = diff + b
		ans = a - res
		temp = math.ceil(ans / diff)
		print(b + (temp + 1) * c)
	t = t - 1


import sys
t = int(sys.stdin.readline().strip())
for i in range(t):
	(a, b, c, d) = list(map(int, sys.stdin.readline().strip().split(' ')))
	total = 0
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		total += b
		a -= b
		r = a % (c - d)
		m = a // (c - d)
		if r == 0:
			total += m * c
		else:
			total += (m + 1) * c
		print(total)


test = int(input())
for i in range(test):
	(a, b, c, d) = [int(i) for i in input().split()]
	if b >= a:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		a -= b
		diff = c - d
		ans = b + (a + diff - 1) // diff * c
		print(ans)


import sys
import math

def read_integer():
	return int(sys.stdin.readline())

def read_integers():
	return [int(x) for x in sys.stdin.readline().split(' ')]

def print_integers(nums):
	print(' '.join([str(x) for x in nums]))

def func():
	[a, b, c, d] = read_integers()
	if b >= a:
		return b
	ans = b
	a -= b
	if d >= c:
		return -1
	return b + math.ceil(a / (c - d)) * c
nt = read_integer()
for _ in range(nt):
	print(func())


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


import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

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

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def solve():
	(a, b, c, d) = num_input()
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ans = b
		a = a - b
		num = math.ceil(a / (c - d))
		print(ans + num * c)
t = int(input())
for _ in range(t):
	solve()


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


import math
num_of_rows = input()
for i in range(int(num_of_rows)):
	data_list = list(map(int, input().split()))
	a = data_list[0]
	b = data_list[1]
	c = data_list[2]
	d = data_list[3]
	if b >= a:
		print(b)
		continue
	elif b < a and d >= c:
		print(-1)
		continue
	else:
		time_passed = b
		a = a - b
		factor = math.ceil(a / (c - d))
		time_passed += factor * c
		print(time_passed)


from math import ceil
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		a -= b
		need = ceil(a / (c - d))
		total = need * c
		print(total + b)


import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))
t = nn()
for _ in range(t):
	(a, b, c, d) = lm()
	sleep_sum = b
	if a > b and c > d:
		print(sleep_sum + math.ceil((a - b) / (c - d)) * c)
	elif b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		print(sleep_sum)


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		left = a - b
		cal = math.ceil(left / (c - d))
		ans = b + c * cal
		print(ans)


import sys
from math import ceil, sqrt
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())

def isp(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
primes = []
for _ in range(cin()):
	(a, b, c, d) = mi()
	ans = b
	d = c - d
	req = max(0, a - b)
	if req > 0 and d <= 0:
		print(-1)
	else:
		df = ceil(req / max(1, d))
		ans += c * df
		print(ans)


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


import math

def func(a, b, c, d):
	if a <= b:
		return b
	elif a > b and c > d:
		l = b + math.ceil((a - b) / (c - d)) * c
		return l
	else:
		return -1
n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	print(func(a, b, c, d))


from math import ceil
t = int(input())
for i in range(t):
	(a, b, c, d) = list(map(int, input().strip().split(' ')))
	ret = 0
	if a <= b:
		print(str(b))
		continue
	if d >= c:
		if a <= b:
			ret = b
		else:
			ret = -1
	else:
		ret = min(a, b) + c * max(0, ceil((a - b) / (c - d)))
	print(str(ret))


from collections import *
from bisect import *
from math import *
from heapq import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	s = b
	rem = a - s
	if c > d:
		f = ceil(rem / (c - d))
		s += f * c
		print(s)
		continue
	else:
		print(-1)


def fun(a, b, c, d):
	sleep = c - d
	need = a - b
	if need > 0:
		if sleep > 0:
			return (need + sleep - 1) // sleep * c + b
		else:
			return -1
	else:
		return b
for _ in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split()]
	print(fun(a, b, c, d))


from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
	(a, b, c, d) = map(int, stdin.readline().split())
	ans = b
	if a <= b:
		print(ans)
		continue
	if c <= d:
		print(-1)
		continue
	ans += math.ceil((a - b) / (c - d)) * c
	print(ans)


import math

def solution():
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		return b
	delta = c - d
	if delta <= 0:
		return -1
	return b + math.ceil((a - b) / delta) * c
for _ in range(int(input())):
	print(solution())


import math
T = int(input())
for tt in range(0, T):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		x = a - b
		y = c - d
		s = b + c * math.ceil(x / y)
		print(s)


t = int(input())
cas_spanja = 0
odg = -1
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	povecava_spa = c - d
	cas_spanja = b
	if b >= a:
		odg = b
	elif d >= c:
		odg = -1
	else:
		odg = b
		rabimo = a - b
		faktor = int(rabimo / povecava_spa)
		if rabimo % povecava_spa != 0:
			faktor += 1
		odg += faktor * c
	print(odg)


from math import ceil
t = int(input())
while t > 0:
	(a, b, c, d) = [int(i) for i in input().split()]
	if a <= b:
		print(b)
	elif a > b and d >= c:
		print(-1)
	else:
		diff = a - b
		diff1 = c - d
		print(b + ceil(diff / diff1) * c)
	t -= 1


import math

def solve():
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		return b
	if d >= c:
		return -1
	return b + c * math.ceil((a - b) / (c - d))

def main():
	t = int(input())
	while t:
		print(solve())
		t -= 1
main()


import math
t = int(input())
for x in range(t):
	ar = input().split()
	a = int(ar[0])
	b = int(ar[1])
	c = int(ar[2])
	d = int(ar[3])
	temp = 0
	if a <= b:
		print(b)
	else:
		a = a - b
		if c <= d:
			print(str(-1))
		else:
			if a % (c - d) != 0:
				temp = int(math.floor(a / (c - d)) + 1)
			else:
				temp = int(a / (c - d))
			print(str(int(b + c * temp)))


xm = int(input())
for rt in range(xm):
	l = input().split()
	for i in range(len(l)):
		l[i] = int(l[i])
	if l[1] >= l[0]:
		print(l[1])
	elif l[3] > l[2]:
		print(-1)
	elif l[2] == l[3]:
		print(-1)
	else:
		m = (l[0] - l[1]) / (l[2] - l[3])
		if not m == int(m):
			k = int(m) + 1
		else:
			k = int(m)
		t = l[2] * k + l[1]
		print(t)


import math
t = int(input())
while t > 0:
	[a, b, c, d] = [int(i) for i in input().split()]
	if b >= a:
		print(b)
		t -= 1
		continue
	a -= b
	if c <= d:
		print(-1)
		t -= 1
		continue
	if a % (c - d) == 0:
		f = a // (c - d)
	else:
		f = a // (c - d)
		f += 1
	print(b + f * c)
	t -= 1


import math
n = int(input())
for i in range(n):
	(a, b, c, d) = list(map(int, input().rstrip().split()))
	if b >= a:
		print(b)
	elif a > b and c > d:
		new_c = b + (c - d)
		new = a - new_c
		repeat = math.ceil(new / (c - d)) + 1
		print(b + c * repeat)
	else:
		print(-1)


import math
import sys
from collections import Counter
input = sys.stdin.readline

def solve(A, B, C, D):
	if A <= B:
		print(B)
	elif C <= D:
		print(-1)
	else:
		ans = B
		res = A - B
		ans += math.ceil(res / (C - D)) * C
		print(ans)
for _ in range(int(input())):
	(A, B, C, D) = map(int, input().split())
	solve(A, B, C, D)


from math import ceil
cases = int(input())
for _ in range(cases):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif a > b and c < d:
		print(-1)
	else:
		try:
			print(b + c * ceil((a - b) / (c - d)))
		except:
			print(-1)


from sys import stdin, stdout
for _ in range(int(stdin.readline().strip())):
	(a, b, c, d) = map(int, stdin.readline().split())
	a -= b
	time = b
	if a <= 0:
		print(time)
	elif c - d > 0:
		time += c * (a // (c - d))
		a -= (c - d) * (a // (c - d))
		if a > 0:
			time += c
		print(time)
	else:
		print(-1)


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


from math import ceil

def compute():
	(a, b, c, d) = [int(x) for x in input().split()]
	if b >= a:
		return b
	if d >= c:
		return -1
	qt_resets = ceil((a - b) / (c - d))
	return b + qt_resets * c
t = int(input())
for _ in range(t):
	print(compute())


from math import *
t = int(input())
for j in range(t):
	g = input().split()
	a = int(g[0])
	b = int(g[1])
	c = int(g[2])
	d = int(g[3])
	k = 0
	if a <= b:
		print(b)
	elif d < c:
		a -= b
		print(b + c * ceil(a / (c - d)))
	else:
		print(-1)


inputTakenAll = []
for i in range(int(input())):
	inputTakenAll.append(input().split(' '))
for inputTaken in inputTakenAll:
	total = int(inputTaken[0])
	alreadySlept = int(inputTaken[1])
	timeToAlarm = int(inputTaken[2])
	gettingToSleep = int(inputTaken[3])
	halfSlept = 0
	totalTime = 0
	if alreadySlept >= total:
		print(alreadySlept)
	elif gettingToSleep >= timeToAlarm:
		print(-1)
	else:
		diff = total - alreadySlept
		mul = int(diff / (timeToAlarm - gettingToSleep))
		mulAdd = mul * (timeToAlarm - gettingToSleep)
		if mulAdd + alreadySlept < total:
			mul += 1
			mulAdd = mul * (timeToAlarm - gettingToSleep)
		mulAdd2 = mul * gettingToSleep
		print(alreadySlept + mulAdd + mulAdd2)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(str(i)[2:-1]) for i in input().rstrip()]
for _ in range(val()):
	(a, b, c, d) = li()
	tot = 0
	if b < a and c <= d:
		print(-1)
		continue
	tot += b
	a -= b
	if a <= 0:
		print(tot)
		continue
	temp = math.ceil(a / (c - d))
	tot += c * temp
	print(tot)


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = (int(x) for x in input().split())
	res = 0
	if a <= b:
		res = b
	elif c <= d:
		res = -1
	else:
		n = math.ceil((a - b) / (c - d))
		res = b + n * c
	print(res)


import math
tc = int(input())
for i in range(tc):
	(a, b, c, d) = input().split(' ')
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(b)
	else:
		add = c - d
		if add > 0:
			n = int(math.ceil((a - b) / add))
			print(n * c + b)
		else:
			print(-1)


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	a -= b
	c1 = c
	c -= d
	if a <= 0:
		a = 0
	if c <= 0:
		if a <= 0:
			print(b)
		else:
			print(-1)
	else:
		print(b + c1 * math.ceil(a / c))


import math
k = int(input())
for i in range(k):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	elif d >= c:
		print(-1)
		continue
	else:
		kef = c - d
		de = a - b
		cef = math.ceil(de / kef)
		io = b + cef * c
		print(io)


import math
n = int(input())
q = []
for i in range(n):
	q.append([int(i) for i in input().split()])
for j in q:
	(a, b, c, d) = j
	total = b
	if a <= b:
		print(b)
		continue
	else:
		if c - d <= 0:
			print(-1)
			continue
		k = c - d
		n = math.ceil((a - b) / k)
		total += n * c
		print(total)


n = int(input())
loop = 0
while loop != n:
	L = input().split(' ')
	for i in range(4):
		L[i] = int(L[i])
	if L[0] <= L[1]:
		print(L[1])
	elif L[2] <= L[3]:
		print(-1)
	else:
		more_sleep = L[0] - L[1]
		snooze = L[2] - L[3]
		if more_sleep % snooze == 0:
			times = int(more_sleep / snooze)
		else:
			times = int(more_sleep / snooze) + 1
		print(L[1] + L[2] * times)
	loop += 1


numTest = int(input())
for _ in range(numTest):
	inputLine = input()
	tempStr = inputLine.split(' ')
	a = int(tempStr[0])
	b = int(tempStr[1])
	c = int(tempStr[2])
	d = int(tempStr[3])
	if b >= a:
		print(b)
	else:
		diff = c - d
		if diff <= 0:
			print(-1)
		else:
			count = int((a - b) / diff)
			count = int(count if (a - b) % diff == 0 else count + 1)
			print(b + c * count)


import sys, math
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	(a, b, c, d) = map(int, I().split())
	total_slep = b
	time = b
	if a > b and d >= c:
		ans += '-1\n'
	elif a <= b:
		ans += str(b) + '\n'
	else:
		x = c - d
		time_need = a - b
		n = math.ceil(time_need / x)
		time += n * c
		ans += str(time) + '\n'
print(ans)


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def calculate_bed_time(a, b, c, d):
	if b >= a:
		return b
	sleep_once_time = c - d
	if sleep_once_time <= 0:
		return -1
	sleep_times = (a - b) // sleep_once_time
	if (a - b) % sleep_once_time != 0:
		sleep_times += 1
	return b + sleep_times * c
iters = int(input())
for i in range(iters):
	(a, b, c, d) = map(int, input().split())
	print(calculate_bed_time(a, b, c, d))


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


import sys

def input():
	return sys.stdin.readline()
answers = []
t = int(input())
for _ in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	if b >= a:
		answers.append(b)
		continue
	a = a - b
	if c - d <= 0:
		answers.append(-1)
		continue
	ans = b
	ans += c * (int((a - 1) / (c - d)) + 1)
	answers.append(ans)
print(*answers, sep='\n')


def sol(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	(res, dt) = (a - b, c - d)
	if res % dt == 0:
		return b + res // dt * c
	return b + (res // dt + 1) * c
n = int(input())
while n > 0:
	(a, b, c, d) = [int(val) for val in input().split(' ')]
	ans = sol(a, b, c, d)
	print(ans)
	n -= 1


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b, flush=True)
	elif d >= c:
		print('-1', flush=True)
	elif c != d:
		z = math.ceil(int(a - b) / int(c - d))
		print(int(z * c + b), flush=True)


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


def first(a, b, c, d):
	if b >= a:
		return b
	elif a > b and c - d <= 0:
		return -1
	elif (a - b) // (c - d) * (c - d) + b >= a:
		return b + (a - b) // (c - d) * c
	else:
		return b + ((a - b) // (c - d) + 1) * c
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	print(first(a, b, c, d))


for _ in range(int(input())):
	l = list(map(int, input().split()))
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	if d >= c and a > b:
		print(-1)
	elif a <= b:
		print(b)
	else:
		x = a - b
		y = c - d
		if x % y == 0:
			count = int(x // y)
		else:
			count = int(x // y + 1)
		r = b + count * c
		print(r)


t = int(input())
for i in range(0, t):
	abcd = input().split()
	a = int(abcd[0])
	b = int(abcd[1])
	c = int(abcd[2])
	d = int(abcd[3])
	if b < a and d >= c:
		print(-1)
	elif b >= a:
		print(b)
	elif b < a and d < c:
		n = (a - b) / (c - d)
		if n > int(n):
			print(b + c * int(n) + c)
		else:
			print(b + c * int(n))


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		s = 0
		df = c - d
		dv = a - b
		ct = math.ceil(dv / df)
		s = b + ct * c
		print(s)


testcases = int(input())
n = 0
while n < testcases:
	n += 1
	time = input()
	time = time.split(' ')
	needs = int(time[0])
	gets = int(time[1])
	more_time = int(time[2])
	to_sleep = int(time[3])
	if gets >= needs:
		print(gets)
	elif more_time <= to_sleep:
		print(-1)
	else:
		more = needs - gets
		actual_sleep = more_time - to_sleep
		if more % actual_sleep != 0:
			multiply_by = more // actual_sleep + 1
		if more % actual_sleep == 0:
			multiply_by = more // actual_sleep
		ans = abs(int(multiply_by * more_time + gets))
		print(ans)


import sys
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	tc = 0
	if tc:
		input = sys.stdin.readline
	else:
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')

def solve():
	for _ in range(ii()):
		(a, b, c, d) = mi()
		if a <= b:
			print(b)
			continue
		a -= b
		if c > d:
			c1 = c - d
			x = ceil(a / c1)
			print(b + x * c)
		else:
			print('-1')
solve()


import math
for _ in range(int(input())):
	final = []
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		final.append(b)
	elif d >= c:
		final.append(-1)
	else:
		(x, y) = (a - b, c - d)
		z = math.ceil(float(x) / float(y))
		final.append(z * c + b)
	print(int(*final))


T = int(input())
for i in range(T):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	else:
		a = a - b
		x = c - d
		count = 0
		if a % x == 0:
			count = a // x
		else:
			count = a // x + 1
		ans = b + c * count
		print(ans)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		lagbe = a - b
		unit = c - d
		if lagbe % unit == 0:
			x = lagbe // unit
		else:
			x = lagbe // unit + 1
		print(b + x * c)


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


import math

def check(a, b, c, d):
	ans = 0
	cnt = b
	ind = 0.0
	flag = True
	if a <= b:
		ans = b
	elif a > b:
		if c - d <= 0:
			ans = -1
		else:
			ind = (a - b) / (c - d)
			ans = math.ceil(ind)
			ans = ans * c + b
	print(ans)
for i in range(int(input())):
	(x, y, l, r) = map(int, input().split())
	check(x, y, l, r)


from sys import stdin
for _ in range(int(stdin.readline())):
	(a, b, c, d) = (int(i) for i in stdin.readline().split())
	f = c - d
	ans = b
	a -= b
	if f <= 0:
		if a > 0:
			ans = -1
	elif a > 0:
		ans += (a + f - 1) // f * c
	print(ans)


import math

def solve(a, b, c, d):
	t = a - b
	if t <= 0:
		return b
	if c <= d:
		return -1
	count = c - d
	res = math.ceil(t / count) * c
	return res + b
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	res = solve(a, b, c, d)
	print(res)


from pprint import pprint
import sys
input = sys.stdin.readline
import math
q = int(input())
for _ in range(q):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	a -= b
	if d >= c:
		print(-1)
		continue
	percycle = c - d
	needcycle = math.ceil(a / percycle)
	print(b + c * needcycle)


import math
t = int(input())
while t > 0:
	t -= 1
	arr = list(map(int, input().split()))
	a = arr[0]
	b = arr[1]
	c = arr[2]
	d = arr[3]
	s = a - b
	if s <= 0:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = a - b
		y = c - d
		z = math.ceil(x / y)
		s = b + z * c
		print(s)


import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
T = int(stdin.readline())
for _ in range(T):
	(a, b, c, d) = list(map(int, stdin.readline().split()))
	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)


import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

def solve(a, b, c, d):
	a -= b
	if a <= 0:
		return b
	if c <= d:
		return -1
	t = c - d
	need = (a + t - 1) // t
	return b + need * c
T = int(input())
for t in range(T):
	(a, b, c, d) = map(int, input().split())
	print(solve(a, b, c, d))


testcase = int(input())
for test in range(testcase):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	else:
		m = (a - b) // (c - d)
		if (a - b) % (c - d) != 0:
			m += 1
		res = m * c + b
		print(res)


def solve(a, b, c, d):
	if b >= a:
		return b
	a -= b
	if c - d <= 0:
		return -1
	ans = b
	ans += a // (c - d) * c
	a %= c - d
	if a == 0:
		return ans
	ans += c
	return ans
t = int(input())
for case in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	print(solve(a, b, c, d))


t = int(input())
for i in range(t):
	(a, b, c, d) = list(map(int, input().split(' ')))
	ans = -1
	if b >= a:
		ans = b
	elif not (b < a and c <= d):
		ans = b
		a -= b
		x = int(a / (c - d))
		ans += (x + 1) * c if a % (c - d) else x * c
	print(ans)


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


t = int(input())
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	if a <= b:
		print(b)
		continue
	elif d >= c:
		print(-1)
		continue
	if (a - b) % (c - d) == 0:
		n = (a - b) // (c - d)
	else:
		n = (a - b) // (c - d) + 1
	ans = b + n * c
	print(ans)


def solve():
	for _ in range(int(input())):
		(a, b, c, d) = map(int, input().split())
		t_t = 0
		if d >= c and b < a:
			print(-1)
		elif a <= b:
			print(b)
		else:
			diff1 = c - d
			diff2 = a - b
			if diff2 % diff1 == 0:
				x = diff2 // diff1
			else:
				x = diff2 // diff1 + 1
			t_t = b + x * c
			print(t_t)
solve()


import math

def funMain(a, b, c, d):
	total = b
	total_sleep = b
	remaining = a - b
	if total_sleep < a:
		if d >= c:
			return -1
		diff = c - d
		times = math.ceil(remaining / diff)
		return b + c * times
	return total
n = int(input())
for i in range(0, n):
	(a, b, c, d) = map(int, input().split(' '))
	print(funMain(a, b, c, d))


t = int(input())
testResults = []
TimeSleep = 0
for i in range(t):
	inp = input()
	test = inp.split(' ')
	a = int(test[0])
	b = int(test[1])
	c = int(test[2])
	d = int(test[3])
	if b >= a:
		testResults.append(b)
	elif c - d <= 0:
		testResults.append(-1)
	else:
		x1 = (a - b) / (c - d)
		x = round(x1 + 0.49999)
		if b + x * (c - d) >= a:
			TimeSleep = b + x * c
			testResults.append(TimeSleep)
		else:
			print('something wrong')
			break
for e1 in range(len(testResults)):
	print(testResults[e1])


import math

def fun(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	num = a - b
	num1 = c - d
	return b + math.ceil(num / num1) * c
t = int(input())
while t > 0:
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	print(fun(a, b, c, d))
	t = t - 1


import math

def process():
	(a, b, c, d) = map(int, input().split())
	t = 0
	if a <= b:
		return b
	a -= b
	t += b
	if d >= c:
		return -1
	return t + c * math.ceil(a / (c - d))
for _ in range(int(input())):
	print(process())


a = [i for i in range(100)]
asewq = 100001
for i in range(int(input())):
	(a, res, c, asdnsa) = map(int, input().split())
	if a <= res:
		print(res)
	elif c <= asdnsa:
		print(-1)
	elif (a - res) % (c - asdnsa) == 0:
		print(res + (a - res) // (c - asdnsa) * c)
	else:
		print(res + ((a - res) // (c - asdnsa) + 1) * c)


from sys import stdin, stdout
n = int(stdin.readline())
for i in range(n):
	(a, b, c, d) = map(int, stdin.readline().split())
	if b >= a:
		stdout.write(str(b) + '\n')
		continue
	need = a - b
	if c - d <= 0:
		stdout.write(str(-1) + '\n')
		continue
	cyc = need // (c - d)
	if need % (c - d) != 0:
		cyc += 1
	ans = cyc * c + b
	stdout.write(str(ans) + '\n')


ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
for _ in range(inp()):
	(a, b, c, d) = ip()
	if a <= b:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	t1 = b
	t2 = c - d
	a -= b
	n = (a - 1) // t2 + 1
	t1 += n * c
	print(t1)


def main():
	t = int(input())
	for _ in range(t):
		(a, b, c, d) = map(int, input().split())
		a -= b
		if a <= 0:
			print(b)
		elif d >= c:
			print(-1)
		else:
			times = a // (c - d)
			if a % (c - d) != 0:
				times += 1
			print(b + times * c)
main()


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


import math

def findTime(a, b, c, d):
	sleep = b
	if sleep < a:
		remaining_time = a - sleep
		if c > d:
			sleep_time = math.ceil(remaining_time / (c - d))
			time = sleep_time * c + sleep
			return time
		else:
			return -1
	else:
		return b
test = int(input())
while test:
	list = input().split()
	a = int(list[0])
	b = int(list[1])
	c = int(list[2])
	d = int(list[3])
	ans = findTime(a, b, c, d)
	print(ans)
	test -= 1


import math
testcases = int(input())
for i in range(testcases):
	(min_req, first_awake, snooze_time, time_to_fall_asleep) = map(int, input().split())
	if first_awake >= min_req:
		print(first_awake)
	elif time_to_fall_asleep >= snooze_time:
		print(-1)
	else:
		total_minutes = int(first_awake)
		hours_remaining = min_req - first_awake
		cycles_required = math.ceil(hours_remaining / (snooze_time - time_to_fall_asleep))
		total_minutes += cycles_required * snooze_time
		print(total_minutes)


n = int(input())
for i in range(n):
	div = 0
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print('-1')
	else:
		diff = a - b
		if diff % (c - d) != 0:
			div = diff // (c - d) + 1
		else:
			div = diff // (c - d)
		print(b + div * c)


T = int(input())
import math
n = [0] * T
m = [0] * T
a = [0] * T
p = [0] * T
for t in range(T):
	(a, b, c, d) = [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)


import math
t = int(input())
for u in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d and a > b:
		print(-1)
	else:
		n = (a - b) / (c - d)
		n1 = math.ceil(n)
		ans = b + n1 * c
		print(ans)


import math
from collections import Counter

def listToString(l):
	s = ''
	for c in l:
		s += c
	return s

def solution():
	n = int(input())
	for i in range(n):
		l = input().split()
		a = int(l[0])
		b = int(l[1])
		c = int(l[2])
		d = int(l[3])
		if a <= b:
			print(b)
		elif c <= d:
			print(-1)
		else:
			a -= b
			rem = math.ceil(a / (c - d))
			print(b + rem * c)
solution()


from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
	(a, b, c, d) = map(int, stdin.readline().split())
	if a <= b:
		stdout.write(str(b) + '\n')
	elif c <= d:
		stdout.write('-1\n')
	else:
		z = c - d
		k = int(math.ceil((a - b) / z))
		z = k * c
		stdout.write(str(b + z) + '\n')


n = int(input())
while n:
	l1 = [int(x) for x in input().split()]
	(a, b, c, d) = (l1[0], l1[1], l1[2], l1[3])
	if b >= a:
		print(b)
	else:
		sleep = c - d
		if sleep <= 0:
			print(-1)
		else:
			a -= b
			ans = b + c * (int(a / sleep) + int(a % sleep != 0))
			print(ans)
	n -= 1


import math
n = int(input())
for i in range(n):
	line = input().split(' ')
	a = int(line[0])
	b = int(line[1])
	c = int(line[2])
	d = int(line[3])
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		q = math.ceil((a - b) / (c - d))
		print(b + q * c)


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


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		rem = a - b
		if d >= c:
			print(-1)
		else:
			rest = c - d
			cnt = math.ceil(rem / rest)
			ans = b + c * cnt
			print(ans)


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


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if c == d or b >= a:
		if b >= a:
			print(b)
		else:
			print(-1)
	elif c < d:
		print(-1)
	else:
		if (a - b) % (c - d) == 0:
			f = (a - b) // (c - d)
		else:
			f = (a - b) // (c - d) + 1
		print(c * f + b)


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


p = int(input())
while p > 0:
	p = p - 1
	ls = list()
	na = input()
	ls = na.split()
	for i in range(0, len(ls)):
		ls[i] = int(ls[i])
	if ls[0] <= ls[1]:
		print(ls[1])
		continue
	if ls[2] <= ls[3]:
		print('-1')
		continue
	n = ls[2] - ls[3]
	x = ls[0] - ls[1]
	z = (x + n - 1) // n
	print(z * ls[2] + ls[1])


from collections import defaultdict as dfd
import math
import sys

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def main():
	for _ in range(int(input())):
		(a, b, c, d) = MI()
		rem = a - b
		if a <= b:
			print(b)
		elif a > b and c <= d:
			print(-1)
		else:
			print(math.ceil(rem / (c - d)) * c + b)
main()


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	ans = 0
	if b >= a:
		ans = b
	elif d >= c:
		ans = -1
	else:
		diff = c - d
		remaining_time = a - b
		ans = b + c * int(ceil(remaining_time / diff))
	print(ans)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		abcd = list(map(int, input().split()))
		(a, b, c, d) = (abcd[0], abcd[1], abcd[2], abcd[3])
		if b >= a:
			print(b)
			continue
		if c <= d:
			print(-1)
			continue
		ans = 0
		count = math.ceil((a - b) / (c - d))
		ans = b + count * c
		print(ans)
main()


for t in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		fa = 0
		ans = 0
		temp = b
		a = a - b
		m = a % (c - d)
		if m == 0:
			ans = a // (c - d)
		else:
			ans = a // (c - d) + 1
		fa = temp + c * ans
		print(fa)


import sys
import math
from collections import deque
input = sys.stdin.buffer.readline
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
	if (a - b) % (c - d) == 0:
		print(b + (a - b) // (c - d) * c)
	else:
		print(b + ((a - b) // (c - d) + 1) * c)


import math
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	x = a - b
	diff = c - d
	if b >= a:
		print(b)
	elif diff <= 0:
		print(-1)
	else:
		mul = math.ceil(x / diff)
		print(b + mul * c)


for i in range(int(input())):
	(ts, fa, na, asl) = map(int, input().split())
	ti = 0
	if fa >= ts:
		print(fa)
		continue
	else:
		ti += fa
		ts -= fa
		du = na - asl
		if du <= 0:
			print(-1)
			continue
		b = ts // du
		if ts % du != 0:
			b += 1
		ti += b * na
		print(ti)


t = int(input())
for we in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		s = b
		diff = c - d
		times = (a - b) // (c - d)
		frac = (a - b) % (c - d)
		if frac != 0:
			times += 1
		b += times * c
		print(b)


from sys import stdin
from math import ceil
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	(a, b, c, d) = [int(x) for x in inp().split()]
	hoursSlept = b
	time = b
	if b >= a:
		print(b)
		continue
	elif d >= c:
		print(-1)
		continue
	time += ceil((a - b) / (c - d)) * c
	print(time)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(b)
	else:
		temp = a - b
		temp1 = c - d
		if temp1 <= 0:
			print(-1)
		else:
			r = math.ceil(temp / temp1)
			print(b + r * c)


import random
import math
LI = lambda : list(map(int, input().split()))
MI = lambda : map(int, input().split())
yes = lambda : print('Yes')
no = lambda : print('No')
I = lambda : list(input())
J = lambda x: ''.join(x)
II = lambda : int(input())
SI = lambda : input()
t = II()
for q in range(t):
	(a, b, c, d) = MI()
	if b < a and c <= d:
		print(-1)
	elif b >= a:
		print(b)
	else:
		print(b + math.ceil((a - b) / (c - d)) * c)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	t = I()
	rr = []
	for _ in range(t):
		(a, b, c, d) = LI()
		if b >= a:
			rr.append(b)
		elif c <= d:
			rr.append(-1)
		else:
			t = c - d
			k = (a - b + t - 1) // t
			rr.append(b + k * c)
	return JA(rr, '\n')
print(main())


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if c <= d and a > b:
		print('-1')
		continue
	elif a <= b:
		print(b)
		continue
	elif c > d and a > b:
		k = (a - b) % (c - d)
		l = (a - b) // (c - d)
		if k == 0:
			print(b + c * l)
		else:
			print(b + c * (l + 1))


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		s = b
		x = math.ceil((a - b) / (c - d))
		s = s + x * c
		print(int(s))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		(a, b, c, d) = list(map(int, input().split()))
		if b >= a:
			yield b
		elif d >= c:
			yield (-1)
		else:
			didi = (a - b) % (c - d)
			bibi = (a - b) // (c - d)
			if didi:
				bibi += 1
			yield (bibi * c + b)
t = int(input())
ans = gift()
print(*ans, sep='\n')


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


import math

def solution():
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		print(b)
		return
	if d >= c:
		print(-1)
		return
	a -= b
	slp = c - d
	rounds = math.ceil(a / slp)
	wake = b + rounds * c
	print(wake)
T = int(input())
for t in range(T):
	solution()


def solve(a, b, c, d):
	ans = b
	a -= b
	if a <= 0:
		print(ans)
		return
	if d >= c:
		print(-1)
		return
	r = (a + (c - d - 1)) // (c - d)
	print(r * c + ans)
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	solve(a, b, c, d)


import collections
from math import ceil

def solve(a, b, c, d):
	if d >= c and a > b:
		return -1
	elif a <= b:
		return b
	sleepCycles = ceil((a - b) / (c - d))
	return b + sleepCycles * c
t = int(input().strip())
for i in range(t):
	[a, b, c, d] = list(map(int, input().strip().split()))
	print(solve(a, b, c, d))


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif a > b and c - d <= 0:
		print(-1)
	elif a > b and c > d:
		if (a - b) % (c - d) != 0:
			cn = (a - b) // (c - d) + 1
		else:
			cn = (a - b) // (c - d)
		gotOut = b + cn * c
		print(gotOut)


import math

def A(a, b, c, d):
	timeremaining = a - b
	timecount = b
	if timeremaining <= 0:
		print(timecount)
		return
	if c - d <= 0:
		print(-1)
		return
	else:
		n = timeremaining / (c - d)
		n = math.ceil(n)
		time = b + n * c
		print(time)
		return
x = int(input())
for _ in range(x):
	(a, b, c, d) = map(int, input().split())
	A(a, b, c, d)


import math
T = int(input())

def solve():
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		return b
	a -= b
	if d >= c:
		return -1
	else:
		return math.ceil(a / (c - d)) * c + b
for _ in range(T):
	print(solve())


def solve1():
	from math import ceil
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		print(b + ceil((a - b) / (c - d)) * c)
for _ in range(int(input())):
	solve1()


import sys
import collections

def set_debug(debug_mode=False):
	if debug_mode:
		fin = open('input.txt', 'r')
		sys.stdin = fin
t = int(input())
for ti in range(1, t + 1):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
		continue
	elif c <= d:
		print(-1)
		continue
	elif (a - b) % (c - d) == 0:
		print(b + (a - b) // (c - d) * c)
		continue
	else:
		print(b + ((a - b) // (c - d) + 1) * c)


import math

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


def sm(a, b, c, d):
	if b >= a:
		print(b)
		return
	if d >= c:
		print('-1')
		return
	print(b + (a - b + c - d - 1) // (c - d) * c)
t = int(input())
while t != 0:
	(a, b, c, d) = map(int, input().split(' '))
	sm(a, b, c, d)
	t = t - 1


def solve():
	(a, b, c, d) = map(int, input().split())
	till_now = 0
	if a <= b:
		return b
	if c <= d:
		return -1
	till_now += b
	rem = a - till_now
	diff = c - d
	if rem % diff == 0:
		n = rem // diff
	else:
		n = rem // diff + 1
	return b + c * n
for _ in range(int(input())):
	print(solve())


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		total = b
		hrs = b
		s = (a - b) // (c - d)
		total += s * c
		if (a - b) % (c - d) != 0:
			total += c
		print(total)


for num in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a - b > 0:
		if c - d <= 0:
			print(-1)
		else:
			sleep_req = a - b
			added_sleep = c - d
			cycles = int(sleep_req / added_sleep) + (sleep_req % added_sleep != 0)
			print(b + cycles * c)
	else:
		print(b)


from math import ceil

def solve(a, b, c, d):
	sleep_left = a - b
	time_passed = b
	sleep_dt = c - d
	if sleep_left <= 0:
		return time_passed
	elif sleep_dt <= 0:
		return -1
	else:
		n = ceil(sleep_left / sleep_dt)
		return time_passed + c * n
T = int(input())
for _ in range(T):
	nums = tuple(map(int, input().split()))
	print(solve(*nums))


import math
t = int(input())
while True:
	if t == 0:
		break
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if c <= d:
		print('-1')
		continue
	sleepleft = a - b
	count = math.ceil(sleepleft / (c - d))
	result = b + count * c
	print(result)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		(a, b, c, d) = In()
		if a <= b:
			print(b)
		elif c <= d:
			print(-1)
		else:
			print(b + int(math.ceil((a - b) / (c - d))) * c)
main()


def fun(a, b, c, d):
	if a <= b:
		print(b)
		return
	if d >= c:
		print(-1)
		return
	val = (a - b) // (c - d)
	if b + (c - d) * val >= a:
		print(b + val * c)
	else:
		print(b + (val + 1) * c)
for i in range(0, int(input())):
	(a, b, c, d) = [int(x) for x in input().split()]
	fun(a, b, c, d)


def inp():
	return map(int, input().split())

def solve():
	(a, b, c, d) = inp()
	if a <= b:
		return b
	if c <= d:
		return -1
	dist = a - b
	v = c - d
	x = (dist + v - 1) // v
	return b + c * x

def main():
	t = int(input())
	while t:
		print(solve())
		t -= 1
main()


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


t = int(input())
while t:
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(b)
	elif a > b and c - d <= 0:
		print('-1')
	else:
		ab = a - b
		cd = c - d
		x = ab // cd
		y = ab % cd
		if y != 0:
			x = x + 1
		total = b + x * c
		print(total)
	t = t - 1


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print('-1')
	else:
		left = a - b
		slept = c - d
		multi = ceil(left / slept)
		ans = b + multi * c
		print(ans)


t = int(input())
while t != 0:
	t -= 1
	l = list(map(int, input().split()))
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	if a < b or a == b:
		print(b)
		continue
	elif d >= c:
		print(-1)
		continue
	else:
		total = a - b
		x = round(total / (c - d))
		if x * (c - d) >= total:
			print(c * x + b)
		else:
			print(c * (x + 1) + b)


def sol(a, b, c, d):
	if a <= b:
		return b
	elif c <= d:
		return -1
	else:
		a -= b
		if a % (c - d) == 0:
			ans = b + a // (c - d) * c
		else:
			ans = b + (a // (c - d) + 1) * c
		return ans
t = int(input())
lst = []
for j in range(t):
	tup = tuple(map(int, input().split()))
	(a, b, c, d) = tup
	lst.append(sol(a, b, c, d))
for i in lst:
	print(i)


def getup(l):
	(a, b, c, d) = l
	if b >= a:
		return b
	if c <= d:
		return -1
	non = (a - b + c - d - 1) // (c - d)
	return b + non * c
t = int(input())
for _ in range(t):
	l = list(map(int, input().split()))
	print(getup(l))


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


import math
test = int(input())
for i in range(test):
	(a, b, c, d) = [int(x) for x in input().split(' ')]
	j = 1
	flag = 1
	if b >= a:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		j = (a - b) // (c - d)
		if b + j * (c - d) >= a:
			print(b + j * c)
		else:
			print(b + (j + 1) * c)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	else:
		rem = a - b
		per = c - d
		ans = b + c * math.ceil(rem / per)
		print(ans)


t = 0
try:
	t = int(input())
except:
	pass
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	x = a - b
	y = c - d
	if a <= b:
		print(b)
	elif a > b and c <= d:
		print(-1)
	else:
		if x % y != 0:
			x = x // y + 1
		else:
			x = x // y
		print(b + c * x)


import math as mt
import sys, string
input = sys.stdin.readline
from collections import defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
t = I()
for i in range(t):
	(a, b, c, d) = M()
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		print(b + mt.ceil((a - b) / (c - d)) * 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)


import math
for test_case in range(int(input())):
	(a, b, c, d) = map(int, input().split()[:4])
	if b >= a:
		print(b)
	elif c > d:
		x = a - b
		y = c - d
		z = math.ceil(x / y)
		print(b + z * c)
	else:
		print(-1)


import math
test = int(input())
for tes in range(test):
	(a, b, c, d) = list(map(int, input().split()))
	left = a - b
	mul = c - d
	if left <= 0:
		print(b)
	elif mul <= 0:
		print(-1)
	else:
		mu = math.ceil(left / mul)
		print(b + mu * c)


from sys import stdin, exit
from math import ceil
input = stdin.readline

def i():
	return input()

def ii():
	return int(input())

def iis():
	return map(int, input().split())

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

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	(a, b, c, d) = iis()
	if b >= a:
		print(b)
	elif c - d > 0:
		print(b + ceil((a - b) / (c - d)) * c)
	else:
		print(-1)


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


import math
t = int(input())
i = 1
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b or d < c:
		if b >= a:
			print(b)
		else:
			n = math.ceil((a - b) / (c - d))
			print(b + n * c)
	else:
		print(-1)


t = int(input())
for i in range(t):
	(a, b, c, d) = [int(tmp) for tmp in input().split()]
	time = 0
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	else:
		a -= b
		s = c - d
		if a % s == 0:
			p = a // s
		else:
			p = a // s + 1
		print(b + p * c)


for t in range(int(input().strip())):
	(a, b, c, d) = tuple(map(int, input().strip().split()))
	r = a - b
	ans = b
	if r > 0:
		p = c - d
		if p > 0:
			ans += c * (r // p)
			if r % p:
				ans += c
		else:
			ans = -1
	print(ans)


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print('-1')
	else:
		a1 = a - b
		b1 = c - d
		c1 = math.ceil(a1 / b1)
		ans = c1 * c + b
		print(ans)


import sys
import heapq
from math import *
import bisect

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, input().split())

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

def srlinput(fl=False):
	return sorted(list(map(int, input().split())), reverse=fl)

def main():
	(a, b, c, d) = rinput()
	if c <= d and a > b:
		print(-1)
		return 0
	if a <= b:
		print(b)
		return 0
	print(b + (a - b + c - d - 1) // (c - d) * c)
for sdfghjkl in range(iinput()):
	main()


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		remain = a - b
		time_elapsed = b
		if c <= d:
			print(-1)
		else:
			diff = c - d
			periods = (remain + diff - 1) // diff
			time_elapsed += periods * c
			print(time_elapsed)


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


for i in range(int(input())):
	lst = list(map(int, input().split()))
	n = lst[0]
	m = lst[1]
	l = lst[2]
	q = lst[3]
	if n <= m:
		print(m)
	elif q >= l:
		print(-1)
	else:
		x = (n - m) // (l - q)
		if (n - m) % (l - q) != 0:
			x += 1
		print(m + x * l)


from sys import stdin, stdout
input = stdin.readline
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	have = b
	if have >= a:
		print(have)
		continue
	if c - d <= 0:
		print(-1)
		continue
	have += ((a - have) // (c - d) + (1 if (a - have) % (c - d) else 0)) * c
	if have >= a:
		print(have)
	else:
		print(-1)


Test = int(input())
for Case in range(Test):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	Ans = b + ((a - b) // (c - d) + min([1, (a - b) % (c - d)])) * c
	print(Ans)


def rn():
	a = int(input())
	return a

def rl():
	a = list(map(int, input().split()))
	return a
for _ in range(int(input())):
	[a, b, c, d] = rl()
	diff = c - d
	ans = -1
	if a <= b:
		ans = b
	if diff > 0:
		ans = b
		diff2 = a - b
		if diff2 > 0:
			fac = (diff2 + diff - 1) // diff
			ans += fac * c
	print(ans)


import sys
input = sys.stdin.readline
import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().strip().split())
	if b >= a:
		print(b)
		continue
	else:
		remain = a - b
		to = c - d
		if remain > 0 and to <= 0:
			print(-1)
		else:
			q = int(math.ceil(remain / (c - d)))
			print(b + q * c)


import math
for tt in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	(tt, ts) = (0, 0)
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		val = (a - b) / (c - d)
		val2 = (a - b) // (c - d)
		if val == val2:
			n = math.floor(val)
		else:
			n = math.floor(val) + 1
		tt = b + c * n
		ts = b + n * (c - d)
		print(tt)


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


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


tests = int(input())

def cannotSleep(c, d):
	if d < c:
		return False
	return True
while tests > 0:
	line = [int(x) for x in input().strip().split()]
	(neededSleep, firstSleepDtn, snoozeDtn, d) = (line[0], line[1], line[2], line[3])
	if firstSleepDtn >= neededSleep:
		print(firstSleepDtn)
	else:
		totalTillNow = firstSleepDtn
		totalSleepTillNow = firstSleepDtn
		if cannotSleep(snoozeDtn, d):
			print(-1)
		else:
			totalRemSleep = neededSleep - totalSleepTillNow
			perCycleSleep = snoozeDtn - d
			if totalRemSleep % perCycleSleep == 0:
				neededCycles = totalRemSleep // perCycleSleep
			else:
				neededCycles = totalRemSleep // perCycleSleep + 1
			totalTillNow += neededCycles * snoozeDtn
			print(totalTillNow)
	tests -= 1


import math
n = int(input())
for i in range(n):
	(rest_time, first_alarm, c, d) = input().split()
	rest_time = int(rest_time)
	first_alarm = int(first_alarm)
	c = int(c)
	d = int(d)
	if first_alarm >= rest_time:
		print(first_alarm)
	elif c <= d:
		print('-1')
	else:
		n = math.ceil((rest_time - first_alarm) / (c - d))
		time_pass = first_alarm + n * c
		print(time_pass)


from math import ceil
s = int(input())
for i in range(s):
	(a, b, c, d) = [int(i) for i in input().split()]
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		print(int(ceil((a - b) / (c - d)) * c + b))


import math
from sys import stdin, stdout
from heapq import heapify, heappop, heappush
from collections import Counter
T = int(stdin.readline())
for i in range(T):
	(a, b, c, d) = list(map(int, stdin.readline().split()))
	if b >= a:
		print(b)
		continue
	if c - d <= 0:
		print(-1)
		continue
	ans = int(math.ceil((a - b) / (c - d))) * c + b
	print(ans)


import math

def solve():
	for _ in range(int(input())):
		(a, b, c, d) = list(map(int, input().split()))
		if b >= a:
			print(b)
		else:
			firstval = a - b
			sec = c - d
			if sec < 1:
				print(-1)
			else:
				third = math.ceil(firstval / sec)
				print(b + c * third)
solve()


import math
a = int(input())
for i in range(0, a):
	t = list(map(int, input().split()))
	if t[0] <= t[1]:
		print(t[1])
	elif t[3] >= t[2]:
		print(-1)
	else:
		ans = t[1] + math.ceil((t[0] - t[1]) / (t[2] - t[3])) * t[2]
		print(ans)


def ans(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	time = b
	q = (a - b) // (c - d)
	if (a - b) % (c - d) != 0:
		q += 1
	time += q * c
	return time
m = int(input())
for i in range(m):
	s = input().split()
	a = int(s[0])
	b = int(s[1])
	c = int(s[2])
	d = int(s[3])
	print(ans(a, b, c, d))


def solve(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	cycle = (a - b) // (c - d) + int((a - b) % (c - d) != 0)
	return b + cycle * c
t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	print(solve(a, b, c, d))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	ans = 0
	ans += b
	a -= b
	if a <= 0:
		print(ans)
		continue
	if c <= d:
		print(-1)
		continue
	ans += (a + c - d - 1) // (c - d) * c
	print(ans)


import math

def calc(a, b, c, d):
	if a > b:
		if c > d:
			sub = c - d
			i = math.ceil((a - b) / (c - d))
			return b + c * i
		else:
			return -1
	elif a <= b:
		return b

def main():
	n = int(input())
	for _ in range(n):
		(a, b, c, d) = map(int, input().split(' '))
		print(calc(a, b, c, d))
main()


for cases in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	sum = 0
	time = 0
	if a == b or b > a:
		print(b)
	else:
		a -= b
		time += b
		if c <= d:
			print('-1')
		else:
			remain = a % (c - d)
			if remain == 0:
				time += a // (c - d) * c
			else:
				time += (a // (c - d) + 1) * c
			print(time)


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


from math import ceil
cases = int(input())
while cases > 0:
	(a, b, c, d) = map(int, input().split())
	time = 0
	slept = 0
	while True:
		if a <= b:
			print(b)
			break
		if c <= d:
			print(-1)
			break
		if a > b:
			slept += b
			dif = c - d
			time = ceil((a - slept) / dif)
		if slept == -1:
			print(slept)
			break
		else:
			time = time * c
			time += b
			print(time)
			break
	cases -= 1


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


import math
t = int(input())
for tt in range(t):
	(a, b, c, d) = map(int, input().split())
	x = b
	if b >= a:
		print(b)
		continue
	elif c <= d:
		print(-1)
		continue
	else:
		a -= b
		dif = c - d
		k = math.ceil(a / dif)
		print(k * c + x)


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


import sys
import math
t = int(sys.stdin.readline())
for i in range(t):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	if d >= c:
		if a <= b:
			print(b)
		else:
			print(-1)
	else:
		r = (a - b) // (c - d)
		if (r + 1) * (c - d) <= a - b:
			r += 1
		if (a - b) % (c - d) != 0:
			r += 1
		if r < 0:
			r = 0
		print(c * r + b)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif a > b:
		if d >= c:
			print(-1)
		else:
			resttime = abs(c - d)
			sum = b
			count = int(abs(a - b) / abs(c - d))
			if a > b + count * (c - d):
				count = count + 1
			print(b + count * c)


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


for _ in range(int(input())):
	flag = 0
	y = 1
	(a, b, c, d) = [int(x) for x in input().split()]
	x = a - b
	if a > b and d >= c:
		flag = 1
	if a <= b:
		flag = 2
	if c - d != 0:
		if x // (c - d) == x / (c - d) and flag == 0:
			y = x // (c - d)
		elif x // (c - d) != x / (c - d) and flag == 0:
			y = x // (c - d) + 1
	z = b + c * y
	if flag == 0:
		print(z)
	elif flag == 2:
		print(b)
	else:
		print(-1)


answer = []
for i in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	count = b
	if b >= a:
		pass
	elif c <= d:
		count = -1
	else:
		x = (a - b) % (c - d)
		if x == 0:
			count += (a - b) // (c - d) * c
		else:
			count += ((a - b) // (c - d) + 1) * c
	answer.append(count)
for i in range(len(answer)):
	print(answer[i])


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
input = sys.stdin.readline
import bisect
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	(total, start, a, b) = rl()
	ans = total - start
	if ans <= 0:
		print(start)
	else:
		s = a - b
		if s <= 0:
			print(-1)
		else:
			print(start + a * math.ceil(ans / s))


import math
t = int(input())
while t:
	t += -1
	(a, b, c, d) = map(int, input().split())
	if b < a and c <= d:
		print(-1)
	elif b >= a:
		print(b)
	else:
		t1 = a - b
		t2 = c - d
		t3 = math.ceil(t1 / t2)
		t4 = b + t3 * c
		print(t4)


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


testcases = int(input())
for testcase in range(testcases):
	temparr = input()
	temparr = temparr.split()
	sleep = int(temparr[0])
	firstalarm = int(temparr[1])
	resetalarm = int(temparr[2])
	timeneed = int(temparr[3])
	if firstalarm >= sleep:
		print(firstalarm)
		continue
	if timeneed >= resetalarm:
		print(-1)
		continue
	left = sleep - firstalarm
	diff = resetalarm - timeneed
	if left % diff == 0:
		times = left // diff
		total = firstalarm + times * resetalarm
		print(total)
	else:
		times = left // diff + 1
		total = firstalarm + times * resetalarm
		print(total)


class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: gcd(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from bisect import bisect_right, bisect_left
from fractions import Fraction

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans
from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def pt(x, y):
	return str(x) + ',' + str(y)
for _ in range(int(input()) if True else 1):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	from math import ceil
	print(b + c * ceil((a - b) / (c - d)))


t = int(input())
for j in range(t):
	(a, b, c, d) = map(int, input().split(' '))
	f = a - b
	if f <= 0:
		v = b
	elif d - c >= 0:
		v = -1
	else:
		k = f // (c - d) + 1 if f % (c - d) != 0 else f // (c - d)
		v = b + k * c
	print(v)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def solve(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	remain = a - b
	cycle = c - d
	ans = b + (remain + cycle - 1) // cycle * c
	return ans

def main():
	t = int(input())
	for _ in range(t):
		(a, b, c, d) = map(int, input().split())
		ans = solve(a, b, c, d)
		sys.stdout.write(str(ans) + '\n')
main()


n = int(input())
b = 0
c = 0
d = 0
e = 0
for i in range(n):
	a = list(map(int, input().split()))
	b = a[0]
	if a[0] < a[1] or a[0] == a[1]:
		print(a[1])
	elif a[3] > a[2] or a[3] == a[2]:
		print('-1')
	else:
		b = b - a[1]
		c = c + a[1]
		e = b // (a[2] - a[3])
		if b % (a[2] - a[3]) != 0:
			e = e + 1
		c = c + a[2] * e
		print(c)
		c = 0
		e = 0


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


import sys
input = sys.stdin.readline
flush = sys.stdout.flush
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
	x = (a - b) // (c - d) + 1
	if (a - b) % (c - d) == 0:
		x -= 1
	t = b + x * c
	print(t)


import os
import sys
from io import BytesIO, IOBase
from math import ceil

def solution(a, b, c, d):
	if b >= a:
		write(b)
		return
	if c <= d:
		write(-1)
		return
	left = a - b
	res = b + c * ceil(left / (c - d))
	write(res)

def main():
	for _ in range(r_int()):
		(a, b, c, d) = r_array()
		solution(a, b, c, d)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


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


import sys
import math

def I():
	return int(sys.stdin.readline())

def IL():
	return list(map(int, sys.stdin.readline().strip().split()))
t = I()
for tt in range(t):
	(a, b, c, d) = IL()
	if b >= a:
		print(b)
		continue
	else:
		req = a - b
		diff = c - d
		if diff > 0:
			x = math.ceil(req / diff)
			ans = c * x
			print(ans + b)
		else:
			print(-1)


input = __import__('sys').stdin.readline
for _ in range(int(input())):
	(a1, b1, c1, d1) = map(int, input().split())
	if a1 > b1 and d1 >= c1:
		print(-1)
	else:
		k1 = min(a1, b1)
		if a1 - k1 == 0:
			print(b1)
		else:
			ans = b1
			if (a1 - b1) % (c1 - d1) == 0:
				ans += (a1 - b1) // (c1 - d1) * c1
			else:
				ans += ((a1 - b1) // (c1 - d1) + 1) * c1
			print(ans)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print('-1')
	else:
		m = abs(a - b)
		n = abs(c - d)
		k = int(m / n)
		if m % n == 0:
			print(b + k * c)
		else:
			print(b + (k + 1) * c)


t = int(input())
if 1 <= t <= 1000:
	for i in range(t):
		(a, b, c, d) = input().split()
		a = int(a)
		b = int(b)
		c = int(c)
		d = int(d)
		if c > d:
			if a > b:
				n = (a - b) / (c - d)
				o = (a - b) // (c - d)
				n = int(n)
				o = int(o)
				if (a - b) % (c - d) == 0:
					print(b + n * c)
				else:
					print(b + (o + 1) * c)
			else:
				print(b)
		elif c <= d:
			if a > b:
				print(-1)
			else:
				print(b)


def main():
	n = int(input())
	for i in range(n):
		(a, b, c, d) = map(int, input().split())
		if b >= a:
			print(b)
		elif c <= d:
			print(-1)
		else:
			a -= b
			counter = b
			if a % (c - d) == 0:
				counter += a // (c - d) * c
			else:
				counter += (a // (c - d) + 1) * c
			print(counter)
main()


import math
t = int(input())
for i in range(t):
	e = list(map(int, input().split()))
	(a, b, c, d) = (e[0], e[1], e[2], e[3])
	if a <= b:
		print(b)
	else:
		k = a - b
		if c > d:
			r = c - d
			x = math.ceil(k / r)
			print(b + x * c)
		else:
			print(-1)


I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(a, b, c, d) = I()
	r = c - d
	if a <= b:
		print(b)
		continue
	elif r <= 0:
		an = -1
	else:
		an = b + c * ((a - b + r - 1) // r)
	print(an)


from math import ceil
for i in range(int(input())):
	(A, B, C, D) = map(int, input().split())
	if A <= B:
		print(B)
	elif A > B and D >= C:
		print(-1)
	else:
		RemainingTime = A - B
		Reset = ceil(RemainingTime / (C - D))
		print(Reset * C + B)


import sys
import math
t = int(input())
for i in range(t):
	(a, b, c, d) = [int(x) for x in sys.stdin.readline().split()]
	if b >= a:
		print(b)
	elif d < c:
		print(b + c * math.ceil((a - b) / (c - d)))
	else:
		print(-1)


import math

def main():
	t = int(input())
	for i in range(t):
		solve()

def solve():
	(a, b, c, d) = list(map(int, input().split(' ')))
	if b >= a:
		print(b)
		return
	a -= b
	if c - d <= 0:
		print(-1)
		return
	total = b + c * math.ceil(a / (c - d))
	print(total)
main()


n = input()
bb = []
for i in range(int(n)):
	testcase = input().split()
	a = int(testcase[0])
	b = int(testcase[1])
	c = int(testcase[2])
	d = int(testcase[3])
	a -= b
	if a > 0:
		if c - d > 0:
			x = a // (c - d)
			if a % (c - d):
				x += 1
			b += x * c
		else:
			b = -1
	bb += [b]
for i in bb:
	print(int(i))


import math
t = int(input())
for q in range(t):
	[a, b, c, d] = [int(x) for x in input().split()]
	r = a - b
	r1 = c - d
	if r1 <= 0:
		if r <= 0:
			print(b)
		else:
			print(-1)
	elif r <= 0:
		print(b)
	else:
		r3 = math.ceil(r / r1)
		print(b + r3 * c)


import math
t = int(input())

def fun(a, b, c, d):
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		s = 0
		s += b
		k = math.ceil((a - b) / (c - d))
		s += k * c
		print(s)
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	fun(a, b, c, d)


from math import ceil

def solve(a, b, c, d):
	if a - b <= 0:
		return b
	if c <= d:
		return -1
	ans = ceil((a - b) / (c - d))
	return ans * c + b
T = int(input())
for t in range(T):
	(a, b, c, d) = list(map(int, input().split()))
	res = solve(a, b, c, d)
	print(res)


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


import math
t = int(input())
ans = []
for i in range(t):
	(a, b, c, d) = [int(i) for i in input().split()]
	if a <= b:
		ans.append(b)
	elif d >= c:
		ans.append(-1)
	else:
		x = c * math.ceil((a - b) / (c - d)) + b
		ans.append(x)
for i in ans:
	print(i)


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, input().split(' ')))
	if a - b <= 0:
		print(b)
	elif d > c:
		print(-1)
	else:
		tl = a - b
		if tl <= 0:
			print(b)
		else:
			pp = c - d
			if pp == 0:
				print(-1)
			elif tl % pp == 0:
				print(b + c * (tl // pp))
			else:
				print(b + c * (math.floor(tl / pp) + 1))


import math

def AlarmClock(a, b, c, d):
	if b >= a:
		return b
	elif d >= c:
		return -1
	i = a - b
	j = c - d
	tot = math.ceil(i / j) * c + b
	return tot
Lista = []
total = int(input())
for i in range(total):
	All = [int(i) for i in input().split()]
	Lista.append(All)
for j in Lista:
	print(AlarmClock(j[0], j[1], j[2], j[3]))


import math
T = int(input())
for _ in range(T):
	(a, b, c, d) = [int(x) for x in input().split(' ')]
	init = a - b
	if init <= 0:
		print(b)
		continue
	second = c - d
	if second <= 0:
		print(-1)
		continue
	print(math.ceil(init / second) * c + b)


n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	l = b
	s = 0
	if a > b and c <= d:
		print(-1)
	else:
		ans = b
		t = c - d
		if a <= b:
			print(ans)
		elif c >= d:
			a -= b
			print(ans + c * [a // t, a // t + 1][a % t != 0])


import math as mt
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
	a -= b
	diff = c - d
	print(b + mt.ceil(a / diff) * c)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	x = a - b
	if x <= 0:
		print(b)
	else:
		sum = 0
		count = 1
		if d >= c:
			print('-1')
		else:
			if x % (c - d) == 0:
				y = x // (c - d)
			else:
				y = x // (c - d) + 1
			count = y * c
			print(count + b)


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		l = [int(i) for i in input().split(' ')]
		a = l[0]
		b = l[1]
		c = l[2]
		d = l[3]
		if a <= b:
			print(b)
		elif c <= d:
			print(-1)
		else:
			diff = c - d
			r = (a - b) % diff
			p = (a - b) // diff
			if r != 0:
				p += 1
			print(b + p * c)
main()


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


import sys

def fun(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	sleep = c - d
	need = a - b
	if need % sleep == 0:
		mt = need // sleep
	else:
		mt = need // sleep
		mt += 1
	return mt * c + b
t = sys.stdin.readline()
ans = []
for _ in range(int(t)):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	ans.append(fun(a, b, c, d))
for i in ans:
	print(i)


from math import ceil
T = int(input())
for j in range(T):
	(a, b, c, d) = map(int, input().split())
	if b < a and c <= d:
		print(-1)
		continue
	if b >= a:
		print(b)
		continue
	a = a - b
	u = c - d
	print(b + ceil(a / u) * c)


import math

def alarm_clock(a, b, c, d):
	times = 0
	if b < a and c - d <= 0:
		return -1
	if b < a:
		times = math.ceil((a - b) / (c - d))
	return b + times * c
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	print(alarm_clock(a, b, c, d))


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


import math
from typing import *

def wake_up_time(a: int, b: int, c: int, d: int) -> int:
	t = b
	if a <= t:
		return t
	if c <= d:
		return -1
	diff = c - d
	return t + math.ceil((a - t) / diff) * c
n = int(input())
variants = [input() for _ in range(n)]
for variant in variants:
	print(wake_up_time(*[int(i) for i in variant.split(' ')]))


t = int(input())
while t > 0:
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		tmp = (a - b) // (c - d)
		if (a - b) % (c - d) > 0:
			tmp += 1
		ans = b + tmp * c
		print(ans)
	t -= 1


import sys
pprint = lambda s: print(' '.join(map(str, s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ans = b
	a -= b
	if c <= d and a > 0:
		print(-1)
		continue
	if a <= 0:
		print(ans)
		continue
	ans += (a + c - d - 1) // (c - d) * c
	print(ans)


import sys
import heapq
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	q = a - b
	if q > 0:
		if d >= c:
			print(-1)
		else:
			w = c - d
			if q % w == 0:
				e = q // w
			else:
				e = q // w + 1
			print(b + e * c)
	else:
		print(b)


tc = int(input().strip())
import math

def solve():
	(a, b, c, d) = map(int, input().strip().split())
	more = a - b
	if more <= 0:
		return b
	add = c - d
	if add <= 0:
		return -1
	return b + c * math.ceil(more / (c - d))
for i in range(1, tc + 1):
	ans = solve()
	print(ans)


t = int(input())
for i in range(0, t):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if a <= b:
		print(b)
	elif d >= c:
		print('-1')
	else:
		r = c - d
		a = a - b
		if a % r != 0:
			o = 1
		else:
			o = 0
		ans = (a // r + o) * c + b
		print(ans)


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


cases = int(input())
for i in range(cases):
	l1 = input().split(' ')
	a = int(l1[0])
	b = int(l1[1])
	c = int(l1[2])
	d = int(l1[3])
	if b >= a:
		print(b)
		continue
	time = sleep = 0
	if c > d:
		sleep = c - d
		rem = a - b
		remainder = True
		if rem % sleep == 0:
			remainder = False
		c1 = int(rem / sleep)
		if c1 == 0:
			time = b + c
			print(time)
			continue
		elif remainder:
			time = b + (c1 + 1) * c
			print(time)
			continue
		elif not remainder:
			time = b + c1 * c
			print(time)
			continue
	elif c <= d:
		print(-1)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	result = 0
	if a <= b:
		print(b)
		continue
	result += b
	if c <= d:
		print(-1)
		continue
	one = c - d
	if (a - b) % one == 0:
		count = (a - b) // one
	else:
		count = (a - b) // one + 1
	print(b + count * c)


import sys

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
from math import *
import copy
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(x) for x in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())
t = fi()
while t > 0:
	t -= 1
	(a, b, c, d) = mi()
	p = a - b
	if p <= 0:
		print(b)
		continue
	if c - d <= 0:
		print(-1)
		continue
	print(b + int(ceil(p / (c - d))) * c)


from math import ceil
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:
		a -= b
		res = b + c * ceil(a / (c - d))
		print(res)


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


for _ in range(int(input())):
	l = list(map(int, input().split()))
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		s = b
		r = a - b
		k = c - d
		if r % k > 0:
			s += (r // k + 1) * c
			print(s)
		else:
			s += r // k * c
			print(s)


import math
cases = int(input())
for case in range(cases):
	values = input().split()
	(total, going_off, delay, cannot_sleep) = (int(values[0]), int(values[1]), int(values[2]), int(values[3]))
	gap = delay - cannot_sleep
	time = 0
	if total <= going_off:
		print(going_off)
	elif gap <= 0:
		print(-1)
	else:
		time += going_off
		time += math.ceil((total - going_off) / gap) * delay
		print(time)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	can_sleep = c > d
	cs = b
	total_time = b
	if cs < a and (not can_sleep):
		print('-1')
		continue
	if b < a and can_sleep:
		if (a - b) % (c - d) != 0:
			count = (a - b) // (c - d) + 1
		else:
			count = (a - b) // (c - d)
		total_time += count * c
	print(total_time)


from collections import defaultdict as dd
from collections import Counter as ccd
from itertools import permutations as pp
from itertools import combinations as cc
from random import randint as rd
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq as hq
import math
mod = pow(10, 9) + 7

def inp(flag=0):
	if flag == 0:
		return list(map(int, input().strip().split(' ')))
	else:
		return int(input())
t = inp(1)
while t:
	t -= 1
	(a, b, c, d) = inp()
	sleep = b
	k = 0
	ans = 0
	if sleep < a:
		if c <= d:
			ans = -1
		else:
			k = math.ceil((a - sleep) / (c - d))
	if ans != -1:
		ans = b + c * k
	print(ans)


def B1():
	t = int(input())
	ans = []
	for _ in range(t):
		(a, b, c, d) = map(int, input().split())
		if b >= a:
			ans.append(int(b))
		elif c <= d:
			ans.append(-1)
		else:
			f = (a - b + c - d - 1) // (c - d) * c
			ans.append(b + f)
	for i in ans:
		print(i)
B1()


import math
n = int(input())
while n > 0:
	arr = list(map(int, input().split()))
	(a, b, c, d) = (arr[0], arr[1], arr[2], arr[3])
	if b >= a:
		print(b)
		n -= 1
		continue
	if c <= d:
		print(-1)
		n -= 1
		continue
	rem = a - b
	ext = math.ceil(rem / (c - d))
	print(b + ext * c)
	n -= 1


t = int(input())
import math
output = []
for _ in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		output.append(b)
	else:
		time = b
		rem = a - b
		if d >= c:
			output.append(-1)
		else:
			sleeps = c - d
			output.append(time + c * int(math.ceil(rem / sleeps)))
for item in output:
	print(item)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		rem = a - b
		dif = c - d
		reset = 0
		if rem % dif == 0:
			reset = int(rem / dif)
		else:
			reset = int(rem / dif) + 1
		total = b + reset * c
		print(total)


t = int(input())
output = []
for i in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	if b >= a:
		output.append(b)
		continue
	if c <= d:
		output.append(-1)
		continue
	a -= b
	dif = c - d
	res = b + (a + dif - 1) // dif * c
	output.append(res)
for x in output:
	print(x)


import math
cases = int(input())
while cases != 0:
	li = [int(i) for i in input().split()]
	if li[0] <= li[1]:
		print(li[1])
	elif li[2] <= li[3]:
		print(-1)
	else:
		print(int(li[1] + math.ceil((li[0] - li[1]) / (li[2] - li[3])) * li[2]))
	cases -= 1


total_cases = int(input())
for _ in range(total_cases):
	(a, b, c, d) = map(int, input().strip().split())
	total = a - b
	if total <= 0:
		print(b)
		continue
	if c <= d:
		print('-1')
		continue
	diff = c - d
	complete_times = total // diff
	if total % diff == 0:
		print(c * complete_times + b)
	else:
		print(c * (complete_times + 1) + b)


import io
import os
from collections import Counter, defaultdict, deque

def solve(A, B, C, D):
	sleep = B
	t = B
	while True:
		if sleep >= A:
			break
		if C - D <= 0:
			return -1
		sleep += C - D
		t += C
	return t

def solve(A, B, C, D):
	sleep = B
	t = B
	if sleep >= A:
		return t
	if C - D <= 0:
		return -1
	cycles = (A - sleep) // (C - D)
	if (sleep - A) % (C - D) != 0:
		cycles += 1
	return t + cycles * C
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(A, B, C, D) = [int(x) for x in input().split()]
	ans = solve(A, B, C, D)
	print(ans)


import math
from sys import stdin, stdout

def L():
	return list(map(int, stdin.readline().split()))

def Si():
	return sorted(list(map(int, stdin.readline().split())))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def main():
	for i in range(int(input())):
		(a, b, c, d) = In()
		if b >= a:
			print(b)
		elif d >= c:
			print('-1')
		else:
			print(b + int(math.ceil((a - b) / (c - d)) * c))
main()


t = int(input())
while t > 0:
	inp = list(map(int, input().split()))
	a = inp[0]
	b = inp[1]
	c = inp[2]
	d = inp[3]
	if b >= a:
		print(b)
	elif d >= c:
		print('-1')
	elif (a - b) % (c - d) == 0:
		print(int(b + c * int((a - b) / (c - d))))
	else:
		print(int(b + c * (int((a - b) / (c - d)) + 1)))
	t -= 1


t = int(input())
for zz in range(t):
	i = input().split()
	a = int(i[0])
	b = int(i[1])
	c = int(i[2])
	d = int(i[3])
	if b >= a:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	a -= b
	dif = c - d
	print(b + (a + dif - 1) // dif * c)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
for _ in range(ii()):
	(a, b, c, d) = mi()
	if a <= b:
		print(b)
	elif a > b and d >= c:
		print(-1)
	else:
		print(b + math.ceil((a - b) / (c - d)) * c)


import math

def fun(a, b, c, d):
	if b >= a:
		return b
	count = b
	slept = b
	if c <= d:
		return -1
	x = math.ceil((a - slept) / (c - d))
	count += c * x
	return count
for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	print(fun(a, b, c, d))


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		ttlsleep = b
		left = a - b
		gain = c - d
		stop = left / gain
		if stop.is_integer():
			ttlsleep += int(stop) * c
			print(int(ttlsleep))
		else:
			stop = math.trunc(stop) + 1
			ttlsleep += stop * c
			print(int(ttlsleep))


import sys
from math import ceil
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def foo():
	(a, b, c, d) = invr()
	ans = x = 0
	if b >= a:
		print(b)
		return
	elif d >= c:
		print(-1)
		return
	else:
		x = ceil((a - b) / (c - d))
		ans = b + c * x
		print(ans)
t = inp()
for i in range(t):
	foo()


for _ in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split()]
	sum1 = b
	st = b
	rt = a - b
	if a > b:
		if c - d > 0:
			rr = rt // (c - d)
			if rt % (c - d) != 0:
				rr += 1
			sum1 = sum1 + rr * c
			print(sum1)
		else:
			print(-1)
	else:
		print(sum1)


from sys import stdin, stdout

def solve():
	(a, b, c, d) = map(int, stdin.readline().split())
	a -= b
	if a <= 0:
		return b
	c -= d
	if c <= 0:
		return -1
	x = a // c
	a -= x * c
	if a > 0:
		x += 1
	return b + x * (c + d)
for _ in range(int(stdin.readline())):
	stdout.write(str(solve()) + '\n')


import sys

def I():
	return sys.stdin.readline().rstrip()
for tc in range(1, 1 + int(I())):
	(a, b, c, d) = map(int, I().split())
	a -= b
	e = c - d
	if a <= 0:
		print(b)
	elif e > 0:
		b += c * (a // e)
		a %= e
		if a > 0:
			b += c
		print(b)
	else:
		print(-1)


n = int(input())
INF = float('inf')
rr = lambda : input()
rri = lambda : int(input())
rrm = lambda : list(map(int, input().split()))

def solve(a, b, c, d):
	need = a - b
	if need <= 0:
		return b
	if c <= d:
		return -1
	return b + c * (need // (c - d) + +(need % (c - d) != 0))
for _ in range(n):
	(a, b, c, d) = rrm()
	print(solve(a, b, c, d))


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		sleep = c - d
		rem = a - b
		if rem % sleep == 0:
			n = rem // sleep
		else:
			n = rem // sleep + 1
		total = n * c + b
		print(total)


t = int(input())
import math
for j in range(t):
	s = input().split(' ')
	a = int(s[0])
	b = int(s[1])
	c = int(s[2])
	d = int(s[3])
	if b >= a:
		print(b)
	else:
		sleep = b
		a = a - b
		if c <= d:
			print(-1)
		else:
			k = math.ceil(float(a) / float(c - d))
			print(int(c * k + b))


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


def solve():
	(a, b, c, d) = map(int, input().split())
	count = b
	start = a
	a -= b
	inc = c - d
	if a <= 0:
		return start - a
	elif c <= d:
		return -1
	else:
		count = count + a // inc * c
		if a % inc != 0:
			count += c
		return count
for _ in range(int(input())):
	print(solve())


t = int(input())
itog = []
for m in range(t):
	stroka = input()
	list = stroka.split()
	a = int(list[0])
	b = int(list[1])
	c = int(list[2])
	d = int(list[3])
	if b >= a:
		itog.append(b)
	elif c <= d:
		itog.append(-1)
	else:
		if (a - b) % (c - d) == 0:
			tim = int((a - b) / (c - d))
		else:
			tim = (a - b) // (c - d) + 1
		res = b + tim * c
		itog.append(res)
for m in range(t):
	print(itog[m])


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		r = c - d
		k = a - (b + r)
		p = math.ceil(k / r)
		p += 1
		ans = b + p * c
		print(ans)


import sys
input = sys.stdin.readline
import math

def inp():
	return int(input())

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().strip().split())

def from_file(f):
	return f.readline

def solution(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	res = math.ceil((a - b) / (c - d)) * c + b
	return res
t = inp()
for _ in range(t):
	(a, b, c, d) = invr()
	print(solution(a, b, c, d))


from sys import stdin, stdout
import math

def II():
	return int(stdin.readline())

def MI():
	return map(int, stdin.readline().split())
t = II()
for _ in range(t):
	(a, b, c, d) = MI()
	ans = 0
	if b >= a:
		ans = b
	elif d >= c:
		ans = -1
	else:
		x = math.ceil((a - b) / (c - d))
		ans = b + x * c
	stdout.write(str(ans) + '\n')


from math import ceil

def get_time(times):
	(a, b, c, d) = list(map(int, times))
	if a <= b:
		return b
	elif c <= d:
		return -1
	else:
		remainder = a - b
		time_for_sleep = c - d
		reps = ceil(remainder / time_for_sleep)
		return b + reps * c
string_count = int(input())
data = []
for i in range(0, string_count):
	times = input().split(' ')
	print(get_time(times))


n = int(input())
k = []
for i in range(n):
	l = list(map(int, input().split()))
	k.append(l)
for i in range(n):
	(a, b, c, d) = (k[i][0], k[i][1], k[i][2], k[i][3])
	if a == b or a < b:
		print(b)
	elif c == d or c < d:
		print(-1)
	else:
		if (a - b) % (c - d) == 0:
			count = (a - b) // (c - d)
		else:
			count = (a - b) // (c - d) + 1
		print(b + c * count)


import os
import sys
from io import BytesIO, IOBase

def readvars():
	k = map(int, input().split())
	return k

def readlist():
	li = list(map(int, input().split()))
	return li

def anslist(li):
	ans = ' '.join([str(v) for v in li])
	return ans

def main():
	t = 1
	t = int(input())
	for xx in range(t):
		(a, b, c, d) = readvars()
		t = 0
		if b >= a:
			print(b)
		else:
			t += b
			rem = a - b
			if d >= c:
				print(-1)
			else:
				if rem % (c - d) == 0:
					t += rem // (c - d) * c
				else:
					t += (rem // (c - d) + 1) * c
				print(t)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math

def solve(a, b, c, d):
	if a <= b:
		return b
	elif c <= d:
		return -1
	else:
		sleep_needed = a - b
		inter_alarm_sleep = c - d
		sleep_segment_needed = math.ceil(float(sleep_needed) / float(inter_alarm_sleep))
		return int(sleep_segment_needed * c) + b
t = int(input())
for i in range(0, t):
	(a, b, c, d) = list(map(int, input().rstrip().split()))
	print(solve(a, b, c, d))


def inlt():
	return list(map(int, input().split()))
import math
res = []
n = int(input())
for i in range(n):
	[a, b, c, d] = inlt()
	if b >= a:
		res.append(b)
	elif d >= c:
		res.append(-1)
	else:
		res.append(math.ceil((a - b) / ((c - d) * 1.0)) * c + b)
for i in range(n):
	print(res[i])


t = int(input())
for test in range(t):
	(a, b, c, d) = input().split()
	(a, b, c, d) = (int(a), int(b), int(c), int(d))
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	minuts = b
	a -= b
	sleep = c - d
	total_sleep_needed = a / sleep
	if total_sleep_needed > int(total_sleep_needed):
		total_sleep_needed = int(total_sleep_needed) + 1
	total_sleep_needed = int(total_sleep_needed)
	minuts += total_sleep_needed * c
	print(minuts)


import sys
import math
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
n = int(input())
for tt in range(n):
	(a, b, c, d) = map(int, input().split())
	time = b
	sleepleft = a - b
	if sleepleft > 0:
		if d >= c:
			print(-1)
			continue
		effec = c - d
		alarm = c * math.ceil(sleepleft / effec)
		print(alarm + time)
	else:
		print(time)


from math import ceil

def tiktok(a, b, c, d):
	if b >= a:
		return b
	if d >= c:
		return -1
	factor = ceil((a - b) / (c - d))
	return b + factor * c
for t in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	print(tiktok(a, b, c, d))


from math import ceil
t = int(input())
for i in range(t):
	x = input()
	x1 = x.split()
	a = int(x1[0])
	b = int(x1[1])
	c = int(x1[2])
	d = int(x1[3])
	if b >= a:
		print(b)
	elif c <= d:
		print('-1')
	else:
		t = ceil((a - b) / (c - d))
		r = b + t * c
		r = int(r)
		print(r)


from collections import defaultdict, Counter
import copy, math
from copy import deepcopy
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a - b <= 0:
		print(b)
	elif c - d > 0:
		x = math.ceil((a - b) / (c - d))
		print(b + x * c)
	else:
		print(-1)


import math
el = int(input())
for i in range(el):
	ar = []
	for j in input().split(' '):
		ar.append(int(j))
	a = ar[0]
	b = ar[1]
	c = ar[2]
	d = ar[3]
	count = b
	temp = a - b
	if a - b <= 0:
		print(b)
	else:
		a = a - b
		if d >= c:
			print(-1)
		else:
			temp = math.ceil(temp / (c - d)) * c
			count = count + temp
			print(count)


import math
t = int(input())
for case in range(t):
	(a, b, c, d) = [int(s) for s in input().split(' ')]
	if b >= a:
		ans = b
	elif d >= c:
		ans = -1
	else:
		ans = b + c * math.ceil((a - b) / (c - d))
	print(ans)


import math as m
for _ in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split()]
	if c < d and a > b:
		print(-1)
	elif a == b:
		print(a)
	elif a < b:
		print(b)
	elif c == d:
		print(-1)
	else:
		k = a - b
		cnt = b
		x = m.ceil(k / (c - d))
		cnt = cnt + x * c
		print(cnt)


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


from sys import stdin, stdout
for _ in range(int(stdin.readline().strip())):
	(a, b, c, d) = list(map(int, stdin.readline().strip().split()))
	if a <= b:
		stdout.write(str(b) + '\n')
		continue
	if d >= c:
		stdout.write('-1' + '\n')
		continue
	if (a - b) % (c - d) == 0:
		ans = b + int((a - b) / (c - d)) * c
	else:
		ans = b + (int((a - b) // (c - d)) + 1) * c
	stdout.write(str(ans) + '\n')


t = int(input())
while t:
	t -= 1
	(need, first, every, resleep) = map(int, input().split())
	rest = need - first
	if rest <= 0:
		print(first)
	else:
		tmp = every - resleep
		if tmp <= 0:
			print(-1)
		else:
			if rest % tmp != 0:
				times = rest // tmp + 1
			else:
				times = int(rest / tmp)
			print(first + every * times)


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


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	n_t = a - b
	alarm = c - d
	if alarm <= 0:
		print(-1)
		continue
	n_a = ceil(n_t / alarm)
	print(n_a * c + b)


import math

def scanf():
	return list(map(int, input().split()))
for _ in range(int(input())):
	(a, b, c, d) = scanf()
	if c > d:
		print(b + c * math.ceil(max(0, a - b) / (c - d)))
	else:
		print(b if b >= a else -1)


import math
import sys
from collections import Counter
input = sys.stdin.readline

def solve():
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		return
	if c <= d:
		print(-1)
		return
	v = c - d
	a = a - b
	m = math.ceil(a / v)
	print(b + m * c)
for _ in range(int(input())):
	solve()


t = int(input())
for q in range(t):
	ch = input()
	L = [int(i) for i in ch.split()]
	(a, b, c, d) = (L[0], L[1], L[2], L[3])
	a -= b
	if a <= 0:
		print(b)
	elif c <= d:
		print(-1)
	else:
		nb = a // (c - d)
		if a % (c - d) != 0:
			nb += 1
		print(b + nb * c)


t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print('-1')
	else:
		r = (a - b) % (c - d)
		p = (a - b) // (c - d)
		if r == 0:
			tot = b + p * c
		else:
			tot = b + (p + 1) * c
		print(tot)


from math import ceil
t_count = int(input().strip())
while t_count > 0:
	(a, b, c, d) = list(map(int, input().strip().split(' ')))
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = a - b
		y = c - d
		factor = int(ceil(x / y))
		wakeup = b + factor * c
		print(wakeup)
	t_count -= 1


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


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)
	elif C > D:
		gone = C - D
		print(B + math.ceil(left / gone) * C)
	else:
		print(-1)


import sys
for _ in range(int(input())):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		p = a - b
		pp = b
		aa = p // (c - d)
		if p % (c - d) == 0:
			pp += aa * c
		else:
			pp += (aa + 1) * c
		print(pp)


n = int(input())
for i in range(n):
	(a, b, c, d) = [int(i) for i in input().split()]
	if b < a and d >= c:
		print(-1)
	elif b >= a:
		print(b)
	else:
		sum = b
		e = c - d
		f = a - b
		g = f // e
		h = f % e
		if h > 0:
			g = g + 1
		print(sum + g * c)


import math
t = int(input())
for i in range(t):
	(a, b, nex, wait) = [int(i) for i in input().split()]
	if a <= b:
		print(b)
	else:
		rem = a - b
		f = False
		t = b
		if wait >= nex:
			f = True
		else:
			w = nex - wait
			t = t + math.ceil(rem / w) * nex
			print(t)
		if f:
			print(-1)


import math
NO = '-1'
tests_count = int(input())
tests = [input() for _ in range(0, tests_count)]
for test in tests:
	(a, b, c, d) = (int(i) for i in test.split(' '))
	if b >= a:
		print(b)
	elif d >= c:
		print(NO)
	else:
		print(math.ceil((a - b) / (c - d)) * c + b)


import math
for w in range(int(input())):
	a = list(map(int, input().split()))
	if a[0] <= a[1]:
		print(a[1])
	elif a[2] <= a[3]:
		print(-1)
	else:
		aux = a[0] - a[1]
		dif = a[2] - a[3]
		aux = math.ceil(aux / dif)
		print(a[1] + a[2] * aux)


import math as m
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if d >= c and a > b:
		print(-1)
		continue
	a = a - b
	if a <= 0:
		print(b)
		continue
	else:
		y = m.ceil(a / (c - d))
		y = int(y)
		print(b + y * c)


def any():
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		p = a - b
		if c <= d:
			print(-1)
		else:
			q = c - d
			if p % q == 0:
				r = p // q
			else:
				r = p // q + 1
			print(b + c * r)
for t in range(int(input())):
	any()


from math import floor
n = input()
result = []
for i in range(int(n)):
	v = [int(x) for x in input().split(' ')]
	if v[0] > v[1]:
		v[0] = v[0] - v[1]
	else:
		result.append(v[1])
		continue
	if v[2] <= v[3]:
		result.append(-1)
		continue
	solan = floor(v[0] / (v[2] - v[3]))
	if v[0] % (v[2] - v[3]) != 0:
		solan += 1
	result.append(v[1] + v[2] * solan)
for i in result:
	print(i)


import math
n = int(input())
for i in range(n):
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		tot = b + c * math.ceil((a - b) / float(c - d))
		print(tot)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().strip().split())
	if b < a and d >= c:
		print(-1)
	elif b >= a:
		print(b)
	else:
		needsleep = a - b
		x = c - d
		if needsleep % x == 0:
			mul = needsleep // x
		else:
			mul = needsleep // x + 1
		ans = b + c * mul
		print(ans)


from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
for _ in range(iinput()):
	(a, b, c, d) = rinput()
	if b >= a:
		print(b)
	else:
		left = a - b
		if d >= c:
			print(-1)
		else:
			temp = math.ceil(float(left) / float(c - d))
			print(b + c * temp)


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


import math
n = int(input())
answ = []
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		answ.append(b)
	elif c <= d:
		answ.append(-1)
	else:
		answ.append(b + math.ceil((a - b) / (c - d)) * c)
for i in range(n):
	print(answ[i])


from sys import stdin, stdout
from math import ceil
t = int(stdin.readline())
for _ in range(t):
	(a, b, c, d) = map(int, stdin.readline().strip().split())
	if a > b and c - d <= 0:
		print(-1)
	elif a <= b:
		print(b)
	else:
		mul = c - d
		num = int(ceil((a - b) / mul))
		ans = num * c
		print(ans + b)


t = int(input())
result = []
for _ in range(t):
	a = list(map(int, input().split(' ')))
	if a[1] >= a[0]:
		result.append(a[1])
	elif a[2] < a[3]:
		result.append(-1)
	else:
		m = a[0] - a[1]
		reset = a[2] - a[3]
		if reset == 0:
			result.append(-1)
		elif m % reset == 0:
			result.append(m // reset * a[2] + a[1])
		else:
			result.append((m // reset + 1) * a[2] + a[1])
print(*result, sep='\n')


from sys import stdin
tc = int(stdin.readline())
for _ in range(tc):
	(a, b, c, d) = list(map(int, stdin.readline().split()))
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		if (a - b) % (c - d) != 0:
			cnt = (a - b) // (c - d) + 1
		else:
			cnt = (a - b) // (c - d)
		print(b + cnt * c)


import math
T = int(input())
while T != 0:
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d > c or d - c == 0:
		print('-1')
	else:
		minutes_slept = c - d
		time_needed = a - b
		time = math.ceil(time_needed / minutes_slept)
		answer = b + time * c
		print(answer)
	T -= 1


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	f = True
	sleep = b
	sum1 = b
	if b > a:
		print(b)
	elif a == b:
		print(a)
	elif a != b and c == d:
		print('-1')
	elif c < d:
		print('-1')
	else:
		left = a - b
		if left % (c - d) == 0:
			sum1 = sum1 + left // (c - d) * c
		else:
			sum1 = sum1 + (left // (c - d) + 1) * c
		print(sum1)


def ls_c(s):
	return [i for i in s.split()]

def ls_i(s):
	return [int(i) for i in s.split()]

def ip():
	return input()
t = int(ip())
for i in range(t):
	[a, b, c, d] = ls_i(ip())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		ans = 0
		slp = 0
		ans += b
		slp += b
		ans += c * ((a - b) // (c - d))
		slp += (c - d) * ((a - b) // (c - d))
		if slp != a:
			ans += c
		print(ans)


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


t = int(input())
for i in range(t):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		min_left = a - b
		slept = c - d
		x = min_left / slept
		x = int(x)
		if min_left % slept == 0:
			print(int(b + c * x))
		else:
			print(b + c * (x + 1))


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	(a, b, c, d) = mp()
	ans = 0
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		print(b + c * ceil(a - b, c - d))


import sys
tc = int(sys.stdin.readline())
for _ in range(tc):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	if b >= a:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		if (a - b) % (c - d) == 0:
			x = (a - b) // (c - d)
		else:
			x = (a - b) // (c - d) + 1
		print(b + c * x)


import sys
from math import ceil
input = sys.stdin.buffer.readline

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

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
for __ in range(int(input())):
	(a, b, c, d) = I()
	if b >= a:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	a -= b
	print(ceil(a / (c - d)) * c + b)


tc = int(input())
while tc > 0:
	(a, b, c, d) = map(int, input().split())
	if a > b and d < c:
		(q, r) = divmod(a - b, c - d)
		if r > 0:
			q += 1
		print(b + q * c)
	else:
		print(b if b >= a else -1)
	tc -= 1


from math import ceil
test_count = int(input())

def ans(p):
	(a, b, c, d) = p
	if a > b and d >= c:
		return -1
	elif a <= b:
		return b
	else:
		a -= b
		k = c - d
		return ceil(a / k) * c + b
for i in range(test_count):
	print(ans(list(map(lambda x: int(x), input().split(' ')))))


import sys

def f(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	if (a - b) % (c - d) == 0:
		return b + (a - b) // (c - d) * c
	else:
		return b + ((a - b) // (c - d) + 1) * c
t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	flag = True
	(a, b, c, d) = [int(i) for i in input().split()]
	res = f(a, b, c, d)
	print(res)


import math

def main():
	for q in range(int(input())):
		(A, B, C, D) = map(int, input().split())
		left = A - B
		if left <= 0:
			print(B)
		else:
			loop = C - D
			if loop <= 0:
				print('-1')
			else:
				ans = B + left // loop * C
				if left % loop != 0:
					ans += C
				print(ans)
main()


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	nokori = a
	res = b
	nokori -= b
	if nokori <= 0:
		print(res)
	elif c <= d:
		print(-1)
	else:
		sleep = -1
		n = int(d / c)
		sleep = c * n - d
		while sleep < 1:
			n += 1
			sleep = c * n - d
		if sleep < 1:
			print(-1)
		else:
			e = nokori % sleep
			f = int(nokori / sleep)
			if e == 0:
				res += c * n * f
			else:
				if c > d:
					res += c * (f + 1)
				if c < d:
					res += c * n * (f + 1)
			print(res)


t = int(input())
for i in range(t):
	(a1, b1, c1, d1) = input().split(' ')
	a = int(a1)
	b = int(b1)
	c = int(c1)
	d = int(d1)
	s = k = b
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		if (a - b) % (c - d) == 0:
			k = int((a - b) / (c - d))
		else:
			k = int((a - b) / (c - d)) + 1
		s = s + k * c
		print(s)


import math
T = int(input())
for test in range(1, T + 1):
	ans = 0
	(a, b, c, d) = map(int, input().split())
	if c <= d and a > b:
		ans = -1
	elif a <= b:
		ans = b
	else:
		rem = a - b
		times = int(math.ceil(rem / (c - d)))
		ans = b + times * c
	print('{:}'.format(ans))


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().strip().split())
	if a <= b:
		print(b)
	elif d >= c and a != b:
		print('-1')
	else:
		count = (a - b + c - d - 1) // (c - d)
		print(b + count * c)


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


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	(a, b, c, d) = list(map(int, stdin.readline().split()))
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		a -= b
		res = b
		if a % (c - d) == 0:
			x = a // (c - d)
		else:
			x = a // (c - d) + 1
		res += x * c
		print(res)


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


from math import ceil
Answers = []
for _ in range(int(input())):
	X = list(map(int, input().split()))
	Time = X[1]
	if Time >= X[0]:
		Answers.append(str(Time))
	elif X[-1] >= X[-2]:
		Answers.append('-1')
	else:
		Dif = X[-2] - X[-1]
		Need = X[0] - Time
		Temp = ceil(Need / Dif)
		Time += Temp * X[-2]
		Answers.append(str(Time))
print('\n'.join(Answers))


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = list(map(int, input().strip().split()))
	if b < a and d >= c:
		print(-1)
	elif b >= a:
		print(b)
	else:
		n = math.ceil((a - b) / (c - d))
		print(c * n + b)


for T in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		k = a - b
		v = c - d
		ans = k // v
		if k % v == 0:
			print(ans * c + b)
		else:
			print((ans + 1) * c + b)


from math import ceil
t = int(input())
while t > 0:
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	n = 1
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		n = ceil((a - b) / (c - d))
		print(b + n * c)
	t -= 1


def solve():
	(a, b, c, d) = [int(x) for x in input().split()]
	if b >= a:
		print(b)
		return
	elif c <= d:
		print(-1)
		return
	else:
		(net, time) = (b, b)
		left = a - net
		if left % (c - d) == 0:
			time += left // (c - d) * c
		else:
			time += (left // (c - d) + 1) * c
		print(time)
t = int(input())
for i in range(t):
	solve()


import math
for ad in range(int(input())):
	(a, b, c, d) = list(map(int, input().split()))
	if b >= a:
		print(b)
	else:
		a -= b
		if d >= c:
			print(-1)
		else:
			x = b
			k = math.ceil(a / (c - d))
			x += k * c
			print(x)


from math import ceil
tests = int(input())
i = 0
while i < tests:
	(need_time, first_clock, next_clock, sleeping) = (int(x) for x in input().split())
	if first_clock >= need_time:
		print(first_clock)
	elif next_clock - sleeping <= 0:
		print(-1)
	else:
		all_time = first_clock + ceil((need_time - first_clock) / (next_clock - sleeping)) * next_clock
		print(all_time)
	i += 1


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		res = c - d
		if res <= 0:
			print('-1')
		else:
			import math as m
			rem = m.ceil((a - b) / res)
			tot = b + rem * c
			print(tot)


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


def read_ints():
	return [int(i) for i in input().strip().split()]

def solve():
	pass

def main():
	t = int(input())
	for i in range(t):
		(a, b, c, d) = read_ints()
		ans = 0
		ans += b
		remain = a - b
		if remain <= 0:
			print(ans)
			continue
		step = c - d
		if step <= 0:
			print(-1)
			continue
		if remain % step == 0:
			ans += remain // step * c
			print(ans)
			continue
		else:
			ans += (remain // step + 1) * c
			print(ans)
			continue
main()


for i in range(int(input())):
	(a, b, x, y) = map(int, input().split(' '))
	if a <= b:
		print(b)
	elif x <= y:
		print('-1')
	elif (a - b) % (x - y) == 0:
		print(b + (a - b) // (x - y) * x)
	else:
		print(b + ((a - b) // (x - y) + 1) * x)


def main():
	for _ in range(int(input())):
		(a, b, c, d) = map(int, input().split())
		if b >= a:
			print(b)
			continue
		elif c <= d:
			print(-1)
			continue
		else:
			t = c - d
			tim = (a - b) // t
			if (a - b) % t != 0:
				tim += 1
			ans = b + c * tim
			print(ans)
main()


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


import math
t = int(input())
list1 = []
for i in range(t):
	inputs = list(map(int, input().split()))
	a = inputs[1]
	tm = inputs[1]
	diff = inputs[0] - inputs[1]
	diff1 = inputs[2] - inputs[3]
	if inputs[1] >= inputs[0]:
		list1.append(inputs[1])
	elif inputs[3] >= inputs[2]:
		list1.append(-1)
	else:
		n = diff / diff1
		n = math.ceil(n)
		tm = tm + n * inputs[2]
		list1.append(tm)
for j in list1:
	print(j)


from math import *
a = int(input())
for x in range(a):
	(b, c, d, e) = map(int, input().split())
	if c >= b:
		print(c)
	elif b > c and e >= d:
		print(-1)
	elif b > c and d > e:
		m = ceil((b - c) / (d - e))
		h = c + d * m
		print(h)


from math import ceil
T = int(input())
for o in range(T):
	(a, b, c, d) = map(int, input().split())
	if a > b:
		a = a - b
		e = c - d
		if e > 0:
			w = ceil(a / e)
			res = b + w * c
			print(res)
		else:
			print(-1)
	else:
		print(b)


for _ in range(int(input())):
	count = 0
	(a, b, c, d) = list(map(int, input().split()))
	sleep = b
	total = b
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		zz = a - b
		count = zz // (c - d)
		if zz % (c - d) != 0:
			count += 1
		total += c * count
		print(total)


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


for _ in range(int(input())):
	(n, m, a, b) = map(int, input().split())
	if m >= n:
		print(m)
	elif n > m and a <= b:
		print(-1)
	elif (n - m) % (a - b) == 0:
		x = (n - m) // (a - b)
		print(x * a + m)
	else:
		x = (n - m) // (a - b) + 1
		print(x * a + m)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
import math
for _ in range(ii()):
	(a, b, c, d) = f()
	if a - b <= 0:
		print(b)
	elif c - d < 0:
		print(-1)
	elif c - d == 0:
		if a <= b:
			print(b)
		else:
			print(-1)
	else:
		l = math.ceil((a - b) / (c - d))
		print(b + c * l)


from math import ceil
for _ 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
	chk = a - b
	ans = ceil(chk / diff)
	print(b + c * ans)


q = int(input())
for _ in range(q):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		zabraklo = a - b
		if d >= c:
			print(-1)
		else:
			e = c - d
			licz = zabraklo // e
			wyn = b + (d + e) * licz
			ilejeszczesnu = zabraklo - licz * e
			if ilejeszczesnu == 0:
				print(wyn)
			else:
				print(wyn + d + e)


from sys import stdin
import math

def inp():
	return stdin.readline().strip()
t = int(inp())
for _ in range(t):
	(a, b, c, d) = [int(x) for x in inp().split()]
	if d >= c and b < a:
		print(-1)
	elif b >= a:
		print(b)
	else:
		print(b + math.ceil((a - b) / (c - d)) * c)


from math import ceil

def solution():
	(a, b, c, d) = map(lambda x: int(x), input().split())
	if b >= a:
		print(b)
		return
	if d >= c:
		print(-1)
		return
	print(b + ceil((a - b) / (c - d)) * c)
t = int(input())
for _ in range(t):
	solution()


import math
for q in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ans = b
	te = a - b
	if te <= 0:
		print(b)
	elif te > 0 and c - d <= 0:
		print(-1)
	else:
		te = math.ceil(te / (c - d))
		te *= c
		ans += te
		print(ans)


import sys
input = sys.stdin.readline
t = int(input())
x = [list(map(int, input().split())) for i in range(t)]
for (a, b, c, d) in x:
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		ans = -(-(a - b) // (c - d)) * c + b
		print(ans)


from math import ceil, log, floor, sqrt
import math
k = 1

def mod_expo(n, p, m):
	result = 1
	while p != 0:
		if p % 2 == 1:
			result = result * n % m
		p //= 2
		n = n * n % m
	return result

def is_prime(n):
	m = 2
	while m * m <= n:
		if n % m == 0:
			return False
		m += 1
	return True

def find_sum(n, a):
	a.insert(0, 0)
	for i in range(1, n + 1):
		prev = a[i] & a[i - 1]
		cur = a[i] | a[i - 1]
		a[i - 1] = prev
		a[i] = cur
	return sum((m * m for m in a))

def prin_abc(x, y, z):
	l = [x, y, z]
	l.sort()
	if l[1] < l[2]:
		print('NO')
		return
	a = b = l[0]
	c = l[2]
	print('YES')
	print(a, b, c)

def get_scores(n, vals):
	l = 0
	r = n - 1
	turn = 0
	sereja = 0
	dima = 0
	while l <= r:
		if vals[l] > vals[r]:
			cur = vals[l]
			l += 1
		else:
			cur = vals[r]
			r -= 1
		if turn == 0:
			sereja += cur
		else:
			dima += cur
		turn = not turn
	print(sereja, dima)

def count_lamps(n, m):
	return (n * m + 1) // 2

def count_moves(n, name):
	mvs = 0
	cnt = 0
	for i in range(n):
		if name[i] == 'x':
			cnt += 1
		else:
			mvs += max(0, cnt - 2)
			cnt = 0
	mvs += max(0, cnt - 2)
	return mvs

def sleep_time(a, b, c, d):
	s = a - b
	if s <= 0:
		return b
	if c > d:
		k = (s + (c - d - 1)) // (c - d)
		return b + k * c
	return -1
t = 1
t = int(input())
while t:
	t = t - 1
	points = []
	(a, b, c, d) = map(int, input().split())
	print(sleep_time(a, b, c, d))


import math

def Process(a, b, c, d):
	if a <= b:
		return b
	if c <= d:
		return -1
	a -= b
	t = b
	t += math.ceil(a / (c - d)) * c
	return t

def ReadDatasets(count):
	datasets = []
	for i in range(count):
		(a, b, c, d) = map(int, input().strip().split())
		datasets.append((a, b, c, d))
	return datasets

def ProcessDatasets(datasets):
	results = []
	for (a, b, c, d) in datasets:
		results.append(Process(a, b, c, d))
	return results
count = int(input().strip())
datasets = ReadDatasets(count)
results = ProcessDatasets(datasets)
for result in results:
	print(result)


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	(a, b, c, d) = [int(x) for x in input().split()]
	rem = a - b
	if rem <= 0:
		print(b)
		return
	if d >= c:
		print(-1)
		return
	q = c - d
	print(b + ceil(rem / q) * c)
tt = int(input())
for test in range(tt):
	solve()


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	time = b
	if b < a:
		a -= b
		if d >= c:
			print(-1)
			continue
		else:
			t1 = math.ceil(a / (c - d))
			time += c * t1
	print(time)


import math
t = int(input())
for testcase in range(t):
	(a, b, c, d) = map(int, input().split())
	answer = b
	sl = a - b
	if sl > 0 and c <= d:
		print(-1)
		continue
	if sl <= 0:
		print(b)
		continue
	m = math.ceil(sl / (c - d))
	answer += m * c
	print(answer)


from math import ceil

def solver(a, b, c, d):
	if a <= b:
		return b
	elif d >= c:
		return -1
	else:
		diff = c - d
		m = ceil((a - b) / diff)
		return b + c * m
for _ in range(int(input())):
	(A, B, C, D) = [int(i) for i in input().split()]
	print(solver(A, B, C, D))


import math
from collections import deque
t = int(input())
rez = []
for i in range(t):
	A = input().split()
	a = int(A[0])
	b = int(A[1])
	c = int(A[2])
	d = int(A[3])
	if b < a and c <= d:
		rez.append(-1)
		continue
	elif b >= a:
		rez.append(b)
	else:
		n = int(math.ceil((a - b) / (c - d)))
		rez.append(b + n * c)
for i in range(len(rez)):
	print(rez[i])


for z in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		x = max(0, (a - b) // (c - d))
		if (a - b) % (c - d) != 0:
			x += 1
		print(b + x * c)


n = int(input())
for i in range(n):
	A = list(map(int, input().split()))
	if A[1] < A[0] and A[2] <= A[3]:
		print(-1)
	elif A[1] >= A[0]:
		print(A[1])
	else:
		d = A[2] - A[3]
		if (A[0] - A[1]) % d == 0:
			print(A[1] + A[2] * ((A[0] - A[1]) // d))
		else:
			print(A[1] + A[2] * ((A[0] - A[1]) // d + 1))


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


testcases = int(input())
while testcases:
	testcases -= 1
	total_time = 0
	(sleep_time, first_alarm, recurring_time, falling_time) = [int(x) for x in input().split()]
	avalable_sleep = recurring_time - falling_time
	slept = first_alarm
	total_time = first_alarm
	if slept >= sleep_time:
		print(total_time)
		continue
	if avalable_sleep <= 0:
		print(-1)
		continue
	slots_needed = (sleep_time - slept) // avalable_sleep
	if sleep_time - slept <= slots_needed * avalable_sleep:
		print(total_time + slots_needed * recurring_time)
	else:
		total_time += (slots_needed + 1) * recurring_time
		print(total_time)


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq
import math

def ri():
	return int(input())

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

def solve():
	(a, b, c, d) = rl()
	if a <= b:
		print(b)
	else:
		a -= b
		time = b
		if c <= d:
			print(-1)
			return
		block = c - d
		remaining = int(math.ceil(a / block))
		print(time + c * remaining)
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


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


import math
answer = []
try:
	for _ in range(int(input())):
		(a, b, c, d) = input().split()
		if int(b) >= int(a):
			answer.append(int(b))
		elif int(c) <= int(d):
			answer.append(-1)
		else:
			e = int(int(a) - int(b))
			f = int(math.ceil(e / (int(c) - int(d))))
			answer.append(int(b) + f * int(c))
except ZeroDivisionError:
	pass
for i in range(len(answer)):
	print(answer[i])


test = int(input())
for i in range(test):
	string = input()
	(a, b, c, d) = [int(a) for a in string.split(' ')]
	time = c - d
	ans = b
	if b >= a:
		print(ans)
	elif time > 0:
		a -= b
		q = int(a / time)
		rem = a % time
		if rem == 0:
			ans += c * q
		else:
			ans += c * (q + 1)
		print(ans)
	else:
		print('-1')


from sys import stdin
import math
inp = stdin.readline
t = int(inp().strip())
while t:
	t -= 1
	(a, b, c, d) = [int(k) for k in inp().strip().split()]
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	timeNeeded = b + c * math.ceil((a - b) / (c - d))
	print(timeNeeded)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	time = 0
	if a <= b:
		print(b)
	else:
		a = a - b
		time += b
		if d >= c:
			print(-1)
		else:
			t = c - d
			time += c * ceil(a / t)
			print(time)


from math import ceil
from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')

def solve(a, b, c, d):
	remaining = a - b
	if remaining <= 0:
		return b
	if c <= d:
		return -1
	result = b
	times = ceil(remaining / (c - d))
	result += c * times
	return result
for i in range(int_in()):
	(a, b, c, d) = arr_in()
	out(solve(a, b, c, d))


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	(a, b, c, d) = mapi()
	if b >= a:
		print(b)
	elif c - d <= 0:
		print(-1)
	elif (a - b) % (c - d) == 0:
		print(b + c * ((a - b) // (c - d)))
	else:
		print(b + c * ((a - b) // (c - d) + 1))


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		time = b
		needed = a - b
		alarms = math.ceil(needed / (c - d))
		time += alarms * c
		print(time)


def answer(a, b, c, d):
	if b >= a:
		return b
	elif c <= d:
		return -1
	else:
		x = (a - b) // (c - d)
		if (a - b) % (c - d) != 0:
			x += 1
		return b + x * c
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	print(answer(a, b, c, d))


import math

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))
n = inp()
for i in range(n):
	(a, b, c, d) = inlt()
	res = b
	baki = a - b
	nap = c - d
	if baki > 0:
		if nap > 0:
			res += math.ceil(baki / nap) * c
		else:
			res = -1
	print(res)


import sys
import math
from math import factorial as f
from collections import defaultdict as dd
mod = 1000000007
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	else:
		x = c - d
		if x <= 0:
			print(-1)
		else:
			q = a - b
			y = q // x
			if q % x:
				y += 1
			ans = b + y * c
			print(ans)


import math
t = int(input())
while t > 0:
	(a, b, c, d) = [int(ele) for ele in input().split()]
	tot_time = 0
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		diff = c - d
		time = a - b
		div = math.ceil(time / diff)
		tot_time = b + div * c
		print(tot_time)
	t -= 1


import math
trials = int(input())
for trial in range(trials):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c and b < a:
		print(-1)
	else:
		x = math.ceil((a - b) / (c - d))
		print(b + x * c)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase
from fractions import *

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(a, b, c, d) = value()
	time = b
	a -= b
	if d >= c and a > 0:
		print(-1)
	elif a <= 0:
		print(time)
		continue
	else:
		k = ceil(a / (c - d))
		time += c * k
		print(time)


import sys
input = sys.stdin.readline

def calc_time(a, b, c, d):
	if b >= a:
		return b
	if c <= d:
		return -1
	remaining = a - b
	next_rest = c - d
	x = int(remaining / next_rest)
	if remaining % next_rest > 0:
		x = x + 1
	return b + x * c
t = int(input())
for i in range(t):
	l = input()
	a = int(l.split()[0])
	b = int(l.split()[1])
	c = int(l.split()[2])
	d = int(l.split()[3])
	print(calc_time(a, b, c, d))


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		a -= b
		if c <= d:
			print(-1)
		else:
			h = c - d
			k = a // h
			ans = b + c * k + int(bool(a % h)) * c
			print(ans)


import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *

class Scanner:

	@staticmethod
	def int():
		return int(sys.stdin.readline().rstrip())

	@staticmethod
	def string():
		return sys.stdin.readline().rstrip()

	@staticmethod
	def map_int():
		return [int(x) for x in Scanner.string().split()]

	@staticmethod
	def string_list(n):
		return [Scanner.string() for i in range(n)]

	@staticmethod
	def int_list_list(n):
		return [Scanner.map_int() for i in range(n)]

	@staticmethod
	def int_cols_list(n):
		return [Scanner.int() for i in range(n)]

class Math:

	@staticmethod
	def gcd(a, b):
		if b == 0:
			return a
		return Math.gcd(b, a % b)

	@staticmethod
	def lcm(a, b):
		return a * b // Math.gcd(a, b)

	@staticmethod
	def divisor(n):
		res = []
		i = 1
		for i in range(1, int(n ** 0.5) + 1):
			if n % i == 0:
				res.append(i)
				if i != n // i:
					res.append(n // i)
		return res

	@staticmethod
	def round_up(a, b):
		return -(-a // b)

	@staticmethod
	def is_prime(n):
		if n < 2:
			return False
		if n == 2:
			return True
		if n % 2 == 0:
			return False
		d = int(n ** 0.5) + 1
		for i in range(3, d + 1, 2):
			if n % i == 0:
				return False
		return True

def pop_count(x):
	x = x - (x >> 1 & 6148914691236517205)
	x = (x & 3689348814741910323) + (x >> 2 & 3689348814741910323)
	x = x + (x >> 4) & 1085102592571150095
	x = x + (x >> 8)
	x = x + (x >> 16)
	x = x + (x >> 32)
	return x & 127
MOD = int(1000000000.0) + 7
INF = int(1000000000000000.0)

def solve():
	(A, B, C, D) = Scanner.map_int()
	if B >= A:
		print(B)
	elif C <= D:
		print(-1)
	else:
		X = Math.round_up(A - B, C - D)
		print(B + X * C)

def main():
	T = Scanner.int()
	for _ in range(T):
		solve()
main()


from math import ceil

def main():
	abcd = input().split()
	a = int(abcd[0])
	b = int(abcd[1])
	c = int(abcd[2])
	d = int(abcd[3])
	timeRemaining = a - b
	if timeRemaining <= 0:
		print(b)
		return
	elif a > b and d >= c:
		print(-1)
		return
	else:
		numDozes = ceil((a - b) / (c - d))
	print(numDozes * c + b)
t = int(input())
for i in range(t):
	main()


import sys
import math

def scan(input_type='int'):
	if input_type == 'int':
		return list(map(int, sys.stdin.readline().strip().split()))
	else:
		return list(map(str, sys.stdin.readline().strip()))

def solution():
	for _ in range(int(input())):
		(a, b, c, d) = scan()
		if b >= a:
			print(b)
		else:
			if c <= d:
				print(-1)
				continue
			h = a - b
			t = math.ceil(h / (c - d))
			print(b + t * c)
solution()


t = int(input(''))
mat = []
for i in range(t):
	mat.append(list(map(int, input().split())))
for i in range(t):
	x = mat[i]
	a = x[0]
	b = x[1]
	c = x[2]
	d = x[3]
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	tot = (a - b) / (c - d)
	if tot != int(tot):
		tot = int(tot) + 1
	else:
		tot = int(tot)
	print(b + tot * c)


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)


from sys import stdin, stdout
for test in range(int(stdin.readline())):
	arr = [int(x) for x in stdin.readline().split()]
	sleep = arr[0]
	first_alarm = arr[1]
	second_alarm = arr[2]
	pre_sleep = arr[3]
	if sleep <= first_alarm:
		stdout.write(str(first_alarm) + '\n')
	else:
		sleep_req = sleep - first_alarm
		if second_alarm <= pre_sleep:
			stdout.write('-1\n')
		elif sleep_req % (second_alarm - pre_sleep) == 0:
			t = second_alarm * (sleep_req // (second_alarm - pre_sleep))
			stdout.write(str(first_alarm + t) + '\n')
		else:
			t = second_alarm * (sleep_req // (second_alarm - pre_sleep) + 1)
			stdout.write(str(first_alarm + t) + '\n')


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	a -= b
	if d >= c:
		print(-1)
		continue
	loop = (a + (c - d - 1)) // (c - d)
	print(b + loop * c)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	diff = c - d
	if a - b >= 0 and diff > 0:
		j = math.ceil((a - b) / diff)
		print(b + c * j)
	elif diff <= 0 and a - b > 0:
		print(-1)
	else:
		print(b)


t = int(input())
for i in range(t):
	num1 = list(map(int, input().split()))
	a = num1[0]
	b = num1[1]
	c = num1[2]
	d = num1[3]
	e = a - b
	f = c - d
	s = 0
	if e <= 0:
		print(b)
		s += 1
	if e > 0:
		if f <= 0:
			print(-1)
			s += 1
		if f > 0:
			g = e // f
			h = e % f
			if h != 0:
				g += 1
			print(b + g * c)


from math import ceil

class Solution:

	def __init__(self):
		(self.a, self.b, self.c, self.d) = [int(x) for x in input().split()]

	def solve_and_print(self):
		if self.a <= self.b:
			print(self.b)
		elif self.c <= self.d:
			print(-1)
		else:
			print(self.b + self.c * ceil((self.a - self.b) / (self.c - self.d)))
for _ in range(int(input())):
	Solution().solve_and_print()


import sys, math
max_int = 1000000001
min_int = -max_int
t = int(input())
for _t in range(t):
	(a, b, c, d) = map(int, sys.stdin.readline().split())
	if b >= a:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	print(b + c * math.ceil((a - b) / (c - d)))


recordArr = []

class AlarmClock:

	def __init__(self, sleepRequireTime, alarmFirstTime, alarmRecurTime, sleepAgainTime):
		super().__init__()
		self.sleepRequireTime = sleepRequireTime
		self.alarmFirstTime = alarmFirstTime
		self.alarmRecurTime = alarmRecurTime
		self.sleepAgainTime = sleepAgainTime
		self.totalUsedTime = 0
		self.totalSleepTime = 0

	def alarmFunction(self):
		if self.alarmFirstTime >= self.sleepRequireTime:
			print(self.alarmFirstTime)
			return
		if self.alarmRecurTime <= self.sleepAgainTime:
			print(-1)
			return
		remainTime = self.sleepRequireTime - self.alarmFirstTime
		sleepTime = self.alarmRecurTime - self.sleepAgainTime
		numberOfSleep = 0
		if remainTime % sleepTime > 0:
			numberOfSleep = int(remainTime / sleepTime) + 1
		else:
			numberOfSleep = int(remainTime / sleepTime)
		self.totalUsedTime = self.alarmFirstTime + numberOfSleep * self.alarmRecurTime
		print(self.totalUsedTime)
		return
numOfRecords = input()
for i in range(0, int(numOfRecords)):
	record = input()
	records = record.split(' ')
	recordArr.append(records)
for i in recordArr:
	ac = AlarmClock(int(i[0]), int(i[1]), int(i[2]), int(i[3]))
	ac.alarmFunction()


from sys import stdin, stdout
import math
t = int(input())
for ii in range(t):
	(a, b, c, d) = list(map(int, input().split()))
	if a <= b:
		print(b)
	elif d >= c:
		print(-1)
	else:
		y = a - b
		g = c - d
		print(b + math.ceil(y / g) * c)


from sys import stdin
from math import ceil, log
input = stdin.readline

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

def sep_input():
	return map(int, input().split())
for _ in range(int(input())):
	(a, b, c, d) = sep_input()
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		ans = b + ceil((a - b) / (c - d)) * c
		print(ans)


for _ in range(int(input())):
	l = list(input().split(' '))
	a = int(l[0])
	b = int(l[1])
	c = int(l[2])
	d = int(l[3])
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	elif (a - b) % (c - d) == 0:
		time = b + (a - b) // (c - d) * c
		print(time)
	else:
		time = b + (a - b) // (c - d) * c + c
		print(time)


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


def run():
	import sys
	sys.stdin = open('/home/punit/Documents/Cpp Programs/input.txt', 'r')
	sys.stdout = open('/home/punit/Documents/Cpp Programs/output.txt', 'w')
from math import ceil, sqrt, floor
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		Sum = b
		x = int(ceil((a - b) / (c - d)))
		x = x * c
		Sum += x
		print(Sum)


import sys
input = sys.stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		(a, b, c, d) = map(int, input().split())
		if a <= b:
			print(b)
		elif c <= d:
			print(-1)
		else:
			sleep = a - b
			x = c - d
			y = sleep // x
			if sleep % x > 0:
				y += 1
			ans = b + y * c
			print(ans)
main()


from sys import stdin, stdout
import math
input = stdin.readline
print = stdout.write
for p in range(int(input())):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if b >= a:
		print(str(b) + '\n')
	elif b < a:
		if d >= c:
			print(str(-1) + '\n')
		else:
			ans = b + c * math.ceil((a - b) / (c - d))
			print(str(ans) + '\n')


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		result = b
	elif b < a and d >= c:
		result = -1
	else:
		time_after_first = a - b
		time_sleep_each_alarm = c - d
		total_round = math.ceil(time_after_first / time_sleep_each_alarm)
		result = b + total_round * c
	print(result)


from math import ceil
t = int(input())
while t:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	tt = b + c * ceil((a - b) / (c - d))
	print(tt)


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


for tc in range(int(input())):
	alist = list(map(int, input().split()))

	def f(a, b, c, d):
		if b >= a:
			return b
		if c - d <= 0:
			return -1
		if (a - b) / (c - d) != int((a - b) / (c - d)):
			te = c * (int((a - b) / (c - d)) + 1)
		else:
			te = c * int((a - b) / (c - d))
		return b + te
	print(f(alist[0], alist[1], alist[2], alist[3]))


def solve(a, b, c, d):
	if b >= a:
		return b
	else:
		p = c - d
		if p <= 0:
			return -1
		elif (a - b) % p == 0:
			m = (a - b) // p
			return b + c * m
		else:
			m = (a - b) // p
			return b + c * (m + 1)
t = int(input())
ans = []
for s in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	ans.append(solve(a, b, c, d))
for it in ans:
	print(it)


import collections as cc
import heapq as hp
import math as mt
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(a, b, c, d) = I()
	if b >= a:
		print(b)
	elif c - d <= 0:
		print(-1)
	else:
		t = c - d
		tt = mt.ceil(abs(a - b) / t)
		print(b + tt * c)


for _ in range(int(input())):
	arr = list(map(int, input().rstrip().split()))[:4]
	(a, b, c, d, count) = (arr[0], arr[1], arr[2], arr[3], 0)
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		(y, x) = (a - b, c - d)
		if y % x != 0:
			an = b + (int(y / x) + 1) * c
		else:
			an = b + int(y / x) * c
		print(an)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

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

def inputstring():
	return [x for x in input()]
t = int(input())
for q in range(t):
	(a, b, c, d) = inputnums()
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	ans = (a - b) // (c - d) + ((a - b) % (c - d) != 0)
	ans *= c
	ans += b
	print(ans)


t = int(input())
i = 0
import math
while i < t:
	(a, b, c, d) = map(int, input().split())
	a = a - b
	if a <= 0:
		print(b)
	else:
		z = c - d
		if z <= 0:
			print(-1)
		else:
			print(b + c * math.ceil(a / z))
	i = i + 1


import math
import sys
n = int(input())
for i in range(n):
	[a, b, c, d] = list(map(int, input().split()))
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		total_time = b
		left_time = a - b
		rate = c - d
		if left_time / rate == int(left_time / rate):
			print(total_time + int(left_time / rate) * c)
		else:
			print(total_time + (int(left_time / rate) + 1) * c)


import math

def task(a, b, c, d):
	remaining = a - b
	total = b
	if remaining <= 0:
		print(b)
		return
	if d >= c:
		print(-1)
		return
	diff = c - d
	sleeps = math.ceil(remaining / diff)
	total += sleeps * c
	print(total)
t = int(input())
for i in range(0, t):
	(a, b, c, d) = map(int, input().split())
	task(a, b, c, d)


import sys
from math import sqrt, floor, factorial, gcd
from collections import deque, Counter, defaultdict
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def solve():
	ans = ''
	for _ in range(int(inp())):
		(a, b, c, d) = read()
		if a <= b:
			ans += str(b) + '\n'
			continue
		elif c <= d:
			ans += '-1\n'
			continue
		else:
			ans += str(b + (a - b) // (c - d) * c + [c, 0][(a - b) % (c - d) == 0]) + '\n'
	print(ans)
solve()


pp = print
li = list
tp = tuple
bg = 1000000000.0
sg = -1000000000.0
rd = round
ss = sorted
sj = ''.join
nl = '\n'

def solve():
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		print(b + (0 - -(a - b) // (c - d)) * c)

def main():
	for i in range(int(input())):
		solve()
main()


t = int(input())
for case in range(t):
	[a, b, c, d] = list(map(int, input().split()))
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		refreshes = (a - b) // (c - d)
		if (a - b) % (c - d) != 0:
			refreshes += 1
		time = b + c * refreshes
		print(time)


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


import sys
import math
import bisect

def solve():
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		return b
	a -= b
	if c <= d:
		return -1
	return b + (a + c - d - 1) // (c - d) * c

def main():
	for _ in range(int(input())):
		print(solve())
main()


t = int(input())
for i in range(t):
	z = []
	z = list(map(int, input().split()))
	a = z[0]
	b = z[1]
	c = z[2]
	d = z[3]
	ans = 0
	if a > b and c <= d:
		print(-1)
	elif a <= b:
		print(b)
	else:
		ans = (a - b) // (c - d) * c + b
		if (a - b) % (c - d) > 0:
			ans += c
		print(ans)


import math
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a > b and d >= c:
		print(-1)
	elif b >= a:
		print(b)
	else:
		s = a - b
		z = math.ceil(s / (c - d))
		print(b + z * c)


import sys
import math
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
mod = 1000000007

def nospace(l):
	ans = ''.join((str(i) for i in l))
	return ans
t = int(input())
for tt in range(t):
	(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)
if fileoperation:
	sys.stdout = orig_stdout
	sys.stdin = orig_stdin
	inputfile.close()
	outputfile.close()


from sys import stdin
import math
t = int(stdin.readline())
for _ in range(t):
	(a, b, c, d) = (int(var) for var in stdin.readline().split())
	sleep_left = max(a - b, 0)
	if sleep_left != 0 and c - d <= 0:
		print(-1)
	elif not c - d <= 0:
		print(b + c * math.ceil(sleep_left / (c - d)))
	elif sleep_left == 0:
		print(b)
	else:
		print(-1)


import sys
import math
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a == b:
		print(a)
	elif a < b:
		print(b)
	elif c > d:
		t = a - b
		s = c - d
		r = 0
		if t % s == 0:
			r = t // s
		else:
			r = t // s + 1
		print(b + c * r)
	else:
		print(-1)


import math
for t in range(int(input())):
	(a, b, c, d) = [int(i) for i in input().split()]
	cycle_one = a - b
	if cycle_one > 0:
		if c > d:
			time_of_actual_sleep = c - d
			time_left = a - b
			cycles_needed = math.ceil(time_left / time_of_actual_sleep)
			total_time = c * cycles_needed + b
			print(total_time)
		else:
			print(-1)
	else:
		print(b)


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


import math
t = int(input())
for i in range(t):
	a = input().split()
	(a, b, c, d) = (int(a[0]), int(a[1]), int(a[2]), int(a[3]))
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		rest_time = c - d
		ans = b
		a -= b
		div = math.ceil(a / rest_time)
		ans += div * c
		print(ans)


import math
t = 1
t = int(input())
for tt in range(t):
	(a, b, c, d) = map(int, input().split())
	s = a - b
	if s > 0 and d >= c:
		print(-1)
	elif s <= 0:
		print(b)
	else:
		print(int(b + math.ceil((a - b) / (c - d)) * c))


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


import os
import sys
import math
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def dijkstra(start, distance, path, n):
	visited = [False for _ in range(n)]
	distance[start] = 0
	for i in range(n):
		v = -1
		for j in range(n):
			if not visited[v] and (v == -1 or distance[j] < distance[v]):
				v = j
		if distance[v] == math.inf:
			break
		visited[v] = True
		for edge in adj[v]:
			destination = edge[0]
			weight = edge[1]
			if distance[v] + weight < distance[destination]:
				distance[destination] = distance[v] + weight
				path[destination] = v

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def npr(n, r):
	return math.factorial(n) // math.factorial(n - r)

def seive(n):
	primes = [True] * (n + 1)
	ans = []
	for i in range(2, n):
		if not primes[i]:
			continue
		j = 2 * i
		while j <= n:
			primes[j] = False
			j += i
	for p in range(2, n + 1):
		if primes[p]:
			ans += [p]
	return ans

def factors(n):
	factors = []
	x = 1
	while x * x <= n:
		if n % x == 0:
			if n // x == x:
				factors.append(x)
			else:
				factors.append(x)
				factors.append(n // x)
		x += 1
	return factors

def main():
	try:
		for _ in range(inp()):
			(a, b, c, d) = invr()
			if b >= a:
				print(b)
				continue
			elif a > b and d >= c:
				print(-1)
				continue
			rem = a - b
			print(math.ceil(rem / (c - d)) * c + b)
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split(' '))
	remainingSleeptime = a - b
	if remainingSleeptime <= 0:
		print(b)
		continue
	sleepDiff = c - d
	if sleepDiff <= 0:
		print(-1)
		continue
	total = b + c * math.ceil(remainingSleeptime / sleepDiff)
	print(total)


from sys import stdin, stdout
from os import path
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
	stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

	def S():
		return stdin.readline().rstrip()

	def Sn():
		return stdin.readline().split(' ')
from math import ceil

def func(a, b, c, d):
	if a <= b:
		return b
	extra = c - d
	if extra < 1:
		return -1
	return b + c * ceil((a - b) / extra)
st = ''
for _ in range(I()):
	(a, b, c, d) = In()
	ans = str(func(a, b, c, d))
	st = '\n'.join((st, ans))
print(st.lstrip())


def check(a, b, c, d):
	if b >= a:
		return b
	count = b
	times = 0
	if d >= c:
		return -1
	num = (a - b) / (c - d)
	if int(num) != num:
		times += int(num) + 1
	else:
		times += int(num)
	return times * c + b
for _ in range(int(input())):
	arr = list(map(int, input().split()))
	(a, b, c, d) = (arr[0], arr[1], arr[2], arr[3])
	res = check(a, b, c, d)
	print(res)


t = int(input())
a = []
b = []
c = []
d = []
for i in range(t):
	p = list(map(int, input().split()))
	a.append(p[0])
	b.append(p[1])
	c.append(p[2])
	d.append(p[3])
for i in range(t):
	if b[i] < a[i] and c[i] <= d[i]:
		print(-1)
	elif b[i] >= a[i]:
		print(b[i])
	else:
		k = c[i] - d[i]
		if (a[i] - b[i]) % k == 0:
			time = b[i] + c[i] * ((a[i] - b[i]) // k)
		else:
			time = b[i] + c[i] * ((a[i] - b[i]) // k + 1)
		print(time)


input = __import__('sys').stdin.readline
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a > b and d >= c:
		print(-1)
	else:
		k = min(a, b)
		if a - k == 0:
			print(b)
		else:
			ans = b
			if (a - b) % (c - d) == 0:
				ans += (a - b) // (c - d) * c
			else:
				ans += ((a - b) // (c - d) + 1) * c
			print(ans)


import math
out = []
t = int(input())
try:
	for i in range(t):
		(a, b, c, d) = input().split()
		if int(b) >= int(a):
			out.append(int(b))
		elif int(c) <= int(d):
			out.append(-1)
		else:
			temp = int(int(a) - int(b))
			temp1 = int(math.ceil(temp / (int(c) - int(d))))
			out.append(int(b) + temp1 * int(c))
	for i in range(t):
		print(out[i])
except ZeroDivisionError:
	pass


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


def rs():
	return input().strip()

def ri():
	return int(input())

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

def ia_to_s(a):
	return ' '.join([str(s) for s in a])

def solve(a, b, c, d):
	if a <= b:
		return b
	slept = b
	left = a - b
	if c <= d:
		return -1
	cycle = c - d
	needed_cycles = left // cycle
	if left % cycle > 0:
		needed_cycles += 1
	return slept + needed_cycles * c

def main():
	for _ in range(ri()):
		(a, b, c, d) = ria()
		print(solve(a, b, c, d))
main()


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


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	(a, b, c, d) = li()
	a -= b
	if a <= 0:
		print(b)
		return
	if c <= d:
		print(-1)
	else:
		b += math.ceil(a / (c - d)) * c
		print(b)
for _ in range(I()):
	solve()


for _ in range(int(input())):
	(a, b, c, d) = list(map(int, input().strip().split()))[:4]
	if a <= b:
		print(b)
	else:
		delta = c - d
		try:
			if delta >= 0:
				print(b + (a - b + (c - d - 1)) // (c - d) * c)
			else:
				print(-1)
		except:
			print(-1)


n = int(input())
for _ in range(n):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		ttl = b
	elif d >= c:
		ttl = -1
	else:
		a -= b
		slp = c - d
		val = a // slp + 1 if a % slp > 0 else a // slp
		ttl = b + val * c
	print(ttl)


n = int(input())
l = []
time = 0
for i in range(n):
	(a, b, c, d) = [int(x) for x in input().split()]
	if a <= b:
		time = b
	elif c <= d:
		if a <= b:
			time = b
		else:
			time = -1
	else:
		p = a - b
		q = c - d
		e = (p + q - 1) / q
		e = int(e)
		time = c * e + b
	l.append(time)
for t in l:
	print(t)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if b < a and d > c:
		print(-1)
	elif b >= a:
		print(b)
	elif b < a and d < c:
		x = 0
		if (a - b) % (c - d) != 0:
			x += 1
		r = (a - b) // (c - d) + x
		print(b + c * r)
	elif b < a and c == d:
		print(-1)


def getN():
	return int(input())

def getNM():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))
from collections import defaultdict, deque
from sys import exit
import math
import copy
from bisect import bisect_left
from heapq import *
import sys
INF = 10 ** 17
MOD = 998244353

def dfs(n, ans, i, l, r):
	h = []
	heappush(h, (n, l, r))
	while h:
		(_, cl, cr) = heappop(h)
		if (cl - cr + 1) % 2 == 1:
			ans[(cl + cr) // 2 - 1] = i
			(nl1, nr1) = (cl, (cl + cr) // 2 - 1)
			if nl1 <= nr1:
				heappush(h, (-(nr1 - nl1 + 1), nl1, nr1))
			(nl2, nr2) = ((cl + cr) // 2 + 1, cr)
			if nl2 <= nr2:
				heappush(h, (-(nr2 - nl2 + 1), nl2, nr2))
		else:
			ans[(cl + cr - 1) // 2 - 1] = i
			(nl1, nr1) = (cl, (cl + cr - 1) // 2 - 1)
			if nl1 <= nr1:
				heappush(h, (-(nr1 - nl1 + 1), nl1, nr1))
			(nl2, nr2) = ((cl + cr - 1) // 2 + 1, cr)
			if nl2 <= nr2:
				heappush(h, (-(nr2 - nl2 + 1), nl2, nr2))
		i += 1
	return ans

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

def main():
	n = getN()
	for _ in range(n):
		solve()
main()


import math

def check(a, b, c, d):
	if b >= a:
		return b
	elif c <= d:
		return -1
	else:
		count = 0
		val = math.ceil((a - b) / (c - d))
		return b + val * c
t = int(input())
while t:
	inp1 = list(map(int, input().strip().split()))[:4]
	(a, b, c, d) = (inp1[0], inp1[1], inp1[2], inp1[3])
	l = check(a, b, c, d)
	print(l)
	t -= 1


import math
test = int(input())
test_arr = []
for _ in range(test):
	(a, b, c, d) = map(int, filter(None, input().split(' ')))
	if a > b:
		total = b
		actual_sleep = b
		remain = a - b
		if d >= c:
			test_arr.append(-1)
			continue
		else:
			diff = int(math.ceil(remain / (c - d)))
			total = total + c * diff
			test_arr.append(total)
	else:
		test_arr.append(b)
for t in test_arr:
	print(t)


from math import ceil, gcd, factorial
from bisect import bisect_left as bl
from bisect import bisect_right as br
from array import array
from collections import Counter as ctr
from collections import deque as dq

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

def arr(a):
	return array('i', a)

def solve():
	for _ in range(int(input())):
		(a, b, c, d) = li()
		if b >= a:
			print(b)
			continue
		t = b
		a -= b
		if d >= c:
			print(-1)
			continue
		t += c * ceil(a / (c - d))
		print(t)
solve()


import math
cases = int(input())
answers = []
for i in range(cases):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		answers.append(b)
	elif c <= d:
		answers.append(-1)
	else:
		n = math.ceil((a - b) / (c - d))
		answers.append(b + n * c)
for ans in answers:
	print(ans)


t = int(input())
final_ans = []
for i in range(t):
	alarm_info = list(map(int, input().split()))
	a = alarm_info[0]
	b = alarm_info[1]
	c = alarm_info[2]
	d = alarm_info[3]
	if b < a and c <= d:
		final_ans.append(-1)
	elif a <= b:
		final_ans.append(b)
	else:
		ans = b + (a - b) // (c - d) * c
		if (a - b) % (c - d) != 0:
			ans += c
			final_ans.append(ans)
		else:
			final_ans.append(ans)
for j in final_ans:
	print(j)


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


__author__ = 'Md. Imrul Hasan @UAP-CSE'
import math

def func(a, b, c, d):
	if b >= a:
		return b
	elif c <= d:
		return -1
	else:
		nSleepTime = a - b
		intervalAlerm = c - d
		alermNeeded = math.ceil(nSleepTime / intervalAlerm)
		return int(alermNeeded * c) + b
t = int(input())
results = list()
for _ in range(0, t):
	(a, b, c, d) = map(int, input().split(' '))
	results.append(func(a, b, c, d))
for result in results:
	print(result)


import math
for case in range(int(input())):
	(a, b, c, d) = [int(x) for x in input().split()]
	if a <= b:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	total_sleep_time = b
	current_time = b
	n = c - d
	k = math.ceil((a - total_sleep_time) / n)
	current_time += k * c
	print(current_time)


import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left
sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7

def input():
	return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

def solve():
	(a, b, c, d) = map(int, input().split())
	a -= b
	if a <= 0:
		print(b)
		return
	if c <= d:
		print(-1)
		return
	if a % (c - d) == 0:
		cds = a // (c - d)
	else:
		cds = a // (c - d) + 1
	print(b + c * cds)
	return
T = int(input())
for t in range(T):
	solve()


import math
for i in range(int(input())):
	m = list(map(int, input().split()))
	if m[0] <= m[1]:
		print(m[1])
	elif m[2] <= m[3]:
		print(-1)
	else:
		print(m[1] + math.ceil((m[0] - m[1]) / (m[2] - m[3])) * m[2])


import math as m
t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif a > b:
		if c <= d:
			print(-1)
		else:
			print(m.ceil((a - b) / (c - d)) * c + b)


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


from math import ceil
T = int(input())
while T:
	(a, b, c, d) = map(int, input().split())
	time = b
	a -= b
	if b >= a + b:
		print(time)
		T -= 1
		continue
	if d >= c and a >= 0:
		print(-1)
	else:
		time += ceil(a / (c - d)) * c
		print(time)
	T -= 1


from math import ceil
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if a - b > 0 and c <= d:
		print(-1)
	elif a - b <= 0:
		print(b)
	else:
		missing_time = a - b
		total_times = ceil(missing_time / (c - d))
		print(b + total_times * c)


class Solution:

	def solve(self) -> None:
		parm = input().split(' ')
		a = int(parm[0])
		b = int(parm[1])
		c = int(parm[2])
		d = int(parm[3])
		if d >= c and b < a:
			print(-1)
			return
		ret = b
		needSleep = a - b
		if needSleep <= 0:
			print(ret)
			return
		slot = c - d
		needTime = needSleep // slot
		if needSleep % slot > 0:
			needTime += 1
		print(ret + c * needTime)
		return
x = Solution()
t = int(input())
for i in range(t):
	x.solve()


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ans = -1
	req = a - b
	if req == 0 or req < 0:
		ans = b
	if req > 0:
		if c > d:
			ans = req // (c - d)
			if req % (c - d) > 0:
				ans += 1
			ans = b + ans * c
	print(ans)


import math
t = int(input())
while t > 0:
	t -= 1
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
		continue
	ans = 0
	ans += b
	a -= b
	if c <= d:
		print(-1)
		continue
	n = math.ceil(a / (c - d))
	ans += n * c
	print(ans)


for _ in range(int(input())):
	splits = input().split(' ')
	a = int(splits[0])
	b = int(splits[1])
	c = int(splits[2])
	d = int(splits[3])
	if a <= b:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	r = a - b
	t = r // (c - d)
	if r % (c - d) != 0:
		t += 1
	print(b + t * c)


import math
t = int(input())
for i in range(t):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if d >= c and a > b:
		print('-1')
	elif b >= a:
		print(b)
	elif c == d:
		l = math.ceil((a - b) / c)
		print(b + l * c)
	else:
		k = c - d
		k1 = a - b
		k2 = math.ceil(k1 / k)
		print(b + k2 * c)


import math
t = int(input())
for i in range(t):
	l = [int(k) for k in input().split()]
	a = l[0]
	b = l[1]
	c = l[2]
	d = l[3]
	if a <= b:
		print(b)
	elif c - d <= 0:
		print('-1')
	else:
		t1 = c - d
		t2 = a - b
		tt = math.ceil(t2 / t1)
		ans = tt * c + b
		print(ans)


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


import sys, math
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	else:
		rt = a - b
		ts = c - d
		if ts <= 0:
			print(-1)
		else:
			print(math.ceil(rt / ts) * c + b)


for _ in range(int(input())):
	(wake_t, t_before_al, t_of_sl, t_to_sl) = map(int, input().split())
	if t_to_sl >= t_of_sl and wake_t > t_before_al:
		print(-1)
	else:
		a = wake_t - t_before_al
		b = t_of_sl - t_to_sl
		if a <= 0:
			print(t_before_al)
		elif a % b == 0:
			print(t_before_al + t_of_sl * (a // b))
		else:
			print(t_before_al + t_of_sl * (a // b + 1))


import math
t = int(input())
while t != 0:
	t = t - 1
	(a, b, c, d) = [int(x) for x in input().split()]
	a = a - b
	if a <= 0:
		print(b)
	else:
		ans = b
		if d >= c:
			print('-1')
		else:
			k = c - d
			k = int(math.ceil(a / k))
			ans = ans + c * k
			print(ans)


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


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


z = int(input())
for h in range(z):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		ans = b
	elif c <= d:
		ans = -1
	else:
		x = c - d
		y = a - b
		if y % x != 0 or y < x:
			ans = b + (y // x + 1) * c
		else:
			ans = b + y // x * c
	print(ans)


T = int(input())
for t in range(T):
	(a, b, c, d) = list(map(int, input().strip().split()))
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		x = a - b
		xt = a - x
		y = c - d
		if y != 0:
			sleeptime = xt + x // y * y
			if sleeptime >= a:
				bedtime = xt + x // y * c
				print(bedtime)
			else:
				bedtime = xt + (x // y + 1) * c
				print(bedtime)


def overall_time(lst):
	(a, b, c, d) = lst
	if b >= a:
		return b
	elif c > d:
		cnt = int((a - b) / (c - d)) + int((a - b) % (c - d) > 0)
		return b + cnt * c
	else:
		return -1
t = int(input())
lst_abcd = []
for i in range(t):
	lst_abcd.append(list(map(int, input().split())))
for lst in lst_abcd:
	print(overall_time(lst))


import math
result = []
t = int(input())
try:
	for i in range(t):
		(t1, t2, t3, t4) = input().split()
		if int(t2) >= int(t1):
			result.append(int(t2))
		elif int(t3) <= int(t4):
			result.append(-1)
		else:
			temp_variable = int(int(t1) - int(t2))
			temp1_variable = int(math.ceil(temp_variable / (int(t3) - int(t4))))
			result.append(int(t2) + temp1_variable * int(t3))
	for i in range(t):
		print(result[i])
except ZeroDivisionError:
	pass


t = int(input())
for i in range(t):
	(a, b, c, d) = [int(x) for x in input().split()]
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = c - d
		y = a - b
		n = 1
		n = int(y / x)
		if n == y / x:
			n = int(n)
		else:
			n = int(n) + 1
		ans = b + n * c
		print(ans)


def ciel(x):
	if int(x) != x:
		return int(x) + 1
	else:
		return int(x)
for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif d >= c:
		print(-1)
	else:
		temp = ciel((a - b) / (c - d))
		out = b + temp * c
		print(out)


def main(a, b, c, d):
	if b >= a:
		return b
	if c <= d:
		return -1
	else:
		x = int(abs(a - b) / abs(d - c))
		if abs(a - b) % abs(d - c) != 0:
			x = x + 1
		return b + x * c
t = int(input())
for t_itr in range(t):
	abcd = list(map(int, input().rstrip().split()))
	a = abcd[0]
	b = abcd[1]
	c = abcd[2]
	d = abcd[3]
	res = main(a, b, c, d)
	print(res)


def int_div_ceil(a, b):
	return int((a - 1) // b + 1)
t = int(input())
for _ in range(t):
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif c <= d:
		print(-1)
	else:
		a -= b
		sleeping_period = c - d
		times = int_div_ceil(a, sleeping_period)
		add_time = times * c
		whole_time = b + add_time
		print(whole_time)


import math
t = int(input())
while t:
	(a, b, c, d) = map(int, input().split())
	if a <= b:
		print(b)
	elif a > b:
		z = a - b
		if c <= d:
			print(-1)
		else:
			q = c - d
			h = math.ceil(z / q)
			print(b + h * c)
	t -= 1


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
for _ in range(int(I1())):
	(a, b, c, d) = I()
	if b >= a:
		print(b)
		continue
	if d >= c:
		print(-1)
		continue
	x = a - b
	y = c - d
	z = 0
	if y:
		z = math.ceil(x / y)
	print(b + c * z)


from collections import defaultdict
import sys
import math
sys.setrecursionlimit(1000000)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')
p2S = lambda x: print(*x, sep=' ')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		(a, b, c, d) = MI()
		if d >= c:
			if b >= a:
				print(b)
			else:
				print(-1)
		elif b >= a:
			print(b)
		else:
			print(c * math.ceil((a - b) / (c - d)) + b)
main()


import math
T = int(input())
while T:
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		x = c - d
		y = a - b
		s = y / x
		z = int(math.ceil(s))
		print(b + z * c)
	T = T - 1


def main():
	(a, b, c, d) = map(int, input().split())
	if b >= a:
		print(b)
	elif c <= d:
		print(-1)
	else:
		res = b
		a -= b
		hrs = c - d
		res += c * (a // hrs)
		if a % hrs != 0:
			res += c
		print(res)
t = int(input())
for i in range(t):
	main()