Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Archaeologist John and his team have unearthed the remains of an ancient civilisation. The partially destroyed walls of its houses contain strange pairs of numbers. John assumes that the first number in each pair always corresponds to a base (itself written in base $10$) and the second number in that pair is written in this base ― for example, 3 2110 denotes $(2110)_3 = 2 \cdot 3^{3} + 1 \cdot 3^{2} + 1 \cdot 3 + 0 = 66$.

Unfortunately, the first number (the base) is often lost or unreadable. John made a list of pairs of numbers found on the walls, where he represents lost bases by $-1$. For example:

-1 10001
3 2110
-1 18565

John is wondering if all these pairs of numbers could represent a single common number ― that is, if it is possible to choose some number $X$ in decimal representation (in base $10$) and fill in all missing bases in such a way that the second numbers in all pairs written on the walls, when converted from their bases to base $10$, are equal to $X$. For each pair with a missing base $(-1, Y)$, we may choose any integer base $B$ such that $2 ≤ B ≤ 36$ and $Y$ is a valid number in base $B$.

Since John does not want to deal with huge numbers, he considers $X > 10^{12}$ to be invalid. Still, there are too many possibilities, so he needs your help. Your task is to find the smallest common number $X$ that satisfies all conditions, or determine that no such common number exists.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
Each of the following $N$ lines contains an integer $B$ followed by a space and a string $Y$, describing a pair of numbers found on the walls; in the string $Y$, digits $10$ through $35$ (in base $10$) are represented by letters 'A' through 'Z'.

------  Output ------
For each test case, print a single line containing one integer ― the smallest possible common number $X$, or $-1$ if there is no valid common number.

------  Constraints ------
$1 ≤ T ≤ 200$
$1 ≤ N ≤ 100$
$2 ≤ B ≤ 36$ or $B = -1$
$Y$ contains only digits '0' through '9' and letters 'A' through 'Z'
for each pair $(B, Y)$ on the input, if $B \neq -1$, $Y$ is a valid number in base $B$
the length of string $Y$ is at most $40$

------  Subtasks ------
Subtask #1 (30 points): there is at least one pair $(B, Y)$ such that $B \neq -1$

Subtask #2 (70 points): original constraints

----- Sample Input 1 ------ 
2

3

-1 10000

8 20

-1 16

3

-1 10100

-1 5A

-1 1011010
----- Sample Output 1 ------ 
16

90
----- explanation 1 ------ 
Example case 1:
- $(10000)$ in base $2$ is $16$ in base $10$
- $(20)$ in base $8$ is $16$ in base $10$
- $(16)$ in base $10$ is $16$ in base $10$

Example case 2:
- $(10100)_3 = (90)_{10}$
- $(5\mathrm{A})_{16} = (90)_{10}$
- $(1011010)_2 = (90)_{10}$

Difficulty: HARD

Solutions:
import math

def get_x(base, string):
	x = 0
	for i in range(len(string)):
		char = string[-(i + 1)]
		try:
			value_of_char = int(char)
		except:
			value_of_char = int(9 + (ord(char) - 64))
		if value_of_char < base:
			x += math.pow(base, i) * value_of_char
		else:
			return -1
	return int(x)
t = int(input())
for _ in range(t):
	n = int(input())
	original_set = set()
	for j in range(n):
		(b, string) = input().split()
		b = int(b)
		set_of_possible_values = set()
		if b != -1:
			X = get_x(b, string)
			if X != -1 and X <= math.pow(10, 12):
				set_of_possible_values.add(X)
		else:
			for k in range(2, 37):
				X = get_x(k, string)
				if X != -1 and X <= math.pow(10, 12):
					set_of_possible_values.add(X)
		if not j:
			original_set = set_of_possible_values
			continue
		else:
			original_set = original_set.intersection(set_of_possible_values)
	if original_set:
		print(sorted(original_set)[0])
	else:
		print(-1)


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

def In():
	return int(input())

def func():
	n = In()
	arr = [input().split() for _ in range(n)]
	temp = [[] for _ in range(n)]
	for i in range(n):
		(b, y) = arr[i]
		b = int(b)
		if b != -1:
			p = int(y, b)
			if p <= 1000000000000.0:
				temp[i].append(p)
		else:
			for base in range(2, 37):
				try:
					p = int(y, base)
					if p <= 1000000000000.0:
						temp[i].append(p)
				except ValueError:
					pass
		if len(temp[i]) == 0:
			return -1
	for i in temp[0]:
		flag = 1
		for j in range(1, n):
			if i not in temp[j]:
				flag = 0
				break
		if flag:
			return i
	return -1

def main():
	for _ in range(In()):
		print(func())
	return
main()


def base(x, l):
	d = 0
	for i in range(len(l)):
		d = d + l[i] * x ** i
	return d
t = int(input())
for i in range(t):
	n = int(input())
	k = []
	for j in range(n):
		h = input()
		h = h.split(' ')
		l = []
		b = int(h[0])
		number = h[1]
		for z in range(len(number) - 1, -1, -1):
			try:
				c = int(number[z])
				l.append(c)
			except:
				c = ord(number[z]) - 55
				l.append(c)
		k.append([b, l])
	q = 0
	for j in range(len(k)):
		if k[j][0] != -1:
			q = 1
			a = base(k[j][0], k[j][1])
			break
	if q == 1:
		count = 0
		for j in range(len(k)):
			if k[j][0] == -1:
				m = max(k[j][1])
				for z in range(m + 1, 37):
					c = base(z, k[j][1])
					if c == a:
						count = count + 1
						break
			else:
				c = base(k[j][0], k[j][1])
				if c == a:
					count = count + 1
		if count == len(k):
			if a > 10 ** 12:
				print(-1)
			else:
				print(a)
		else:
			print(-1)
	else:
		h = []
		for j in range(len(k)):
			d = []
			m = max(k[j][1])
			for z in range(m + 1, 37):
				c = base(z, k[j][1])
				d.append(c)
			h.append(d)
		h = set.intersection(*map(set, h))
		h = list(h)
		h.sort()
		if len(h) > 0:
			if h[0] > 10 ** 12:
				print(-1)
			else:
				print(h[0])
		else:
			print(-1)


for _ in range(int(input())):
	n = int(input())
	l = []
	t = -1
	ans = 0
	for _ in range(n):
		(u, v) = input().split()
		l.append(v)
		if u != '-1':
			if t == -1:
				t = int(v, int(u))
			elif t != int(v, int(u)):
				ans = -1
	l.sort()
	l = set(l)
	l = list(l)
	if len(l) > 35:
		ans = -1
	m = []
	for i in l:
		k = max(i)
		if ord(k) >= 65:
			k = ord(k) - 54
		else:
			if int(k) == 0:
				k = 1
			k = int(k) + 1
		m.append(k)
	if t != -1 and ans != -1:
		ans = t
		num = 0
		for i in range(len(l)):
			fac = 0
			k = m[i]
			for j in range(k, 37):
				if t == int(l[i], j):
					fac = 1
			if fac == 0:
				ans = -1
				break
	if t == -1 and ans != -1:
		ans = -1
		k = m[0]
		for i in range(k, 37):
			imp = 0
			t = int(l[0], i)
			for i in range(1, len(l)):
				fac = 0
				k = m[i]
				for j in range(k, 37):
					if t == int(l[i], j):
						fac = 1
						break
				if fac == 0:
					imp = 1
					break
			if imp == 0:
				ans = t
				break
	if ans > pow(10, 12):
		ans = -1
	print(ans)


def func2(c):
	if c >= '0' and c <= '9':
		return ord(c) - ord('0')
	else:
		return ord(c) - ord('A') + 10

def func1(str, base):
	length = len(str)
	expo = 1
	num = 0
	for i in range(length - 1, -1, -1):
		if func2(str[i]) >= base:
			return -1
		num += func2(str[i]) * expo
		expo = expo * base
	return num
for _ in range(int(input())):
	n = int(input())
	query = []
	mat = []
	f = 0
	for i in range(n):
		(base, word) = list(map(str, input().split()))
		base = int(base)
		query.append([base, word])
	for i in query:
		temp = []
		if i[0] == -1:
			for j in range(1, 37):
				t1 = func1(i[1], j)
				temp.append(t1)
		else:
			temp.append(func1(i[1], i[0]))
		mat.append(temp)
	temp = mat[0]
	ans = -1
	final = []
	for i in temp:
		if i != -1:
			f = 0
			for j in mat:
				if i in j:
					pass
				else:
					f = 1
					break
			if f == 0:
				final.append(i)
	if final == []:
		print(-1)
	elif min(final) > 10 ** 12:
		print(-1)
	else:
		print(min(final))


import sys

def change_y_chars(ch):
	if ch.isupper():
		ch_new = str(10 + int(ord(ch) - ord('A')))
		return ch_new
	return ch

def cal_base10_val(b, y):
	m = len(y)
	val = 0
	flag = True
	for i in range(m):
		ch_y = change_y_chars(y[i])
		if int(ch_y) < b:
			val += b ** (m - i - 1) * int(ch_y)
		else:
			return -1
	return val
t = int(input())
while t:
	n = int(input())
	A = []
	set_list = []
	flag = False
	for i in range(n):
		(b, y) = input().split()
		b = int(b)
		A.append([b, y])
		set_X = set()
		if b == -1:
			for j in range(2, 37):
				val = cal_base10_val(j, y)
				if val != -1:
					set_X.add(val)
		else:
			flag = True
			a = cal_base10_val(b, y)
			if a <= 10 ** 12 and a != -1:
				set_X.add(a)
		if len(set_X):
			set_list.append(set_X)
		else:
			set_list.append(set([-1]))
	final_set = set_list[0]
	for i in range(1, len(set_list)):
		final_set = set_list[i].intersection(final_set)
	final_set = list(final_set)
	if len(final_set) and min(final_set) <= 10 ** 12:
		print(min(final_set))
	else:
		print(-1)
	t -= 1


def decimal(base, number):
	if number.isdigit() and int(number) == 0:
		return 0
	num = []
	for i in number:
		n = ord(i)
		num.append(n - 48 if n < 64 else n - 55)
	dec = 0
	size = len(num)
	for i in range(size):
		if num[size - i - 1] >= base:
			return None
		dec += num[size - i - 1] * base ** i
	return dec

def B_exists():
	dec = None
	for i in range(len(B)):
		if B[i] != -1:
			if dec == None:
				dec = decimal(B[i], Y[i])
			elif decimal(B[i], Y[i]) != dec:
				dec = -1
	return dec
(base_min, base_max) = (2, 36)
T = int(input())
for t in range(T):
	N = int(input())
	(B, Y) = ([], [])
	for n in range(N):
		(b, y) = input().split()
		b = int(b)
		B.append(b)
		Y.append(y)
	dec = B_exists()
	if dec == -1:
		print(-1)
	elif dec != None:
		for i in range(N):
			if B[i] != -1:
				continue
			base_found = False
			for base in range(base_min, base_max + 1):
				if dec == decimal(base, Y[i]):
					base_found = True
					break
			if not base_found:
				print(-1)
				break
		else:
			print(dec if dec <= 10 ** 12 else -1)
	else:
		dec_values = []
		for base in range(base_min, base_max + 1):
			dec = decimal(base, Y[0])
			if dec != None:
				dec_values.append(dec)
		for i in range(1, N):
			dec_temp = []
			for base in range(base_max, 1, -1):
				dec = decimal(base, Y[i])
				if dec != None and dec in dec_values:
					dec_temp.append(dec)
			dec_values = list(dec_temp)
		if len(dec_values) > 0:
			dec_values.sort()
			print(dec_values[0] if dec_values[0] <= 10 ** 12 else -1)
		else:
			print(-1)


from collections import Counter
lol = 10 ** 12
for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		(a, b) = input().split()
		a = int(a)
		z = []
		if a != -1:
			kkk = int(b, base=a)
			if kkk <= lol:
				l.append(kkk)
		else:
			for j in range(2, 37):
				try:
					zz = int(b, base=j)
					if zz <= lol:
						z.append(zz)
				except:
					vnj = 0
			l.extend(list(set(z)))
	dd = dict(Counter(l))
	ans = []
	for i in dd.keys():
		if dd[i] == n:
			ans.append(i)
	if ans:
		print(min(ans))
	else:
		print(-1)


