Let's define a multiplication operation between a string $a$ and a positive integer $x$: $a \cdot x$ is the string that is a result of writing $x$ copies of $a$ one after another. For example, "abc" $\cdot~2~=$ "abcabc", "a" $\cdot~5~=$ "aaaaa".

A string $a$ is divisible by another string $b$ if there exists an integer $x$ such that $b \cdot x = a$. For example, "abababab" is divisible by "ab", but is not divisible by "ababab" or "aa".

LCM of two strings $s$ and $t$ (defined as $LCM(s, t)$) is the shortest non-empty string that is divisible by both $s$ and $t$.

You are given two strings $s$ and $t$. Find $LCM(s, t)$ or report that it does not exist. It can be shown that if $LCM(s, t)$ exists, it is unique.


-----Input-----

The first line contains one integer $q$ ($1 \le q \le 2000$) — the number of test cases.

Each test case consists of two lines, containing strings $s$ and $t$ ($1 \le |s|, |t| \le 20$). Each character in each of these strings is either 'a' or 'b'.


-----Output-----

For each test case, print $LCM(s, t)$ if it exists; otherwise, print -1. It can be shown that if $LCM(s, t)$ exists, it is unique.


-----Examples-----

Input
3
baba
ba
aa
aaa
aba
ab
Output
baba
aaaaaa
-1


-----Note-----

In the first test case, "baba" = "baba" $\cdot~1~=$ "ba" $\cdot~2$.

In the second test case, "aaaaaa" = "aa" $\cdot~3~=$ "aaa" $\cdot~2$.

Source: codeforces | Difficulty: EASY
Tags: Number theory

Complete search

Mathematics

String algorithms


