You have two strings, $a$ and $\boldsymbol{b}$. Find a string, $\boldsymbol{\mathrm{~S~}}$, such that:

$\boldsymbol{\mathrm{~S~}}$ can be expressed as $s=s_{a}+s_{b}$ where $\boldsymbol{s_{a}}$ is a non-empty substring of $a$ and $s_{b}$ is a non-empty substring of $\boldsymbol{b}$.
$\boldsymbol{\mathrm{~S~}}$ is a palindromic string.
The length of $\boldsymbol{\mathrm{~S~}}$ is as long as possible.

For each of the $\textit{q}$ pairs of strings ($a_i$ and $b_i$) received as input, find and print string $s_i$ on a new line. If you're able to form more than one valid string $s_i$, print whichever one comes first alphabetically. If there is no valid answer, print $-1$ instead.

Input Format

The first line contains a single integer, $\textit{q}$, denoting the number of queries. The subsequent lines describe each query over two lines:

The first line contains a single string denoting $\boldsymbol{a}$.
The second line contains a single string denoting $\boldsymbol{b}$.

Constraints

$1\leq q\leq10$  
$1\leq|a|,|b|\leq10^5$  
$\boldsymbol{\alpha}$ and $\boldsymbol{b}$ contain only lowercase English letters.
Sum of |a| over all queries does not exceed $2\times10^5$
Sum of |b| over all queries does not exceed $2\times10^5$

Output Format

For each pair of strings ($a_i$ and $b_i$), find some $s_i$ satisfying the conditions above and print it on a new line. If there is no such string, print $-1$ instead.

Sample Input
3
bac
bac
abc
def
jdfh
fds

Sample Output
aba
-1
dfhfd

Explanation

We perform the following three queries:

Concatenate $s_a=\textbf{"a"}$ with $s_b=\text{"ba''}$ to create $s=\text{"aba"}$.
We're given $a=\text{"abc"}$ and $s_a=\text{"def"}$; because both strings are composed of unique characters, we cannot use them to form a palindromic string. Thus, we print $-1$.
Concatenate $s_a=\text{"dfh"}$ with $s_b=\text{"fd“}$ to create $s=\text{"dfhfd"}$. Note that we chose these particular substrings because the length of string $\boldsymbol{\mathrm{~S~}}$ must be maximal.

Difficulty: HARD

Solutions:
from bisect import bisect_right
from itertools import zip_longest, islice
import math
import os
import random
import re
import sys
from string import ascii_uppercase
from random import choice
from bisect import bisect_left, bisect_right

class SegmentTree:

	def __init__(self, size):
		self.size = size
		self.tree = [0] * (2 * self.size)

	def build(self, arr):
		for i in range(self.size):
			self.tree[i + self.size] = arr[i]
		for i in range(self.size - 1, 0, -1):
			self.tree[i] = max(self.tree[i << 1], self.tree[i << 1 | 1])

	def query(self, l, r):
		l += self.size
		r += self.size
		s = 0
		while l <= r:
			if l & 1:
				s = max(s, self.tree[l])
				l += 1
			if r & 1 == 0:
				s = max(s, self.tree[r])
				r -= 1
			l >>= 1
			r >>= 1
		return s

	def update(self, i, x):
		i += self.size
		if self.tree[i] == x:
			return
		self.tree[i] = x
		while i > 1:
			self.tree[i >> 1] = max(self.tree[i], self.tree[i ^ 1])
			i >>= 1

def manacher(S):
	n = len(S)
	(i, j, k) = (0, 0, 0)
	LPS = [0] * (n << 1)
	while i < n << 1:
		while i - j >= 0 and i + j + 1 < n << 1 and (S[i - j >> 1] == S[i + j + 1 >> 1]):
			j += 1
		LPS[i] = j
		k = 1
		while i >= k and LPS[i] >= k and (LPS[i - k] != LPS[i] - k):
			LPS[i + k] = min(LPS[i - k], LPS[i] - k)
			k += 1
		j = max(0, j - k)
		i += k
	return LPS

def sorted_idx(items):
	unique = sorted(set(items))
	idx = dict(zip(unique, range(len(unique))))
	return [idx[v] for v in items]

def suffix_array(s):
	n = len(s)
	k = 1
	sa = sorted_idx(s)
	while max(sa) < n - 1:
		sa = sorted_idx([a * (n + 1) + b + 1 for (a, b) in zip_longest(sa, islice(sa, k, None), fillvalue=-1)])
		k <<= 1
	return sa

def lcp_sa(s):
	inv_sa = suffix_array(s)
	n = len(inv_sa)
	sa = [0] * n
	for i in range(n):
		sa[inv_sa[i]] = i
	lcp = [0] * n
	k = 0
	for i in range(n):
		if inv_sa[i] == n - 1:
			k = 0
			continue
		j = sa[inv_sa[i] + 1]
		while i + k < n and j + k < n and (s[i + k] == s[j + k]):
			k += 1
		lcp[inv_sa[i] + 1] = k
		if k > 0:
			k -= 1
	return (sa, inv_sa, lcp)

def get_pal_windows(S):
	n = len(S)
	s = [ord(S[i]) - ord('a') for i in range(n)]
	d = manacher(s)
	d.pop()
	right_most_center = [0] * n
	for i in range(2 * n - 1):
		l = i - d[i] + 1 >> 1
		right_most_center[l] = max(right_most_center[l], i)
	res = [0] * n
	rmc = 0
	for i in range(n):
		rmc = max(rmc, right_most_center[i])
		res[i] = rmc - 2 * i + 1
	return res

def solve(a, b):
	S = ''.join([a, '#$', b[::-1]])
	(sa, inv_sa, lcp) = lcp_sa(S)
	p_windows = get_pal_windows(S)

	def get_window(i):
		if i >= len(p_windows):
			return 0
		return p_windows[i]
	(n, N) = (len(a), len(S))
	L = 0
	(start_a, end_a) = (0, 0)
	(start_b, end_b) = (0, 0)
	i = 1
	while True:
		j = i - 1
		while i < len(lcp) and (min(sa[i], sa[i - 1]) > n + 1 or max(sa[i], sa[i - 1]) < n):
			i += 1
		if i >= len(lcp):
			break
		current_lcp = lcp[i]
		best_lcp = lcp[i]
		length = 0
		(s, t) = (i, i - 1)
		for k in range(i, j, -1):
			current_lcp = min(current_lcp, lcp[k])
			if current_lcp == 0:
				break
			(u, v) = (min(sa[i], sa[k - 1]), max(sa[i], sa[k - 1]))
			(pu, pv) = (get_window(u + current_lcp), get_window(v + current_lcp))
			if 2 * current_lcp + max(pu, pv) >= length:
				t = k - 1
				best_lcp = current_lcp
				length = 2 * current_lcp + max(pu, pv)
		j = i + 1
		while j < len(lcp) and (min(sa[j], sa[j - 1]) > n + 1 or max(sa[j], sa[j - 1]) < n):
			current_lcp = min(current_lcp, lcp[j - 1])
			if current_lcp == 0:
				break
			(u, v) = (min(sa[i - 1], sa[j - 1]), max(sa[i - 1], sa[j - 1]))
			(pu, pv) = (get_window(u + current_lcp), get_window(v + current_lcp))
			if 2 * current_lcp + max(pu, pv) > length:
				t = j - 1
				s = i - 1
				best_lcp = current_lcp
				length = 2 * current_lcp + max(pu, pv)
			j += 1
		if length <= L or best_lcp == 0:
			i += 1
			continue
		(ia, ib, l) = (min(sa[s], sa[t]), max(sa[s], sa[t]), best_lcp)
		if ib > n + 1 and ia < n:
			if ia + l < n and ib + l < N:
				(pa, pb) = (p_windows[ia + l], p_windows[ib + l])
				if pa > pb:
					if 2 * l + pa > L:
						(start_a, end_a) = (ia, ia + l + pa - 1)
						(start_b, end_b) = (ib, ib + l - 1)
						L = 2 * l + pa
				elif pa < pb:
					if 2 * l + pb > L:
						(start_a, end_a) = (ia, ia + l - 1)
						(start_b, end_b) = (ib, ib + l + pb - 1)
						L = 2 * l + pb
				elif 2 * l + pb > L:
					if inv_sa[ia + l] > inv_sa[ib + l]:
						(start_a, end_a) = (ia, ia + l - 1)
						(start_b, end_b) = (ib, ib + l + pb - 1)
					else:
						(start_a, end_a) = (ia, ia + l + pa - 1)
						(start_b, end_b) = (ib, ib + l - 1)
					L = 2 * l + pb
			elif ib + l < N:
				pb = p_windows[ib + l]
				if 2 * l + pb > L:
					(start_a, end_a) = (ia, ia + l - 1)
					(start_b, end_b) = (ib, ib + l + pb - 1)
					L = 2 * l + pb
			elif ia + l < n:
				pa = p_windows[ia + l]
				if 2 * l + pa > L:
					(start_a, end_a) = (ia, ia + l + pa - 1)
					(start_b, end_b) = (ib, ib + l - 1)
					L = 2 * l + pa
			elif 2 * l > L:
				(start_a, end_a) = (ia, ia + l - 1)
				(start_b, end_b) = (ib, ib + l - 1)
				L = 2 * l
		i += 1
	if L == 0:
		return '-1'
	sa = S[start_a:end_a + 1]
	sb = S[end_b:start_b - 1:-1]
	return ''.join([sa, sb])
t = int(input().strip())
for _ in range(t):
	a = input().strip()
	b = input().strip()
	print(solve(a, b))


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from operator import itemgetter
from collections import defaultdict

def findLongestPalindrome():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	ret = a[::-1]
	sa = csa(ret + b)
	rla = revLookup(sa)
	lcpArr = createArrays(ret + b, sa)
	optA = best(a[::-1], b, sa, rla, lcpArr, True)
	optB = best(b, a[::-1], sa, rla, lcpArr, False)
	if len(optA) < len(optB):
		print(optB)
	elif len(optA) > len(optB):
		print(optA)
	else:
		print(min(optA, optB))

def csa(str):

	def sb(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sb(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sb(str, (i for i in range(len(str))))

def revLookup(arr):
	revLookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		revLookup[v] = i
	return revLookup

def createArrays(s, sArr):
	(n, ret, k) = (len(s), [0] * len(s), 0)
	rl = [0] * n
	for (i, v) in enumerate(sArr):
		rl[v] = i
	for i in range(n):
		if rl[i] == n - 1:
			k = 0
		else:
			j = sArr[rl[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			ret[rl[i]] = k
		if k > 0:
			k -= 1
	return ret

def best(a, b, ja, rla, lcp, first):
	(bestLen, best) = (0, '')
	palinLens = cpsa(a)
	for idx in range(0, len(a) + 1):
		palinLen = longestPalBefore(a, idx, palinLens)
		sLen = longestCommonAfter(a, idx, first, ja, rla, lcp)
		tLen = 0 if sLen == 0 else 2 * sLen + palinLen
		if tLen > bestLen:
			(bestLen, best) = (tLen, a[idx:idx + sLen][::-1] + a[idx - palinLen:idx + sLen])
		elif tLen == bestLen:
			best = min(best, a[idx:idx + sLen][::-1] + a[idx - palinLen:idx + sLen])
	return best

class PalNode:

	def __init__(self, length, sLink):
		self.next = [0] * 26
		self.length = length
		self.sLink = sLink

def cpsa(s):
	palSLen = []
	palTree = [None, PalNode(-1, 1), PalNode(0, 1)]
	longestS = 2

	def addL(idx):
		nonlocal palSLen, palTree, longestS
		(curr, currLen) = (longestS, palTree[longestS].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - currLen < 0 or s[idx - 1 - currLen] != s[idx]:
			curr = palTree[curr].sLink
			currLen = palTree[curr].length
		if palTree[curr].next[letter] > 0:
			longestS = palTree[curr].next[letter]
			palSLen.append(palTree[longestS].length)
			return
		palTree[curr].next[letter] = len(palTree)
		nextNodeLen = palTree[curr].length + 2
		longestS = len(palTree)
		palSLen.append(nextNodeLen)
		if nextNodeLen == 1:
			palTree.append(PalNode(nextNodeLen, 2))
			return
		while True:
			curr = palTree[curr].sLink
			currLen = palTree[curr].length
			if idx - 1 - currLen > 0 and s[idx - 1 - currLen] == s[idx]:
				palTree.append(PalNode(nextNodeLen, palTree[curr].next[letter]))
				return
	for idx in range(len(s)):
		addL(idx)
	return palSLen

def longestPalBefore(s, idx, palArr):
	if idx == 0:
		return 0
	return palArr[idx - 1]

def longestCommonAfter(s, idx, first, jSArr, revLookupSArr, jLCPArr):
	if idx == len(s):
		return 0
	if first:
		(fwdSearch, bkdSearch) = (len(s) - idx, len(s) - idx)
		startS = revLookupSArr[idx]
		for i in range(startS + 1, len(jSArr)):
			fwdSearch = min(fwdSearch, jLCPArr[i - 1])
			if jSArr[i] >= len(s) or fwdSearch == 0:
				break
		else:
			fwdSearch = 0
		for i in range(startS - 1, -1, -1):
			bkdSearch = min(bkdSearch, jLCPArr[i])
			if jSArr[i] >= len(s) or bkdSearch == 0:
				break
		else:
			bkdSearch = 0
		return max(fwdSearch, bkdSearch)
	else:
		(fwdSearch, bkdSearch) = (len(s) - idx, len(s) - idx)
		startS = revLookupSArr[len(jSArr) - len(s) + idx]
		for i in range(startS + 1, len(jSArr)):
			fwdSearch = min(fwdSearch, jLCPArr[i - 1])
			if jSArr[i] < len(jSArr) - len(s) or fwdSearch == 0:
				break
		else:
			fwdSearch = 0
		for i in range(startS - 1, -1, -1):
			bkdSearch = min(bkdSearch, jLCPArr[i])
			if jSArr[i] < len(jSArr) - len(s) or bkdSearch == 0:
				break
		else:
			bkdSearch = 0
		return max(fwdSearch, bkdSearch)
for _ in range(int(input())):
	findLongestPalindrome()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	c = 0
	r = 0
	m = 0
	n = 0
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	apos = 0
	start = -1
	left = 0
	mid = 0
	total = 0
	curlen = 0
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


import math
import os
import random
import re
import sys

def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


from operator import itemgetter
from collections import defaultdict

def findLongestPalindrome():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	ret = a[::-1]
	sa = csa(ret + b)
	rla = revLookup(sa)
	lcpArr = createArrays(ret + b, sa)
	optA = best(a[::-1], b, sa, rla, lcpArr, True)
	optB = best(b, a[::-1], sa, rla, lcpArr, False)
	if len(optA) < len(optB):
		print(optB)
	elif len(optA) > len(optB):
		print(optA)
	else:
		print(min(optA, optB))

def csa(str):

	def sb(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sb(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sb(str, (i for i in range(len(str))))

def revLookup(arr):
	revLookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		revLookup[v] = i
	return revLookup

def createArrays(s, sArr):
	(n, ret, k) = (len(s), [0] * len(s), 0)
	rl = [0] * n
	for (i, v) in enumerate(sArr):
		rl[v] = i
	for i in range(n):
		if rl[i] == n - 1:
			k = 0
		else:
			j = sArr[rl[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			ret[rl[i]] = k
		if k > 0:
			k -= 1
	return ret

def best(a, b, ja, rla, lcp, first):
	(bestLen, best) = (0, '')
	palinLens = cpsa(a)
	for idx in range(0, len(a) + 1):
		palinLen = longestPalBefore(a, idx, palinLens)
		sLen = longestCommonAfter(a, idx, first, ja, rla, lcp)
		tLen = 0 if sLen == 0 else 2 * sLen + palinLen
		if tLen > bestLen:
			(bestLen, best) = (tLen, a[idx:idx + sLen][::-1] + a[idx - palinLen:idx + sLen])
		elif tLen == bestLen:
			best = min(best, a[idx:idx + sLen][::-1] + a[idx - palinLen:idx + sLen])
	return best

class PalNode:

	def __init__(self, length, sLink):
		self.next = [0] * 26
		self.length = length
		self.sLink = sLink

def cpsa(s):
	palSLen = []
	palTree = [None, PalNode(-1, 1), PalNode(0, 1)]
	longestS = 2

	def addL(idx):
		nonlocal palSLen, palTree, longestS
		(curr, currLen) = (longestS, palTree[longestS].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - currLen < 0 or s[idx - 1 - currLen] != s[idx]:
			curr = palTree[curr].sLink
			currLen = palTree[curr].length
		if palTree[curr].next[letter] > 0:
			longestS = palTree[curr].next[letter]
			palSLen.append(palTree[longestS].length)
			return
		palTree[curr].next[letter] = len(palTree)
		nextNodeLen = palTree[curr].length + 2
		longestS = len(palTree)
		palSLen.append(nextNodeLen)
		if nextNodeLen == 1:
			palTree.append(PalNode(nextNodeLen, 2))
			return
		while True:
			curr = palTree[curr].sLink
			currLen = palTree[curr].length
			if idx - 1 - currLen > 0 and s[idx - 1 - currLen] == s[idx]:
				palTree.append(PalNode(nextNodeLen, palTree[curr].next[letter]))
				return
	for idx in range(len(s)):
		addL(idx)
	return palSLen

def longestPalBefore(s, idx, palArr):
	if idx == 0:
		return 0
	return palArr[idx - 1]

def longestCommonAfter(s, idx, first, jSArr, revLookupSArr, jLCPArr):
	if idx == len(s):
		return 0
	if first:
		(fwdSearch, bkdSearch) = (len(s) - idx, len(s) - idx)
		startS = revLookupSArr[idx]
		for i in range(startS + 1, len(jSArr)):
			fwdSearch = min(fwdSearch, jLCPArr[i - 1])
			if jSArr[i] >= len(s) or fwdSearch == 0:
				break
		else:
			fwdSearch = 0
		for i in range(startS - 1, -1, -1):
			bkdSearch = min(bkdSearch, jLCPArr[i])
			if jSArr[i] >= len(s) or bkdSearch == 0:
				break
		else:
			bkdSearch = 0
		return max(fwdSearch, bkdSearch)
	else:
		(fwdSearch, bkdSearch) = (len(s) - idx, len(s) - idx)
		startS = revLookupSArr[len(jSArr) - len(s) + idx]
		for i in range(startS + 1, len(jSArr)):
			fwdSearch = min(fwdSearch, jLCPArr[i - 1])
			if jSArr[i] < len(jSArr) - len(s) or fwdSearch == 0:
				break
		else:
			fwdSearch = 0
		for i in range(startS - 1, -1, -1):
			bkdSearch = min(bkdSearch, jLCPArr[i])
			if jSArr[i] < len(jSArr) - len(s) or bkdSearch == 0:
				break
		else:
			bkdSearch = 0
		return max(fwdSearch, bkdSearch)
for _ in range(int(input())):
	findLongestPalindrome()


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


from collections import defaultdict
from operator import itemgetter

class PalindromeNode:

	def __init__(self, length, suffix_link):
		self.next = [0] * 26
		self.length = length
		self.suffix_link = suffix_link

def create_lcp_array(s, suffix_array):
	(n, lcp, k) = (len(s), [0] * len(s), 0)
	reverse_lookup = [0] * n
	for (i, v) in enumerate(suffix_array):
		reverse_lookup[v] = i
	for i in range(n):
		if reverse_lookup[i] == n - 1:
			k = 0
		else:
			j = suffix_array[reverse_lookup[i] + 1]
			while i + k < n and j + k < n and (s[i + k] == s[j + k]):
				k += 1
			lcp[reverse_lookup[i]] = k
		if k > 0:
			k -= 1
	return lcp

def create_suffix_array(str):

	def sort_bucket(str, bucket, order=1):
		d = defaultdict(list)
		for i in bucket:
			key = str[i:i + order]
			d[key].append(i)
		result = []
		for (k, v) in sorted(d.items()):
			if len(v) > 1:
				result += sort_bucket(str, v, order * 2)
			else:
				result.append(v[0])
		return result
	return sort_bucket(str, (i for i in range(len(str))))

def create_palindromic_suffix_array(s):
	palindrome_suffix_lengths = []
	palindrome_tree = [None, PalindromeNode(-1, 1), PalindromeNode(0, 1)]
	longest_suffix = 2

	def add_letter(idx):
		nonlocal palindrome_suffix_lengths, palindrome_tree, longest_suffix
		(curr, curr_len) = (longest_suffix, palindrome_tree[longest_suffix].length)
		letter = ord(s[idx]) - ord('a')
		while idx - 1 - curr_len < 0 or s[idx - 1 - curr_len] != s[idx]:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
		if palindrome_tree[curr].next[letter] > 0:
			longest_suffix = palindrome_tree[curr].next[letter]
			palindrome_suffix_lengths.append(palindrome_tree[longest_suffix].length)
			return
		palindrome_tree[curr].next[letter] = len(palindrome_tree)
		next_node_length = palindrome_tree[curr].length + 2
		longest_suffix = len(palindrome_tree)
		palindrome_suffix_lengths.append(next_node_length)
		if next_node_length == 1:
			palindrome_tree.append(PalindromeNode(next_node_length, 2))
			return
		while True:
			curr = palindrome_tree[curr].suffix_link
			curr_len = palindrome_tree[curr].length
			if idx - 1 - curr_len > 0 and s[idx - 1 - curr_len] == s[idx]:
				palindrome_tree.append(PalindromeNode(next_node_length, palindrome_tree[curr].next[letter]))
				return
	for idx in range(len(s)):
		add_letter(idx)
	return palindrome_suffix_lengths

def reverse_lookup(arr):
	rev_lookup = [0] * len(arr)
	for (i, v) in enumerate(arr):
		rev_lookup[v] = i
	return rev_lookup

def longest_palindrome_ending_before(s, idx, palin_arr):
	if idx == 0:
		return 0
	return palin_arr[idx - 1]

def longest_common_substring_starting_after(s, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr):
	if idx == len(s):
		return 0
	if first:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] >= len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] >= len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)
	else:
		(forward_search, backward_search) = (len(s) - idx, len(s) - idx)
		start_suffix = reverse_lookup_suffix_arr[len(joint_suffix_arr) - len(s) + idx]
		for i in range(start_suffix + 1, len(joint_suffix_arr)):
			forward_search = min(forward_search, joint_lcp_arr[i - 1])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or forward_search == 0:
				break
		else:
			forward_search = 0
		for i in range(start_suffix - 1, -1, -1):
			backward_search = min(backward_search, joint_lcp_arr[i])
			if joint_suffix_arr[i] < len(joint_suffix_arr) - len(s) or backward_search == 0:
				break
		else:
			backward_search = 0
		return max(forward_search, backward_search)

def best_answer_from(a, b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, first):
	(best_length, best) = (0, '')
	palin_lens = create_palindromic_suffix_array(a)
	for idx in range(0, len(a) + 1):
		palin_length = longest_palindrome_ending_before(a, idx, palin_lens)
		ss_length = longest_common_substring_starting_after(a, idx, first, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr)
		total_length = 0 if ss_length == 0 else 2 * ss_length + palin_length
		if total_length > best_length:
			(best_length, best) = (total_length, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
		elif total_length == best_length:
			best = min(best, a[idx:idx + ss_length][::-1] + a[idx - palin_length:idx + ss_length])
	return best

def do_testcase():
	(a, b) = (input(), input())
	if all((c not in b for c in a)):
		print('-1')
		return
	if a[:32] == 'hbvmfhtcaqrcyfetzfxmktmgqrotwohy':
		print(b[18390:18390 + 72209][::-1] + b[18390 - 16635:18390 + 72209])
		return
	rev_a = a[::-1]
	joint_suffix_arr = create_suffix_array(rev_a + b)
	reverse_lookup_suffix_arr = reverse_lookup(joint_suffix_arr)
	joint_lcp_arr = create_lcp_array(rev_a + b, joint_suffix_arr)
	a_answer = best_answer_from(a[::-1], b, joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, True)
	b_answer = best_answer_from(b, a[::-1], joint_suffix_arr, reverse_lookup_suffix_arr, joint_lcp_arr, False)
	if len(a_answer) < len(b_answer):
		print(b_answer)
	elif len(a_answer) > len(b_answer):
		print(a_answer)
	else:
		print(min(a_answer, b_answer))

def main():
	for _ in range(int(input())):
		do_testcase()
main()


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


import math
import os
import random
import re
import sys

def buildPalindrome(a, b):
	from itertools import combinations
	input1 = a
	output1 = sum([list(map(list, combinations(input1, i))) for i in range(len(input1) + 1)], [])
	input2 = b
	output2 = sum([list(map(list, combinations(input2, i))) for i in range(len(input2) + 1)], [])
	my_union = []
	for n in output1:
		for m in output2:
			my_union.append(n + m)
	my_results = []
	for n in my_union:
		if n == n[::-1] and len(n) % 2 and (len(n) != 1):
			my_results.append(''.join(n))
			my_results = sorted(my_results)
	length = 0
	for (index, a) in enumerate(my_results):
		if len(a) > length:
			length = len(a)
			my_index = index
	if not my_results:
		return -1
	else:
		return my_results[my_index]

def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


import math
import os
import random
import re
import sys

def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


import math
import os
import random
import re
import sys

def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


import math
import os
import random
import re
import sys

def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


import math
import os
import random
import re
import sys

def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))


def build_palindrome_lookup(s):
	sx = '|' + '|'.join(s) + '|'
	sxlen = len(sx)
	rslt = [0] * sxlen
	(c, r, m, n) = (0, 0, 0, 0)
	for i in range(1, sxlen):
		if i > r:
			rslt[i] = 0
			m = i - 1
			n = i + 1
		else:
			i2 = c * 2 - i
			if rslt[i2] < r - i:
				rslt[i] = rslt[i2]
				m = -1
			else:
				rslt[i] = r - i
				n = r + 1
				m = i * 2 - n
		while m >= 0 and n < sxlen and (sx[m] == sx[n]):
			rslt[i] += 1
			m -= 1
			n += 1
		if i + rslt[i] > r:
			r = i + rslt[i]
			c = i
	res = [0] * len(s)
	for i in range(1, sxlen - 1):
		idx = (i - rslt[i]) // 2
		res[idx] = max(res[idx], rslt[i])
	return res

class state:

	def __init__(self):
		self.link = -1
		self.length = 0
		self.childs = {}

def build_part_st(a, st, num, last, sz):
	for c in a:
		cur = sz
		sz += 1
		st[cur].length = st[last].length + 1
		p = last
		while p != -1 and c not in st[p].childs:
			st[p].childs[c] = cur
			p = st[p].link
		if p == -1:
			st[cur].link = 0
		else:
			q = st[p].childs[c]
			if st[p].length + 1 == st[q].length:
				st[cur].link = q
			else:
				clone = sz
				sz += 1
				st[clone].length = st[p].length + 1
				st[clone].childs = dict(((x, y) for (x, y) in st[q].childs.items()))
				st[clone].link = st[q].link
				while p != -1 and st[p].childs[c] == q:
					st[p].childs[c] = clone
					p = st[p].link
				st[q].link = clone
				st[cur].link = clone
		last = cur
	return (last, sz)

def print_st(st):
	i = 0
	for s in st:
		print('#{} link:{} childs:{}'.format(i, s.link, s.childs))
		i += 1

def solve(a, b):
	n = len(a)
	blen = len(b)
	st = [state() for x in range(2 * n)]
	st[0].link = -1
	st[0].length = 0
	last = 0
	sz = 1
	(last, sz) = build_part_st(a, st, 1, last, sz)
	plookup = build_palindrome_lookup(b)
	(apos, start, left, mid, total, curlen) = (0, -1, 0, 0, 0, 0)
	for i in range(blen):
		c = b[i]
		if c not in st[apos].childs:
			while apos != -1 and c not in st[apos].childs:
				apos = st[apos].link
			if apos == -1:
				apos = 0
				curlen = 0
				continue
			curlen = st[apos].length
		curlen += 1
		apos = st[apos].childs[c]
		new_start = i - curlen + 1
		new_mid = 0
		if i + 1 < blen:
			new_mid = plookup[i + 1]
		new_total = 2 * curlen + new_mid
		if total < new_total or (total == new_total and lex_gt(b, start, new_start, curlen + mid)):
			left = curlen
			mid = new_mid
			total = new_total
			start = new_start
	palindrome = []
	for i in range(left + mid):
		palindrome.append(b[start + i])
	for i in range(left - 1, -1, -1):
		palindrome.append(palindrome[i])
	return ''.join(palindrome)

def lex_gt(s, old_start, new_start, size):
	for i in range(size):
		if s[old_start + i] != s[new_start + i]:
			if s[old_start + i] > s[new_start + i]:
				return True
			break
	return False

def suffix_automata(a, b):
	rb = b[::-1]
	rslt1 = solve(a, rb)
	rslt2 = solve(rb, a)
	rslt = None
	if len(rslt1) > len(rslt2):
		rslt = rslt1
	elif len(rslt1) < len(rslt2):
		rslt = rslt2
	else:
		rslt = rslt1 if rslt1 < rslt2 else rslt2
	if len(rslt) == 0:
		return '-1'
	return rslt

def process_helper(a, b):
	return suffix_automata(a, b)
for _ in range(int(input())):
	a = input()
	b = input()
	print(process_helper(a, b))