value = {}
ch = '0'
for i in range(10):
	value[ch] = i
	ch = chr(ord(ch) + 1)
ch = 'A'
for i in range(10, 36):
	value[ch] = i
	ch = chr(ord(ch) + 1)
maxLimit = 10 ** 12

def base10(y, base):
	power = len(y) - 1
	b_10_n = 0
	for i in y:
		b_10_n += value[i] * base ** power
		power -= 1
		if b_10_n > maxLimit:
			return -1
	return b_10_n
for _ in range(int(input())):
	n = int(input())
	ans = {}
	final_ans = []
	for j in range(n):
		(b, y) = input().split()
		b = int(b)
		ans_set = set([])
		if b != -1:
			y_10 = base10(y, b)
			if y_10 < 0:
				continue
			if y_10 not in ans.keys():
				ans[y_10] = 1
			else:
				ans[y_10] += 1
		else:
			start = value[max(y)]
			for i in range(start + 1, 37):
				y_10 = base10(y, i)
				if y_10 < 0:
					continue
				if y_10 not in ans_set:
					ans_set.add(y_10)
					if y_10 not in ans.keys():
						ans[y_10] = 1
					else:
						ans[y_10] += 1
	for (key, values) in ans.items():
		if values == n:
			final_ans.append(key)
	if len(final_ans) == 0:
		print(-1)
	else:
		print(min(final_ans))


import string
d = {}
vari = string.digits + string.ascii_uppercase
d = {vari[i]: i for i in range(len(vari))}

def check(no):
	min_base = d[max(no)] + 1
	no = no[::-1]
	ret = set()
	for base in range(min_base, 37):
		cno = 0
		to_add = True
		for j in range(len(no)):
			cno += pow(base, j) * d[no[j]]
			if cno > 10 ** 12:
				to_add = False
				break
		if to_add:
			ret.add(cno)
	return ret
t = int(input())
for _ in range(t):
	n = int(input())
	for i in range(n):
		(b, y) = [x for x in input().strip().split()]
		b = int(b)
		if i == 0:
			if b != -1:
				cset = {int(y, b)}
			else:
				cset = check(y)
		elif b != -1 and cset:
			cset = cset.intersection({int(y, b)})
		elif cset:
			cset = cset.intersection(check(y))
	if cset and min(cset) <= 10 ** 12:
		print(min(cset))
	else:
		print(-1)


t = int(input())
for m in range(t):
	n = int(input())
	a = []
	mi = 100000000000000
	for u in range(n):
		l1 = list(input().split(' '))
		d = int(l1[0])
		e = l1[1]
		l = []
		di = {}
		if d != -1:
			s1 = 0
			flag = 0
			for i in range(len(e)):
				v1 = 0
				if e[i] >= 'A' and e[i] <= 'Z':
					v1 = ord(e[i]) - 55
				else:
					v1 = int(e[i])
				if v1 >= d:
					flag = 1
					break
				s1 = s1 + v1 * d ** (len(e) - i - 1)
				if s1 > 1000000000000:
					flag = 1
					break
			if flag == 0:
				l.append(s1)
		else:
			for i in range(2, 37):
				s1 = 0
				flag = 0
				for j in range(len(e)):
					v1 = 0
					if e[j] >= 'A' and e[j] <= 'Z':
						v1 = ord(e[j]) - 55
					else:
						v1 = int(e[j])
					if v1 >= i:
						flag = 1
						break
					s1 = s1 + v1 * i ** (len(e) - j - 1)
					if s1 > 1000000000000:
						flag = 1
						break
				if flag == 0:
					l.append(s1)
		set1 = set(l)
		for i in set1:
			a.append(i)
	for i in range(len(a)):
		if a[i] in di.keys():
			di[a[i]] += 1
		else:
			di[a[i]] = 1
		if di[a[i]] == n:
			mi = min(mi, a[i])
	if mi > 1000000000000:
		print(-1)
	else:
		print(mi)


def val(c):
	if c >= '0' and c <= '9':
		return ord(c) - ord('0')
	else:
		return ord(c) - ord('A') + 10

def it(str, base):
	llen = len(str)
	power = 1
	num = 0
	for i in range(llen - 1, -1, -1):
		if num > 1000000000000 or val(str[i]) >= base:
			return -1
		num += val(str[i]) * power
		power = power * base
	if num > 1000000000000:
		return -1
	return num

def tt(c):
	if ord(c) <= ord('9') and ord(c) >= ord('0'):
		return int(c)
	else:
		return 10 + ord(c) - ord('A')
for _ in range(int(input())):
	k = int(input())
	s1 = set()
	t = False
	for i in range(k):
		s2 = set()
		(a, s) = map(str, input().split())
		if t:
			continue
		a = int(a)
		if a == -1:
			for base in range(2, 37):
				p = it(s, base)
				if p == -1:
					continue
				if i == 0:
					s1.add(p)
				else:
					s2.add(p)
		elif i == 0:
			s1.add(it(s, a))
		else:
			s2.add(it(s, a))
		if i != 0:
			s1 = s1 & s2
			if len(s1) == 0:
				t = True
	if len(s1) == 0:
		print(-1)
	else:
		print(min(s1))


def maxi(num):
	max = 0
	for k in range(len(num)):
		ind = len(num) - k - 1
		if num[ind] >= 'A' and num[ind] <= 'Z':
			dig = ord(num[ind]) - 55
		else:
			dig = int(num[ind])
		if dig > max:
			max = dig
	return max

def decimal(num, b):
	power = 1
	nu = 0
	for k in range(len(num)):
		ind = len(num) - k - 1
		if num[ind] >= 'A' and num[ind] <= 'Z':
			dig = ord(num[ind]) - 55
		else:
			dig = int(num[ind])
		nu = nu + power * dig
		if nu > 1000000000000:
			return -1
		power = power * b
	return nu
t = int(input())
for _ in range(t):
	n = int(input())
	base = []
	number = []
	for i in range(n):
		(b, num) = input().split()
		b = int(b)
		base.append(b)
		number.append(num)
	start = 0
	lst = []
	flag = 0
	for i in range(n):
		b = base[i]
		num = number[i]
		new2 = set([])
		if b != -1:
			nu = decimal(num, b)
			new2.add(nu)
			lst.append(new2)
		else:
			mx = maxi(num)
			for k in range(mx + 1, 37):
				nu = decimal(num, k)
				if nu != -1:
					new2.add(nu)
			lst.append(new2)
	new1 = set.intersection(*lst)
	if len(new1) == 0:
		print('-1')
	else:
		print(min(new1))


T = int(input())
for _ in range(T):
	n = int(input())
	array = [[-1 for i in range(35)] for j in range(n)]
	for i in range(0, n):
		(b, y) = input().split()
		b = int(b)
		if b == -1:
			for j in range(2, 37):
				try:
					array[i][j - 2] = int(y, j)
				except:
					val = 'Invalid'
		else:
			array[i][0] = int(y, b)
	ans = 1000000000001
	for i in range(35):
		z = int(array[0][i])
		count = 0
		if z != -1 and z <= 1000000000000:
			for i in range(1, n):
				if z in array[i]:
					count += 1
				else:
					break
		if count == n - 1:
			ans = min(ans, z)
	if ans != 1000000000001:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	lis = [[-1 for i in range(35)] for j in range(n)]
	for i in range(0, n):
		(b, y) = map(str, input().split())
		b = int(b)
		if b == -1:
			for x in range(2, 37):
				try:
					lis[i][x - 2] = int(y, x)
				except:
					doNothing = 'Kuch mat kar'
		else:
			lis[i][0] = int(y, b)
	ans = 9999999999999999999999999999
	for i in range(35):
		z = int(lis[0][i])
		count = 0
		if z != -1 and z <= 1000000000000:
			for i in range(1, n):
				if z in lis[i]:
					count += 1
				else:
					break
		if count == n - 1:
			ans = min(ans, z)
	if ans != 9999999999999999999999999999:
		print(ans)
	else:
		print('-1')


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : [int(x) for x in rr().split()]

def validb(num, s):
	for i in range(2, 37):
		try:
			if int(s, i) == num:
				return True
		except:
			pass
	return False

def sol(n):
	if n == 1:
		x = rr().split()
		if int(x[0]) != -1:
			return int(x[1], int(x[0]))
		for i in range(2, 37):
			try:
				return int(x[1], i)
			except:
				pass
	lst = []
	for i in range(n):
		lst.append(rr().split())
	num = -1
	for s in lst:
		if int(s[0]) != -1:
			if num == -1:
				num = int(s[1], int(s[0]))
			elif num != int(s[1], int(s[0])):
				return -1
	if num != -1:
		for s in lst:
			if not validb(num, s[1]):
				return -1
		return num
	x = lst[0]
	y = lst[1]
	for i in range(2, 37):
		try:
			num = int(x[1], i)
			if validb(num, y[1]):
				bol = False
				for sj in lst:
					if not validb(num, sj[1]):
						bol = True
				if not bol:
					return num
		except ValueError:
			pass
	return -1
T = rri()
for _ in range(T):
	n = rri()
	ans = sol(n)
	if ans <= 10 ** 12:
		print(ans)
	else:
		print(-1)


t = input()
t = int(t)
bases = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35}
while t > 0:
	t -= 1
	n = int(input())
	ans = list()
	while n > 0:
		n -= 1
		(base, s) = map(str, input().split())
		base = int(base)
		possiblebase = 2
		min_base = possiblebase
		ls = list()
		if base == -1:
			for i in range(min_base, 37):
				try:
					k = int(s, i)
				except:
					continue
				if k <= 10 ** 12:
					ls.append(k)
				else:
					break
			ans.append(ls)
		elif base >= 2 and base <= 36:
			try:
				k = int(s, base)
				if k <= 10 ** 12:
					ans.append([k])
				else:
					ans.append([])
			except:
				ans.append([])
		else:
			ans.append([])
	l = len(ans)
	final_ans = 0
	if l < 1:
		final_ans = -1
	else:
		final = set(ans[0])
		for i in range(1, l):
			final = final & set(ans[i])
		if len(final) > 0:
			final = sorted(final)
			final_ans = final[0]
		else:
			final_ans = -1
	print(final_ans)


from functools import reduce
import string

def check(num, base, i=0):
	if i >= len(num):
		return True
	if not num[i].isdigit():
		val = string.ascii_lowercase.find(num[i].lower())
		if val == -1 or val + 10 >= base:
			return False
	elif int(num[i]) >= base:
		return False
	return check(num, base, i + 1)

def minbase(s):
	for i in range(2, 37):
		if check(s, i):
			return i
for _ in range(int(input())):
	n = int(input())
	L = []
	B = []
	Y = []
	for mb in range(n):
		(b, y) = map(str, input().split())
		B.append(int(b))
		Y.append(y)
	for ab in range(n):
		if B[ab] == -1:
			start = minbase(Y[ab])
		else:
			start = B[ab]
		l = []
		for ba in range(start, 37):
			l.append(int(Y[ab], ba))
		L.append(l)
	res = list(reduce(lambda p, q: p & q, (set(x) for x in L)))
	if len(res) > 0:
		k = min(res)
		if k <= 10 ** 12:
			print(k)
		else:
			print(-1)
	else:
		print(-1)


for _ in range(int(input())):
	l = []
	for __ in range(int(input())):
		(b, s) = input().split()
		b = int(b)
		z = max(s)
		if z >= 'A':
			minBase = ord(z) - ord('A') + 11
		else:
			minBase = ord(z) - ord('0') + 1
		minBase = max(2, minBase)
		if b != -1:
			l.append(set([int(s, b)]))
		else:
			l.append(set([int(s, x) for x in range(minBase, 37)]))
		intersection = l[0]
		for i in range(1, len(l)):
			intersection &= l[i]
	if len(intersection) == 1:
		for i in intersection:
			if i > 10 ** 12:
				print(-1)
			else:
				print(i)
	elif len(intersection) > 1:
		res = min(intersection)
		if res <= 10 ** 12:
			print(res)
		else:
			print(-1)
	else:
		print(-1)