Solutions:
import math
for _ in range(int(input())):
	s = input()
	t = input()
	sb = ''
	k = 0
	for i in range(1, min(len(s), len(t)) + 1):
		if len(s) % i == 0 and len(t) % i == 0:
			sub = s[0:i]
			if s == sub * (len(s) // i) and t == sub * (len(t) // i):
				sb = sub
				k = i
				break
	if sb == '':
		print(-1)
	else:
		a = len(s) // k
		b = len(t) // k
		print(sb * (a * b // math.gcd(a, b)))


def ceil(c):
	if int(c) == c:
		return int(c)
	return int(c) + 1

def compute_lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm

def solve():
	s = input()
	t = input()
	ls = len(s)
	lt = len(t)
	if s == t:
		return s
	mi = min(ls, lt)
	(ts, tt) = ('', '')
	for i in range(ls):
		ts += s[i]
		if s.count(ts) == ceil(ls / len(ts)):
			break
	for i in range(lt):
		tt += t[i]
		if t.count(tt) == ceil(lt / len(tt)):
			break
	if ts != tt:
		return -1
	return ts * compute_lcm(t.count(ts), s.count(ts))
for _ in range(int(input())):
	print(solve())


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

def lcm(x, y):
	return int(x * y / gcd(x, y))
for _ in range(int(input())):
	s = input()
	t = input()
	lcm_s = lcm(len(s), len(t))
	s *= lcm_s // len(s)
	t *= lcm_s // len(t)
	if s == t:
		print(s)
	else:
		print(-1)


import math
I = input
exec(int(I()) * 's=I();t=I();n=len(s);m=len(t);d=math.gcd(n,m);print((m//d*s,-1)[m//d*s!=n//d*t]);')


def gcd(a, b):
	while b:
		a %= b
		(a, b) = (b, a)
	return a

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

def main():
	q = int(input())
	for i in range(q):
		a = input()
		b = input()
		na = len(a)
		nb = len(b)
		x = lcm(na, nb)
		a *= int(x / na)
		b *= int(x / nb)
		if a == b:
			print(a)
		else:
			print(-1)
main()


def GCD(a: int, b: int):
	return a if b == 0 else GCD(b, a % b)
q = int(input())
for _ in range(q):
	s = input()
	t = input()
	(ls, lt) = (len(s), len(t))
	lcm = ls * lt // GCD(ls, lt)
	S = s * (lcm // ls)
	print(S if S == t * (lcm // lt) else '-1')


import math
for test_var in range(int(input())):
	a = input()
	b = input()
	if len(a) < len(b):
		(a, b) = (b, a)
	if a * len(b) == b * len(a):
		gc = math.gcd(len(a), len(b))
		print(a * (len(b) // gc))
	else:
		print(-1)


def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
for _ in range(int(input())):
	a = input()
	b = input()
	l1 = len(a)
	l2 = len(b)
	if l1 == l2:
		if a == b:
			print(a)
		else:
			print(-1)
	else:
		l = lcm(l1, l2)
		ans = ''
		k = 0
		if l2 > l1:
			(b, a) = (a, b)
		n = len(b)
		for _ in range(l):
			a1 = k % n
			ans = ans + b[a1]
			k += 1
		f = 0
		k = 0
		n = len(a)
		for i in range(l):
			a1 = k % n
			if ans[i] != a[a1]:
				f = 1
				break
			k += 1
		if f == 1:
			print(-1)
		else:
			print(ans)


from math import gcd

def lcm(a, b):
	return abs(a * b) // gcd(a, b)
q = int(input())
for i in range(q):
	s = input()
	t = input()
	for j in range(1, len(s) + 1):
		k = 0
		while k < len(s):
			if s[:j] != s[k:k + j]:
				break
			k += j
		else:
			n = j
			break
	k = 0
	while k < len(t):
		if s[:n] != t[k:k + n]:
			print(-1)
			break
		k += n
	else:
		print(s[:n] * lcm(len(s) // n, len(t) // n))


from math import *
T = int(input())
while T:
	s1 = input()
	s2 = input()
	l1 = len(s1)
	l2 = len(s2)
	l = gcd(l1, l2)
	lcm = l1 * l2 // l
	rep = s1[:l]
	if rep * (l1 // l) == s1 and rep * (l2 // l) == s2:
		print(rep * (lcm // l))
	else:
		print(-1)
	T -= 1


import math

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

def lcm(a, b):
	return a / gcd(a, b) * b
try:
	t = int(input())
	while t:
		t -= 1
		s = input()
		s1 = input()
		m = lcm(len(s), len(s1))
		r1 = int(m // len(s))
		r2 = int(m // len(s1))
		a = s * r1
		b = s1 * r2
		if a == b:
			print(a)
		else:
			print(-1)
except:
	pass


def LCMString():
	for _ in range(int(input())):
		S = input()
		t = input()
		if not max(len(S), len(t)) % min(len(S), len(t)):
			x = max(len(S), len(t)) // min(len(S), len(t))
			if len(S) > len(t):
				if t * x == S:
					print(S)
				else:
					print(-1)
			elif S * x == t:
				print(t)
			else:
				print(-1)
		else:
			PPCM = LCM(len(S), len(t))
			S = S * (PPCM // len(S))
			t = t * (PPCM // len(t))
			if S == t:
				print(S)
			else:
				print(-1)

def LCM(a, b):
	if a == b:
		return a
	elif a > b:
		return LCM(a - b, b) * a // (a - b)
	else:
		return LCM(a, b - a) * b // (b - a)
LCMString()


from sys import stdin
import math
for _ in range(int(stdin.readline())):
	s1 = stdin.readline().strip()
	s2 = stdin.readline().strip()
	if len(s1) > len(s2):
		(s1, s2) = (s2, s1)
	l1 = len(s1)
	l2 = len(s2)
	g = math.gcd(l1, l2)
	r1 = l1 // g
	r2 = l2 // g
	u1 = s1[:g]
	u2 = s2[:g]
	if u1 == u2 and u1 * r1 == s1 and (u2 * r2 == s2):
		lcm = l1 * l2 // g
		print(s1 * (lcm // l1))
	else:
		print(-1)


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

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

def LCM(a, b):
	lenlcm = lcm(len(a), len(b))
	t = a * (lenlcm // len(a))
	if t == b * (lenlcm // len(b)):
		return t
	else:
		return '-1'
q = int(input())
for i in range(q):
	s = input()
	t = input()
	print(LCM(s, t))


from math import gcd
t = int(input())
for i in range(t):
	s = str(input())
	ss = str(input())
	n = len(s)
	nn = len(ss)
	o = gcd(n, nn)
	lcm = n * nn // o
	if s * (lcm // n) == ss * (lcm // nn):
		print(s * (lcm // n))
	else:
		print(-1)


def lcm(l1, l2):
	if l1 > l2:
		larger = l1
	else:
		larger = l2
	while True:
		if larger % l1 == 0 and larger % l2 == 0:
			lcm = larger
			break
		larger += 1
	return lcm

def solve(str1, str2):
	l1 = len(str1)
	l2 = len(str2)
	temp = lcm(l1, l2)
	r1 = int(temp / l1)
	r2 = int(temp / l2)
	if str1 * r1 == str2 * r2:
		print(str1 * r1)
	else:
		print(-1)
n = int(input())
for i in range(n):
	str1 = str(input())
	str2 = str(input())
	solve(str1, str2)


try:
	import math

	def lcm(x, y):
		lcm = abs(x * y) // math.gcd(x, y)
		return lcm
	t = int(input())
	while t != 0:
		str1 = input()
		str2 = input()
		if str1[0] != str2[0] or str1[-1] != str2[-1]:
			print(-1)
		else:
			s1 = len(set(list(str1)))
			s2 = len(set(list(str2)))
			if s1 == 1 and s2 == 1:
				print(str2[0] * lcm(len(str1), len(str2)))
			else:
				(len1, len2) = (len(str1), len(str2))
				l = lcm(len1, len2)
				a = l // len1
				b = l // len2
				if str1 * a == str2 * b:
					print(str1 * a)
				else:
					print(-1)
		t -= 1
except EOFError:
	print(' ')


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
for tc in range(int(input())):
	s = input()
	t = input()
	if s[0] != t[0] or s[-1] != t[-1]:
		print('-1')
	else:
		lens = len(s)
		lent = len(t)
		if s.count(s[0]) == lens and t.count(t[0]) == lent:
			print(s[0] * lcm(lens, lent))
			continue
		if lent > lens:
			(lens, lent, s, t) = (lent, lens, t, s)
		while lens != lent:
			t = t * (lcm(lens, lent) // lent)
			lent = len(t)
			(lens, lent, s, t) = (lent, lens, t, s)
		if t == s:
			print(s)
		else:
			print('-1')


for i in range(int(input())):
	s = input()
	t = input()
	if len(t) > len(s):
		(s, t) = (t, s)
	flag = 0
	ans = ''
	for i in range(100):
		d = s * (i + 1)
		val = len(d) // len(t)
		c = t * val
		if d == c:
			ans = d
			flag = 1
			break
	if flag == 1:
		print(ans)
	else:
		print(-1)


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

def lcm(a, b):
	return a / gcd(a, b) * b
t = int(input())
for z in range(t):
	s = input()
	k = input()
	s1 = len(s)
	k1 = len(k)
	d = lcm(s1, k1)
	s = s * int(d / s1)
	k = k * int(d / k1)
	if s == k:
		print(s)
	else:
		print('-1')


from math import gcd

def main():
	for _ in range(int(input())):
		s = input()
		t = input()
		n = len(s)
		m = len(t)
		g = gcd(n, m)
		if m // g * s == n // g * t:
			print(m // g * s)
		else:
			print(-1)
main()


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

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

def solve():
	s = input()
	t = input()
	ls = len(s)
	lt = len(t)
	lc = lcm(ls, lt)
	s *= lc // ls
	t *= lc // lt
	print(s if s == t else -1)
for _ in range(int(input())):
	solve()


import sys

def rs():
	return sys.stdin.readline().rstrip()

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

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

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math
from collections import defaultdict, Counter
for _ in range(ri()):
	(s, t) = (rs(), rs())
	a = len(s)
	b = len(t)
	l = a * b // math.gcd(a, b)
	(x, y) = ('', '')
	while len(x) < l:
		x += s
	while len(y) < l:
		y += t
	if x == y:
		ws(x)
	else:
		wi(-1)


def lcm(s, t):
	if s == 0:
		return t
	return lcm(t % s, s)
for t in range(int(input())):
	a = input()
	b = input()
	(s, t) = (len(a), len(b))
	z = s * t // lcm(s, t)
	if a * (z // s) == b * (z // t):
		print(a * (z // s))
	else:
		print(-1)


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

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

def printLCM(s, t):
	ls = len(s)
	lt = len(t)
	sRep = s * ls
	lsr = len(sRep)
	for i in range(0, lsr, lt):
		rlen = min(i + lt, lsr)
		if t != sRep[i:rlen]:
			print(-1)
			return
		if rlen % ls == 0:
			print(sRep[0:rlen])
			return
	return -1

def solve():
	s = getStr()
	t = getStr()
	ls = len(s)
	lt = len(t)
	if s == t:
		print(s)
	elif ls > lt:
		printLCM(s, t)
	else:
		printLCM(t, s)
t = getInt()
while t > 0:
	t -= 1
	solve()


for _ in ' ' * int(input()):
	(a, b) = (input(), input())
	if len(a) < len(b):
		(a, b) = (b, a)
	res = ''
	bad = len(a) * len(b)
	while True:
		res += a
		if len(res) % len(b) == 0:
			n = len(res) // len(b)
			if b * n == res:
				print(res)
				break
		if len(res) == bad:
			print(-1)
			break


from math import gcd
t = int(input())
for i in range(t):
	s = str(input())
	k = str(input())
	tr = min(len(k), len(s))
	while True:
		if tr > 0 and s[0:tr] == k[0:tr] and (len(k) % tr == 0) and (len(s) % tr == 0) and (s[0:tr] * (len(s) // tr) == s) and (k[0:tr] * (len(k) // tr) == k):
			tet = s[0:tr] * (len(s) * len(k) // gcd(len(s), len(k)))
			pp = gcd(tet.count(s), tet.count(k))
			tet = tet[0:len(tet) // pp]
			print(tet)
			break
		else:
			tr -= 1
		if tr == 0:
			print(-1)
			break


import math
t = int(input())
while t != 0:
	s1 = input()
	s2 = input()
	m = len(s1)
	n = len(s2)
	l = m * n // math.gcd(m, n)
	s1 = s1 * (l // m)
	s2 = s2 * (l // n)
	if s1 == s2:
		print(s1)
	else:
		print('-1')
	t = t - 1


def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
t = int(input())
for i in range(0, t):
	s = input()
	q = input()
	l1 = len(s)
	l2 = len(q)
	l = lcm(l1, l2)
	s1 = s * int(l / l1)
	q1 = q * int(l / l2)
	if s1 == q1:
		print(s1)
	else:
		print('-1')


def GCD(a, b):
	if b == 0:
		return a
	return GCD(b, a % b)
for t in range(int(input())):
	(s, t) = (input(), input())
	g = GCD(len(s), len(t))
	(a, b) = (t * (len(s) // g), s * (len(t) // g))
	if a == b:
		print(a)
	else:
		print(-1)


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

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

def solve(s, t):
	ls = len(s)
	lt = len(t)
	l = lcm(ls, lt)
	a = l // ls * s
	b = l // lt * t
	if a == b:
		return a
	else:
		return -1
n = int(input())
ans = []
for i in range(n):
	s = input()
	t = input()
	ans.append(solve(s, t))
for i in ans:
	print(i)


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

def lcm(x, y):
	return x // gcd(x, y) * y
tests = int(input())
for _ in range(tests):
	s = input().strip()
	t = input().strip()
	result = s * (lcm(len(s), len(t)) // len(s))
	ok = True
	for i in range(0, len(result), len(t)):
		if result[i:i + len(t)] != t:
			ok = False
	if ok:
		print(s * (lcm(len(s), len(t)) // len(s)))
	else:
		print(-1)


def solve():
	from math import gcd
	s = str(input())
	t = str(input())
	n = len(s)
	m = len(t)
	l = n * m // gcd(n, m)
	if s * (l // n) == t * (l // m):
		print(s * (l // n))
	else:
		print(-1)

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


from sys import stdin
input = stdin.readline

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
q = int(input())
for _ in range(q):
	s = input()
	t = input()
	s = s[:-1]
	t = t[:-1]
	ls = len(s)
	lt = len(t)
	x = ls // gcd(ls, lt)
	x *= lt
	p = ''
	while len(p) < x:
		p = p + s
	pp = ''
	while len(pp) < x:
		pp = pp + t
	if pp == p:
		print(pp)
	else:
		print(-1)


from sys import stdin
from sys import stdout
from math import *
import sys
for __ in range(int(input())):
	s = input()
	t = input()
	a = len(s)
	b = len(t)
	lcm = a * b // gcd(a, b)
	m = lcm // a
	ans = s * m
	pos = 1
	for i in range(len(ans)):
		if ans[i] != s[i % a] or ans[i] != t[i % b]:
			pos = 0
			break
	if pos == 0:
		print(-1)
	else:
		print(ans)


import math
n = int(input())

def lcm(a, b):
	return a * b // math.gcd(a, b)
for i in range(n):
	a = input()
	b = input()
	tmp = lcm(len(a), len(b))
	if a * (tmp // len(a)) == b * (tmp // len(b)):
		print(a * (tmp // len(a)))
	else:
		print(-1)


import math
T = int(input())
while T != 0:
	T -= 1
	s = input()
	t = input()
	lens = len(s)
	lent = len(t)
	g = math.gcd(lens, lent)
	tep = lens * lent // g
	s *= tep // lens
	t *= tep // lent
	if s == t:
		print(s)
	else:
		print(-1)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split()))
from math import *

def lcm(a, b):
	return a * b // gcd(a, b)
tc = 1
(tc,) = inp()
for _ in range(tc):
	a = str(input())
	b = str(input())
	n = len(a)
	m = len(b)

	def co(a):
		for i in range(len(a)):
			t = a[:i + 1]
			if t * (len(a) // (i + 1)) == a:
				return i + 1
		return 1
	if a + b == b + a:
		print(a * (lcm(n, m) // n))
	else:
		print(-1)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 1000000007

def problemb():
	num_cases = inp()

	def do_lcm(x, y):
		if x > y:
			greater = x
		else:
			greater = y
		while True:
			if greater % x == 0 and greater % y == 0:
				lcm = greater
				break
			greater += 1
		return lcm

	def divisible(s1, s2):
		if len(s2) <= len(s1):
			return s1 == s2 * (len(s1) // len(s2))
		return False
	for _ in range(num_cases):
		str1 = strng()
		str2 = strng()
		lcm_lengths = do_lcm(len(str1), len(str2))
		smaller = str1 if len(str1) < len(str2) else str2
		larger = str2 if smaller != str2 else str1
		lcm_string = smaller * (lcm_lengths // len(smaller))
		if divisible(lcm_string, smaller) and divisible(lcm_string, larger):
			print(lcm_string)
		else:
			print('-1')

def main():
	problemb()
main()


def qu(s):
	l = len(s)
	for i in range(1, l + 1):
		if l % i != 0:
			continue
		if s[:i] * (l // i) == s:
			return s[:i]

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

def lcm(a, b):
	return a // gcd(a, b) * b
t = int(input())
for i in range(t):
	s = input()
	t = input()
	j_s = qu(s)
	j_t = qu(t)
	if j_s != j_t:
		print('-1')
	else:
		print(j_s * lcm(len(s) // len(j_s), len(t) // len(j_t)))


import math
for T in range(int(input())):
	(s, t) = (input(), input())
	g = math.gcd(len(s), len(t))
	(s1, s2) = (s * (len(t) // g), t * (len(s) // g))
	print(s1 if s1 == s2 else -1)


for _ in range(int(input())):
	s1 = input()
	s2 = input()
	st1 = ''
	st2 = ''
	if len(s1) == len(s2):
		if s1 == s2:
			print(s1)
		else:
			print(-1)
		continue
	if len(s1) < len(s2):
		st1 = s1
		st2 = s2
	else:
		st1 = s2
		st2 = s1
	d = dict()
	for i in range(1, 21):
		x = s2 * i
		d[x] = 1
	ans = -1
	for i in range(1, 21):
		x = s1 * i
		if d.get(x):
			ans = x
			break
	print(ans)


import math
a = int(input())
for i in range(a):
	n1 = input()
	n2 = input()
	gcd = math.gcd(len(n1), len(n2))
	n11 = n2 * (len(n1) // gcd)
	n22 = n1 * (len(n2) // gcd)
	if n11 == n22:
		print(n11)
	else:
		print('-1')


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

def lcm(a, b):
	return a // gcd(a, b) * b
for _ in range(int(input())):
	s1 = input()
	s2 = input()
	l1 = len(s1)
	l2 = len(s2)
	f1 = 0
	if l1 == l2:
		if s1 == s2:
			print(s1)
		else:
			print(-1)
	elif l1 > l2:
		for i in range(l2):
			if s1[i] != s2[i]:
				f1 = 1
				break
		if f1 == 1:
			print(-1)
		elif l1 % l2 == 0:
			if s2 * (l1 // l2) == s1:
				print(s1)
			else:
				print(-1)
		else:
			lc = lcm(l1, l2)
			if s1 * (lc // l1) == s2 * (lc // l2):
				print(s1 * (lc // l1))
			else:
				print(-1)
	elif l2 > l1:
		for i in range(l1):
			if s1[i] != s2[i]:
				f1 = 1
				break
		if f1 == 1:
			print(-1)
		elif l2 % l1 == 0:
			if s1 * (l2 // l1) == s2:
				print(s2)
			else:
				print(-1)
		else:
			lc = lcm(l1, l2)
			if s1 * (lc // l1) == s2 * (lc // l2):
				print(s1 * (lc // l1))
			else:
				print(-1)


import math
t = int(input())
for i in range(t):
	s1 = input()
	s2 = input()
	if s1.count('a') == len(s1) and s2.count('a') == len(s2):
		print('a' * int(len(s1) * len(s2) / math.gcd(len(s1), len(s2))))
	elif s1.count('b') == len(s1) and s2.count('b') == len(s2):
		print('b' * int(len(s1) * len(s2) / math.gcd(len(s1), len(s2))))
	else:
		l = []
		s = 0
		for i in range(1, 50):
			l.append(s1 * i)
		for i in range(1, 50):
			if s2 * i in l:
				s = 1
				print(s2 * i)
				break
		if s == 0:
			print(-1)


import math
t = int(input())
for _ in range(t):
	s = input()
	t = input()
	lcm = len(s) * len(t) / math.gcd(len(s), len(t))
	if s * int(lcm / len(s)) == t * int(lcm / len(t)):
		print(s * int(lcm / len(s)))
	else:
		print(-1)


def check_div(div, di):
	di_count = div.count(di)
	if di * di_count == div:
		return True
	return False
T = int(input())
for _ in range(T):
	p = input()
	q = input()
	if len(p) < len(q):
		(p, q) = (q, p)
	flag = False
	for i in range(1, len(p) + 1):
		ans = check_div(p * i, q)
		if ans:
			flag = True
			st = p * i
			break
	if flag:
		print(st)
	else:
		print(-1)


import math

def stu(f):
	j = ''
	for i in range(len(f)):
		j = f[0:i + 1]
		if len(''.join(f.split(j))) == 0:
			break
	return j
tt = int(input())
while tt:
	tt = tt - 1
	s = input()
	t = input()
	y = stu(s)
	(q, w, e, r) = (0, s.split(y), t.split(y), 1)
	if ''.join(e) == '':
		print(y * ((len(w) - 1) * (len(e) - 1) // math.gcd(len(w) - 1, len(e) - 1)))
	else:
		print(-1)


import math
q = int(input())
for i in range(q):
	s = input()
	t = input()
	l1 = len(s)
	l2 = len(t)
	ans = l1 * l2 // math.gcd(l1, l2)
	if t * (ans // l2) == s * (ans // l1):
		print(s * (ans // l1))
	else:
		print(-1)


from math import ceil, gcd

def lcm(a, b):
	g = gcd(a, b)
	l = a * b // g
	return l
for _ in range(int(input())):
	s = input()
	t = input()
	sl = len(s)
	tl = len(t)
	lc = lcm(sl, tl)
	ns = s * (lc // sl)
	nt = t * (lc // tl)
	ans = -1
	if ns == nt:
		ans = ns
	print(ans)


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(input())):
	s = str(input())
	t = str(input())
	a = len(s)
	b = len(t)
	c = lcm(a, b)
	if s * (c // a) == t * (c // b):
		print(s * (c // a))
	else:
		print(-1)


t = int(input())
import math

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

def lcm(a, b):
	return a / gcd(a, b) * b
while t > 0:
	t -= 1
	s1 = input()
	s2 = input()
	lm = lcm(len(s1), len(s2))
	if s1 * int(lm / len(s1)) == s2 * int(lm / len(s2)):
		print(s1 * int(lm / len(s1)))
	else:
		print(-1)


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

def string(a, b):
	n = gcd(len(a), len(b))
	if a * len(b) == b * len(a):
		return a * int(len(b) / n)
	else:
		return -1
q = int(input())
A = [0] * q
B = [0] * q
for i in range(q):
	A[i] = input()
	B[i] = input()
for i in range(q):
	print(string(A[i], B[i]))


import math
for _ in range(int(input())):
	d = math.gcd(len((s := input())), len((t := input())))
	print([s * (len(t) // d), -1][s * (len(t) // d) != t * (len(s) // d)])


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

def lcm(a, b):
	return a / gcd(a, b) * b
t = int(input())
for i in range(t):
	s = input()
	d = input()
	l1 = len(s)
	d1 = len(d)
	fuck = int(lcm(l1, d1))
	if l1 < d1:
		g = fuck // l1
		s = s * g
		d = d * (fuck // d1)
		if s == d:
			print(s)
		else:
			print(-1)
	else:
		g = fuck // l1
		s = s * g
		d = d * (fuck // d1)
		if s == d:
			print(s)
		else:
			print(-1)


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

def lcm(a, b):
	return a / gcd(a, b) * b
n = int(input())
for i in range(n):
	s1 = input()
	s2 = input()
	l1 = len(s1)
	l2 = len(s2)
	t1 = s1 * l2
	t2 = s2 * l1
	if t1 != t2:
		print(-1)
	else:
		temp = int(lcm(l1, l2) / l1)
		print(temp * s1)


import sys
import math

def solve(s, t):
	gcd = math.gcd(len(s), len(t))
	ss = s * (len(t) // gcd)
	tt = t * (len(s) // gcd)
	return ss if ss == tt else -1
q = int(sys.stdin.readline().rstrip())
res = []
for _ in range(q):
	s = sys.stdin.readline().rstrip()
	t = sys.stdin.readline().rstrip()
	res.append(solve(s, t))
for r in res:
	print(r)


def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
for i in range(int(input())):
	a = input()
	b = input()
	if a.count('a') == 0 and b.count('a') == 0:
		print('b' * int(lcm(len(a), len(b))))
		continue
	if a.count('b') == 0 and b.count('b') == 0:
		print('a' * int(lcm(len(a), len(b))))
		continue
	if a.count('b') == 0 and b.count('b') != 0:
		print(-1)
		continue
	if a.count('b') != 0 and b.count('b') == 0:
		print(-1)
		continue
	if a.count('a') == 0 and b.count('a') != 0:
		print(-1)
		continue
	if a.count('a') != 0 and b.count('a') == 0:
		print(-1)
		continue
	if len(a) == len(b):
		if a == b:
			print(a)
		else:
			print(-1)
		continue
	if a * (lcm(len(a), len(b)) // len(a)) == b * (lcm(len(a), len(b)) // len(b)):
		print(a * (lcm(len(a), len(b)) // len(a)))
	else:
		print(-1)


import math
from collections import defaultdict

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

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	(m, n) = (len(a), len(b))
	LCM = lcm(m, n)
	if a * (LCM // m) == b * (LCM // n):
		print(a * (LCM // m))
	else:
		print(-1)


import math
q = int(input())
for _ in range(q):
	s = input()
	t = input()
	tar = len(s) * len(t) // math.gcd(len(s), len(t))
	s = s * (tar // len(s))
	t = t * (tar // len(t))
	if s == t:
		print(s)
	else:
		print(-1)


import math
n = int(input())
for i in range(n):
	num1 = input()
	num2 = input()
	gcd1 = math.gcd(len(num1), len(num2))
	num11 = num1 * (len(num2) // gcd1)
	num22 = num2 * (len(num1) // gcd1)
	if num11 == num22:
		print(num11)
	else:
		print(-1)


from math import gcd

def solve():
	s1 = input()
	s2 = input()
	k = len(s1) * len(s2) // gcd(len(s1), len(s2))
	if s1 * (k // len(s1)) == s2 * (k // len(s2)):
		print(s1 * (k // len(s1)))
	else:
		print(-1)
for i in range(int(input())):
	solve()


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
import re
mod = 1000000007
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
intl = lambda : list(map(int, inp().split(' ')))
mint = lambda : map(int, inp().split())
flol = lambda : list(map(float, inp().split(' ')))
flush = lambda : sys.stdout.flush()

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

def compute_lcm(x, y):
	lcm = x * y // compute_gcd(x, y)
	return lcm

def myCode():
	s = inp()
	t = inp()
	s_len = len(s)
	t_len = len(t)
	if s * t_len == t * s_len:
		if s_len <= t_len:
			print(s * (compute_lcm(s_len, t_len) // s_len))
		elif s_len > t_len:
			print(t * (compute_lcm(s_len, t_len) // t_len))
	else:
		print(-1)

def main():
	for t in range(iinp()):
		myCode()
main()


def gcd(x, y):
	if x == 0:
		return y
	return gcd(y % x, x)

def lcm(x, y):
	return int(x / gcd(x, y) * y)
for _ in range(int(input())):
	a = input()
	b = input()
	res1 = ''
	temp1 = (a + a).find(a, 1, -1)
	if temp1 != -1:
		res1 = a[:temp1]
	res2 = ''
	temp2 = (b + b).find(b, 1, -1)
	if temp2 != -1:
		res2 = b[:temp2]
	if res1 == res2 or res1 == b or res2 == a:
		if res1 == '' and res2 == '':
			if a != b:
				print(-1)
			else:
				print(a)
		elif res1 == b:
			print(a)
		elif res2 == a:
			print(b)
		else:
			count1 = a.count(res1)
			count2 = b.count(res2)
			print(res1 * lcm(count1, count2))
	else:
		print(-1)


q = int(input())
for test in range(q):
	s = input()
	t = input()
	if len(s) > len(t):
		longer = s
		short = t
	else:
		longer = t
		short = s
	result = -1
	if short in longer:
		greater = len(longer)
		while True:
			if greater % len(longer) == 0 and greater % len(short) == 0:
				lcm = greater
				i = lcm // len(longer)
				j = lcm // len(short)
				if longer * i == short * j:
					result = longer * i
				break
			else:
				greater = greater + 1
	print(result)


def compute_hcf(x, y):
	if x > y:
		smaller = y
	else:
		smaller = x
	for i in range(1, smaller + 1):
		if x % i == 0 and y % i == 0:
			hcf = i
	return hcf
t = int(input())
for j in range(t):
	s = input()
	t = input()
	S = len(s)
	T = len(t)
	if s * T != t * S:
		print(-1)
	else:
		lc = S * T // compute_hcf(S, T)
		lcs = lc // S
		lct = lc // T
		k = 1
		f = 0
		while lc * k <= S * T:
			if s * (lcs * k) == t * (lct * k):
				f = 1
				break
			else:
				k = k + 1
		if f == 1:
			print(s * (lcs * k))
		else:
			print(-1)


from math import gcd

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

def sdiv(s1, s2):
	return s1.count(s2)

def try_lcm(s1, s2):
	d = int(lcm(len(s1), len(s2)) / len(s1))
	if sdiv(s1 * d, s2) == len(s1) * d / len(s2):
		return s1 * d
	return -1
q = int(input())
for i in range(q):
	s = input()
	t = input()
	if s < t:
		(s, t) = (t, s)
	print(try_lcm(s, t))


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
q = int(input())
li = []
while q != 0:
	q = q - 1
	s = input()
	t = input()
	len_s = len(s)
	len_t = len(t)
	lcm = len_t / gcd(len_s, len_t) * len_s
	a = int(lcm / len_s)
	b = int(lcm / len_t)
	if s * a == t * b:
		li.append(s * a)
	else:
		li.append(-1)
for i in li:
	print(i)


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

def lcm(a, b):
	return a * b // gcd(a, b)
q = int(input())
for _ in range(q):
	s = str(input())
	t = str(input())
	l = lcm(len(s), len(t))
	p = [l // len(s), l // len(t)]
	for i in range(1, 4):
		if s * (p[0] * i) == t * (p[1] * i):
			print(s * (p[0] * i))
			break
	else:
		print(-1)


from math import gcd
t = int(input())
for _ in range(t):
	a = input()
	b = input()
	LCM = len(a) * len(b) // gcd(len(a), len(b))
	s1 = a * (LCM // len(a))
	s2 = b * (LCM // len(b))
	if s1 == s2:
		print(s1)
	else:
		print(-1)


import sys

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

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

def stringLSM(s1, s2):
	ls1 = len(s1)
	ls2 = len(s2)
	lsm = int(lcm(ls1, ls2))
	f1 = lsm // ls1 * s1
	f2 = lsm // ls2 * s2
	if f1 == f2:
		return f1
	else:
		return '-1'
cases = int(sys.stdin.readline())
for i in range(cases):
	s1 = sys.stdin.readline().rstrip('\n')
	s2 = sys.stdin.readline().rstrip('\n')
	ans = stringLSM(s1, s2)
	sys.stdout.write(ans + '\n')


t = int(input())

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
for i in range(t):
	n1 = input()
	n2 = input()
	g = gcd(len(n1), len(n2))
	if n1 * (len(n2) // g) == n2 * (len(n1) // g):
		print(n1 * (len(n2) // g))
	else:
		print(-1)


import sys
from math import gcd
zz = 1
sys.setrecursionlimit(10 ** 5)
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def ii():
	return input().rstrip()

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def gtc(tc, ans):
	print('Case #' + str(tc) + ':', ans)

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

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

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
uu = t

def check(s, b, m):
	return m % len(s) == 0 and s * (m // len(s)) == b
while t > 0:
	t -= 1
	a = ii()
	b = ii()
	n = len(a)
	m = len(b)
	ans = 0
	for i in range(1, n + 1):
		if check(a[:i], b, m) and check(a[:i], a, n):
			ans = i
			break
	if ans == 0:
		print(-1)
	else:
		p = n // i
		q = m // i
		w = p * q // gcd(p, q)
		print(a[:i] * w)


def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
q = int(input())
for i in range(q):
	str1 = input()
	str2 = input()
	a = int(len(str1))
	b = int(len(str2))
	temp = lcm(a, b)
	x = temp // a
	y = temp // b
	if x * str1 == y * str2:
		print(x * str1)
	else:
		print(-1)


w = int(input())

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

def lcm(a, b):
	return a / gcd(a, b) * b
for e in range(w):
	s = input()
	t = input()
	r = int(lcm(len(s), len(t)))
	if r // len(s) * s == r // len(t) * t:
		print(r // len(s) * s)
	else:
		print(-1)


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

def lcm(a, b):
	return a / gcd(a, b) * b
t = int(input())
for w in range(0, t):
	s1 = str(input())
	s2 = str(input())
	n1 = len(s1)
	n2 = len(s2)
	n = lcm(n1, n2)
	s1 *= int(n / n1)
	s2 *= int(n / n2)
	if s1 == s2:
		print(s1)
	else:
		print(-1)


def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
t = int(input())
for i in range(t):
	a = input()
	b = input()
	(len_a, len_b) = (len(a), len(b))
	lc = lcm(len_a, len_b)
	lcm1 = a * int(lc / len_a)
	lcm2 = b * int(lc / len_b)
	if lcm1 == lcm2:
		print(lcm1)
	else:
		print(-1)


def ass(a, b):
	if a != b:
		print(f'Assertion error: {a} != {b}')
	else:
		print('OK')

def number():
	return int(input())

def numbers():
	return [int(item) for item in input().split()]

def word():
	return input()

def words():
	return [item for item in input().split()]

def dump(lst):
	for (i, item) in enumerate(lst):
		print(i, item)

def binsearch(arr, a, b):
	m = (a + b) // 2
	if a >= b:
		return a - 1 if arr[a] == 0 else a
	return binsearch(arr, a, m - 1) if arr[m] == 0 else binsearch(arr, m + 1, b)

def gcd(x, y):
	return x if y == 0 else gcd(y, x % y)

def lcm(x, y):
	return x * y // gcd(x, y)

def f(s, t):
	ns = len(s)
	nt = len(t)
	n = gcd(ns, nt)
	res = ns * nt // n
	item = s[0:n]
	if ns // n * item == s and nt // n * item == t:
		return res // n * item
	else:
		return -1
for i in range(number()):
	s = word()
	t = word()
	print(f(s, t))


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

def solve(s, t):
	if len(s) > len(t):
		(t, s) = (s, t)
	lcm = len(s) * len(t) // gcd(len(s), len(t))
	left = s * (lcm // len(s))
	right = t * (lcm // len(t))
	if left == right:
		return left
	return -1
for i in range(int(input())):
	s = input()
	t = input()
	print(solve(s, t))


import math
for z in range(int(input())):
	s = input()
	t = input()
	a = len(s)
	b = len(t)
	num1 = a * b // math.gcd(a, b)
	if s * (num1 // a) == t * (num1 // b):
		print(s * (num1 // a))
	else:
		print(-1)


from math import gcd
for i in range(int(input())):
	s = input()
	t = input()
	(ls, lt) = (len(s), len(t))
	n = int(ls * lt / gcd(ls, lt))
	if s * (n // ls) == t * (n // lt):
		print(s * (n // ls))
	else:
		print(-1)


from math import gcd
q = int(input())
for _ in range(q):
	t = input()
	s = input()
	n1 = len(t)
	n2 = len(s)
	x = gcd(n1, n2) * (n1 // gcd(n1, n2)) * (n2 // gcd(n1, n2))
	if t * (x // n1) == s * (x // n2):
		print(t * (x // n1))
	else:
		print(-1)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for t in range(int(input())):
	s1 = input()
	s2 = input()
	(sa, sb) = (list(s1).copy(), list(s2).copy())
	(s1, s2) = (list(s1), list(s2))
	(s1, s2) = (s1 * 50, s2 * 50)
	ik = min(len(s1), len(s2))
	pos = 1
	for i in range(ik):
		if s1[i] == s2[i]:
			pass
		else:
			pos = 0
			break
	if pos == 0:
		print(-1)
	else:
		val = gcd(len(sa), len(sb))
		val = len(sa) * len(sb) // val
		sa = sa * (len(sa) * len(sb))
		sa = sa[:val]
		sa = ''.join(sa)
		print(sa)


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

def lcm(a, b):
	return a / gcd(a, b) * b
t = int(input())
for _ in range(t):
	s = input()
	t = input()
	l = int(lcm(len(s), len(t)))
	a1 = s * (l // len(s))
	a2 = t * (l // len(t))
	if a1 == a2:
		print(a1)
	else:
		print(-1)


import math
for _ in range(int(input())):
	s1 = input()
	s2 = input()
	l1 = len(s1)
	l2 = len(s2)
	g = l1 * l2 // math.gcd(l1, l2)
	s11 = s1 * (g // l1)
	s22 = s2 * (g // l2)
	if s11 == s22:
		print(s11)
	else:
		print(-1)


import math

def solve(s, t):
	(slen, tlen) = (len(s), len(t))
	s *= tlen
	t *= slen
	lcm = int(slen * tlen / math.gcd(slen, tlen))
	if s != t:
		print(-1)
	else:
		print(s[:lcm])
tcs = int(input())
for i in range(tcs):
	s = input()
	t = input()
	solve(s, t)


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

def compute_lcm(x, y):
	lcm = x * y // compute_gcd(x, y)
	return lcm
for _ in range(int(input())):
	s = input()
	r = input()
	if len(s) < len(r):
		s1 = s
		s2 = r
	else:
		s1 = r
		s2 = s
	res = s1
	temp = (s1 + s1).find(s1, 1, -1)
	if temp != -1:
		res = s1[:temp]
	if s2.replace(res, '') != '':
		print(-1)
	else:
		k = s2.count(res)
		l = s1.count(res)
		print(res * compute_lcm(k, l))


from math import gcd

def div(a, b):
	la = len(a)
	lb = len(b)
	i = 1
	while lb * i <= la:
		if b * i == a:
			return True
		i += 1
	return False

def solve():
	for i in range(q):
		a = input()
		b = input()
		la = len(a)
		lb = len(b)
		lc = la * lb // gcd(la, lb)
		c = lc // lb * b
		if div(c, b) and div(c, a):
			return c
		else:
			return -1
q = int(input())
for i in range(q):
	print(solve())


import sys, os, io
from math import gcd
for _ in range(int(input())):
	a = input()
	b = input()
	lcm = len(a) * len(b) // gcd(len(a), len(b))
	ta = a[:]
	tb = b[:]
	while len(a) < lcm:
		a += ta
	while len(b) < lcm:
		b += tb
	if a == b:
		print(a)
	else:
		print(-1)


def gcd(al, bl):
	if bl == 0:
		return al
	return gcd(bl, al % bl)

def Lcm(a, b):
	al = len(a)
	bl = len(b)
	lcm = al * bl / gcd(al, bl)
	al = int(lcm / al)
	bl = int(lcm / bl)
	a = a * al
	b = b * bl
	if a == b:
		print(a)
	else:
		print(-1)
num = int(input())
for i in range(num):
	a = input()
	b = input()
	Lcm(a, b)


import math

def min_rep(s):
	m = len(s)
	for i in range(1, len(s) // 2 + 1):
		if len(s) % i == 0 and s == s[:i] * (len(s) // i):
			m = min(m, i)
			break
	return (m, s[:m])

def solve():
	s = input()
	t = input()
	lcl = len(s) * len(t) // math.gcd(len(s), len(t))
	if min_rep(s) == min_rep(t):
		return min_rep(s)[1] * (lcl // min_rep(s)[0])
	else:
		return -1
q = int(input())
for case in range(q):
	print(solve())


for _ in range(int(input())):
	t = 0
	x = str(input())
	y = str(input())
	if len(x) < len(y):
		temp = x
		x = y
		y = temp
	for i in range(20):
		for j in range(20):
			if x * (i + 1) == y * (j + 1):
				print(x * (i + 1))
				t = 1
				break
		if t == 1:
			break
	if t != 1:
		print(-1)


import math
for _ in range(int(input())):
	s = input()
	t = input()
	m = len(s)
	n = len(t)
	lcm = m * n // math.gcd(m, n)
	S = s * (lcm // m)
	T = t * (lcm // n)
	if S == T:
		print(S)
	else:
		print(-1)


import math
for i in range(int(input())):
	a = input()
	b = input()
	m = len(a)
	n = len(b)
	lcm = m * n // math.gcd(m, n)
	A = a * (lcm // m)
	B = b * (lcm // n)
	print(A if A == B else -1)


def lcm(a, b):
	m = a * b
	while a != 0 and b != 0:
		if a > b:
			a %= b
		else:
			b %= a
	return m // (a + b)
q = int(input())
res = []
for i in range(q):
	s1 = input()
	s2 = input()
	l = lcm(len(s1), len(s2))
	if s1 * (l // len(s1)) == s2 * (l // len(s2)):
		res.append(s1 * (l // len(s1)))
	else:
		res.append(-1)
for i in range(q):
	print(res[i])


from math import gcd
for _ in range(int(input())):
	s = input()
	t = input()
	ls = len(s)
	lt = len(t)
	lc = abs(ls * lt) // gcd(ls, lt)
	ans1 = t * (lc // lt)
	ans2 = s * (lc // ls)
	if ans1 == ans2:
		ans = ans1
	else:
		ans = -1
	print(ans)


import math
for z in range(int(input())):
	a = input()
	b = input()
	c = len(a)
	d = len(b)
	x = 0
	y = 0
	for i in range(1, c + 1):
		if c % i == 0 and a[:i] * (c // i) == a:
			x = i
			break
	for i in range(1, d + 1):
		if d % i == 0 and b[:i] * (d // i) == b:
			y = i
			break
	if a[:x] == b[:y]:
		p = c // x
		q = d // x
		print(a[:x] * (p * q // math.gcd(p, q)))
	else:
		print(-1)


from sys import stdin
from math import gcd
input = lambda : stdin.readline().strip()
ipnut = input
for i in range(int(input())):
	s = input()
	t = input()
	a = len(s)
	b = len(t)
	l = a * b // gcd(a, b)
	s1 = s * (l // a)
	t1 = t * (l // b)
	if s1 == t1:
		print(s1)
	else:
		print(-1)


def gcd(x, y):
	if x == 0:
		return y
	return gcd(y % x, x)
for t in range(int(input())):
	a = input()
	b = input()
	(x, y) = (len(a), len(b))
	z = x * y // gcd(x, y)
	if a * (z // x) == b * (z // y):
		print(a * (z // x))
	else:
		print(-1)


def repeater(s):
	index = (s + s)[1:-1].find(s)
	if index == -1:
		return s
	else:
		return s[:index + 1]

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

def getLCM(x, y):
	lcm = x * y // getGCD(x, y)
	return lcm
t = int(input())
for i in range(t):
	a = input()
	b = input()
	ar = repeater(a)
	br = repeater(b)
	if ar != br:
		print(-1)
	else:
		aCount = len(a) / len(ar)
		bCount = len(b) / len(br)
		lcm = getLCM(aCount, bCount)
		print(ar * int(lcm))


for i in range(int(input())):
	A = a = input()
	B = b = input()
	while A != B:
		if len(A) < len(B):
			A += a
		else:
			B += b
		if min(len(A), len(B)) > 400:
			print(-1)
			break
	else:
		print(A)


import math as mt

def main():
	s = input()
	t = input()
	len_s = len(s)
	len_t = len(t)
	lcm = int(len_s * len_t / mt.gcd(len_s, len_t))
	s = s * int(lcm / len_s)
	t = t * int(lcm / len_t)
	if s == t:
		print(s)
	else:
		print(-1)
t = int(input())
while t:
	main()
	t -= 1


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

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(input())):
	s = input()
	s1 = input()
	if lcm(len(s), len(s1)) // len(s) * s == lcm(len(s), len(s1)) // len(s1) * s1:
		print(lcm(len(s), len(s1)) // len(s1) * s1)
	else:
		print(-1)


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

def lcm(a, b):
	return a // gcd(a, b) * b
k = int(input())
while k > 0:
	s = input()
	t = input()
	n = len(s)
	m = len(t)
	z = lcm(n, m)
	s *= z // n
	t *= z // m
	if t.__eq__(s):
		print(t)
	else:
		print(-1)
	k -= 1


for _ in range(int(input())):
	s = input()
	t = input()
	(m, n) = (len(s), len(t))
	ans = ''
	(i, j) = (0, 0)
	while s[i] == t[j]:
		ans += s[i]
		i += 1
		j += 1
		if i == m and j == n:
			break
		if i == m:
			i = 0
		if j == n:
			j = 0
	if i == m:
		i = 0
	if j == n:
		j = 0
	if s[i] == t[j]:
		print(ans)
	else:
		print(-1)


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(input())

def can_be_built(s1, s2):
	if s1.count(s2) * len(s2) == len(s1):
		return True
	elif s2.count(s1) * len(s1) == len(s2):
		return True
	return False

def is_divisible(s1, s2):
	if s1.count(s2) > 0 or s2.count(s1) > 0:
		return True
	return False
for i in range(n):
	s1 = input()
	s2 = input()
	if not is_divisible(s1, s2):
		print('-1')
	else:
		l_s1 = len(s1)
		l_s2 = len(s2)
		f_len = lcm(l_s1, l_s2)
		aux = ''
		if s1 > s2:
			aux = s2
		else:
			aux = s1
		ans = aux * (f_len // len(aux))
		if can_be_built(ans, s1) and can_be_built(ans, s2):
			print(ans)
		else:
			print('-1')


import math
testCases = int(input())
for i in range(testCases):
	stringA = input()
	stringB = input()
	a = len(stringA)
	b = len(stringB)
	LCM = a * b // math.gcd(a, b)
	stringAlcm = stringA * (LCM // a)
	stringBlcm = stringB * (LCM // b)
	if stringAlcm == stringBlcm:
		print(stringAlcm)
	else:
		print(-1)


from math import gcd
t = int(input())
for _ in range(t):
	s = input()
	d = input()
	n = len(s)
	m = len(d)
	g = gcd(n, m)
	l = n * m // g
	s *= l // n
	d *= l // m
	if s == d:
		print(s)
	else:
		print(-1)


n = int(input())
for i in range(n):
	(a, q) = (input(), input())
	from math import gcd
	w = len(a) * len(q) // gcd(len(a), len(q))
	x = q * (w // len(q))
	y = a * (w // len(a))
	if x == y:
		print(x)
	else:
		print('-1')


from math import gcd

def solve():
	a = input()
	b = input()
	l = gcd(len(a), len(b))
	l = len(a) * len(b) // l
	aa = l // len(a) * a
	bb = l // len(b) * b
	if aa == bb:
		print(aa)
	else:
		print(-1)
n = int(input())
for i in range(n):
	solve()


import math

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
for i in range(int(input())):
	s1 = input()
	s2 = input()
	l = lcm(len(s1), len(s2))
	if s1 * (l // len(s1)) == s2 * (l // len(s2)):
		print(s1 * (l // len(s1)))
	else:
		print(-1)


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

def lcm(a, b):
	return a / gcd(a, b) * b
for i in range(int(input())):
	s = input()
	t = input()
	a = len(s)
	b = len(t)
	val = -1
	val = int(lcm(a, b))
	sval = val // a
	tval = val // b
	if sval * s == tval * t:
		print(sval * s)
	else:
		print(-1)


import math

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

def solve():
	a = input()
	b = input()
	ans = lcm(len(a), len(b))
	a = a * (ans // len(a))
	b = b * (ans // len(b))
	if a == b:
		print(a, end='\n')
	else:
		print(-1, end='\n')
t = int(input())
while t > 0:
	t -= 1
	solve()


def gcd(a, b):
	if a < b:
		(a, b) = (b, a)
	while a % b != 0:
		(a, b) = (b, a % b)
	return b
for _ in range(int(input())):
	s1 = input()
	s2 = input()
	ans = gcd(len(s1), len(s2))
	(c1, c2) = (s1.count(s1[:ans]), s2.count(s1[:ans]))
	if c1 * s1[:ans] == s1 and c2 * s2[:ans] == s2:
		if c1 % c2 == 0 or c2 % c1 == 0:
			if c1 >= c2:
				print(s1[:ans] * c1)
			else:
				print(s1[:ans] * c2)
		else:
			print(s1[:ans] * (c1 * c2))
	else:
		print(-1)


import math

def lcm(s, k):
	res = ''
	while k > 0:
		res += s
		k -= 1
	return res
for _ in range(int(input())):
	s = input()
	t = input()
	(n, m) = (len(s), len(t))
	g = math.gcd(n, m)
	x = lcm(s, m // g)
	y = lcm(t, n // g)
	if x == y:
		print(x)
	else:
		print(-1)


import math
q = int(input())
while q > 0:
	q -= 1
	s1 = input()
	s2 = input()
	lc = len(s1) * len(s2) // math.gcd(len(s1), len(s2))
	if s1 * (lc // len(s1)) == s2 * (lc // len(s2)):
		print(s1 * (lc // len(s1)))
	else:
		print(-1)


from math import gcd

def f(A, B):
	g = gcd(len(A), len(B))
	m = len(A) * len(B) // g
	S = [B[i % g] for i in range(m)]
	for i in range(m):
		if S[i] != A[i % len(A)] or S[i] != B[i % len(B)]:
			return -1
	return ''.join(S)
t = int(input())
for _ in range(t):
	a = input()
	b = input()
	print(f(a, b))


import math
for i in range(int(input())):
	s = input()
	t = input()
	val = math.gcd(len(s), len(t))
	lcm = len(s) * len(t) // val
	if s[:val] == t[:val]:
		x = lcm // val * s[:val]
		if len(s) // val * s[:val] == s and len(t) // val * t[:val] == t:
			print(x)
		else:
			print(-1)
	else:
		print(-1)


import sys
input = sys.stdin.readline
import math

def solve():
	s = input().strip()
	t = input().strip()
	(n, m) = (len(s), len(t))
	lcm = n * m // math.gcd(n, m)
	return s * (lcm // n) if s * (lcm // n) == t * (lcm // m) else -1
for _ in range(int(input())):
	print(solve())


def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)

def solve(s):
	sd = s
	for i in range(1, len(s) // 2 + 1):
		if s[0:i] != s[-i:]:
			continue
		if len(s) % i != 0:
			continue
		tmp = s[0:i]
		pos = len(tmp)
		flag = True
		while pos < len(s):
			if s[pos:pos + len(tmp)] != tmp:
				flag = False
				break
			pos = pos + len(tmp)
		if flag:
			sd = tmp
			break
	return sd
tc = int(input())
for test in range(tc):
	s = input()
	t = input()
	sd = solve(s)
	td = solve(t)
	if sd != td:
		print(-1)
	else:
		x = len(s) // len(sd)
		y = len(t) // len(td)
		tmp = gcd(x, y)
		lcm = tmp * (x // tmp) * (y // tmp)
		print(sd * lcm)


import math
n = int(input())

def lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
for i in range(n):
	a = input()
	b = input()
	c = lcm(len(a), len(b))
	a1 = ''
	b1 = ''
	for j in range(c // len(a)):
		a1 += a
	for j in range(c // len(b)):
		b1 += b
	if a1 == b1:
		print(a1)
	else:
		print(-1)


def LCM(s, t):
	s1 = s
	t1 = t
	while s != t:
		if len(s) < len(t):
			s += s1
		elif len(t) < len(s):
			t += t1
		else:
			return -1
	return s
q = int(input())
for _ in range(q):
	s = str(input())
	t = str(input())
	print(LCM(s, t))


import sys
rl = lambda : list(map(int, sys.stdin.readline().split()))

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for _ in range(int(input())):
	s = input()
	t = input()
	(lenS, lenT) = (len(s), len(t))
	lcm = lenS * lenT // gcd(lenS, lenT)
	s *= lcm // lenS
	t *= lcm // lenT
	print(s) if s == t else print('-1')


import math
n = int(input())
for i in range(0, n):
	a = input()
	b = input()
	al = len(a)
	bl = len(b)
	if a * (bl // math.gcd(al, bl)) == b * (al // math.gcd(al, bl)):
		print(a * (bl // math.gcd(al, bl)))
	else:
		print(-1)


from collections import *
import sys
input = sys.stdin.readline

def ri():
	return int(input())

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

def compute_lcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm
t = ri()
for _ in range(t):
	s = input().strip('\n')
	t = input().strip('\n')
	n = len(s)
	m = len(t)
	l = compute_lcm(n, m)
	alpha = l // n
	beta = l // m
	ss = s * alpha
	tt = t * beta
	test = True
	for i in range(l):
		if ss[i] != tt[i]:
			test = False
			break
	if test == False:
		print(-1)
	else:
		print(ss)


def solve(s, t):
	a = len(s)
	b = len(t)
	if a > b:
		greater = a
	else:
		greater = b
	while True:
		if greater % a == 0 and greater % b == 0:
			lcm = greater
			break
		greater += 1
	x = lcm // a
	y = lcm // b
	if s * x == t * y:
		return s * x
	else:
		return -1
q = int(input())
while q:
	s = input()
	t = input()
	print(solve(s, t))
	q -= 1


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

def check(x, y):
	a = len(x) // gcd(len(x), len(y)) * len(y)
	x1 = x * (a // len(x))
	y1 = y * (a // len(y))
	if x1 == y1:
		return x1
	return -1
	pass
for _ in range(int(input())):
	x = input()
	y = input()
	print(check(x, y))


import math
t = int(input())
while t > 0:
	t -= 1
	a = input()
	b = input()
	if a == b:
		print(a)
	else:
		l = len(a) * len(b) // math.gcd(len(a), len(b))
		f = l // len(a)
		s = l // len(b)
		a = a * f
		b = b * s
		if a == b:
			print(a)
		else:
			print(-1)


import math

def lcm(n1, n2):
	return n1 * n2 // math.gcd(n1, n2)

def answer(s, t):
	if s * len(t) != t * len(s):
		return -1
	alen = lcm(len(s), len(t))
	ans = s * (alen // len(s))
	return ans

def main():
	q = int(input())
	s = ['x'] * q
	t = ['x'] * q
	for j in range(q):
		s[j] = input()
		t[j] = input()
	for j in range(q):
		print(answer(s[j], t[j]))
main()


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

def compute_lcm(x, y):
	lcm = x * y // compute_gcd(x, y)
	return lcm
n = int(input())
for i in range(n):
	s1 = input()
	s2 = input()
	lcm = compute_lcm(len(s1), len(s2))
	if s1 * int(lcm / len(s1)) == s2 * int(lcm / len(s2)):
		print(s1 * int(lcm / len(s1)))
	else:
		print(-1)


from math import gcd
for _ in range(int(input())):
	s = input()
	b = input()
	if s == b:
		print(s)
		continue
	sl = len(s)
	bl = len(b)
	lcm = sl * bl // gcd(sl, bl)
	f = lcm // sl
	t = lcm // bl
	s = s * f
	b = b * t
	if s == b:
		print(s)
	else:
		print(-1)


T = int(input().strip())

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

def solve():
	s1 = input()
	s2 = input()
	lcm_len = len(s1) * len(s2) // gcd(len(s1), len(s2))
	s1 *= lcm_len // len(s1)
	s2 *= lcm_len // len(s2)
	if s1 == s2:
		print(s1)
	else:
		print(-1)
for _ in range(T):
	solve()


import sys
readline = sys.stdin.readline

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(readline().strip())
for _ in range(n):
	a = readline().strip()
	b = readline().strip()
	(na, nb) = (len(a), len(b))
	l = lcm(na, nb)
	ax = a * (l // na)
	bx = b * (l // nb)
	if ax == bx:
		print(ax)
	else:
		print(-1)


def doit():
	A = input()
	B = input()
	g = gcd(len(A), len(B))
	s = A * (len(B) // g)
	print(s if s == B * (len(A) // g) else '-1')
from math import gcd
T = int(input())
for _ in range(T):
	doit()


import io
import os
from collections import Counter, defaultdict, deque
from math import gcd

def solve(S, T):
	N = len(S)
	M = len(T)
	g = gcd(N, M)
	l = N * M // g
	n = l // N
	m = l // M
	temp1 = S * n
	temp2 = T * m
	if temp1 == temp2:
		return temp1
	return -1
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	S = input().decode().rstrip()
	T = input().decode().rstrip()
	ans = solve(S, T)
	print(ans)


import math
for _ in range(int(input())):
	a = input()
	b = input()
	x = len(a) * len(b) // math.gcd(len(a), len(b))
	n = a * (x // len(a))
	m = b * (x // len(b))
	if n == m:
		print(n)
	else:
		print(-1)


import math
t = int(input())
for x in range(t):
	a = input()
	b = input()
	l = len(a) * len(b) // math.gcd(len(a), len(b))
	s1 = a * (l // len(a))
	s2 = b * (l // len(b))
	if s1 == s2:
		print(s1)
	else:
		print(-1)


import math
for _ in range(int(input())):
	p = input()
	q = input()
	n = len(p)
	m = len(q)
	if n < m:
		(p, q) = (q, p)
		(n, m) = (m, n)
	k = 0
	i = 0
	while i < m:
		if q[i] != p[i]:
			print('-1')
			break
		i += 1
	else:
		pp = p * m
		qq = q * n
		d = math.gcd(m, n)
		d = m * n // d
		if pp != qq:
			print('-1')
		else:
			print(p * (d // n))


def main():
	num_cases = int(input())
	for case_num in range(1, num_cases + 1):
		solve(case_num)

def solve(case_num):
	s = input()
	t = input()
	l = lcm(len(s), len(t))
	first = s * (l // len(s))
	second = t * (l // len(t))
	if first == second:
		print(first)
	else:
		print(-1)

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

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


import math
q = int(input())
for _ in range(q):
	s = input()
	t = input()
	lcm = abs(len(s) * len(t)) // math.gcd(len(s), len(t))
	lcms = lcm // len(s)
	lcmt = lcm // len(t)
	if s * lcms == t * lcmt:
		print(s * lcms)
	else:
		print(-1)


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')
for _ in range(int(input())):
	s = input()
	n = len(s)
	t = input()
	m = len(t)
	f = 0
	if len(s) > len(t):
		tt = s
		var = n // m
		if t * var == s:
			f = 1
			print(s)
	else:
		tt = t
		var = m // n
		if s * var == t:
			f = 1
			print(t)
	if f == 0:
		if n > m:
			while len(tt) <= 405:
				var = len(tt) // m
				if t * var == tt:
					f = 1
					break
				else:
					tt += s
		else:
			while len(tt) <= 405:
				var = len(tt) // n
				if s * var == tt:
					f = 1
					break
				else:
					tt += t
		if f == 0:
			print(-1)
		else:
			print(tt)


import math
for i in range(int(input())):
	a = input()
	b = input()
	al = len(a)
	bl = len(b)
	lcm = int(al * bl / math.gcd(al, bl))
	ac = lcm // al
	bc = lcm // bl
	am = a * ac
	bm = b * bc
	if am == bm:
		print(am)
	else:
		print('-1')


import sys, collections, math
sys.setrecursionlimit(1000)

def in_out():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(int(input())):
	s = input()
	t = input()
	if s == t:
		print(s)
		continue
	if len(s) > len(t):
		(s, t) = (t, s)
	m = len(s)
	n = len(t)
	if s * n != t * m:
		print(-1)
	elif n % m == 0:
		print(t)
	else:
		lcm = m * n // math.gcd(m, n)
		print(s * (lcm // m))


def EulerGCD(a, b):
	if a == 1 or b == 1:
		return 1
	if a % b == 0:
		return b
	else:
		return EulerGCD(b, a % b)
for _ in range(int(input())):
	s1 = input()
	s2 = input()
	gcd = EulerGCD(len(s1), len(s2))
	if s1 * (len(s2) // gcd) != s2 * (len(s1) // gcd):
		print(-1)
	else:
		print(s1 * (len(s2) // gcd))


from math import gcd
t = int(input())
for i in range(t):
	a = input()
	b = input()
	x = len(a) * len(b) // gcd(len(a), len(b))
	if x // len(a) * a == x // len(b) * b:
		print(x // len(a) * a)
	else:
		print(-1)


def Alcm(x, y):
	if x > y:
		greater = x
	else:
		greater = y
	while True:
		if greater % x == 0 and greater % y == 0:
			lcm = greater
			break
		greater += 1
	return lcm

def co(r):
	c1 = 0
	for j in r:
		if j == '':
			c1 = c1 + 1
		else:
			return 'x'
	return c1

def solve(a, b):
	c = 0
	c1 = 0
	mi = ''
	r = []
	for i in range(len(a)):
		mi = a[0:i + 1]
		r = b.split(mi)
		c = 0
		c1 = 0
		c = co(r)
		if c == 'x':
			continue
		c = float(c - 1)
		if c == len(b) / len(mi):
			r = a.split(mi)
			c1 = co(r)
			if c1 == 'x':
				continue
			c1 = float(c1 - 1)
			return int(Alcm(c1, c)) * mi
	return -1
for _ in range(int(input())):
	a = input()
	b = input()
	print(solve(a, b))


for q in range(int(input())):
	s = input()
	t = input()
	a = len(s)
	b = len(t)
	m = a * b
	while a != 0 and b != 0:
		if a > b:
			a %= b
		else:
			b %= a
	d = m // (a + b)
	if s * (d // len(s)) == t * (d // len(t)):
		print(s * (d // len(s)))
	else:
		print(-1)


import math

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
for i in range(int(input())):
	s = input()
	t = input()
	if s * len(t) == t * len(s):
		print(int(lcm(len(s), len(t)) / len(s)) * s)
	else:
		print(-1)


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

def lcd(a, b):
	return a * b // gcd(a, b)
t = int(input())
for _ in range(t):
	(s1, s2) = (input(), input())
	(l1, l2) = (len(s1), len(s2))
	if l1 % l2 == 0 and s1 == s2 * (l1 // l2):
		print(s1)
	elif l2 % l1 == 0 and s2 == s1 * (l2 // l1):
		print(s2)
	elif s1 * (lcd(l1, l2) // l1) == s2 * (lcd(l1, l2) // l2):
		print(s1 * (lcd(l1, l2) // l1))
	else:
		print(-1)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import *
for _ in range(int(input())):
	a = input()
	b = input()
	an = len(a)
	bn = len(b)
	faca = set([a])
	facb = set([b])
	for i in range(an // 2):
		if a.count(a[:i + 1]) * (i + 1) == an:
			faca.add(a[:i + 1])
	for i in range(bn // 2):
		if b.count(b[:i + 1]) * (i + 1) == bn:
			facb.add(b[:i + 1])
	com = faca.intersection(facb)
	if len(com):
		final = a + b
		for i in com:
			if len(i) < len(final):
				final = i
		lcm = a.count(final) * b.count(final) // gcd(a.count(final), b.count(final))
		print(final * lcm)
	else:
		print(-1)


def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
n = int(input())
for i in range(n):
	s1 = input()
	s2 = input()
	l1 = len(s1)
	l2 = len(s2)
	if l1 > l2:
		d = gcd(l1, l2)
	else:
		d = gcd(l2, l1)
	s1 = s1 * (l2 // d)
	s2 = s2 * (l1 // d)
	if s1 == s2:
		print(s1)
	else:
		print(-1)


from math import gcd
ans = []
t = int(input())
for _ in range(t):
	a = input()
	b = input()
	ln = len(a) * len(b) // gcd(len(a), len(b))
	if a * (ln // len(a)) == b * (ln // len(b)):
		ans.append(a * (ln // len(a)))
	else:
		ans.append(-1)
for i in ans:
	print(i)


list_ans = []
for _ in range(int(input())):
	s = input()
	t = input()
	a = 1
	for i in range(2, min(len(s), len(t)) + 1):
		if len(s) % i == 0 and len(t) % i == 0:
			a = i
	x = len(s) * len(t) // a
	str1 = s * (x // len(s))
	str2 = t * (x // len(t))
	if str1 == str2:
		list_ans.append(str1)
	else:
		list_ans.append(-1)
for item in list_ans:
	print(item)


import sys
import math
inf = float('inf')

def lcm(a, b):
	return int(a / math.gcd(a, b) * b)
for _ in range(int(input())):
	a = input()
	b = input()
	x = len(a)
	y = len(b)
	if y < x:
		z = lcm(x, y)
		z1 = z // y
		z2 = z // x
		ans1 = b * z1
		ans2 = a * z2
		if ans1 == ans2:
			print(ans1)
		else:
			print(-1)
	else:
		z = lcm(x, y)
		z1 = z // x
		z2 = z // y
		ans1 = a * z1
		ans2 = b * z2
		if ans1 == ans2:
			print(ans1)
		else:
			print(-1)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
for i in range(int(input())):
	a = input()
	b = input()
	a1 = len(a)
	b1 = len(b)
	c = 0
	x = gcd(a1, b1)
	g = []
	for j in range(a1 * b1 // x):
		if a[j % a1] != b[j % b1]:
			c = c + 1
		else:
			g.append(a[j % a1])
	if c == 0:
		print(''.join(map(str, g)))
	else:
		print('-1')


import math
t = int(input())
for t in range(0, t):
	a = input()
	b = input()
	lcm = len(a) * len(b) // math.gcd(len(a), len(b))
	a = a * (lcm // len(a))
	b = b * (lcm // len(b))
	if a == b:
		print(a)
	else:
		print(-1)


from math import *
for _ in range(int(input())):
	a = input()
	b = input()
	s = set()
	if len(b) > len(a):
		(a, b) = (b, a)
	for i in range(1, int(sqrt(len(b))) + 1):
		if len(b) % i == 0:
			s.add(i)
			s.add(len(b) // i)
	l = list(s)
	l.sort()
	g_c_d = gcd(len(a), len(b))
	for i in l:
		if b[:i] * (len(b) // i) == b:
			d = b[:i]
			break
	if len(a) // len(d) * d == a:
		if len(a) % len(b) == 0:
			ans = len(a) // len(d) * d
		else:
			ans = len(a) * len(b) // g_c_d // len(d) * d
	else:
		ans = -1
	print(ans)


def fu(a, b):
	if a == len(a) // len(b) * b:
		return 1
	else:
		return 0
t = int(input())
for _ in range(t):
	a = input()
	b = input()
	f = 0
	for i in range(1, 40):
		for j in range(1, 40):
			if a * i == b * j:
				f = 1
				print(a * i)
				break
		if f == 1:
			break
	if f == 0:
		print(-1)


import math
for _ in range(int(input())):
	a = input()
	b = input()
	c = len(a)
	d = len(b)
	e = math.gcd(len(a), len(b))
	lcm = c * d // e
	if c == d and a != b:
		print(-1)
	else:
		a = [a] * (lcm // c)
		a = ''.join(a)
		b = [b] * (lcm // d)
		b = ''.join(b)
		if a == b:
			print(a)
		else:
			print(-1)


from math import *
for _ in range(int(input())):
	a = input()
	b = input()
	(n, k) = (len(a), len(b))
	g = gcd(n, k)
	s = a[:g]
	if s * (n // g) == a and s * (k // g) == b:
		print(s * (n * k // g ** 2))
	else:
		print(-1)


import sys
input = sys.stdin.readline
import math
for _ in range(int(input())):
	s = input()[:-1]
	w = input()[:-1]
	x = len(s) * len(w) // math.gcd(len(s), len(w))
	s = s * (x // len(s))
	w = w * (x // len(w))
	if s != w:
		print(-1)
	else:
		print(s)


from math import gcd

def solve():
	s = input()
	t = input()
	ls = len(s)
	lt = len(t)
	lc = ls * lt // gcd(ls, lt)
	c = lc // ls * s
	if c == lc // lt * t:
		return c
	else:
		return -1
q = int(input())
for _ in range(q):
	print(solve())


import math
for _ in range(int(input())):
	s1 = input()
	s2 = input()
	g = math.gcd(len(s1), len(s2))
	if len(s2) // g * s1 == len(s1) // g * s2:
		print(len(s2) // g * s1)
	else:
		print(-1)


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

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

def main():
	T = int(input())
	for c in range(T):
		s1 = input().rstrip()
		s2 = input().rstrip()
		s = s1 if len(s1) <= len(s2) else s2
		l = s2 if len(s1) <= len(s2) else s1
		bs = [0 for i in range(len(s))]
		bl = [0 for i in range(len(l))]
		pl = 0
		res = True
		while True:
			ps = 0
			bl[pl] = 1
			while ps < len(s):
				if s[ps] != l[pl]:
					res = False
					break
				ps += 1
				pl = (pl + 1) % len(l)
			if res == False:
				break
			if bl[pl] == 1:
				res = pl == 0
				break
		if res == False:
			print('-1')
		else:
			lcm = compute_lcm(len(s), len(l))
			ans = ''
			for c in range(int(lcm / len(s))):
				ans += s
			print(ans)
main()


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

def lcm(a, b):
	return a / gcd(a, b) * b
for _ in range(int(input())):
	s = input()
	t = input()
	l1 = len(s)
	l2 = len(t)
	m = lcm(l1, l2)
	m = int(m)
	sf = s * (m // l1)
	tf = t * (m // l2)
	if sf == tf:
		print(sf)
	else:
		print('-1')


import math
q = int(input())
for use in range(0, q):
	s = input()
	t = input()
	ns = len(s)
	nt = len(t)
	g = math.gcd(ns, nt)
	al = s * (nt // g)
	if al == t * (ns // g):
		print(al)
	else:
		print(-1)


def GCD(a, b):
	remainder = max(a, b) % min(a, b)
	if remainder == 0:
		return min(a, b)
	else:
		if a % remainder == 0:
			return remainder
		return GCD(remainder, min(a, b))

def LCMString():
	n = int(input())
	for i in range(n):
		s = input()
		t = input()
		found = False
		LCM = len(s) * len(t) / GCD(len(s), len(t))
		sString = int(LCM / len(s)) * s
		tString = int(LCM / len(t)) * t
		if sString == tString:
			found = True
			print(sString)
		if not found:
			print(-1)
LCMString()


import math

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

def solve():
	(s, t) = (input(), input())
	(base_s, base_t) = (s, t)
	(mul_s, mul_t) = (1, 1)
	for i in range(1, len(s) + 1):
		if len(s) % i == 0 and s[0:i] * (len(s) // i) == s:
			base_s = s[0:i]
			mul_s = len(s) // i
			break
	for i in range(1, len(t) + 1):
		if len(t) % i == 0 and t[0:i] * (len(t) // i) == t:
			base_t = t[0:i]
			mul_t = len(t) // i
			break
	if base_s == base_t:
		print(base_s * lcm(mul_s, mul_t))
	else:
		print(-1)
q = int(input())
for _ in range(q):
	solve()


from math import gcd
n = int(input())
for _ in range(0, n):
	a = input()
	b = input()
	(a, b) = sorted([a, b], key=len)
	x = len(a) * len(b) // gcd(len(a), len(b))
	if a * (x // len(a)) == b * (x // len(b)):
		print(a * (x // len(a)))
	else:
		print(-1)


from math import gcd

def fun(s1, s2, x, h):
	if s1 == s2 * (h // x):
		return True
	return False
t = int(input())
for _ in range(t):
	s = input().rstrip()
	t = input().rstrip()
	m = len(s)
	n = len(t)
	if m == n:
		if s == t:
			print(s)
		else:
			print(-1)
	else:
		h = m * n // gcd(m, n)
		if m > n:
			s1 = s * (h // m)
			if fun(s1, t, n, h):
				print(s1)
			else:
				print(-1)
		else:
			s1 = t * (h // n)
			if fun(s1, s, m, h):
				print(s1)
			else:
				print(-1)


import math

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
for _ in range(int(input())):
	s = input()
	t = input()
	a = len(s)
	b = len(t)
	if a == b and s == t:
		print(s)
	elif a == b:
		print('-1')
	elif a < b:
		p = lcm(a, b)
		ok = True
		res = s * (p // a)
		for i in range(0, len(res), a):
			curr = res[i:i + a]
			if curr != s:
				ok = False
				break
		for i in range(0, len(res), b):
			curr = res[i:i + b]
			if curr != t:
				ok = False
				break
		if ok == False:
			print('-1')
		else:
			print(res)
	else:
		p = lcm(a, b)
		ok = True
		res = t * (p // b)
		for i in range(0, len(res), a):
			curr = res[i:i + a]
			if curr != s:
				ok = False
				break
		for i in range(0, len(res), b):
			curr = res[i:i + b]
			if curr != t:
				ok = False
				break
		if ok == False:
			print('-1')
		else:
			print(res)


import math
q = int(input())
for _ in range(q):
	s = input()
	t = input()
	ls = len(s)
	lt = len(t)
	n = ls * lt // math.gcd(ls, lt)
	a = n // ls
	b = n // lt
	if s * a == t * b:
		print(s * a)
	else:
		print(-1)


from math import gcd

def lcm(x, y):
	return x * y // gcd(x, y)
for _ in range(int(input())):
	s = str(input())
	t = str(input())
	x = len(s)
	y = len(t)
	c = lcm(x, y)
	if s * (c // x) == t * (c // y):
		print(s * (c // x))
	else:
		print(-1)


n = int(input())
l = [-1] * n

def compute_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for i in range(n):
	sn = input()
	sm = input()
	b = compute_gcd(len(sn), len(sm))
	b = len(sn) * len(sm) // b
	sn = sn * (b // len(sn))
	sm = sm * (b // len(sm))
	if sn == sm:
		l[i] = sm
for j in range(n):
	print(l[j])


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
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, Counter as dc
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())

def getMat(n):
	return [getInts() for _ in range(n)]

def isInt(s):
	return '0' <= s[0] <= '9'
MOD = 10 ** 9 + 7

def solve():
	S = getStr()
	T = getStr()
	N = len(S)
	M = len(T)
	LCM = N * M // math.gcd(N, M)
	ans = LCM // N * S
	if ans == LCM // M * T:
		return ans
	return -1
for _ in range(getInt()):
	print(solve())


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

def lcm(a, b):
	return a / gcd(a, b) * b
for kchgcv in range(int(input())):
	a = input()
	b = input()
	if len(a) < len(b):
		(a, b) = (b, a)
	if len(a) % len(b) == 0:
		if b * (len(a) // len(b)) == a:
			print(a)
			continue
		else:
			print(-1)
			continue
	else:
		f = lcm(len(a), len(b))
		s = int(f // len(a)) * a
		if s == int(f // len(b)) * b:
			print(s)
		else:
			print(-1)


from __future__ import division, print_function
import os
import sys
from math import gcd
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	q = int(input())
	for _ in range(q):
		s = input()
		t = input()
		l = len(s) * len(t) // gcd(len(s), len(t))
		sol = s * (l // len(s))
		if sol == t * (l // len(t)):
			print(sol)
		else:
			print(-1)
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
for _ in range(int(input())):
	s = input().strip()
	t = input().strip()
	(s_len, t_len) = (len(s), len(t))
	g = math.gcd(s_len, t_len)
	(temp1, temp2) = ('', '')
	temp1 = s * (t_len // g)
	temp2 = t * (s_len // g)
	if temp1 == temp2:
		print(temp1)
	else:
		print(-1)


import math
for _ in range(int(input())):
	s = input()
	t = input()
	pre = ''
	for i in range(len(s)):
		pre += s[i]
		if len(s) % (i + 1) == 0:
			if s.count(pre) == len(s) // (i + 1):
				break
	if len(t) % len(pre) == 0 and t.count(pre) == len(t) // len(pre):
		k = t.count(pre) * s.count(pre) // math.gcd(t.count(pre), s.count(pre))
		print(pre * k)
	else:
		print(-1)


import math
for _ in range(int(input())):
	a = input()
	b = input()
	g = math.gcd(len(a), len(b))
	(x, y) = (a * (len(b) // g), b * (len(a) // g))
	print(x if x == y else -1)


import math
t = int(input())
while t != 0:
	s = input()
	p = input()
	m = len(s)
	n = len(p)
	l = m * n // math.gcd(m, n)
	s = s * (l // m)
	p = p * (l // n)
	if s == p:
		print(s)
	else:
		print('-1')
	t = t - 1


import math

def lcm(a, b):
	return a * b // math.gcd(a, b)
for _ in range(int(input())):
	aa = input()
	bb = input()
	if len(aa) > len(bb):
		a = aa
		b = bb
	else:
		a = bb
		b = aa
	a1 = len(a)
	b1 = len(b)
	ans = lcm(a1, b1)
	a11 = a * (ans // a1)
	b11 = b * (ans // b1)
	if a11 != b11:
		print(-1)
	else:
		print(a11)


import math
test_cases = int(input())

def get_lcm(num1, num2):
	return abs(num1 * num2) // math.gcd(num1, num2)

def print_string_lcm(word_one, word_two):
	if len(word_two) > len(word_one):
		(word_one, word_two) = (word_two, word_one)
	if len(word_one) % len(word_two) == 0:
		iterations = len(word_one) // len(word_two)
		if word_two * iterations != word_one:
			print(-1)
		else:
			print(word_one)
	else:
		target_length = get_lcm(len(word_one), len(word_two))
		iterations_one = target_length // len(word_one)
		iterations_two = target_length // len(word_two)
		if word_one * iterations_one != word_two * iterations_two:
			print(-1)
		else:
			print(word_one * iterations_one)
for _ in range(test_cases):
	word_one = input()
	word_two = input()
	print_string_lcm(word_one, word_two)


import math
n = int(input())
for i in range(n):
	s1 = input()
	s2 = input()
	l1 = len(s1)
	l2 = len(s2)
	h = math.gcd(l1, l2)
	g1 = s1 * (l2 // h)
	g2 = s2 * (l1 // h)
	if g1 == g2:
		print(g1)
	else:
		print(-1)