def power(x, y):
	res = 1
	while y > 0:
		if y & 1 == 1:
			res = res * x
		y = y >> 1
		x = x * x
	return res
t = int(input())
for _ in range(t):
	l = []
	n = int(input())
	for i in range(n):
		s = input().split()
		s[1] = s[1][::-1]
		l.append(s)
	vv = []
	for i in l:
		v = set()
		flag = 0
		if i[0] == '-1':
			for b in range(2, 37):
				flag = 0
				ans = 0
				for k in range(0, len(i[1])):
					if ord(i[1][k]) - 48 >= 0 and ord(i[1][k]) - 48 <= 9:
						if ord(i[1][k]) - 48 >= b:
							flag = 1
							break
						ans = ans + int(i[1][k]) * power(b, k)
						if ans > int(1000000000000.0):
							flag = 1
							break
					else:
						if ord(i[1][k]) - 65 + 10 >= b:
							flag = 1
							break
						ans = ans + (ord(i[1][k]) - 65 + 10) * power(b, k)
						if ans > int(1000000000000.0):
							flag = 1
							break
				if ans <= int(1000000000000.0) and flag == 0:
					v.add(ans)
			vv.append(v)
		else:
			b = int(i[0])
			ans = 0
			flag = 0
			for k in range(0, len(i[1])):
				if ord(i[1][k]) - 48 >= 0 and ord(i[1][k]) - 48 <= 9:
					if ord(i[1][k]) - 48 >= b:
						flag = 1
						break
					ans = ans + int(i[1][k]) * power(b, k)
					if ans > int(1000000000000.0):
						flag = 1
						break
				else:
					if ord(i[1][k]) - 65 + 10 >= b:
						flag = 1
						break
					ans = ans + (ord(i[1][k]) - 65 + 10) * power(b, k)
					if ans > int(1000000000000.0):
						flag = 1
						break
			if ans <= int(1000000000000.0) and flag == 0:
				v.add(ans)
			vv.append(v)
	ans = vv[0]
	for i in range(1, len(vv)):
		ans = ans & vv[i]
	if len(ans) == 0:
		print(-1)
	else:
		print(min(ans))


while True:
	try:
		for _ in range(int(input())):
			n = int(input())
			(Temp, DP) = ({}, {})
			inputs = []
			for _ in range(n):
				(b, num) = input().split()
				inputs.append((int(b), num))
			inputs = list(set(inputs))
			n = len(inputs)
			for i in inputs:
				Temp = {}
				if i[0] == -1:
					minbase = max(i[1])
					minbase = max(int(minbase, 36) + 1, 2)
					for j in range(minbase, 37):
						if int(i[1], j) not in Temp:
							Temp[int(i[1], j)] = 1
						else:
							Temp[int(i[1], j)] += 1
					for (k, v) in Temp.items():
						if k not in DP:
							DP[k] = 1
						else:
							DP[k] += 1
				elif int(i[1], i[0]) not in DP:
					DP[int(i[1], i[0])] = 1
				else:
					DP[int(i[1], i[0])] += 1
			GotAns = False
			DP = sorted(DP.items(), key=lambda x: x[0])
			for (k, v) in DP:
				if v == n:
					if k > 1000000000000:
						break
					else:
						GotAns = True
						print(k)
						break
			if not GotAns:
				print(-1)
	except:
		break


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

def ai():
	a = list(map(int, input().split()))
	return a

def mi():
	a = map(int, input().split())
	return a
for _ in range(ii()):
	n = ii()
	ar = [[-1 for x in range(37)] for x in range(n)]
	for i in range(n):
		(b, y) = input().split()
		b = int(b)
		if b != -1:
			ar[i][b] = int(y, b)
		else:
			for j in range(2, 37):
				try:
					ar[i][j] = int(y, j)
				except:
					z = 0
	o = []
	for i in range(2, 37):
		cur_dec = ar[0][i]
		cnt = 0
		if cur_dec <= 1000000000000 and cur_dec != -1:
			for j in range(1, n):
				a = ar[j]
				if cur_dec in a:
					cnt += 1
				else:
					break
		if cnt == n - 1:
			o.append(cur_dec)
	if len(o) > 0:
		print(min(o))
	else:
		print(-1)


t = int(input())
while t > 0:
	n = int(input())
	l = []
	ans = -1
	for j in range(n):
		(base, k) = map(str, input().split())
		base = int(base)
		l3 = []
		if base != -1:
			ans = int(k, base)
			l.append(ans)
		else:
			for i in range(2, 37):
				try:
					l3.append(int(k, i))
				except:
					pass
		l3 = list(dict.fromkeys(l3))
		l.extend(l3)
	l2 = list(dict.fromkeys(l))
	l2.sort()
	b = False
	l.sort()
	for i in range(len(l2)):
		fin = l.count(l2[i])
		if fin == n:
			ans = l2[i]
			b = True
			break
	if b and ans <= 10 ** 12:
		print(ans)
	else:
		print(-1)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	ar = [[-1 for x in range(37)] for x in range(n)]
	for i in range(n):
		(b, y) = map(str, input().split())
		b = int(b)
		if b != -1:
			ar[i][b] = int(y, b)
		else:
			for j in range(2, 37):
				try:
					ar[i][j] = int(y, j)
				except:
					z = 0
	o = []
	for i in range(2, 37):
		y = ar[0][i]
		cnt = 0
		if y != -1 and y <= 1000000000000:
			for j in range(1, n):
				if y in ar[j]:
					cnt += 1
				else:
					break
		if cnt == n - 1:
			o.append(ar[0][i])
	if len(o) > 0:
		print(min(o))
	else:
		print('-1')


def add(y: str):
	start = 2
	end = 36
	power = 2
	s = set([])
	while power < end + 1:
		number = 0
		for ch in y:
			x = ord(ch) - ord('0')
			if x > 9:
				x = x - 7
			if x >= power:
				number = -1
				break
			else:
				number = number * power + x
		if number > 10 ** 12:
			return s
		if number != -1:
			s.add(number)
		power = power + 1
	return s

def cal(x: str, y: str):
	x = int(x)
	number = 0
	for ch in y:
		tt = ord(ch) - ord('0')
		if tt > 9:
			tt = tt - 7
		number = number * x + tt
	s = set()
	if number > 10 ** 12:
		return s
	s.add(number)
	return s
t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		(x, y) = map(str, input().split())
		if x[0] == '-':
			s = add(y)
			l.append(s)
		else:
			s = cal(x, y)
			l.append(s)
	s = l[0]
	for i in range(1, n):
		s = s & l[i]
	li = list(s)
	li.sort()
	if len(li) == 0:
		print(-1)
	else:
		print(li[0])


def val(c):
	if c >= '0' and c <= '9':
		return ord(c) - ord('0')
	else:
		return ord(c) - ord('A') + 10

def toDeci(str, base):
	llen = len(str)
	power = 1
	num = 0
	for i in range(llen - 1, -1, -1):
		num += val(str[i]) * power
		power = power * base
	return num

def horner(poly, n, x):
	result = poly[0]
	for i in range(1, n):
		result = result * x + poly[i]
	return result

def check(n, target):
	m = 1
	x = []
	for i in n:
		t = val(i)
		x.append(t)
		if t > m:
			m = val(i)
	for i in range(m + 1, 37):
		q = horner(x, len(x), i)
		if q == target:
			return True
		if q > target:
			return False
t = int(input())
p = pow(10, 12)
for _ in range(t):
	n = int(input())
	flag = 1
	stop = False
	numbers = []
	answer = -1
	for _ in range(n):
		(b, y) = input().split()
		b = int(b)
		if b != -1 and flag:
			flag = 0
			answer = toDeci(y, b)
		elif b != -1:
			if answer != toDeci(y, b):
				stop = True
		else:
			numbers.append(y)
	if stop:
		print(-1)
	elif answer != -1:
		for i in numbers:
			possible = check(i, answer)
			if not possible:
				stop = True
				break
		if stop:
			print(-1)
		elif answer > p:
			print(-1)
		else:
			print(answer)
	else:
		possibility = []
		for i in numbers:
			x = []
			temp = []
			m = 1
			for j in i:
				t = val(j)
				x.append(t)
				if t > m:
					m = val(j)
			for j in range(m + 1, 37):
				z = horner(x, len(x), j)
				if z > p:
					break
				temp.append(z)
			possibility.append(temp)
		printed = False
		for i in possibility[0]:
			flag = 1
			for j in range(1, len(numbers)):
				if not i in possibility[j]:
					flag = 0
					break
			if flag == 1:
				print(i)
				printed = True
				break
		if not printed:
			print(-1)


def recProgress(progress, final, A, S, value):
	if progress == final:
		return progress
	else:
		pos = progress
		base = A[pos][1]
		n_prog = -1
		for i in range(base, 37):
			if calculate(i, A[pos][0], S) == value:
				n_prog = recProgress(progress + 1, final, A, S, value)
				break
			if calculate(i, A[pos][0], S) > value:
				n_prog = -1
				break
		return n_prog

def calculate(X, Y, S):
	base = int(X)
	Y = str(Y)
	power = 0
	Value = 0
	Str = reversed(Y)
	for i in Str:
		Value += base ** power * S[i]
		power += 1
	return Value
T = int(input())
S = {}
for i in range(10):
	S[str(i)] = i
for i in range(26):
	S[chr(65 + i)] = 10 + i
while T > 0:
	T -= 1
	A = []
	found = []
	N = int(input())
	for i in range(N):
		(X, Y) = input().split()
		if X != '-1':
			Y = calculate(X, Y, S)
			found.append(Y)
		else:
			temp = list(Y)
			base = S[max(temp)] + 1
			A.append([Y, base])
	length = len(found)
	if length == 0:
		flag = False
		final = len(A)
		base = A[0][1]
		for i in range(base, 37):
			value = calculate(i, A[0][0], S)
			if value > 1000000000000:
				break
			progress = recProgress(1, final, A, S, value)
			if progress == final:
				flag = True
				break
		if flag == True:
			print(value)
		else:
			print('-1')
	if length >= 1:
		flag = True
		if length != 1:
			for i in range(length - 1):
				if found[i] != found[i + 1]:
					flag = False
		if flag == False:
			print('-1')
			continue
		value = found[0]
		final = len(A)
		if value > 1000000000000:
			progress = -1
		else:
			progress = recProgress(0, final, A, S, value)
		if progress == -1:
			print('-1')
		else:
			print(value)


t = int(input())
while t > 0:
	l = []
	l1 = []
	flag = 0
	n = int(input())
	res = []
	minus_c = 0
	for _ in range(0, n):
		(a, b) = input().split()
		l.append(int(a))
		l1.append(b)
	for i in range(0, n):
		lis = []
		res.append(lis)
	for i in range(0, n):
		if l[i] == -1:
			minus_c = minus_c + 1
			for j in range(2, 37):
				try:
					var = int(l1[i], j)
					if var <= 1000000000000:
						res[i].append(var)
				except:
					continue
		else:
			var = int(l1[i], l[i])
			if var <= 1000000000000:
				res[i].append(var)
	for i in range(0, len(res[0])):
		k = res[0][i]
		c = 0
		for j in range(0, n):
			if k in res[j]:
				c = c + 1
		if c == n:
			print(k)
			flag = 1
			break
	if flag == 0:
		print('-1')
	t = t - 1


t = int(input())
while t > 0:
	rl = []
	cl = []
	q = int(input())
	q1 = q
	hl = []
	while q > 0:
		(x, y) = input().split()
		rl.append(int(x))
		cl.append(y)
		q = q - 1
	for i in range(0, q1):
		ll = []
		hl.append(ll)
	for i in range(0, q1):
		x = rl[i]
		y = cl[i]
		if x != -1:
			kk = int(y, x)
			if kk <= 1000000000000:
				hl[i].append(kk)
		else:
			for j in range(2, 37):
				x = j
				try:
					kk = int(y, x)
					if kk <= 1000000000000:
						hl[i].append(kk)
				except:
					flag = 1
	flag = 0
	for i in range(0, len(hl[0])):
		k = hl[0][i]
		kc = 0
		for j in range(0, q1):
			if k in hl[j]:
				kc = kc + 1
		if kc == q1:
			print(k)
			flag = 1
			break
	if flag == 0:
		print(-1)
	t = t - 1


arr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
dic = {arr[i]: i + 1 for i in range(36)}
dic['0'] = 2
for t in range(int(input())):
	num = []
	n = int(input())
	for i in range(n):
		lis = list(map(str, input().split()))
		lis[0] = int(lis[0])
		num.append(lis)
	(x, y) = (0, 2)
	for i in range(n):
		if num[i][0] == -1:
			c = 2
			for j in num[i][1]:
				if dic[j] > c:
					c = dic[j]
			if y > c:
				y = c
				x = i
			num[i][0] = c
		if y < num[i][0]:
			y = num[i][0]
			x = i
	i = num[x][0]
	while i < 37:
		(l, j) = (int(num[x][1], i), 0)
		while j < n:
			(fs, ls) = (num[j][0], 36)
			while fs <= ls:
				m = (fs + ls) // 2
				l1 = int(num[j][1], m)
				if l == l1:
					break
				elif l < l1:
					ls = m - 1
				else:
					fs = m + 1
			if fs > ls:
				break
			j += 1
		if j == n:
			if l > 1000000000000:
				print(-1)
			else:
				print(l)
			break
		i += 1
	if i == 37:
		print(-1)


p = 1000000000000
for _ in range(int(input())):
	n = int(input())
	di = dict()
	fk = 0
	for w in range(n):
		(x, y) = input().split()
		x = int(x)
		l = len(y)
		d = set()
		if x == -1:
			b = 2
			while b <= 36:
				num = 0
				f = 0
				for i in range(l - 1, -1, -1):
					gt = 0
					if ord(y[i]) >= 65:
						gt = ord(y[i]) - ord('A') + 10
					else:
						gt = int(y[i])
					if gt >= b:
						f = 1
						break
					num += gt * pow(b, l - 1 - i)
					if num > p:
						f = 1
						break
				if not f:
					d.add(num)
				b += 1
		else:
			num = 0
			fk = 0
			for i in range(l - 1, -1, -1):
				gt = 0
				if ord(y[i]) >= 65:
					gt = ord(y[i]) - ord('A') + 10
				else:
					gt = int(y[i])
				if gt >= x:
					fk = 1
					break
				num += gt * pow(x, l - 1 - i)
				if num > p:
					fk = 1
					break
			if not fk:
				d.add(num)
		for i in d:
			if not di.get(i):
				di.update({i: 1})
			else:
				di[i] += 1
	mina = -1
	for (key, value) in di.items():
		if value == n:
			if mina == -1:
				mina = key
			mina = min(key, mina)
	print(mina)


def ToInteger(a):
	if a >= '0' and a <= '9':
		return ord(a) - 48
	elif a >= 'A' and a <= 'Z':
		return ord(a) - 55

def ToDecimal(a, b):
	base = 1
	val = 0
	for i in range(len(a) - 1, -1, -1):
		val = val + ToInteger(a[i]) * base
		base = base * b
	return val
t = int(input())
for test in range(t):
	B = []
	Y = []
	n = int(input())
	for i in range(n):
		l = input().split()
		B.append(int(l[0]))
		Y.append(l[1])
	valid_base = {}
	for i in range(n):
		if B[i] == -1:
			for j in Y[i]:
				m = ToInteger(j)
				Base = 1
				if Base < m:
					Base = m
			valid_base[i] = Base + 1
		else:
			valid_base[i] = B[i]
	X = []
	for i in range(n):
		if B[i] == -1:
			z = []
			for j in range(valid_base[i], 37):
				z.append(ToDecimal(Y[i], j))
			X.append(z)
		else:
			X.append([ToDecimal(Y[i], B[i])])
	for i in range(0, len(X[0])):
		flag = 1
		p = X[0][i]
		Z = p
		for j in range(1, len(X)):
			if p in X[j]:
				Z = X[0][i]
			else:
				flag = 0
				break
		if flag == 1:
			break
	if flag == 0:
		print(-1)
	elif Z > 1000000000000:
		print(-1)
	else:
		print(Z)


def ToInteger(a):
	bases = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35}
	return bases[a]

def ToDecimal(a, b):
	base = 1
	val = 0
	for i in range(len(a) - 1, -1, -1):
		val = val + ToInteger(a[i]) * base
		base = base * b
	return val
t = int(input())
for test in range(t):
	B = []
	Y = []
	n = int(input())
	for i in range(n):
		l = input().split()
		B.append(int(l[0]))
		Y.append(l[1])
	valid_base = {}
	for i in range(n):
		if B[i] == -1:
			for j in Y[i]:
				m = ToInteger(j)
				Base = 1
				if Base < m:
					Base = m
			valid_base[i] = Base + 1
		else:
			valid_base[i] = B[i]
	X = []
	for i in range(n):
		if B[i] == -1:
			z = []
			for j in range(valid_base[i], 37):
				z.append(ToDecimal(Y[i], j))
			X.append(z)
		else:
			X.append([ToDecimal(Y[i], B[i])])
	for i in range(0, len(X[0])):
		flag = 1
		p = X[0][i]
		Z = p
		for j in range(1, len(X)):
			if p in X[j]:
				Z = X[0][i]
			else:
				flag = 0
				break
		if flag == 1:
			break
	if flag == 0:
		print(-1)
	elif Z > 1000000000000:
		print(-1)
	else:
		print(Z)


for q in range(int(input())):
	n = int(input())
	t = 1
	c = 0
	while t <= n:
		l = list(input().split(' '))
		integer = []
		for i in l[1]:
			if i.isdigit():
				integer.append(int(i))
			else:
				number = ord(i) - 65 + 10
				integer.append(number)
		if int(l[0]) != -1:
			c = c + 1
			multiplicant = 1
			ans2 = []
			ans1 = 0
			for i in reversed(range(0, len(integer))):
				ans1 = ans1 + integer[i] * multiplicant
				multiplicant = multiplicant * int(l[0])
			ans2.append(ans1)
		else:
			c = c + 1
			ans2 = []
			multiplicant = 1
			ans1 = 0
			power = max(integer)
			for j in range(power + 1, 37):
				ans1 = 0
				multiplicant = 1
				for i in reversed(range(0, len(integer))):
					ans1 = ans1 + integer[i] * multiplicant
					multiplicant = multiplicant * j
				if ans1 <= 1000000000000:
					ans2.append(ans1)
				else:
					break
		if c == 1:
			answer = ans2[:]
		else:
			answer = list(set(ans2).intersection(set(answer)))
		t = t + 1
		answer.sort()
	if len(answer) > 0 and int(answer[0]) <= 1000000000000:
		print(int(answer[0]))
	else:
		print('-1')


def convertToDecimal(string, base):
	val = 0
	for i in range(len(string)):
		if string[i] in '0123456789':
			b = int(string[i])
		else:
			b = ord(string[i]) - 55
		val += b * base ** (len(string) - i - 1)
	return val

def allValues(string):
	decimals = []
	base = 2
	while base <= 36:
		val = 0
		for i in range(len(string)):
			if string[i] in '0123456789':
				b = int(string[i])
			else:
				b = ord(string[i]) - 55
			if b >= base:
				val = -1
				break
			val += b * base ** (len(string) - 1 - i)
		base += 1
		if val != -1:
			decimals.append(val)
	return decimals

def intersection(lst1, lst2):
	lst3 = [value for value in lst1 if value in lst2]
	lst3 = list(set(lst3))
	return lst3
for t in range(int(input())):
	bases = []
	nums = []
	for i in range(int(input())):
		(b, s) = input().split()
		bases.append(int(b))
		nums.append(s)
	n = len(bases)
	if n != 1:
		a = allValues(nums[0])
		b = allValues(nums[1])
		if bases[0] != -1:
			a = [convertToDecimal(nums[0], bases[0])]
		if bases[1] != -1:
			b = [convertToDecimal(nums[1], bases[1])]
		arr = intersection(a, b)
		for i in range(2, n):
			if len(arr) == 0:
				break
			if bases[i] == -1:
				arr = intersection(arr, allValues(nums[i]))
			else:
				arr = intersection(arr, [convertToDecimal(nums[i], bases[i])])
		arr.sort()
		if len(arr) == 0:
			print(-1)
		elif arr[0] <= 10 ** 12:
			print(arr[0])
		else:
			print(-1)
	else:
		arr = []
		if bases[0] == -1:
			arr = allValues(nums[0])
			if len(arr) == 0:
				print(-1)
			else:
				print(arr[0])
		else:
			print(convertToDecimal(nums[0], bases[0]))


def toAllBases(num):
	bases = []
	base = 2
	while base < 37:
		val = 0
		for i in range(len(num)):
			if num[i] in '0123456789':
				x = int(num[i])
			else:
				x = ord(num[i]) - 55
			if x >= base:
				val = -1
				break
			val += x * base ** (len(num) - 1 - i)
		base += 1
		if val != -1:
			bases.append(val)
	return bases

def toDecimal(base, num):
	val = 0
	for i in range(len(num)):
		if num[i] in '0123456789':
			x = int(num[i])
		else:
			x = ord(num[i]) - 55
		val += x * base ** (len(num) - 1 - i)
	return val

def findIntersection(arr1, arr2):
	arr = [value for value in arr1 if value in arr2]
	arr = list(set(arr))
	return arr
for T in range(int(input())):
	bases = []
	nums = []
	for i in range(int(input())):
		(base, num) = input().split()
		bases.append(int(base))
		nums.append(num)
	n = len(bases)
	if n != 1:
		x = toAllBases(nums[0])
		if bases[0] != -1:
			x = [toDecimal(bases[0], nums[0])]
		arr = x
		for i in range(1, n):
			if len(arr) == 0:
				break
			if bases[i] == -1:
				arr = findIntersection(arr, toAllBases(nums[i]))
			else:
				arr = findIntersection(arr, [toDecimal(bases[i], nums[i])])
		arr.sort()
		if len(arr) == 0:
			print(-1)
		elif arr[0] <= 10 ** 12:
			print(arr[0])
		else:
			print(-1)
	else:
		arr = []
		if bases[0] == -1:
			arr = toAllBases(nums[0])
			if len(arr) == 0:
				print(-1)
			else:
				print(arr[0])
		else:
			print(toDecimal(bases[0], nums[0]))


mod = 10 ** 12
T = int(input())
for t in range(T):
	oNum = []
	num = []
	count = 0
	n = int(input())
	for i in range(n):
		oNum.append(input())
	for i in range(n):
		num.append(oNum[i].split())
	for i in range(n):
		if num[i][0] != '-1':
			count = 1
			break
	if count == 1:
		s = num[i][1]
		op = 0
		abc = []
		for j in range(len(s)):
			if s[j] == '0' or s[j] == '1' or s[j] == '2' or (s[j] == '3') or (s[j] == '4') or (s[j] == '5') or (s[j] == '6') or (s[j] == '7') or (s[j] == '8') or (s[j] == '9'):
				abc.append(int(s[j]))
			else:
				abc.append(int(s[j], 36))
		for j in range(len(s)):
			power = int(num[i][0]) ** j
			op += abc[len(s) - 1 - j] * power
			if op > mod:
				break
		if op > mod:
			print(-1)
		else:
			tempCount = []
			for i in range(n):
				tempCount.append(0)
			for i in range(n):
				s = num[i][1]
				abc = []
				bat = 1
				for j in range(len(s)):
					if s[j] == '0' or s[j] == '1' or s[j] == '2' or (s[j] == '3') or (s[j] == '4') or (s[j] == '5') or (s[j] == '6') or (s[j] == '7') or (s[j] == '8') or (s[j] == '9'):
						if int(s[j]) > bat:
							bat = int(s[j])
						abc.append(int(s[j]))
					else:
						if int(s[j], 36) > bat:
							bat = int(s[j], 36)
						abc.append(int(s[j], 36))
				for k in range(bat + 1, 37):
					temp = 0
					for j in range(len(s)):
						power = k ** j
						temp += abc[len(s) - 1 - j] * power
						if temp > mod:
							break
					if temp == op:
						tempCount[i] = 1
						break
			clock = 0
			for i in range(n):
				if tempCount[i] != 1:
					clock = 1
					break
			if clock == 1:
				print(-1)
			else:
				print(op)
	else:
		valueArr = 35
		s = num[0][1]
		value = [[0 for i in range(2)] for j in range(36)]
		abc = []
		bat = 1
		for j in range(len(s)):
			if s[j] == '0' or s[j] == '1' or s[j] == '2' or (s[j] == '3') or (s[j] == '4') or (s[j] == '5') or (s[j] == '6') or (s[j] == '7') or (s[j] == '8') or (s[j] == '9'):
				if int(s[j]) > bat:
					bat = int(s[j])
				abc.append(int(s[j]))
			else:
				if int(s[j], 36) > bat:
					bat = int(s[j], 36)
				abc.append(int(s[j], 36))
		for k in range(2, bat + 1):
			value[k - 2][0] = -1
		for k in range(bat + 1, 37):
			temp = 0
			roughCount = 0
			for j in range(len(s)):
				power = k ** j
				temp += abc[len(s) - 1 - j] * power
				if temp > mod:
					roughCount = 1
					break
			if roughCount == 0:
				value[k - 2][0] = temp
			else:
				value[k - 2][0] = -1
		for i in range(1, n):
			for g in range(valueArr):
				value[g][1] = 0
			s = num[i][1]
			abc = []
			bat = 1
			for j in range(len(s)):
				if s[j] == '0' or s[j] == '1' or s[j] == '2' or (s[j] == '3') or (s[j] == '4') or (s[j] == '5') or (s[j] == '6') or (s[j] == '7') or (s[j] == '8') or (s[j] == '9'):
					if int(s[j]) > bat:
						bat = int(s[j])
					abc.append(int(s[j]))
				else:
					if int(s[j], 36) > bat:
						bat = int(s[j], 36)
					abc.append(int(s[j], 36))
			for k in range(bat + 1, 37):
				temp = 0
				for j in range(len(s)):
					power = k ** j
					temp += abc[len(s) - 1 - j] * power
					if temp > mod:
						break
				for l in range(valueArr):
					if temp == value[l][0] and value[l][0] != -1:
						value[l][1] = 1
			k = 0
			while k < valueArr:
				if value[k][1] == 0:
					value.pop(k)
					valueArr = valueArr - 1
				else:
					k += 1
		tempArr = []
		for g in range(valueArr):
			tempArr.append(value[g][0])
		countxyz = 0
		tempArr.sort()
		if valueArr > 0:
			for g in range(valueArr):
				if tempArr[g] >= 0 and tempArr[g] <= mod:
					print(tempArr[g])
					countxyz = 1
					break
			if countxyz == 0:
				print(-1)
		else:
			print(-1)


import sys
import socket
import select
import string
import itertools as itt
from sys import stdin, stdout
import functools
import numpy as np

def overlapping_elements(l):
	result = set(l[0])
	for s in l[1:]:
		result.intersection_update(s)
	return result

def valid_or_not(given_no, radix, i=0):
	if i >= len(given_no):
		return True
	if not given_no[i].isdigit():
		val = string.ascii_lowercase.find(given_no[i].lower())
		if val == -1 or val + 10 >= radix:
			return False
	elif int(given_no[i]) >= radix:
		return False
	return valid_or_not(given_no, radix, i + 1)
for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		(b, val) = map(str, input().split())
		f = []
		if b == '-1':
			for j in range(2, 37):
				if valid_or_not(val, j):
					kk = int(val, j)
					f.append(kk)
			l.append(f)
		else:
			b = int(b)
			kkk = int(val, b)
			f.append(kkk)
			l.append(f)
	xx = overlapping_elements(l)
	if len(xx) == 0:
		print(-1)
	elif min(xx) > 10 ** 12:
		print('-1')
	else:
		print(min(list(xx)))


d = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35}
mainLi = []
sumCal = -1

def calDec(x, y):
	sumDec = 0
	power = 1
	for i in range(len(y) - 1, -1, -1):
		s = int(d[y[i]])
		sumDec += s * power
		power = power * x
	return sumDec

def createList(x, y):
	del mainLi[:]
	for j in range(x, 37):
		a = calDec(j, y)
		if a <= 1000000000000:
			mainLi.append(a)
		else:
			break
	if len(mainLi) == 0:
		return -1
	else:
		return mainLi[0]

def checkSum(x, y):
	newLi = []
	for j in range(x, 37):
		a = calDec(j, y)
		if a <= 1000000000000:
			if a in mainLi:
				newLi.append(a)
	if len(newLi) == 0:
		return -1
	else:
		del mainLi[:]
		mainLi.extend(newLi)
		return mainLi[0]

def checkSumD(x, y):
	for i in range(x, 37):
		a = calDec(i, y)
		if a == sumCal:
			return 1
		elif a > sumCal:
			return 0
	return 0
t = int(input())
for _ in range(t):
	n = int(input())
	Nu = n
	sumCal = -1
	li = []
	sum1 = 0
	fc = 0
	f = 0
	for _ in range(n):
		(x, y) = map(str, input().split())
		x = int(x)
		if f != -1:
			if x == -1:
				maxB = int(d[y[0]])
				for i in range(len(y)):
					w = int(d[y[i]])
					if w > maxB:
						maxB = w
				if maxB < 2:
					maxB = 1
				li.append([maxB + 1, y])
			else:
				Nu -= 1
				if f == 0:
					sumCal = calDec(x, y)
					f = 1
					if sumCal > 1000000000000:
						f = -1
				else:
					sum1 = calDec(x, y)
					if sum1 != sumCal:
						f = -1
	if f != -1:
		if sumCal == -1:
			f12 = 0
			k = 0
			for i in range(Nu):
				if i == 0:
					k = createList(li[0][0], li[0][1])
				else:
					k = checkSum(li[i][0], li[i][1])
				if k == -1:
					f12 = 1
					print('-1')
					break
			if f12 == 0:
				print(k)
		else:
			for i in range(Nu):
				k = checkSumD(li[i][0], li[i][1])
				if k == 0:
					print('-1')
					fc = 1
					break
			if fc == 0:
				print(sumCal)
	else:
		print('-1')


def reVal(num):
	if num >= 0 and num <= 9:
		return chr(num + ord('0'))
	else:
		return chr(num - 10 + ord('A'))

def strev(strr):
	len = len(strr)
	for i in range(int(len / 2)):
		temp = strr[i]
		strr[i] = strr[len - i - 1]
		strr[len - i - 1] = temp

def fromDeci(res, base, inputNum):
	if inputNum == 0:
		return '0'
	else:
		index = 0
		while inputNum > 0:
			res += reVal(inputNum % base)
			inputNum = int(inputNum / base)
		res = res[::-1]
	return res

def val(c):
	if c >= '0' and c <= '9':
		return ord(c) - ord('0')
	else:
		return ord(c) - ord('A') + 10

def toDeci(strr, base):
	llen = len(strr)
	power = 1
	num = 0
	for i in range(llen - 1, -1, -1):
		if val(strr[i]) >= base:
			return -1
		num += val(strr[i]) * power
		power = power * base
	return num
for t in range(int(input())):
	Y = [] * 101
	F = [] * 36
	k = 0
	c = 0
	count = 0
	check = 0
	n = int(input())
	for i in range(n):
		(b, y) = input().split(' ')
		b = int(b)
		if b != -1:
			l = toDeci(y, b)
			if l == -1 and check == 0:
				check = 1
			count += 1
		Y.append(y)
		if k == 0 and b != -1:
			strr = y
			base = b
			k = 1
	if count > 0:
		z = toDeci(strr, base)
		if z > 1000000000000.0:
			print('-1')
		if z <= 1000000000000.0 and z > 0:
			if check == 1:
				print('-1')
			else:
				for i in range(2, 37):
					res = ''
					res = fromDeci(res, i, z)
					F.append(res)
				for r in Y:
					if r in F:
						c += 1
				if c == n:
					print(z)
				else:
					print('-1')
		if z == 0:
			if check == 1:
				print('-1')
			else:
				for i in range(2, 37):
					res = ''
					res = fromDeci(res, i, z)
					F.append(res)
				for r in Y:
					if r in F:
						c += 1
				if c == n:
					print(z)
				else:
					print('-1')
	if count == 0:
		ans = -1
		d = {}
		s = 0
		one = 0
		for w in Y:
			if w == '0':
				s += 1
		for dv in Y:
			if w == '1':
				one += 1
		if s == n:
			ans = 0
		else:
			for w in Y:
				Z = []
				for i in range(2, 37):
					result = toDeci(w, i)
					if result not in Z:
						Z.append(result)
						if result != -1:
							if result not in d:
								d[result] = 1
							else:
								d[result] += 1
			mini = 100000000000000
			bv = 0
			for v in d:
				if d[v] == n:
					bv += 1
					if v < mini:
						mini = v
			if bv == 0:
				mini = -1
			ans = mini
			if ans > 0:
				if ans > 1000000000000.0:
					ans = -1
		print(ans)


try:

	def mal(c):
		if c >= '0' and c <= '9':
			return ord(c) - ord('0')
		else:
			return ord(c) - ord('A') + 10

	def deciconver(strin, bas):
		lengt = len(strin)
		pow = 1
		numb = 0
		for i in range(lengt - 1, -1, -1):
			if mal(strin[i]) >= bas:
				return -1
			numb += mal(strin[i]) * pow
			pow = pow * bas
		if numb > 1000000000000:
			return -1
		return numb
	t = int(input())
	while t > 0:
		n = int(input())
		l = []
		for i in range(0, n):
			(x, y) = input().split()
			l.append((int(x), y))
		l1 = []
		l2 = []
		l3 = []
		if l[0][0] == -1:
			for i in range(2, 37):
				var = deciconver(l[0][1], i)
				if var != -1:
					l1.append(var)
		else:
			var = deciconver(l[0][1], l[0][0])
			if var != -1:
				l1.append(var)
		if n == 1:
			if len(l1) == 0:
				print('-1')
				continue
			else:
				print(l1[0])
				continue
		for i in range(1, n):
			if l[i][0] == -1:
				for j in range(2, 37):
					var = deciconver(l[i][1], j)
					if var != -1:
						l2.append(var)
			else:
				var = deciconver(l[i][1], l[i][0])
				if var != -1:
					l2.append(var)
			r = len(l1)
			s = len(l2)
			p = 0
			q = 0
			while p < r and q < s:
				if l1[p] < l2[q]:
					p += 1
				elif l1[p] > l2[q]:
					q += 1
				else:
					l3.append(l1[p])
					p += 1
					q += 1
			u = len(l3)
			if u == 0:
				print('-1')
				break
			l1 = l3.copy()
			l3.clear()
			l2.clear()
		if u != 0:
			print(l1[0])
		t -= 1
except:
	pass


t = int(input())

def convert_to_int(x):
	o = ord(x)
	if 48 <= o <= 57:
		return o - 48
	if 65 <= o <= 90:
		return o - 55

def convert_to_num(arr, base):
	n = len(arr)
	b = 1
	num = 0
	for i in range(n - 1, -1, -1):
		num += arr[i] * b
		b = b * base
	return num

def validate_base_b(arr, b):
	for num in arr:
		if num > b - 1:
			return False
	return True
for _ in range(t):
	n = int(input())
	sets = [set() for i in range(n)]
	for k in range(n):
		(b, y) = list(map(str, input().split()))
		b = int(b)
		y = [convert_to_int(x) for x in list(y)]
		if b != -1:
			sets[k].add(convert_to_num(y, b))
		else:
			for i in range(2, 37):
				if validate_base_b(y, i):
					sets[k].add(convert_to_num(y, i))
	s = sets[0]
	for i in range(1, n):
		s.intersection_update(sets[i])
	if len(s) == 0 or min(s) > pow(10, 12):
		print(-1)
	else:
		print(min(s))


dic = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35}
for _ in range(int(input())):
	ans = set()
	for n in range(int(input())):
		(b, y) = input().split(' ')
		b = int(b)
		y = list(y)
		for i in range(len(y)):
			y[i] = dic[y[i]]
		minbase = max(y) + 1
		mid = set()
		if b != -1:
			val = 0
			for i in range(len(y)):
				val = val * b + y[i]
			mid.add(val)
		else:
			for i in range(minbase, 37):
				val = 0
				for j in range(len(y)):
					val = val * i + y[j]
				mid.add(val)
		if n == 0:
			ans = ans.union(mid)
		else:
			ans = ans.intersection(mid)
	if len(ans) == 0:
		print(-1)
	else:
		abc = min(ans)
		if abc > 10 ** 12:
			print(-1)
		else:
			print(abc)


def f(num):
	original = -1
	if num[0][0] != -1:
		original = int(num[0][1], base=num[0][0])
		for i in num:
			if i[0] != -1:
				if int(i[1], base=i[0]) != original:
					return -1
			else:
				count = 0
				for j in range(2, 37):
					try:
						if int(i[1], base=j) == original:
							count += 1
							break
					except ValueError:
						pass
				if count == 0:
					return -1
		if original >= 1000000000000.0:
			return -1
		else:
			return original
	else:
		boom = []
		for k in range(2, 37):
			try:
				boom.append(int(num[0][1], base=k))
			except ValueError:
				pass
		for j in num:
			y = []
			for k in range(2, 37):
				try:
					y.append(int(j[1], base=k))
				except ValueError:
					pass
			boom = [value for value in y if value in boom]
			boom.sort()
			if len(boom) == 0:
				return -1
		if boom[0] > 1000000000000.0:
			return -1
		else:
			return boom[0]
	return original
for t in range(int(input())):
	n = int(input())
	num = []
	for i in range(n):
		(a, b) = input().split()
		a = int(a)
		num.append([a, b])
	num = sorted(num, key=lambda x: x[0], reverse=True)
	print(f(num))


for _ in range(int(input())):
	n = int(input())
	ls = [[-1 for i in range(37)] for i in range(n)]
	for i in range(n):
		(b, y) = map(str, input().split())
		m = int(b)
		if m != -1:
			ls[i][m] = int(y, m)
		else:
			for j in range(2, 37):
				try:
					ls[i][j] = int(y, j)
				except:
					z = 0
	arr = []
	for i in range(2, 37):
		y = ls[0][i]
		count = 0
		if y != -1 and y <= 1000000000000:
			for j in range(1, n):
				if y in ls[j]:
					count += 1
				else:
					break
		if count == n - 1:
			arr.append(ls[0][i])
	if len(arr) > 0:
		print(min(arr))
	else:
		print('-1')


MAXNUMBER = 1000000000000
Total_number_of_testcase = int(input())
for testcase in range(Total_number_of_testcase):
	number = input()
	number = int(number)
	counter = False
	result = 0
	my_list = []
	for index1 in range(number):
		(intermediate_number, string) = input().split()
		intermediate_number = int(intermediate_number)
		if intermediate_number != -1 and counter == False:
			if int(string, intermediate_number) > MAXNUMBER:
				result = -1
			else:
				counter = int(string, intermediate_number)
		elif intermediate_number != -1 and counter != False:
			if counter == int(string, intermediate_number):
				pass
			else:
				result = -1
		else:
			intermediate = [intermediate_number, string]
			my_list.append(intermediate)
	length_of_list = len(my_list)
	if result == -1:
		print(result)
	elif counter == False:
		new_set = set()
		for number2 in range(2, 37):
			try:
				temporary = int(my_list[0][1], number2)
				if temporary > MAXNUMBER:
					pass
				else:
					new_set.add(temporary)
			except ValueError:
				pass
		for index2 in range(1, length_of_list):
			another_set = set()
			for index3 in range(2, 37):
				try:
					temporary = int(my_list[index2][1], index3)
					if temporary > MAXNUMBER:
						pass
					else:
						another_set.add(temporary)
				except ValueError:
					pass
			new_set.intersection_update(another_set)
		if len(new_set) != 0:
			print(min(new_set))
		else:
			print(-1)
	else:
		answer = 0
		for index4 in my_list:
			for index5 in range(2, 37):
				try:
					if int(index4[1], index5) != counter:
						pass
					else:
						answer += 1
						break
				except ValueError:
					pass
		if answer != length_of_list:
			print(-1)
		else:
			print(counter)


t = int(input())
while t > 0:
	n = int(input())
	ans = []
	minimum = 10 ** 16
	for i in range(n):
		l = list(input().split(' '))
		x = int(l[0])
		y = l[1]
		lst = []
		d = {}
		if x == -1:
			for i in range(2, 37):
				s = 0
				flag = 0
				for j in range(len(y)):
					v = 0
					if y[j] >= 'A' and y[j] <= 'Z':
						v = ord(y[j]) - 55
					else:
						v = int(y[j])
					if v >= i:
						flag = 1
						break
					s = s + v * i ** (len(y) - j - 1)
					if s > 10 ** 12:
						flag = 1
						break
				if flag == 0:
					lst.append(s)
		else:
			s = 0
			flag = 0
			for i in range(len(y)):
				v = 0
				if y[i] >= 'A' and y[i] <= 'Z':
					v = ord(y[i]) - 55
				else:
					v = int(y[i])
				if v >= x:
					flag = 1
					break
				s = s + v * x ** (len(y) - i - 1)
				if s > 10 ** 12:
					flag = 1
					break
			if flag == 0:
				lst.append(s)
		s1 = set(lst)
		for i in s1:
			ans.append(i)
	for i in range(len(ans)):
		if ans[i] in d.keys():
			d[ans[i]] += 1
		else:
			d[ans[i]] = 1
		if d[ans[i]] == n:
			minimum = min(minimum, ans[i])
	if minimum > 10 ** 12:
		print(-1)
	else:
		print(minimum)
	t -= 1


def conversion(s, b):
	return int(s, b)
for _ in range(int(input())):
	N = int(input())
	mm = 1000000000000
	arr = []
	forward = True
	X = -1
	for _ in range(N):
		(b, s) = input().split()
		b = int(b)
		if b == -1:
			arr.append(s)
		if b != -1 and forward:
			x = conversion(s, b)
			if X == -1 and forward:
				X = x
			elif X != x or forward == False or x > mm:
				print(-1)
				forward = False
	if forward and len(arr) != N:
		flag = True
		for s in arr:
			flag2 = True
			for B in range(2, 37):
				try:
					x = conversion(s, B)
					if X == x and x <= mm:
						flag2 = False
						break
				except ValueError:
					continue
			if flag2:
				flag = False
				break
		if flag:
			print(X)
		else:
			print(-1)
	elif forward:
		mainFlag = False
		X = 0
		for i in range(2, 37):
			try:
				X = conversion(arr[0], i)
				flag = True
				for s in arr:
					flag2 = True
					for B in range(2, 37):
						try:
							x = conversion(s, B)
							if X == x and x <= mm:
								flag2 = False
								break
						except ValueError:
							continue
					if flag2:
						flag = False
						break
				if flag:
					mainFlag = True
					break
			except ValueError:
				continue
		if mainFlag:
			print(X)
		else:
			print(-1)


def base(b, pakoda):
	pakoda[0] = b
	return change(pakoda)

def change(amitabh):
	j = len(amitabh[1]) - 1
	c = 0
	gamma = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
	for k in amitabh[1]:
		if k in gamma:
			c += (ord(k) - 55) * int(amitabh[0]) ** j
		else:
			c += int(k) * int(amitabh[0]) ** j
		j -= 1
	return c

def amma(h):
	dadi = 1
	gamma = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
	for k in h:
		if k in gamma:
			if dadi < ord(k) - 55:
				dadi = ord(k) - 55
		elif dadi < int(k):
			dadi = int(k)
	return dadi + 1
for _ in range(int(input())):
	N = int(input())
	x = []
	ans = []
	quora = 0
	check = 0
	count = 0
	for i in range(N):
		x.append(str.split(input()))
		for i in x:
			if i[0] != '-1':
				l = change(i)
				if l not in ans:
					ans.append(l)
	quora = len(ans)
	if quora == 0:
		m = []
		count = N
		for i in x:
			maxx = amma(i[1])
			m.append(maxx)
		r = int(max(m))
		for i in x:
			maxx = amma(i[1])
			if maxx == r:
				ensure = i
		for i in range(r, 37):
			check = 0
			MYOUTPUT = base(i, ensure)
			for i in x:
				maxx = amma(i[1])
				for j in range(maxx, 37):
					q = base(j, i)
					if q > MYOUTPUT:
						break
					if q == MYOUTPUT:
						check += 1
			if check == count:
				break
	if quora == 1:
		MYOUTPUT = ans[0]
	if quora > 1 or MYOUTPUT > 10 ** 12:
		MYOUTPUT = -1
	if MYOUTPUT != -1:
		for i in x:
			if i[0] == '-1':
				maxx = amma(i[1])
				count += 1
				for j in range(maxx, 37):
					q = base(j, i)
					if q > MYOUTPUT:
						break
					elif q == MYOUTPUT:
						check += 1
						break
	if check < count:
		print('-1')
	else:
		print(MYOUTPUT)


test = input()
test = int(test)
for u in range(test):
	n = input()
	n = int(n)
	arr = [['-1' for a in range(37)] for b in range(n)]
	for a in range(n):
		(num, y) = map(str, input().split())
		num = int(num)
		if num != -1:
			arr[a][num] = int(y, num)
		else:
			for b in range(2, 37):
				try:
					arr[a][b] = int(y, b)
				except:
					qwery = 0
	arr2 = []
	for a in range(2, 37):
		e = arr[0][a]
		k = 0
		if int(e) != -1 and int(e) <= 1000000000000:
			for b in range(1, n):
				if e in arr[b]:
					k += 1
				else:
					break
		if k == n - 1:
			arr2.append(arr[0][a])
	if len(arr2) > 0:
		print(min(arr2))
	else:
		print('-1')


for _ in range(int(input())):
	no = int(input())
	miss = [['-1' for i in range(37)] for j in range(no)]
	for i in range(no):
		(num, y) = map(str, input().split())
		num = int(num)
		if num != -1:
			miss[i][num] = int(y, num)
		else:
			for j in range(2, 37):
				try:
					miss[i][j] = int(y, j)
				except:
					tatti = 0
	s = []
	for i in range(2, 37):
		ment = miss[0][i]
		k = 0
		if int(ment) != -1 and int(ment) <= 1000000000000:
			for j in range(1, no):
				if ment in miss[j]:
					k += 1
				else:
					break
		if k == no - 1:
			s.append(miss[0][i])
	if len(s) > 0:
		print(min(s))
	else:
		print('-1')


for _ in range(int(input())):
	n = int(input())
	lis = [['-1' for i in range(37)] for j in range(n)]
	for i in range(n):
		(num, y) = map(str, input().split())
		num = int(num)
		if num != -1:
			lis[i][num] = int(y, num)
		else:
			for j in range(2, 37):
				try:
					lis[i][j] = int(y, j)
				except:
					doSomething = 0
	lis[0].sort
	lis2 = []
	for i in range(2, 37):
		elem = lis[0][i]
		k = 0
		if int(elem) != -1 and int(elem) <= 1000000000000:
			for j in range(1, n):
				if elem in lis[j]:
					k += 1
				else:
					break
		if k == n - 1:
			lis2.append(lis[0][i])
	if len(lis2) > 0:
		print(min(lis2))
	else:
		print('-1')


def con_to_dec(string, base):
	val = 0
	for i in range(len(string)):
		if string[i] in '0123456789':
			b = int(string[i])
		else:
			b = ord(string[i]) - 55
		val += b * base ** (len(string) - i - 1)
	return val

def con_to_all_dec(string):
	alldecimals = []
	base = 2
	while base < 37:
		val = 0
		for i in range(len(string)):
			if string[i] in '0123456789':
				b = int(string[i])
			else:
				b = ord(string[i]) - 55
			if b >= base:
				val = -1
				break
			val += b * base ** (len(string) - 1 - i)
		base += 1
		if val != -1:
			alldecimals.append(val)
	return alldecimals

def intersection(lst1, lst2):
	lst3 = [value for value in lst1 if value in lst2]
	lst3 = list(set(lst3))
	return lst3
for t in range(int(input())):
	bases = []
	strings = []
	for i in range(int(input())):
		(b, s) = input().split()
		bases.append(int(b))
		strings.append(s)
	n = len(bases)
	if n != 1:
		a = con_to_all_dec(strings[0])
		b = con_to_all_dec(strings[1])
		if bases[0] != -1:
			a = [con_to_dec(strings[0], bases[0])]
		if bases[1] != -1:
			b = [con_to_dec(strings[1], bases[1])]
		arr = intersection(a, b)
		for i in range(2, n):
			if len(arr) == 0:
				break
			if bases[i] == -1:
				arr = intersection(arr, con_to_all_dec(strings[i]))
			else:
				arr = intersection(arr, [con_to_dec(strings[i], bases[i])])
		arr.sort()
		if len(arr) == 0:
			print(-1)
		elif arr[0] <= 10 ** 12:
			print(arr[0])
		else:
			print(-1)
	else:
		arr = []
		if bases[0] == -1:
			arr = con_to_all_dec(strings[0])
			if len(arr) == 0:
				print(-1)
			else:
				print(arr[0])
		else:
			print(con_to_dec(strings[0], bases[0]))


for opt in range(int(input())):
	number = int(input())
	ar = [[-1 for i in range(37)] for i in range(number)]
	for i in range(number):
		(base, y) = map(str, input().split())
		base = int(base)
		if base != -1:
			ar[i][base] = int(y, base)
		else:
			for j in range(2, 37):
				try:
					ar[i][j] = int(y, j)
				except:
					z = 0
	orde = []
	for i in range(2, 37):
		y = ar[0][i]
		cnt = 0
		if y != -1 and y <= 1000000000000:
			for j in range(1, number):
				if y in ar[j]:
					cnt += 1
				else:
					break
		if cnt == number - 1:
			orde.append(ar[0][i])
	if len(orde) > 0:
		print(min(orde))
	else:
		print('-1')


def val(c):
	if c >= '0' and c <= '9':
		return ord(c) - ord('0')
	else:
		return ord(c) - ord('A') + 10

def toDeci(str, base):
	llen = len(str)
	power = 1
	num = 0
	for i in range(llen - 1, -1, -1):
		if val(str[i]) >= base:
			return -1
		num += val(str[i]) * power
		power = power * base
	return num
t = int(input())
while t > 0:
	n = int(input())
	s = set()
	l2 = []
	p = []
	ans = 0
	n1 = n
	c1 = 0
	c2 = 0
	while n > 0:
		(B, Y) = map(str, input().split())
		B = int(B)
		if B != -1:
			c1 += 1
			x = toDeci(Y, B)
			if x != -1:
				s.add(x)
		else:
			c2 += 1
			for i in range(2, 37):
				x = toDeci(Y, i)
				if x != -1:
					l2.append(x)
			p.append(l2)
			l2 = []
		n -= 1
	if c1 == n1:
		if len(s) == 1:
			temp = list(s)
			if temp[0] <= 10 ** 12:
				print(temp[0])
			else:
				print(-1)
		else:
			print(-1)
	elif c2 == n1:
		ans = set(p[0])
		for i in p:
			ans = ans.intersection(i)
		if len(ans) == 0:
			print(-1)
		elif min(ans) <= 10 ** 12:
			print(min(ans))
		else:
			print(-1)
	else:
		ans = set(p[0])
		for i in p:
			ans = ans.intersection(i)
		if len(s) == 1:
			ans = ans.intersection(s)
			if len(ans) == 1:
				temp = list(ans)
				if temp[0] <= 10 ** 12:
					print(temp[0])
				else:
					print(-1)
			else:
				print(-1)
		else:
			print(-1)
	t -= 1


def minbase(Y):
	m = 0
	for j in range(0, len(Y)):
		if ord(Y[j]) >= 65:
			v = ord(Y[j]) - 55
			if v > m:
				m = v
		else:
			v = int(Y[j])
			if v > m:
				m = v
	return m + 1

def vue(B, Y):
	res = 0
	for j in range(0, len(Y)):
		if ord(Y[j]) >= 65:
			v = ord(Y[j]) - 55
			res += v * B ** (len(Y) - j - 1)
		else:
			v = int(Y[j])
			res += v * B ** (len(Y) - j - 1)
	return res
te = int(input())
for j in range(0, te):
	N = int(input())
	B1 = []
	B2 = []
	Y1 = []
	Y2 = []
	for i in range(0, N):
		a2 = input()
		a1 = a2.split()
		if int(a1[0]) != -1:
			B1.append(int(a1[0]))
			Y1.append(a1[1])
		else:
			B2.append(int(a1[0]))
			Y2.append(a1[1])
	r = -1
	flag1 = 0
	b = 'True'
	for j in range(0, len(B1)):
		v = vue(B1[j], Y1[j])
		if j == 0:
			r = v
		if j > 0:
			if r == v:
				continue
			else:
				r = -1
				b = 'False'
				break
	if b == 'True':
		if r != -1:
			b1flag = 'True'
			for k in range(0, len(B2)):
				if b1flag == 'False':
					break
				m = minbase(Y2[k])
				for l in range(m, 37):
					v = vue(l, Y2[k])
					if r == v:
						break
					elif l == 36:
						r = -1
						b1flag = 'False'
						break
		else:
			m = minbase(Y2[0])
			k = 0
			for m in range(m, 38):
				if k == 1:
					break
				if m == 37:
					r = -1
					break
				r = vue(m, Y2[0])
				if len(Y2) == 1:
					break
				b1flag = 'True'
				for k in range(1, len(Y2)):
					if b1flag == 'False':
						break
					m = minbase(Y2[k])
					for l in range(m, 37):
						v = vue(l, Y2[k])
						if r == v:
							if k == len(Y2) - 1:
								k = 1
							break
						elif l == 36:
							b1flag = 'False'
							break
	if r > 10 ** 12:
		print('-1')
	else:
		print(r)


d = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35}

def solve(a, rolf):
	n = len(rolf)
	c = 1
	lol = 0
	for i in range(n - 1, -1, -1):
		lol += d[rolf[i]] * c
		c *= a
	return lol

def solo(ls, k):
	number = 0
	for i in ls:
		c = -1
		mon = 1
		for j in i:
			mon = max(mon, d[j])
		for j in range(mon + 1, 37):
			x = solve(j, i)
			if x == k:
				c = 0
				break
		if c == -1:
			return False
	return True

def group(ls):
	a = []
	rolf = ls[0]
	mon = 1
	for j in rolf:
		mon = max(mon, d[j])
	for j in range(mon + 1, 37):
		x = solve(j, rolf)
		a.append(x)
	n = len(ls)
	for i in range(1, n):
		mon = 1
		b = []
		rolf = ls[i]
		for j in rolf:
			mon = max(mon, d[j])
		for j in range(mon + 1, 37):
			x = solve(j, rolf)
			if x in a:
				b.append(x)
		if len(b) == 0:
			return -1
		a = b
	a.sort()
	return a[0]
for _ in range(int(input())):
	n = int(input())
	ls = []
	oye = 0
	k = -1
	for _ in range(n):
		(a, b) = input().split()
		if int(a) != -1:
			k1 = solve(int(a), b)
			if k == -1:
				k = k1
			elif k != k1:
				oye = -1
		else:
			ls.append(b)
	if oye == -1 or k > 1000000000000:
		print(-1)
	elif len(ls) == n:
		k = group(ls)
		if k > 1000000000000:
			print(-1)
		else:
			print(k)
	elif solo(ls, k):
		print(k)
	else:
		print(-1)


def convert(s, base):
	c = 1
	d = 0
	m = 0
	for x in reversed(s):
		if ord(x) > 64:
			m = ord(x) - 55
		else:
			m = int(x)
		d = d + m * c
		c *= base
	return d

def check(a, b, res, start, n, k):
	f = 1
	for i in range(start, n):
		if i != k:
			f2 = 0
			if a[i] == -1:
				m = max(b[i])
				if ord(m) > 64:
					m = ord(m) - 55
				else:
					m = int(m)
				for j in range(m + 1, 37):
					dec = convert(b[i], j)
					if dec == res:
						f2 = 1
						break
					elif dec > res:
						f = 0
						break
			else:
				dec = convert(b[i], a[i])
				if dec == res:
					f2 = 1
				else:
					f = 0
			if not f2:
				f = 0
		if not f:
			return 0
	return f
t = int(input())
while t:
	t -= 1
	n = int(input())
	res = -1
	k = -1
	a = [0 for x in range(n)]
	b = []
	for i in range(n):
		(x, y) = map(str, input().split())
		x = int(x)
		a[i] = x
		b.append(y)
		if x != -1:
			res = convert(y, x)
			k = i
	f = 1
	if res > 1000000000000:
		print(-1)
		continue
	if k != -1:
		f = check(a, b, res, 0, n, k)
		if f:
			print(res)
		else:
			print(-1)
	elif n == 1:
		dec = convert(b[0], 2)
		print(dec)
	else:
		comm = []
		d = {}
		m = max(b[0])
		if ord(m) > 64:
			m = ord(m) - 55
		else:
			m = int(m)
		for j in range(m + 1, 37):
			dec = convert(b[0], j)
			if dec <= 1000000000000:
				d[dec] = 1
		m = max(b[1])
		if ord(m) > 64:
			m = ord(m) - 55
		else:
			m = int(m)
		for j in range(m + 1, 37):
			dec = convert(b[1], j)
			if dec in d:
				comm.append(dec)
		f = 0
		for res in comm:
			f = check(a, b, res, 2, n, 0)
			if f:
				print(res)
				break
		if not f:
			print(-1)


def check1(aim, Num):
	(Min, Max) = (2, 37)
	while Min != Max:
		Aveg = (Min + Max) // 2
		try:
			St = int(Num, base=Aveg)
			if St == aim:
				return Aveg
			elif St < aim:
				Min = Aveg + 1
			else:
				Max = Aveg
		except:
			Min = Aveg + 1
	return False

def check(x):
	X = set()
	for i in range(36, 1, -1):
		try:
			t = int(x, base=i)
			if t > 10 ** 12:
				continue
			X.add(t)
		except:
			break
	return X

def solve1(known1, unknown1):
	loc_known = known1
	if len(loc_known) >= 2:
		return -1
	common = loc_known.pop()
	if common > 10 ** 12:
		return -1
	for i in unknown1:
		if not check1(common, i):
			return -1
	return common

def solve2(unknown1):
	loc_unkn = set()
	loc_unkn = check(unknown1.pop())
	for i in unknown1:
		loc_unkn &= check(i)
	return min(loc_unkn) if loc_unkn else -1
for _ in range(int(input())):
	N = int(input())
	(known, unknown) = (set(), set())
	for _ in range(N):
		(Base, Number) = input().split()
		if Base != '-1':
			known.add(int(Number, base=int(Base)))
		else:
			unknown.add(Number)
	ans = solve1(known, unknown) if known else solve2(unknown)
	print(ans)


def val(c):
	if c >= '0' and c <= '9':
		return ord(c) - ord('0')
	else:
		return ord(c) - ord('A') + 10

def convert(s, base):
	l = len(s)
	power = 1
	num = 0
	i = l - 1
	while i >= 0:
		if val(s[i]) >= base:
			return -1
		num = num + val(s[i]) * power
		if num > 1000000000000:
			return -1
		power = power * base
		i = i - 1
	return num
t = int(input())
for it in range(t):
	m = {}
	l = {}
	n = int(input())
	b = [0] * n
	s = [0] * n
	i = 0
	while i < n:
		(b[i], s[i]) = input().split()
		b[i] = int(b[i])
		i = i + 1
	i = 0
	f = 0
	while i < n:
		if b[i] == -1:
			f = f + 1
		i = i + 1
	if f == n:
		i = 0
		while i < n:
			j = 2
			while j <= 36:
				x = convert(s[i], j)
				if x != -1 and x in l.keys():
					j = j + 1
					continue
				if x != -1:
					if x in m.keys():
						m[x] = m[x] + 1
					else:
						m[x] = 1
					l[x] = 1
				j = j + 1
			l = {}
			i = i + 1
		flag = 0
		for iz in sorted(m):
			if m[iz] == n:
				flag = flag + 1
				ans = iz
				break
		if flag == 1:
			print(ans)
		else:
			print(-1)
	else:
		i = 0
		while i < n:
			if b[i] != -1:
				x2 = convert(s[i], b[i])
				break
			i = i + 1
		i = 0
		zz = 0
		while i < n:
			if b[i] != -1:
				x3 = convert(s[i], b[i])
				if x3 != x2 or x2 == -1 or x3 == -1:
					zz = 1
					break
			i = i + 1
		if zz == 1:
			print(-1)
		else:
			i = 0
			g = 0
			while i < n:
				if b[i] == -1:
					y = 0
					j = 2
					while j <= 36:
						x3 = convert(s[i], j)
						if x3 == x2 and x3 != -1 and (x2 != -1):
							y = 1
							break
						j = j + 1
					if y == 0:
						g = 1
						break
				i = i + 1
			if g == 1:
				print(-1)
			else:
				print(x2)


import math

def count(s, c):
	res = 0
	for i in range(len(s)):
		if s[i] == c:
			res = res + 1
	return res
for _ in range(int(input())):
	arr = []
	arr2 = []
	m = 0
	di = {}
	(v1, v2) = (0, 0)
	n = int(input())
	for itr in range(n):
		arr = []
		arr2 = []
		(b, st) = map(str, input().split())
		b = int(b)
		c = '0'
		res = count(st, c)
		v1 = v1 + res
		v2 = v2 + len(st)
		a = ord(st[0])
		m = 0
		for i in range(len(st)):
			if a < ord(st[i]):
				a = ord(st[i])
		if a >= 65:
			a = a - 55
		else:
			a = a - 48
		a = a + 1
		limit = 36
		if b != -1:
			a = b
			limit = b
		if len(st) == 1:
			a = 0
			limit = 0
		for i in range(a, limit + 1):
			s = 0
			power = 0
			flag = 0
			for j in range(len(st), 0, -1):
				ch = ord(st[j - 1])
				if ch >= 65:
					ch = ch - 55
				else:
					ch = ch - 48
				s = s + ch * math.pow(i, power)
				power = power + 1
				if s > 1000000000000:
					flag = 1
					break
			if flag == 0:
				arr.append(s)
		for i in range(len(arr)):
			if arr[i] in di.keys():
				di[arr[i]] = di[arr[i]] + 1
			else:
				di[arr[i]] = 1
		pos = 0
		for item in di.items():
			if item[1] == n:
				arr2.append(item[0])
				pos = pos + 1
	if pos != 0:
		arr2.sort()
		print(int(arr2[0]))
	elif v1 == v2:
		print(0)
	else:
		print(-1)


from collections import Counter

def val(c):
	if c >= '0' and c <= '9':
		return ord(c) - ord('0')
	else:
		return ord(c) - ord('A') + 10

def get_mini_base(s):
	d1 = Counter(s)
	curr = -1
	for key in d1:
		if val(key) > curr:
			curr = val(key)
	return curr + 1

def toDeci(str, base):
	llen = len(str)
	power = 1
	num = 0
	for i in range(llen - 1, -1, -1):
		if val(str[i]) >= base:
			return -1
		num += val(str[i]) * power
		power = power * base
	return num
T = int(input())
while T > 0:
	N = int(input())
	bases = []
	numbers = []
	ans_found = False
	final_ans = 0
	sure_shot_ans = 0
	for i in range(0, N):
		arr = [j for j in input().split()]
		B = int(arr[0])
		Y = str(arr[1])
		bases.append(B)
		numbers.append(Y)
	curr_possible = []
	for i in range(0, N):
		if bases[i] != -1:
			dec_num = toDeci(numbers[i], bases[i])
			if dec_num == -1:
				final_ans = -1
				break
			else:
				curr_possible.append(dec_num)
	d = Counter(curr_possible)
	if len(d) != 0:
		if final_ans != -1:
			if len(d) != 1:
				final_ans = -1
			else:
				final_ans = curr_possible[0]
				for i in range(0, N):
					if bases[i] == -1:
						for j in range(get_mini_base(numbers[i]), 37):
							gfh = toDeci(numbers[i], j)
							if gfh != -1 and gfh in curr_possible:
								final_ans = gfh
								break
							else:
								final_ans = -1
						if final_ans == -1:
							break
		if final_ans > 10 ** 12:
			final_ans = -1
		print(final_ans)
	else:
		curr_pos = []
		b = get_mini_base(numbers[0])
		for i in range(b, 37):
			dec = toDeci(numbers[0], i)
			if dec != -1:
				curr_pos.append(dec)
		d1 = Counter(curr_pos)
		for i in range(1, N):
			temp = []
			b = get_mini_base(numbers[i])
			for j in range(b, 37):
				dec = toDeci(numbers[i], j)
				if dec != -1:
					temp.append(dec)
			d2 = Counter(temp)
			d1 = d1 & d2
		curr_min = 10 ** 12 + 1
		for key in d1:
			if d1[key] > 0 and key < curr_min:
				curr_min = key
		if curr_min > 10 ** 12:
			curr_min = -1
		print(curr_min)
	T -= 1


def bcon(n):
	return int(n)

def dicon(n):
	l = []
	for j in n:
		if j.isalpha():
			l.append(ord(j) - ord('A') + 10)
		else:
			l.append(int(j))
	return l

def con(base, d):
	k = 0
	p = 0
	for i in reversed(d):
		k += i * base ** p
		p = p + 1
	return k

def find(b, d):
	l = []
	if b != -1:
		t = con(b, d)
		return [t]
	s = max(d) + 1
	ls = 37
	for j in range(s, ls):
		t = con(j, d)
		if t > 10 ** 12:
			break
		l.append(t)
	return l

def intersect(x, y):
	return x.intersection(set(y))
test = int(input())
while test > 0:
	test = test - 1
	n = int(input())
	val = []
	for i in range(n):
		(base, st) = input().split()
		d = dicon(st)
		b = bcon(base)
		val.append(find(b, d))
	l = set(val[0])
	for i in range(1, n):
		l = intersect(l, val[i])
	l = list(l)
	if len(l) > 0:
		minm = min(l)
		if minm > 10 ** 12:
			print(-1)
		else:
			print(minm)
	else:
		print(-1)


def is_int(s, b):
	try:
		int(s, b)
		return True
	except ValueError:
		return False
t = int(input())
for _ in range(t):
	n = int(input())
	inp = [input().split() for _ in range(n)]
	bases = [int(i[0]) for i in inp]
	nums = [i[1] for i in inp]
	free = True
	for i in range(n):
		if bases[i] != -1:
			free = int(nums[i], bases[i])
			break
	if free == True:
		cool = True
		for base in range(2, 37):
			cool = True
			if not is_int(nums[0], base):
				continue
			num = int(nums[0], base)
			if num > 10 ** 12:
				cool = False
				break
			for i in range(1, n):
				can = False
				for bas in range(2, 37):
					if is_int(nums[i], bas) and int(nums[i], bas) == num:
						can = True
						break
				if not can:
					cool = False
					break
			if cool:
				print(num)
				break
		if not cool:
			print(-1)
	else:
		cool = True
		for i in range(n):
			if bases[i] == -1:
				can = False
				for bas in range(2, 37):
					if is_int(nums[i], bas) and int(nums[i], bas) == free:
						can = True
						break
				if not can:
					cool = False
					break
			elif int(nums[i], bases[i]) != free:
				cool = False
				break
		if cool and free <= 10 ** 12:
			print(free)
		else:
			print(-1)


def check(num, string):
	rrr = max([dic[i] for i in string]) + 1
	for i in range(rrr, 37):
		anss = number(i, string)
		if anss == -1:
			return False
		if anss == num:
			return True
	return False

def number(base, string):
	l = len(string)
	ans = 0
	for i in range(l):
		ans += dic[string[-(1 + i)]] * base ** i
		if ans > 10 ** 12:
			ans = -1
			return ans
	return ans
dic = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35}
t = int(input())
for i in range(t):
	n = int(input())
	temp = [[] for i in range(n)]
	for i in range(n):
		(b, y) = [i for i in input().split()]
		temp[i].append(int(b))
		temp[i].append(y)
	ans = -1
	if temp[0][0] == -1:
		rrr = max([dic[i] for i in temp[0][1]]) + 1
		ii = rrr
		while ii < 37:
			num = number(ii, temp[0][1])
			if num == -1:
				ans = -1
				break
			j = 1
			while j < n:
				if temp[j][0] == -1:
					tt = check(num, temp[j][1])
					if tt == False:
						break
					j += 1
				else:
					tt = number(temp[j][0], temp[j][1])
					if num != tt:
						break
					j += 1
			if j == n:
				ans = num
				break
			ii += 1
		if ii == 37:
			ans = -1
		print(ans)
	else:
		num = number(temp[0][0], temp[0][1])
		j = 1
		if num == -1:
			ans = -1
		while j < n:
			if temp[j][0] == -1:
				tt = check(num, temp[j][1])
				if tt == False:
					break
				j += 1
			else:
				tt = number(temp[j][0], temp[j][1])
				if num != tt:
					break
				j += 1
		if j == n and num != -1:
			ans = num
		print(ans)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	num = n
	c = 0
	m = defaultdict(lambda : 0)
	for i in range(n):
		k = 0
		max = 1
		xs = input().split()
		x = int(xs[0])
		s = str(xs[1])
		for OO in range(len(s)):
			if ord(s[OO]) >= 48 and ord(s[OO]) <= 57:
				k = ord(s[OO]) - 48
			else:
				k = ord(s[OO]) - 55
			if k + 1 > max:
				max = k + 1
		if x != -1:
			max = x
		for j in range(max, 37):
			ans = 0
			r = 0
			for _ in range(len(s) - 1, -1, -1):
				if ord(s[_]) >= 48 and ord(s[_]) <= 57:
					k = ord(s[_]) - 48
				else:
					k = ord(s[_]) - 55
				z = pow(j, r)
				if z > 10 ** 12:
					ans = -1
					break
				ans = ans + z * k
				r += 1
				if ans > 10 ** 12:
					break
			if ans <= 10 ** 12 and ans != -1 and (m[ans] == c):
				m[ans] += 1
		c += 1
	q = -1
	for (i, j) in m.items():
		if j == num:
			q = i
			break
	print(q)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [0] * n
	b = [0] * n
	for i in range(n):
		(a[i], b[i]) = map(str, input().split())
		a[i] = int(a[i])
	a1 = set()
	a2 = set()
	for i in range(n):
		a2.clear()
		if a[i] != -1:
			y = int(b[i], base=a[i])
			if i == 0:
				a2.add(y)
			elif y in a1:
				a2.add(y)
		else:
			for k in range(2, 37):
				try:
					y = int(b[i], base=k)
					if i == 0:
						a2.add(y)
					elif y in a1:
						a2.add(y)
				except ValueError:
					pass
		a1 = a2.copy()
	if len(a1) == 0:
		print(-1)
	else:
		x = min(a1)
		if x >= 0 and x <= 1000000000000:
			print(x)
		else:
			print(-1)