Berland shop sells $n$ kinds of juices. Each juice has its price $c_i$. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice can contain one, two or all three types of vitamins in it.

Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.


-----Input-----

The first line contains a single integer $n$ $(1 \le n \le 1\,000)$ — the number of juices.

Each of the next $n$ lines contains an integer $c_i$ $(1 \le c_i \le 100\,000)$ and a string $s_i$ — the price of the $i$-th juice and the vitamins it contains. String $s_i$ contains from $1$ to $3$ characters, and the only possible characters are "A", "B" and "C". It is guaranteed that each letter appears no more than once in each string $s_i$. The order of letters in strings $s_i$ is arbitrary.


-----Output-----

Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.


-----Examples-----
Input
4
5 C
6 B
16 BAC
4 A

Output
15

Input
2
10 AB
15 BA

Output
-1

Input
5
10 A
9 BC
11 CA
4 A
5 B

Output
13

Input
6
100 A
355 BCA
150 BC
160 AC
180 B
190 CA

Output
250

Input
2
5 BA
11 CB

Output
16



-----Note-----

In the first example Petya buys the first, the second and the fourth juice. He spends $5 + 6 + 4 = 15$ and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is $16$, which isn't optimal.

In the second example Petya can't obtain all three vitamins, as no juice contains vitamin "C".

Difficulty: EASY

Solutions:
def minv(a, b=0, c=0):
	if a | b | c != 7:
		return mins
	s = opts[a] + opts[b] + opts[c]
	return min(mins, s)
opts = [1000000] * 8
opts[0] = 0
for _ in range(int(input())):
	(c, l) = input().split()
	(v, c) = (0, int(c))
	if 'A' in l:
		v += 1
	if 'B' in l:
		v += 2
	if 'C' in l:
		v += 4
	opts[v] = min(opts[v], c)
mins = 10000000
for a in range(1, 8):
	mins = minv(a)
	for b in range(a + 1, 8):
		mins = minv(a, b)
		for c in range(b + 1, 8):
			mins = minv(a, b, c)
print(-1 if mins >= 1000000 else mins)


import math

def bitadd(a, b):
	c = ['0', '0', '0']
	for i in range(3):
		if a[i] == '1' or b[i] == '1':
			c[i] = '1'
	return ''.join(c)

def comp(a, b):
	return val(a) & val(b)

def bitsub(a, b):
	c = val(a) - val(b)
	s = ''
	while c >= 1:
		s = s + str(c % 2)
		c = int(c / 2)
	s = '0' * (3 - len(s)) + s[::-1]
	return s

def val(a):
	return (ord(a[0]) - ord('0')) * 4 + (ord(a[1]) - ord('0')) * 2 + (ord(a[2]) - ord('0'))
n = int(input())
bitmasks = {'A': '001', 'B': '010', 'C': '100'}
dp = []
dp.append({'000': math.inf, '001': math.inf, '010': math.inf, '100': math.inf, '011': math.inf, '101': math.inf, '110': math.inf, '111': math.inf})
for _ in range(n):
	dp.append({'000': math.inf, '001': math.inf, '010': math.inf, '100': math.inf, '011': math.inf, '101': math.inf, '110': math.inf, '111': math.inf})
for i in range(0, n + 1):
	dp[i]['000'] = 0
prices = [0] * n
weight = ['000'] * n
for i in range(n):
	inp = input().split()
	prices[i] = int(inp[0])
	for c in inp[1]:
		weight[i] = bitadd(bitmasks[c], weight[i])
extra = {'011': ['001', '010'], '101': ['001', '100'], '110': ['100', '010'], '111': ['001', '010', '100', '110', '011', '101']}
for i in range(1, n + 1):
	for w in ['001', '010', '100', '011', '101', '110', '111']:
		if val(weight[i - 1]) <= val(w) and comp(weight[i - 1], w):
			dp[i][w] = min(prices[i - 1] + dp[i - 1][bitsub(w, weight[i - 1])], dp[i - 1][w])
		else:
			dp[i][w] = dp[i - 1][w]
		if w in extra.keys():
			for t in extra[w]:
				dp[i][t] = min(dp[i][t], dp[i][w])
if dp[n]['111'] != math.inf:
	print(dp[n]['111'])
else:
	print(-1)


s = dict(zip(['a', 'b', 'c', 'ab', 'bc', 'ac', 'abc'], [float('inf')] * 7))
for _ in range(int(input())):
	(d1, d2) = input().split()
	d1 = int(d1)
	d2 = ''.join(sorted(d2.lower()))
	s[d2] = min(s[d2], d1)
s['ab'] = min([s['a'] + s['b'], s['ab']])
s['ac'] = min([s['a'] + s['c'], s['ac']])
s['bc'] = min([s['b'] + s['c'], s['bc']])
r = min([s['abc'], s['ab'] + s['c'], s['ac'] + s['b'], s['bc'] + s['a'], s['ab'] + s['ac'], s['ab'] + s['bc'], s['ac'] + s['bc']])
print(-1 if r == float('inf') else r)


n = int(input())
(a, b, c, ab, bc, ac, abc) = (10 ** 10, 10 ** 10, 10 ** 10, 10 ** 10, 10 ** 10, 10 ** 10, 10 ** 10)
for _ in range(n):
	(val, word) = list(map(str, input().split()))
	val = int(val)
	if word == 'A':
		a = min(a, val)
	elif word == 'B':
		b = min(b, val)
	elif word == 'C':
		c = min(c, val)
	elif word in ['AB', 'BA']:
		ab = min(ab, val)
	elif word in ['BC', 'CB']:
		bc = min(bc, val)
	elif word in ['AC', 'CA']:
		ac = min(ac, val)
	else:
		abc = min(abc, val)
ans = 10 ** 10
ans = min(ans, a + b + c, ab + bc, bc + ac, a + abc, b + abc, c + abc, ab + abc, bc + abc, ac + abc, a + bc, b + ac, c + ab, ac + ab, abc)
if ans == 10 ** 10:
	print(-1)
else:
	print(ans)


n = int(input())
jugos = []
A = []
B = []
C = []
AB = []
AC = []
BC = []
ABC = []
for i in range(n):
	a = input().split()
	if a[1] == 'A':
		if len(A) != 0:
			if A[0][0] > int(a[0]):
				A[0] = [int(a[0]), a[1]]
		else:
			A.append([int(a[0]), a[1]])
	elif a[1] == 'B':
		if len(B) != 0:
			if B[0][0] > int(a[0]):
				B[0] = [int(a[0]), a[1]]
		else:
			B.append([int(a[0]), a[1]])
	elif a[1] == 'C':
		if len(C) != 0:
			if C[0][0] > int(a[0]):
				C[0] = [int(a[0]), a[1]]
		else:
			C.append([int(a[0]), a[1]])
	elif a[1] == 'AB' or a[1] == 'BA':
		if len(AB) != 0:
			if AB[0][0] > int(a[0]):
				AB[0] = [int(a[0]), a[1]]
		else:
			AB.append([int(a[0]), a[1]])
	elif a[1] == 'AC' or a[1] == 'CA':
		if len(AC) != 0:
			if AC[0][0] > int(a[0]):
				AC[0] = [int(a[0]), a[1]]
		else:
			AC.append([int(a[0]), a[1]])
	elif a[1] == 'BC' or a[1] == 'CB':
		if len(BC) != 0:
			if BC[0][0] > int(a[0]):
				BC[0] = [int(a[0]), a[1]]
		else:
			BC.append([int(a[0]), a[1]])
	elif a[1] == 'ABC' or a[1] == 'ACB' or a[1] == 'BAC' or (a[1] == 'BCA') or (a[1] == 'CAB') or (a[1] == 'CBA'):
		if len(ABC) != 0:
			if ABC[0][0] > int(a[0]):
				ABC[0] = [int(a[0]), a[1]]
		else:
			ABC.append([int(a[0]), a[1]])
if len(A) != 0:
	jugos.append(A[0])
if len(B) != 0:
	jugos.append(B[0])
if len(C) != 0:
	jugos.append(C[0])
if len(AB) != 0:
	jugos.append(AB[0])
if len(AC) != 0:
	jugos.append(AC[0])
if len(BC) != 0:
	jugos.append(BC[0])
if len(ABC) != 0:
	jugos.append(ABC[0])

def hacer(jugos, vitaminas, precio):
	if len(solucion) != 0:
		if precio > solucion[0]:
			return False
	if len(vitaminas) == 3:
		if len(solucion) == 0:
			solucion.append(precio)
		elif precio < solucion[0]:
			solucion[0] = precio
		return
	else:
		for i in range(len(jugos)):
			if len(jugos[i][1]) == 1:
				if jugos[i][1] not in vitaminas:
					vitaminas.append(jugos[i][1])
					hacer(jugos[:i] + jugos[i + 1:], vitaminas, precio + jugos[i][0])
					vitaminas.pop()
			else:
				no = True
				for r in jugos[i][1]:
					if r not in vitaminas:
						no = False
						break
				if not no:
					tt = 0
					for x in jugos[i][1]:
						if x not in vitaminas:
							vitaminas.append(x)
							tt += 1
					hacer(jugos[:i] + jugos[i + 1:], vitaminas, precio + jugos[i][0])
					for _ in range(tt):
						vitaminas.pop()
solucion = []
hacer(jugos, [], 0)
if len(solucion) == 0:
	print(-1)
else:
	print(solucion[0])


n = int(input())
dic = {}
dic['A'] = float('inf')
dic['B'] = float('inf')
dic['C'] = float('inf')
dic['AB'] = float('inf')
dic['BC'] = float('inf')
dic['AC'] = float('inf')
dic['ABC'] = float('inf')
for i in range(n):
	(c, s) = input().split()
	c = int(c)
	s = ''.join(sorted(s))
	dic[s] = min(dic[s], c)
q = float('inf')
q = min(q, dic['A'] + dic['B'] + dic['C'])
q = min(q, dic['A'] + dic['BC'])
q = min(q, dic['B'] + dic['AC'])
q = min(q, dic['C'] + dic['AB'])
q = min(q, dic['AB'] + dic['BC'])
q = min(q, dic['AC'] + dic['BC'])
q = min(q, dic['AC'] + dic['AB'])
q = min(q, dic['ABC'])
if q == float('inf'):
	print(-1)
else:
	print(q)


abc = 1000001
ab = 1000001
bc = 1000001
ac = 1000001
a = 1000001
b = 1000001
c = 1000001
aa = bb = cc = 0
n = int(input())
while n > 0:
	e = input().split()
	vl = int(e[0])
	vit = sorted(e[1])
	vt = ''
	for i in vit:
		vt += i
	if vt == 'ABC':
		aa = bb = cc = 1
		if abc > vl:
			abc = vl
	elif vt == 'AB':
		aa = bb = 1
		if ab > vl:
			ab = vl
	elif vt == 'BC':
		bb = cc = 1
		if bc > vl:
			bc = vl
	elif vt == 'AC':
		aa = cc = 1
		if ac > vl:
			ac = vl
	elif vt == 'A':
		aa = 1
		if a > vl:
			a = vl
	elif vt == 'B':
		bb = 1
		if b > vl:
			b = vl
	else:
		cc = 1
		if c > vl:
			c = vl
	n -= 1
if aa + bb + cc == 3:
	l = [abc, ab + c, a + bc, ac + b, a + b + c, ab + bc, ac + bc, ac + ab]
	print(min(l))
else:
	print(-1)


A = []
B = []
C = []
AB = []
BC = []
AC = []
ABC = []
n = int(input())
for __ in range(n):
	(j, c) = input().split()
	j = int(j)
	c = ''.join(sorted(list(c)))
	if c == 'A':
		A.append(j)
	elif c == 'B':
		B.append(j)
	elif c == 'C':
		C.append(j)
	elif c == 'AB':
		AB.append(j)
	elif c == 'BC':
		BC.append(j)
	elif c == 'AC':
		AC.append(j)
	elif c == 'ABC':
		ABC.append(j)
l = []
if A and B and C:
	l.append(min(A) + min(B) + min(C))
if AB and BC and AC:
	l.append(min(AB) + min(BC) + min(AC))
if AB and C:
	l.append(min(AB) + min(C))
if A and BC:
	l.append(min(A) + min(BC))
if AC and BC:
	l.append(min(AC) + min(BC))
if AB and BC:
	l.append(min(AB) + min(BC))
if AC and AB:
	l.append(min(AC) + min(AB))
if AC and B:
	l.append(min(AC) + min(B))
if ABC:
	l.append(min(ABC))
print(min(l) if l else -1)


n = int(input())
d = {'A': 10000000000.0, 'B': 10000000000.0, 'C': 10000000000.0, 'AB': 10000000000.0, 'BC': 10000000000.0, 'AC': 10000000000.0, 'ABC': 10000000000.0}
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	b = ''.join(sorted(list(b)))
	d[b] = min(d.get(b), a)
k = min(d['A'] + d['B'] + d['C'], d['AB'] + d['BC'], d['BC'] + d['AC'], d['AB'] + d['AC'], d['ABC'], d['BC'] + d['A'], d['AB'] + d['C'], d['AC'] + d['B'])
if k >= 10000000000.0:
	print(-1)
else:
	print(k)


import sys
import math
import bisect

def main():
	n = int(input())
	A = []
	for i in range(n + 1):
		A.append([10 ** 9] * 8)
	A[0][0] = 0
	for i in range(1, n + 1):
		(cost, s) = input().split()
		cost = int(cost)
		a = 0
		if 'A' in s:
			a += 1
		if 'B' in s:
			a += 2
		if 'C' in s:
			a += 4
		for j in range(8):
			A[i][j] = A[i - 1][j]
		for j in range(8):
			A[i][j | a] = min(A[i][j | a], A[i - 1][j] + cost)
	if A[n][7] == 10 ** 9:
		print(-1)
	else:
		print(A[n][7])
main()


n = int(input())
l = [[10 ** 8] for i in range(7)]
for i in range(n):
	p = input().split()
	s = p[1]
	c = int(p[0])
	if s == 'A':
		l[0].append(c)
	elif s == 'B':
		l[1].append(c)
	elif s == 'C':
		l[2].append(c)
	elif s == 'AB' or s == 'BA':
		l[3].append(c)
	elif s == 'BC' or s == 'CB':
		l[4].append(c)
	elif s == 'AC' or s == 'CA':
		l[5].append(c)
	else:
		l[6].append(c)
m = min(min(l[0]) + min(l[2]) + min(l[1]), min(l[6]), min(l[0]) + min(l[4]), min(l[1]) + min(l[5]), min(l[3]) + min(l[2]), min(l[3]) + min(l[4]), min(l[3]) + min(l[5]), min(l[4]) + min(l[5]))
if m == 10 ** 8:
	print(-1)
else:
	print(m)


n = int(input())
a = 10 ** 10
b = 10 ** 10
c = 10 ** 10
ab = 10 ** 10
bc = 10 ** 10
ac = 10 ** 10
abc = 10 ** 10
for i in range(n):
	juice = input().split()
	if juice[1] == 'A':
		a = min(a, int(juice[0]))
	elif juice[1] == 'B':
		b = min(b, int(juice[0]))
	elif juice[1] == 'C':
		c = min(c, int(juice[0]))
	elif 'A' not in juice[1]:
		bc = min(bc, int(juice[0]))
	elif 'B' not in juice[1]:
		ac = min(ac, int(juice[0]))
	elif 'C' not in juice[1]:
		ab = min(ab, int(juice[0]))
	else:
		abc = min(abc, int(juice[0]))
ans = [a + b + c, ab + bc, ab + c, bc + a, bc + ac, ac + b, ac + ab, abc]
f = min(ans)
if f == 10 ** 10:
	f = -1
print(f)


n = int(input())
d = {}
st = set()
for _ in range(n):
	(n, s) = input().split()
	n = int(n)
	s = list(s)
	s.sort()
	s = ''.join(s)
	st.update(s)
	if s in d:
		d[s] = min(d[s], n)
	else:
		d[s] = n
if len(st) != 3:
	print('-1')
else:
	ans = float('+inf')
	if 'A' in d and 'B' in d and ('C' in d):
		ans = min(ans, d['A'] + d['B'] + d['C'])
	if 'AB' in d and 'C' in d:
		ans = min(ans, d['AB'] + d['C'])
	if 'BC' in d and 'A' in d:
		ans = min(ans, d['BC'] + d['A'])
	if 'AC' in d and 'B' in d:
		ans = min(ans, d['AC'] + d['B'])
	if 'AC' in d and 'BC' in d:
		ans = min(ans, d['AC'] + d['BC'])
	if 'AB' in d and 'BC' in d:
		ans = min(ans, d['AB'] + d['BC'])
	if 'AC' in d and 'AB' in d:
		ans = min(ans, d['AC'] + d['AB'])
	if 'ABC' in d:
		ans = min(ans, d['ABC'])
	if ans == float('+inf'):
		print('-1')
	else:
		print(ans)


n = int(input())
d = {'A': 0, 'B': 1, 'C': 2}
price = {(0, 0, 0): 0}
for i in range(n):
	(b, a) = input().split()
	bit_no = [0, 0, 0]
	if 'A' in a:
		bit_no[0] = 1
	if 'B' in a:
		bit_no[1] = 1
	if 'C' in a:
		bit_no[2] = 1
	if tuple(bit_no) in price:
		price[tuple(bit_no)] = min(price[tuple(bit_no)], int(b))
	else:
		price[tuple(bit_no)] = int(b)
r = float('inf')
for i in price:
	for j in price:
		for k in price:
			if i[0] | j[0] | k[0] == 1 and i[1] | j[1] | k[1] == 1 and (i[2] | j[2] | k[2] == 1):
				r = min(r, price[i] + price[j] + price[k])
if r == float('inf'):
	print(-1)
	exit()
print(r)


INF = 10 ** 18
n = int(input())
min_cost = {'A': INF, 'B': INF, 'C': INF}
vitamins = []
for i in range(n):
	(cost, s) = input().split()
	vitamins.append((int(cost), s))
for v in vitamins:
	for c in v[1]:
		min_cost[c] = min(min_cost[c], v[0])

def missing(s, t):
	return ''.join([c for c in ['A', 'B', 'C'] if c not in s and c not in t])
ans = INF
for v in vitamins:
	if missing(v[1], '') == '':
		ans = min(ans, v[0])
for i in range(n):
	for j in range(i + 1, n):
		x = missing(vitamins[i][1], vitamins[j][1])
		if len(x) == 0:
			ans = min(ans, vitamins[i][0] + vitamins[j][0])
		elif len(x) == 1:
			ans = min(ans, vitamins[i][0] + vitamins[j][0] + min_cost[x])
print(-1 if ans == INF else ans)


t = int(input())
di = dict()
for _ in range(t):
	(c, s) = map(str, input().split())
	s = ''.join(sorted(set(s)))
	c = int(c)
	if s in di:
		di[s] = min(di[s], c)
	else:
		di[s] = c
ans = float('inf')
if 'ABC' in di:
	ans = min(ans, di['ABC'])
if 'AB' in di and 'C' in di:
	ans = min(ans, di['AB'] + di['C'])
if 'AB' in di and 'AC' in di:
	ans = min(ans, di['AB'] + di['AC'])
if 'AB' in di and 'BC' in di:
	ans = min(ans, di['AB'] + di['BC'])
if 'AB' in di and 'ABC' in di:
	ans = min(ans, di['AB'] + di['ABC'])
if 'A' in di and 'BC' in di:
	ans = min(ans, di['A'] + di['BC'])
if 'AB' in di and 'BC' in di:
	ans = min(ans, di['AB'] + di['BC'])
if 'AC' in di and 'BC' in di:
	ans = min(ans, di['AC'] + di['BC'])
if 'ABC' in di and 'BC' in di:
	ans = min(ans, di['ABC'] + di['BC'])
if 'B' in di and 'AC' in di:
	ans = min(ans, di['AC'] + di['B'])
if 'AB' in di and 'AC' in di:
	ans = min(ans, di['AC'] + di['AB'])
if 'BC' in di and 'AC' in di:
	ans = min(ans, di['AC'] + di['BC'])
if 'ABC' in di and 'AC' in di:
	ans = min(ans, di['AC'] + di['ABC'])
if 'A' in di and 'B' in di and ('C' in di):
	ans = min(ans, di['A'] + di['C'] + di['B'])
if ans == float('inf'):
	print('-1')
else:
	print(ans)


n = int(input())
d = {'A': float('Inf'), 'B': float('Inf'), 'C': float('Inf'), 'AB': float('Inf'), 'AC': float('Inf'), 'BC': float('Inf'), 'ABC': float('Inf')}
for i in range(n):
	r = input().split()
	c = int(r[0])
	s = r[1]
	s = ''.join(sorted([x for x in s]))
	d[s] = min(d[s], c)
ans = min(d['ABC'], d['A'] + d['BC'], d['AB'] + d['BC'], d['AB'] + d['AC'], d['AC'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['A'] + d['B'] + d['C'])
if ans < float('Inf'):
	print(ans)
else:
	print(-1)


import sys, itertools, math, collections

def ia():
	return [int(i) for i in sys.stdin.readline().strip().split(' ')]

def ii():
	return int(sys.stdin.readline().strip())

def istr():
	return sys.stdin.readline().strip()
V = {}
for i in range(ii()):
	(c, v) = istr().split(' ')
	c = int(c)
	v = ''.join(sorted(v))
	if v in V:
		V[v] = min(c, V[v])
	else:
		V[v] = c
ans = 10 ** 32
for i in range(len(V) + 1):
	for c in itertools.combinations(V, i):
		s = 0
		vit = ''
		for v in c:
			s += V[v]
			vit += v
		if 'A' in vit and 'B' in vit and ('C' in vit):
			ans = min(ans, s)
if ans < 10 ** 32:
	print(ans)
else:
	print(-1)


a = b = c = ab = ac = bc = abc = 500000
for i in range(int(input())):
	(cost, s) = input().split()
	cost = int(cost)
	if 'A' in s:
		if 'B' in s:
			if 'C' in s:
				if abc > cost:
					abc = cost
			elif ab > cost:
				ab = cost
		elif 'C' in s:
			if ac > cost:
				ac = cost
		elif a > cost:
			a = cost
	elif 'B' in s:
		if 'C' in s:
			if bc > cost:
				bc = cost
		elif b > cost:
			b = cost
	elif c > cost:
		c = cost
m = min(a + b + c, abc, a + bc, ab + bc, ac + bc, b + ac, ab + ac, bc + ac, c + ab, ac + ab, bc + ab)
print(m if m <= 300000 else -1)


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
d = {'A': [N], 'B': [N], 'C': [N], 'AB': [N], 'BC': [N], 'AC': [N], 'ABC': [N]}
for i in range(I()):
	x = S().split()
	d[''.join(sorted(x[-1]))] += (int(x[0]),)
for i in d:
	d[i] = sorted(d[i])[0]
x = d['ABC']
x = min(x, d['A'] + d['B'] + d['C'])
x = min(x, d['A'] + d['BC'])
x = min(x, d['B'] + d['AC'])
x = min(x, d['C'] + d['AB'])
x = min(x, d['AB'] + d['BC'])
x = min(x, d['AB'] + d['AC'])
x = min(x, d['AC'] + d['BC'])
print(-1 if x >= N else x)


from collections import defaultdict
n = int(input())
dic = {}

def solve(dic, a, b):
	t1 = float('inf')
	t2 = float('inf')
	if a in dic:
		t1 = dic[a]
	if b in dic:
		t2 = dic[b]
	if t1 == float('inf') or t2 == float('inf'):
		return float('inf')
	return t1 + t2
(a, b, c) = (False, False, False)
for i in range(n):
	(price, vit) = map(str, input().split())
	price = int(price)
	vit = vit.lower()
	vit = ''.join(sorted(vit))
	if 'a' in vit:
		a = True
	if 'b' in vit:
		b = True
	if 'c' in vit:
		c = True
	if vit not in dic:
		dic[vit] = price
	else:
		dic[vit] = min(dic[vit], price)
if not (a and b and c):
	print(-1)
else:
	ans = float('inf')
	if 'a' in dic and 'b' in dic and ('c' in dic):
		tmp = dic['a'] + dic['b'] + dic['c']
		ans = min(ans, tmp)
	for i in dic:
		if len(i) == 3:
			ans = min(ans, dic[i])
	ans = min(ans, solve(dic, 'a', 'bc'))
	ans = min(ans, solve(dic, 'ab', 'c'))
	ans = min(ans, solve(dic, 'ac', 'b'))
	ans = min(ans, solve(dic, 'ab', 'bc'))
	ans = min(ans, solve(dic, 'ac', 'bc'))
	ans = min(ans, solve(dic, 'ac', 'ab'))
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


import sys
min_value = sys.maxsize

def minimumPrice(arr, price=0, vitamins=''):
	global min_value
	if 'A' in vitamins and 'B' in vitamins and ('C' in vitamins):
		if min_value > price:
			min_value = price
		return min_value
	dp_arr = [min_value]
	for (i, obj) in enumerate(arr):
		if obj.price + price > min_value:
			continue
		flag = 0
		for objvitamins in obj.vitamins:
			if objvitamins not in vitamins:
				flag += 1
				break
		if not flag:
			continue
		dp_arr.append(minimumPrice(arr[i + 1:], price + obj.price, vitamins + obj.vitamins))
	return min(dp_arr)

class graph:

	def __init__(self, price, vitamins):
		self.price = int(price)
		self.vitamins = vitamins
t = int(input())
arr = []
for i in range(t):
	(price, vitamins) = map(str, input().strip().split())
	arr.append(graph(price, vitamins))
sol = minimumPrice(arr)
if sol == sys.maxsize:
	sol = -1
print(sol)


n = int(input())
dic = {}
for i in range(0, n):
	(x, s) = input().split()
	x = int(x)
	l = list(s)
	l.sort()
	s = ''.join(l)
	if s in dic:
		dic[s] = min(dic[s], x)
	else:
		dic[s] = x
ans = 1000000
if 'A' in dic and 'B' in dic and ('C' in dic):
	ans = min(ans, dic['A'] + dic['B'] + dic['C'])
if 'A' in dic and 'BC' in dic:
	ans = min(ans, dic['A'] + dic['BC'])
if 'B' in dic and 'AC' in dic:
	ans = min(ans, dic['B'] + dic['AC'])
if 'C' in dic and 'AB' in dic:
	ans = min(ans, dic['C'] + dic['AB'])
if 'AB' in dic and 'BC' in dic:
	ans = min(ans, dic['AB'] + dic['BC'])
if 'AC' in dic and 'BC' in dic:
	ans = min(ans, dic['AC'] + dic['BC'])
if 'AB' in dic and 'AC' in dic:
	ans = min(ans, dic['AB'] + dic['AC'])
if 'ABC' in dic:
	ans = min(ans, dic['ABC'])
if ans == 1000000:
	ans = -1
print(ans)


n = int(input())
a = 300001
b = 300001
c = 300001
ab = 300001
ac = 300001
cb = 300001
abc = 300001
for i in range(n):
	l = input().split()
	ch = int(l[0])
	t = l[1]
	if t == 'A':
		a = min(a, ch)
	elif t == 'B':
		b = min(b, ch)
	elif t == 'C':
		c = min(c, ch)
	elif 'A' in t and 'B' in t and ('C' in t):
		abc = min(abc, ch)
	elif 'A' in t and 'B' in t:
		ab = min(ab, ch)
	elif 'A' in t and 'C' in t:
		ac = min(ac, ch)
	elif 'C' in t and 'B' in t:
		cb = min(cb, ch)
t = min(a + b + c, ab + c, ab + cb, ab + ac, abc, ac + cb, ac + b, cb + a)
if t >= 300001:
	print(-1)
else:
	print(t)


n = int(input())
a = []
b = []
c = []
ab = []
ac = []
bc = []
abc = []
taken = []
one = 0
two = 0
three = 0
for i in range(n):
	(cost, vit) = map(lambda x: x, input().split())
	cost = int(cost)
	vitamins = ''.join(sorted(vit))
	if 'A' in vitamins:
		a.append([cost, vitamins])
	if 'B' in vitamins:
		b.append([cost, vitamins])
	if 'C' in vitamins:
		c.append([cost, vitamins])
	if 'AB' in vitamins:
		ab.append([cost, vitamins])
	if 'AC' in vitamins:
		ac.append([cost, vitamins])
	if 'BC' in vitamins:
		bc.append([cost, vitamins])
	if 'ABC' in vitamins:
		abc.append([cost, vitamins])
if not a or not b or (not c):
	print(-1)
else:
	a.sort(key=lambda x: x[0])
	b.sort(key=lambda x: x[0])
	c.sort(key=lambda x: x[0])
	ac.sort(key=lambda x: x[0])
	ab.sort(key=lambda x: x[0])
	bc.sort(key=lambda x: x[0])
	abc.sort(key=lambda x: x[0])
	possible = []
	taken.extend(a[0][1])
	one += a[0][0]
	if b not in taken:
		taken.extend(b[0][1])
		one += b[0][0]
	if c not in taken:
		taken.extend(c[0][1])
		one += c[0][0]
	if ab:
		possible.append(ab[0][0] + c[0][0])
	if bc:
		possible.append(bc[0][0] + a[0][0])
	if ac:
		possible.append(ac[0][0] + b[0][0])
	if possible:
		two = min(possible)
	else:
		two = 999999999
	if abc:
		three = abc[0][0]
	else:
		three = 999999999
	print(min(one, two, three))


import sys
A = dict()
A['A'] = sys.maxsize
A['B'] = sys.maxsize
A['C'] = sys.maxsize
A['ABC'] = sys.maxsize
A['AB'] = sys.maxsize
A['BC'] = sys.maxsize
A['AC'] = sys.maxsize
n = int(input())
for _ in range(0, n):
	(i, c) = input().split()
	a = ''.join(map(str, sorted(c)))
	A[a] = min(int(i), A[a])
ans = min(A['A'] + A['B'] + A['C'], A['ABC'], A['AB'] + A['BC'], A['AB'] + A['AC'], A['AB'] + A['C'], A['BC'] + A['AC'], A['BC'] + A['A'], A['AC'] + A['B'])
if ans == sys.maxsize:
	print('-1')
else:
	print(ans)


n = int(input())
l1 = [10000000]
l2 = [10000000]
l3 = [10000000]
l4 = [10000000]
l5 = [10000000]
l6 = [10000000]
l7 = [10000000]
for _ in range(n):
	(c, v) = input().lower().split()
	c = int(c)
	v = ''.join(sorted(v))
	if v == 'abc':
		l1.append(c)
	if v == 'ab':
		l2.append(c)
	if v == 'bc':
		l3.append(c)
	if v == 'ac':
		l4.append(c)
	if v == 'a':
		l5.append(c)
	if v == 'b':
		l6.append(c)
	if v == 'c':
		l7.append(c)
m1 = min(l1)
m2 = min(l2)
m3 = min(l3)
m4 = min(l4)
m5 = min(l5)
m6 = min(l6)
m7 = min(l7)
m = min(m1, m2 + m3, m2 + m4, m2 + m7, m3 + m4, m3 + m5, m4 + m6, m5 + m6 + m7)
if m == 10000000:
	print(-1)
else:
	print(m)


n = int(input())

def func(v):
	res = 0
	if 'A' in v:
		res |= 1
	if 'B' in v:
		res |= 2
	if 'C' in v:
		res |= 4
	return res
cost = [10 ** 10] * 8
cost[0] = 0
for _ in range(n):
	(c, v) = input().split()
	c = int(c)
	v = func(v)
	for i in range(8):
		cost[i | v] = min(cost[i | v], cost[i] + c)
print(cost[7] if cost[7] < 10 ** 10 else -1)


n = int(input())
d = [2 ** 31 for i in range(8)]
ans = 2 ** 31
for i in range(n):
	(r, s) = input().split()
	x = 1 * ('A' in s) + 2 * ('B' in s) + 4 * ('C' in s)
	d[x] = min(d[x], int(r))
ans = min(d[1] + d[2] + d[4], d[5] + d[3], d[3] + d[6], d[6] + d[5], d[3] + d[4], d[1] + d[6], d[5] + d[2], d[7])
if ans < 2 ** 31:
	print(ans)
else:
	print(-1)


from collections import defaultdict
INF = float('inf')
n = int(input())
memo = defaultdict(lambda : INF)
for _ in range(n):
	(cost, vit) = input().split(' ')
	cost = int(cost)
	vit = tuple(sorted(list(vit)))
	memo[vit] = min(memo[vit], cost)
ta = tuple('A')
tb = tuple('B')
tc = tuple('C')
tab = ('A', 'B')
tbc = ('B', 'C')
tca = ('A', 'C')
tabc = ('A', 'B', 'C')
m = min(memo[ta] + memo[tb] + memo[tc], memo[tabc], memo[tab] + memo[tc], memo[tbc] + memo[ta], memo[tca] + memo[tb], memo[tab] + memo[tbc], memo[tab] + memo[tca], memo[tbc] + memo[tca], memo[ta] + memo[tb] + memo[tbc], memo[ta] + memo[tb] + memo[tca], memo[tc] + memo[tb] + memo[tca], memo[tc] + memo[tb] + memo[tab], memo[ta] + memo[tc] + memo[tbc], memo[ta] + memo[tc] + memo[tab])
if m == INF:
	m = -1
print(m)


inf = 1000000000.0
(a, b, c, ab, bc, ac, abc) = (inf, inf, inf, inf, inf, inf, inf)
for _ in range(int(input())):
	(p, s) = list(input().split())
	p = int(p)
	if s == 'A':
		a = min(p, a)
	if s == 'B':
		b = min(p, b)
	if s == 'C':
		c = min(p, c)
	if 'AB' == ''.join(sorted(s)):
		ab = min(p, ab)
	if 'BC' == ''.join(sorted(s)):
		bc = min(p, bc)
	if 'AC' == ''.join(sorted(s)):
		ac = min(p, ac)
	if 'ABC' == ''.join(sorted(s)):
		abc = min(p, abc)
l = [a, b, c, ab, bc, ac, abc]
t = ['a', 'b', 'c', 'ab', 'bc', 'ac', 'abc']
mini = abc
for i in range(7):
	for j in range(7):
		for k in range(7):
			if ''.join(sorted(''.join(set(t[i] + t[j] + t[k])))) == 'abc':
				mini = min(mini, l[i] + l[j] + l[k])
for i in range(7):
	for j in range(7):
		if ''.join(sorted(''.join(set(t[i] + t[j])))) == 'abc':
			mini = min(mini, l[i] + l[j])
if mini >= inf:
	print(-1)
else:
	print(mini)


n = int(input())
s = []
for i in range(n):
	(a, b) = map(str, input().split())
	a = int(a)
	s.append((a, b))
ans = float('inf')
mina = ans
minb = mina
minc = minb
for i in s:
	if 'A' in i[1] and mina > i[0]:
		mina = i[0]
	if 'B' in i[1] and minb > i[0]:
		minb = i[0]
	if 'C' in i[1] and minc > i[0]:
		minc = i[0]
ans = mina + minb + minc
for i in s:
	if len(i[1]) == 2:
		if 'A' in i[1] and 'B' in i[1] and (minc + i[0] < ans):
			ans = minc + i[0]
		if 'A' in i[1] and 'C' in i[1] and (minb + i[0] < ans):
			ans = minb + i[0]
		if 'C' in i[1] and 'B' in i[1] and (mina + i[0] < ans):
			ans = mina + i[0]
	elif len(i[1]) == 3:
		ans = min(ans, i[0])
if ans == float('inf'):
	print(-1)
else:
	print(ans)


p = []
v = []
a = 0
b = 0
ab = 0
ac = 0
bc = 0
abc = 0
mns = {}
mn = []
for i in range(int(input())):
	c = input().split()
	p.append(int(c[0]))
	v.append(''.join(sorted(list(c[1]))))
del c
c = 0
for i in range(len(v)):
	if not v[i] in mns or mns[v[i]] > p[i]:
		mns[v[i]] = p[i]
for k in mns:
	if len(k) == 1:
		if k == 'A':
			a = mns[k]
		elif k == 'B':
			b = mns[k]
		else:
			c = mns[k]
	elif len(k) == 2:
		if k == 'AB':
			ab = mns[k]
		elif k == 'AC':
			ac = mns[k]
		elif k == 'BC':
			bc = mns[k]
	else:
		abc = mns[k]
if a > 0 and b > 0 and (c > 0):
	mn.append(a + b + c)
if ab > 0 and c > 0:
	mn.append(ab + c)
if ac > 0 and b > 0:
	mn.append(ac + b)
if bc > 0 and a > 0:
	mn.append(bc + a)
if ab > 0 and ac > 0:
	mn.append(ab + ac)
if ab > 0 and bc > 0:
	mn.append(ab + bc)
if ac > 0 and bc > 0:
	mn.append(ac + bc)
if ac > 0 and ab > 0:
	mn.append(ac + ab)
if abc > 0:
	mn.append(abc)
if len(mn) == 0:
	mn.append(-1)
print(min(mn))


def main():
	n = int(input())
	dd = {}
	for i in range(n):
		(p, s) = input().split()
		s = ''.join(sorted(list(s)))
		if s in dd:
			dd[s] = min(dd[s], int(p))
		else:
			dd[s] = int(p)
	ans = 10 ** 12
	if 'A' in dd and 'B' in dd and ('C' in dd):
		ans = min(ans, dd['A'] + dd['B'] + dd['C'])
	if 'A' in dd and 'BC' in dd:
		ans = min(ans, dd['A'] + dd['BC'])
	if 'AB' in dd and 'C' in dd:
		ans = min(ans, dd['AB'] + dd['C'])
	if 'AC' in dd and 'B' in dd:
		ans = min(ans, dd['AC'] + dd['B'])
	if 'AB' in dd and 'BC' in dd:
		ans = min(ans, dd['AB'] + dd['BC'])
	if 'AB' in dd and 'AC' in dd:
		ans = min(ans, dd['AB'] + dd['AC'])
	if 'AC' in dd and 'BC' in dd:
		ans = min(ans, dd['AC'] + dd['BC'])
	if 'ABC' in dd:
		ans = min(ans, dd['ABC'])
	if ans == 10 ** 12:
		print(-1)
	else:
		print(ans)
main()


n = int(input())
minA = minB = minC = minBC = minAC = minABC = minAB = 100001
for i in range(n):
	(cash, vit) = input().split()
	cash = int(cash)
	if 'A' in vit and 'B' in vit and ('C' in vit):
		minABC = cash if cash < minABC else minABC
	elif 'A' in vit and 'B' in vit:
		minAB = cash if cash < minAB else minAB
	elif 'C' in vit and 'B' in vit:
		minBC = cash if cash < minBC else minBC
	elif 'C' in vit and 'A' in vit:
		minAC = cash if cash < minAC else minAC
	elif 'A' in vit:
		minA = cash if cash < minA else minA
	elif 'B' in vit:
		minB = cash if cash < minB else minB
	elif 'C' in vit:
		minC = cash if cash < minC else minC
if minA == 100001:
	minA = 1000000
if minB == 100001:
	minB = 1000000
if minC == 100001:
	minC = 1000000
if minBC == 100001:
	minBC = 1000000
if minAB == 100001:
	minAB = 1000000
if minAC == 100001:
	minAC = 1000000
if minABC == 100001:
	minABC = 1000000
min_sum = min(minABC, minA + minB + minC, minA + minBC, minB + minAC, minC + minAB, minAB + minAC, minAB + minBC, minBC + minAC)
if min_sum > 500000:
	min_sum = -1
print(min_sum)


dict1 = {}
d = []
for _ in range(int(input())):
	arr1 = [i for i in input().split()]
	g = sorted(arr1[1])
	s = ''
	for i in g:
		s = s + i
	if dict1.get(s) == None:
		dict1[s] = int(arr1[0])
		d.append(s)
	elif dict1[s] > int(arr1[0]):
		dict1[s] = int(arr1[0])
count1 = 0
count2 = 0
count3 = 0
count4 = 0
count5 = 0
op = []
count = 1000000000000000
if dict1.get('A') != None and dict1.get('B') != None and (dict1.get('C') != None):
	count1 += dict1['A'] + dict1['B'] + dict1['C']
if count1 > 0:
	op.append(count1)
for i in d:
	if i.count('A') > 0 and i.count('B') > 0 and (i.count('C') > 0):
		count2 = dict1[i]
		op.append(count2)
	else:
		for j in d:
			s = i + j
			if s.count('A') > 0 and s.count('B') > 0 and (s.count('C') > 0):
				count2 = dict1[i] + dict1[j]
				op.append(count2)
if op == []:
	print(-1)
else:
	print(min(op))


n = int(input())
c = {}
c['A'] = 999999
c['B'] = 999999
c['C'] = 999999
c['AB'] = 999999
c['BC'] = 999999
c['AC'] = 999999
c['ABC'] = 999999
cost = 999999
for _ in range(n):
	t = input().split()
	a = int(t[0])
	s = t[1]
	s = ''.join(sorted(s))
	c[s] = min(c[s], a)
cost = min(cost, c['A'] + c['B'] + c['C'])
cost = min(cost, c['A'] + c['BC'])
cost = min(cost, c['B'] + c['AC'])
cost = min(cost, c['C'] + c['AB'])
cost = min(cost, c['AB'] + c['BC'])
cost = min(cost, c['AB'] + c['AC'])
cost = min(cost, c['BC'] + c['AC'])
cost = min(cost, c['ABC'])
if cost > 300000:
	print('-1')
else:
	print(cost)


from sys import stdin, stdout

def ok(n, s):
	st = ''.join(sorted(s))
	d[st] = min(d[st], int(n))
d = {'A': 10000000, 'B': 10000000, 'C': 10000000, 'AB': 10000000, 'BC': 10000000, 'AC': 10000000, 'ABC': 10000000}
a = [(lambda n, s: ok(n, s))(*stdin.readline().split()) for i in range(int(stdin.readline()))]
a[0] = min(d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['AB'] + d['BC'], d['AC'] + d['AB'], d['AC'] + d['BC'], d['ABC'])
stdout.write('%d' % (-1 if a[0] >= 10000000 else a[0]))


n = int(input())
costs = {}
for i in range(n):
	(c, s) = input().split()
	s = ''.join(sorted(s.lower()))
	c = int(c)
	if s not in costs:
		costs[s] = c
	else:
		costs[s] = min(costs[s], c)
costs[''] = 0
INF = int(1e+18)
mx = INF
for el1 in costs:
	for el2 in costs:
		for el3 in costs:
			c = costs[el1] + costs[el2] + costs[el3]
			if len(set(el1 + el2 + el3)) == 3:
				mx = min(mx, c)
if mx == INF:
	print(-1)
else:
	print(mx)


n_types = int(input())
vita = {'A': float('inf'), 'B': float('inf'), 'C': float('inf'), 'AB': float('inf'), 'AC': float('inf'), 'BC': float('inf'), 'ABC': float('inf')}
for juice in range(n_types):
	info = input().split()
	vits_id = ''.join(sorted(info[1]))
	if vita[vits_id] > int(info[0]):
		vita[vits_id] = int(info[0])
op1 = vita['A'] + vita['B'] + vita['C']
op2 = vita['A'] + vita['BC']
op3 = vita['B'] + vita['AC']
op4 = vita['C'] + vita['AB']
op5 = vita['AB'] + vita['BC']
op6 = vita['AC'] + vita['AB']
op7 = vita['AC'] + vita['BC']
op8 = vita['ABC']
res = min(op1, op2, op3, op4, op5, op6, op7, op8)
if res == float('inf'):
	print(-1)
else:
	print(res)


import sys

def get_line():
	return list(map(str, sys.stdin.readline().strip().split()))

def in1():
	return int(input())
d = {}
for _ in range(in1()):
	(x, s) = get_line()
	x = int(x)
	if s == 'BA' or s == 'AB':
		s = 'AB'
	elif s == 'BC' or s == 'CB':
		s = 'BC'
	elif s == 'AC' or s == 'CA':
		s = 'AC'
	elif s == 'ABC' or s == 'BAC' or s == 'BCA' or (s == 'CAB') or (s == 'CBA') or (s == 'ACB'):
		s = 'ABC'
	if s not in d.keys():
		d[s] = x
	elif d[s] > x:
		d[s] = x
b = []
if 'ABC' in d.keys():
	b.append(d['ABC'])
if 'A' in d.keys() and 'BC' in d.keys():
	b.append(d['A'] + d['BC'])
if 'B' in d.keys() and 'AC' in d.keys():
	b.append(d['B'] + d['AC'])
if 'C' in d.keys() and 'AB' in d.keys():
	b.append(d['C'] + d['AB'])
if 'AB' in d.keys() and 'BC' in d.keys():
	b.append(d['AB'] + d['BC'])
if 'BC' in d.keys() and 'AC' in d.keys():
	b.append(d['BC'] + d['AC'])
if 'AB' in d.keys() and 'AC' in d.keys():
	b.append(d['AB'] + d['AC'])
if 'A' in d.keys() and 'B' in d.keys() and ('C' in d.keys()):
	b.append(d['A'] + d['B'] + d['C'])
if 'A' in d.keys() and 'B' in d.keys() and ('AC' in d.keys()):
	b.append(d['A'] + d['B'] + d['AC'])
if 'A' in d.keys() and 'B' in d.keys() and ('BC' in d.keys()):
	b.append(d['A'] + d['B'] + d['BC'])
if 'A' in d.keys() and 'C' in d.keys() and ('BC' in d.keys()):
	b.append(d['A'] + d['C'] + d['BC'])
if 'A' in d.keys() and 'C' in d.keys() and ('AB' in d.keys()):
	b.append(d['A'] + d['C'] + d['AB'])
if 'B' in d.keys() and 'C' in d.keys() and ('AC' in d.keys()):
	b.append(d['C'] + d['B'] + d['AC'])
if 'C' in d.keys() and 'B' in d.keys() and ('AB' in d.keys()):
	b.append(d['C'] + d['B'] + d['AB'])
if len(b) == 0:
	print(-1)
else:
	print(min(b))


dic = {}
num = 10 ** 10
dic['a'] = num
dic['b'] = num
dic['c'] = num
dic['ac'] = num
dic['ab'] = num
dic['bc'] = num
dic['abc'] = num
for _ in range(int(input())):
	(n, s) = input().split()
	n = int(n)
	s = s.lower()
	if s == 'a' and dic['a'] > n:
		dic['a'] = n
	elif s == 'b' and dic['b'] > n:
		dic['b'] = n
	elif s == 'c' and dic['c'] > n:
		dic['c'] = n
	elif len(s) == 2 and s.count('a') > 0 and (s.count('b') > 0) and (dic['ab'] > n):
		dic['ab'] = n
	elif len(s) == 2 and s.count('a') > 0 and (s.count('c') > 0) and (dic['ac'] > n):
		dic['ac'] = n
	elif len(s) == 2 and s.count('b') > 0 and (s.count('c') > 0) and (dic['bc'] > n):
		dic['bc'] = n
	elif len(s) == 3 and dic['abc'] > n:
		dic['abc'] = n
ans = dic['abc']
if dic['a'] + dic['b'] + dic['c'] < ans:
	ans = dic['a'] + dic['b'] + dic['c']
if dic['b'] + dic['ac'] < ans:
	ans = dic['b'] + dic['ac']
if dic['a'] + dic['bc'] < ans:
	ans = dic['a'] + dic['bc']
if dic['ab'] + dic['c'] < ans:
	ans = dic['ab'] + dic['c']
if dic['ab'] + dic['bc'] < ans:
	ans = dic['ab'] + dic['bc']
if dic['ac'] + dic['ab'] < ans:
	ans = dic['ac'] + dic['ab']
if dic['bc'] + dic['ab'] < ans:
	ans = dic['bc'] + dic['ab']
if dic['bc'] + dic['ac'] < ans:
	ans = dic['bc'] + dic['ac']
print(-1 if ans == 10 ** 10 else ans)


from collections import defaultdict

def main():
	n = int(input())
	di = defaultdict(lambda : 1000000)
	for i in range(n):
		x = input().split()
		(x, y) = (int(x[0]), ''.join(sorted(x[1])))
		if di[y] > x:
			di[y] = x
	ans1 = [di['A'] + di['B'] + di['C'], di['A'] + di['BC'], di['AB'] + di['C'], di['AB'] + di['AC'], di['AB'] + di['BC'], di['AC'] + di['B'], di['AC'] + di['BC'], di['AC'] + di['AB'], di['ABC']]
	ans3 = min(ans1)
	if ans3 >= 1000000:
		print('-1')
	else:
		print(ans3)
main()


from functools import reduce
import os
import sys
from collections import *
from decimal import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]

def Ceil(a, b):
	return a // b + int(a % b > 0)
albhabet = 'abcdefghijklmnopqrstuvwxyz'
mo = 1000000007
inf = 1e+18
div = 998244353
for _ in range(1):
	d = defaultdict(list)
	n = inn()
	for i in range(n):
		(val, vit) = map(str, input().split())
		d[''.join(sorted(vit))] += [int(val)]
	mini = defaultdict(int)
	for i in d:
		mini[i] = min(d[i])
	mn = inf
	if 'ABC' in d:
		mn = min(mn, mini['ABC'])
	if 'AB' in d and 'C' in d:
		mn = min(mn, mini['AB'] + mini['C'])
	if 'BC' in d and 'A' in d:
		mn = min(mn, mini['BC'] + mini['A'])
	if 'AC' in d and 'B' in d:
		mn = min(mn, mini['AC'] + mini['B'])
	if 'AB' in d and 'BC' in d:
		mn = min(mn, mini['AB'] + mini['BC'])
	if 'AB' in d and 'AC' in d:
		mn = min(mn, mini['AB'] + mini['AC'])
	if 'BC' in d and 'AC' in d:
		mn = min(mn, mini['BC'] + mini['AC'])
	if 'A' in d and 'B' in d and ('C' in d):
		mn = min(mn, mini['A'] + mini['B'] + mini['C'])
	if mn == inf:
		mn = -1
	print(mn)


import sys
n = int(input())
d = {'A': 1, 'B': 2, 'C': 4}
d1 = {1: 'A', 2: 'B', 4: 'C', 3: 'AB', 5: 'AC', 6: 'BC', 7: 'ABC'}
d2 = {i: sys.maxsize for i in d1}
for i in range(n):
	(c, s) = input().split()
	c = int(c)
	s = sum([d[i] for i in s])
	d2[s] = min(d2[s], c)
l1 = [1, 2, 4]
for k in range(3):
	i = l1[k]
	for l in range(k + 1, 3):
		j = l1[l]
		d2[i + j] = min(d2[i + j], d2[i] + d2[j])
l1 = [3, 5, 6]
for i in l1:
	d2[7] = min(d2[7], d2[i] + d2[7 - i])
d2[7] = min(d2[7], d2[3] + d2[5], d2[5] + d2[6], d2[6] + d2[3])
if d2[7] > 300000:
	print(-1)
else:
	print(d2[7])


n = int(input())
l = []
min_a = 100001
min_b = 100001
min_c = 100001
min_abc = 100001

def find(x, y):
	for i in x:
		if i == y:
			return True
for i in range(n):
	juice = input()
	l.append(juice.split())
for i in range(n):
	if l[i][1] == 'A':
		if int(l[i][0]) < min_a:
			min_a = int(l[i][0])
	elif l[i][1] == 'B':
		if int(l[i][0]) < min_b:
			min_b = int(l[i][0])
	elif l[i][1] == 'C':
		if int(l[i][0]) < min_c:
			min_c = int(l[i][0])
min1 = 300001
bad = True
for d in range(n):
	for c in range(n):
		all = l[d][1] + l[c][1]
		if find(all, 'A') == True and find(all, 'B') == True and (find(all, 'C') == True):
			bad = False
			if min1 > int(l[d][0]) + int(l[c][0]):
				min1 = int(l[d][0]) + int(l[c][0])
if min_a != 100001 and min_b != 100001 and (min_c != 100001):
	price = min_a + min_b + min_c
else:
	price = -1
for i in range(n):
	if find(l[i][1], 'A') == True and find(l[i][1], 'B') == True and (find(l[i][1], 'C') == True):
		if int(l[i][0]) < min_abc:
			min_abc = int(l[i][0])
if min_abc != 100001 and bad == False and (price != -1):
	print(min(price, min_abc, min1))
elif min_abc != 100001 and bad == False:
	print(min(min_abc, min1))
elif bad == False and price != -1:
	print(min(price, min1))
elif min_abc != 100001 and price != -1:
	print(min(price, min_abc))
elif min_abc != 100001:
	print(min_abc)
elif bad == False:
	print(min1)
elif price != -1:
	print(price)
else:
	print('-1')


import math
from collections import defaultdict
n = int(input())
min_prices = defaultdict(lambda : math.inf)
for i in range(n):
	(c, v) = input().split()
	c = int(c)
	v = ''.join(sorted(v))
	if min_prices[v] > c:
		min_prices[v] = c
min_price = min_prices['ABC']
min_price = min(min_price, min_prices['AC'] + min(min_prices['B'], min_prices['AB'], min_prices['BC']))
min_price = min(min_price, min_prices['AB'] + min(min_prices['C'], min_prices['BC'], min_prices['AC']))
min_price = min(min_price, min_prices['BC'] + min(min_prices['A'], min_prices['AB'], min_prices['AC']))
min_price = min(min_price, min_prices['A'] + min_prices['B'] + min_prices['C'])
if min_price == math.inf:
	print(-1)
else:
	print(min_price)


gi = lambda : list(map(int, input().split()))
(n,) = gi()
d = {}
for k in range(n):
	l = input().split()
	l[1] = ''.join(sorted(list(l[1])))
	if l[1] in d:
		d[l[1]] = min(d[l[1]], int(l[0]))
	else:
		d[l[1]] = int(l[0])
ans = 1000000000
if 'A' in d and 'B' in d and ('C' in d):
	ans = d['A'] + d['B'] + d['C']
if 'AC' in d:
	if 'B' in d:
		ans = min(ans, d['AC'] + d['B'])
	if 'AB' in d:
		ans = min(ans, d['AC'] + d['AB'])
	if 'BC' in d:
		ans = min(ans, d['AC'] + d['BC'])
if 'AB' in d:
	if 'C' in d:
		ans = min(ans, d['AB'] + d['C'])
	if 'AC' in d:
		ans = min(ans, d['AB'] + d['AC'])
	if 'BC' in d:
		ans = min(ans, d['AB'] + d['BC'])
if 'BC' in d:
	if 'A' in d:
		ans = min(ans, d['BC'] + d['A'])
	if 'AC' in d:
		ans = min(ans, d['BC'] + d['AC'])
	if 'AB' in d:
		ans = min(ans, d['BC'] + d['AB'])
if 'ABC' in d:
	ans = min(ans, d['ABC'])
if 'A' in d and 'B' in d:
	if 'AC' in d:
		ans = min(ans, d['AC'] + d['A'] + d['B'])
	if 'BC' in d:
		ans = min(ans, d['BC'] + d['A'] + d['B'])
if 'A' in d and 'C' in d:
	if 'AB' in d:
		ans = min(ans, d['AB'] + d['A'] + d['C'])
	if 'BC' in d:
		ans = min(ans, d['BC'] + d['A'] + d['C'])
if 'B' in d and 'C' in d:
	if 'AB' in d:
		ans = min(ans, d['AB'] + d['C'] + d['B'])
	if 'AC' in d:
		ans = min(ans, d['AC'] + d['C'] + d['B'])
if ans == 1000000000:
	print(-1)
else:
	print(ans)


import string
vitamin = {}
n = input()
for i in range(int(n)):
	a = input()
	b = a.split(' ')
	k = sorted(b[1])
	key = ''
	for i in range(len(k)):
		key += k[i]
	if key in vitamin:
		if int(vitamin[key]) > int(b[0]):
			vitamin[key] = b[0]
	else:
		vitamin[key] = b[0]
if 'A' in vitamin and 'B' in vitamin and ('C' in vitamin):
	s1 = int(vitamin['A']) + int(vitamin['B']) + int(vitamin['C'])
else:
	s1 = 900000
if 'AB' in vitamin and 'C' in vitamin:
	s2 = int(vitamin['AB']) + int(vitamin['C'])
else:
	s2 = 900000
if 'AC' in vitamin and 'B' in vitamin:
	s3 = int(vitamin['AC']) + int(vitamin['B'])
else:
	s3 = 900000
if 'BC' in vitamin and 'A' in vitamin:
	s4 = int(vitamin['BC']) + int(vitamin['A'])
else:
	s4 = 900000
if 'AB' in vitamin and 'AC' in vitamin:
	s5 = int(vitamin['AB']) + int(vitamin['AC'])
else:
	s5 = 900000
if 'AB' in vitamin and 'BC' in vitamin:
	s6 = int(vitamin['AB']) + int(vitamin['BC'])
else:
	s6 = 900000
if 'BC' in vitamin and 'AC' in vitamin:
	s7 = int(vitamin['BC']) + int(vitamin['AC'])
else:
	s7 = 900000
if 'ABC' in vitamin:
	s8 = int(vitamin['ABC'])
else:
	s8 = 900000
List = [s1, s2, s3, s4, s5, s6, s7, s8]
M = min(List)
if M == 900000:
	print('-1')
else:
	print(M)


import sys
n = int(sys.stdin.readline().strip('\n'))
p = [[100000000 for i in range(8)] for i in range(n + 1)]
l = [[0 for i in range(2)] for i in range(n)]
for i in range(n):
	line = sys.stdin.readline().strip('\n')
	(c, v) = list(map(str, line.split()))
	c = int(c)
	b = 0
	if v.count('A') > 0:
		b |= 1
	if v.count('B') > 0:
		b |= 2
	if v.count('C') > 0:
		b |= 4
	l[i] = (c, b)
p[0][0] = 0
for i in range(n):
	for j in range(8):
		p[i + 1][j] = p[i][j]
	for j in range(8):
		if p[i][j] + l[i][0] < p[i + 1][j | l[i][1]]:
			p[i + 1][j | l[i][1]] = p[i][j] + l[i][0]
if p[n][7] == 100000000:
	print(-1)
else:
	print(p[n][7])


def tobin(abc):
	r = 0
	for c in abc:
		b = ord(c) - ord('A')
		r |= 1 << b
	return r
M = 7
INF = 10 ** 20
n = int(input())
dp = [0] + [INF] * M
for _ in range(n):
	(v, s) = input().split(' ')
	(v, s) = (int(v), tobin(s))
	for (i, u) in enumerate(dp):
		dp[i | s] = min(dp[i | s], dp[i] + v)
print(dp[-1] if dp[-1] < INF else -1)


n = int(input())
a = [0] * n
d = dict()
for i in range(n):
	l = input().split()
	a[i] = [int(l[0]), list(l[1])]
	a[i][1].sort()
	a[i][1] = ''.join(a[i][1])
	d[a[i][1]] = min(d.get(a[i][1], 300001), a[i][0])
d['AB'] = min(d.get('AB', 300001), d.get('A', 300001) + d.get('B', 300001))
d['AC'] = min(d.get('AC', 300001), d.get('A', 300001) + d.get('C', 300001))
d['BC'] = min(d.get('BC', 300001), d.get('B', 300001) + d.get('C', 300001))
ans = 300001
ans = min(ans, d.get('AB', 300001) + min([d.get('C', 300001), d.get('BC', 300001), d.get('AC', 300001)]))
ans = min(ans, d.get('AC', 300001) + min([d.get('B', 300001), d.get('BC', 300001), d.get('AB', 300001)]))
ans = min(ans, d.get('BC', 300001) + min([d.get('A', 300001), d.get('AB', 300001), d.get('AC', 300001)]))
ans = min(d.get('ABC', 300001), ans)
if ans != 300001:
	print(min(d.get('ABC', 300001), ans))
else:
	print(-1)


n = int(input())
mn = {frozenset('A'): float('inf'), frozenset('B'): float('inf'), frozenset('C'): float('inf'), frozenset('AB'): float('inf'), frozenset('AC'): float('inf'), frozenset('BC'): float('inf'), frozenset('ABC'): float('inf')}
for _ in range(n):
	(c, s) = map(str, input().split())
	c = int(c)
	s = frozenset(s)
	if mn[s] > c:
		mn[s] = c
ans = min(mn[frozenset('A')] + mn[frozenset('B')] + mn[frozenset('C')], mn[frozenset('AB')] + mn[frozenset('C')], mn[frozenset('AC')] + mn[frozenset('B')], mn[frozenset('BC')] + mn[frozenset('A')], mn[frozenset('ABC')], mn[frozenset('AB')] + mn[frozenset('BC')], mn[frozenset('AB')] + mn[frozenset('AC')], mn[frozenset('AC')] + mn[frozenset('BC')])
if ans == float('inf'):
	ans = -1
print(ans)


n = int(input())
m_ab = 0
m_ac = 0
m_cb = 0
a1 = 0
b1 = 0
c1 = 0
abc = 0
for i in range(n):
	h = []
	(c, v) = map(str, input().split())
	c = int(c)
	if v == 'AB' or v == 'BA':
		if m_ab == 0 or c < m_ab:
			m_ab = c
	elif v == 'AC' or v == 'CA':
		if m_ac == 0 or c < m_ac:
			m_ac = c
	elif v == 'BC' or v == 'CB':
		if m_cb == 0 or c < m_cb:
			m_cb = c
	elif v == 'A':
		if a1 == 0 or c < a1:
			a1 = c
	elif v == 'B':
		if b1 == 0 or c < b1:
			b1 = c
	elif v == 'C':
		if c1 == 0 or c < c1:
			c1 = c
	elif v == 'ABC' or v == 'ACB' or v == 'BCA' or (v == 'BAC') or (v == 'CBA') or (v == 'CAB'):
		if abc == 0 or c < abc:
			abc = c
k = []
if m_ab != 0 and c1 != 0:
	k.append(m_ab + c1)
if m_ac != 0 and b1 != 0:
	k.append(m_ac + b1)
if m_cb != 0 and a1 != 0:
	k.append(m_cb + a1)
if a1 != 0 and b1 != 0 and (c1 != 0):
	k.append(c1 + b1 + a1)
if abc != 0:
	k.append(abc)
if m_ab != 0 and m_ac != 0:
	k.append(m_ab + m_ac)
if m_ab != 0 and m_cb != 0:
	k.append(m_cb + m_ab)
if m_ac != 0 and m_cb != 0:
	k.append(m_ac + m_cb)
if k != []:
	print(min(k))
else:
	print(-1)


d = {'A': 0, 'B': 0, 'C': 0, 'AB': 0, 'BC': 0, 'AC': 0, 'ABC': 0}
n = int(input())
for i in range(n):
	(c, s) = input().split()
	c = int(c)
	if s == 'AB' or s == 'BA':
		if d['AB'] == 0:
			d['AB'] = c
		else:
			d['AB'] = min(d['AB'], c)
	elif s == 'BC' or s == 'CB':
		if d['BC'] == 0:
			d['BC'] = c
		else:
			d['BC'] = min(d['BC'], c)
	elif s == 'AC' or s == 'CA':
		if d['AC'] == 0:
			d['AC'] = c
		else:
			d['AC'] = min(d['AC'], c)
	elif s == 'A':
		if d['A'] == 0:
			d['A'] = c
		else:
			d['A'] = min(d['A'], c)
	elif s == 'B':
		if d['B'] == 0:
			d['B'] = c
		else:
			d['B'] = min(d['B'], c)
	elif s == 'C':
		if d['C'] == 0:
			d['C'] = c
		else:
			d['C'] = min(d['C'], c)
	elif sorted(s) == ['A', 'B', 'C']:
		if d['ABC'] == 0:
			d['ABC'] = c
		else:
			d['ABC'] = min(d['ABC'], c)
ans = 10 ** 6
if d['ABC']:
	ans = min(ans, d['ABC'])
if d['AB']:
	if d['C']:
		ans = min(ans, d['AB'] + d['C'])
	if d['BC']:
		ans = min(ans, d['AB'] + d['BC'])
	if d['AC']:
		ans = min(ans, d['AB'] + d['AC'])
if d['BC']:
	if d['A']:
		ans = min(ans, d['A'] + d['BC'])
	if d['AB']:
		ans = min(ans, d['AB'] + d['BC'])
	if d['AC']:
		ans = min(ans, d['BC'] + d['AC'])
if d['AC']:
	if d['B']:
		ans = min(ans, d['AC'] + d['B'])
	if d['AB']:
		ans = min(ans, d['AB'] + d['AC'])
	if d['BC']:
		ans = min(ans, d['BC'] + d['AC'])
if d['A'] and d['B'] and d['C']:
	ans = min(ans, d['A'] + d['B'] + d['C'])
if ans == 10 ** 6:
	print(-1)
else:
	print(ans)


def checkindex(a):
	check = [0, 0, 0]
	for index in a:
		if A[index] == 1:
			check[0] = 1
		if B[index] == 1:
			check[1] = 1
		if C[index] == 1:
			check[2] = 1
	if 0 not in check:
		return True

def sumprice(a):
	sump = 0
	for index in a:
		sump += price[index]
	return sump
n = int(input())
A = [0] * n
B = [0] * n
C = [0] * n
price = []
for i in range(n):
	(c, s) = map(str, input().rstrip().split())
	price.append(int(c))
	for ele in s:
		if ele == 'A':
			A[i] += 1
		elif ele == 'B':
			B[i] += 1
		else:
			C[i] += 1
if 1 not in A or 1 not in B or 1 not in C:
	print(-1)
else:
	indexABC = [price[i] for i in range(n) if A[i] == 1 and B[i] == 1 and (C[i] == 1)]
	minpriceACB = min(indexABC, default=999999999999999999999999999)
	indexAB = [price[i] for i in range(n) if A[i] == 1 and B[i] == 1]
	minpriceAB = min(indexAB, default=999999999999999999999999999)
	indexAC = [price[i] for i in range(n) if A[i] == 1 and C[i] == 1]
	minpriceAC = min(indexAC, default=999999999999999999999999999)
	indexBC = [price[i] for i in range(n) if B[i] == 1 and C[i] == 1]
	minpriceBC = min(indexBC, default=999999999999999999999999999)
	indexA = [price[i] for i in range(n) if A[i] == 1]
	minpriceA = min(indexA, default=999999999999999999999999999)
	indexB = [price[i] for i in range(n) if B[i] == 1]
	minpriceB = min(indexB, default=999999999999999999999999999)
	indexC = [price[i] for i in range(n) if C[i] == 1]
	minpriceC = min(indexC, default=999999999999999999999999999)
	minprice = min(minpriceACB, minpriceAB + minpriceAC, minpriceAB + minpriceC, minpriceAB + minpriceBC, minpriceAC + minpriceBC, minpriceAC + minpriceB, minpriceBC + minpriceA, minpriceA + minpriceB + minpriceC)
	print(minprice)


def print_return(func):

	def wrapper(*args, **kwargs):
		retval = func(*args, **kwargs)
		print(retval)
		return retval
	return wrapper

@print_return
def solve_a(n=None, m=None, bench_idx___people_nr=None):
	if n is None or m is None or bench_idx___people_nr is None:
		n = int(input())
		m = int(input())
		bench_idx___people_nr = [int(input()) for _ in range(n)]
	else:
		bench_idx___people_nr = [int(x) for x in bench_idx___people_nr.split('\n')]
	mx = max(bench_idx___people_nr) + m
	mn = (sum(bench_idx___people_nr) + m + n - 1) // n
	return '{} {}'.format(mn, mx)

@print_return
def solve_b(n=None, rest=None):
	if n is None:
		n = int(input())
		prices__vitamins = [input().split() for _ in range(n)]
		prices__vitamins = [(int(x[0]), list(x[1])) for x in prices__vitamins]
	else:
		prices__vitamins = [x.split() for x in rest.split('\n')]
		prices__vitamins = [(int(x[0]), list(x[1])) for x in prices__vitamins]
	prices__vitamins = sorted(prices__vitamins, key=lambda x: x[0])
	met_vitamins = []
	for (price, vitamins) in prices__vitamins:
		for vitamin in vitamins:
			if vitamin not in met_vitamins:
				met_vitamins.append(vitamin)
		if len(met_vitamins) > 2:
			break
	if len(met_vitamins) < 3:
		return -1
	min_price = 3 * 10 ** 6
	for (idx, pr_vt) in enumerate(prices__vitamins):
		(price, vitamins) = pr_vt
		curr_set = set(vitamins)
		curr_price = price
		for (idx2, pr_vt) in enumerate(prices__vitamins):
			if idx == idx2:
				continue
			(pr, vts) = pr_vt
			good = False
			for vt in vts:
				if vt not in curr_set:
					curr_set.add(vt)
					good = True
			if good:
				curr_price += pr
		min_price = min(curr_price, min_price)
	return min_price
solve_b()


from collections import defaultdict as dc
n = int(input())
p = dc(list)
for _ in range(n):
	(a, s) = [i for i in input().split()]
	a = int(a)
	s = sorted(s)
	p[str(s)].append(a)
a = []
if len(p["['C']"]) != 0 and len(p["['A']"]) != 0 and (len(p["['B']"]) != 0):
	a.append(min(p["['C']"]) + min(p["['B']"]) + min(p["['A']"]))
if len(p["['A', 'B']"]) != 0 and len(p["['C']"]) != 0:
	a.append(min(p["['A', 'B']"]) + min(p["['C']"]))
if len(p["['A', 'C']"]) != 0 and len(p["['B']"]) != 0:
	a.append(min(p["['A', 'C']"]) + min(p["['B']"]))
if len(p["['B', 'C']"]) != 0 and len(p["['A']"]) != 0:
	a.append(min(p["['B', 'C']"]) + min(p["['A']"]))
if len(p["['A', 'B', 'C']"]) != 0:
	a.append(min(p["['A', 'B', 'C']"]))
if len(p["['A', 'B']"]) != 0 and len(p["['B', 'C']"]) != 0:
	a.append(min(p["['A', 'B']"]) + min(p["['B', 'C']"]))
if len(p["['B', 'C']"]) != 0 and len(p["['A', 'C']"]) != 0:
	a.append(min(p["['A', 'C']"]) + min(p["['B', 'C']"]))
if len(p["['A', 'C']"]) != 0 and len(p["['A', 'B']"]) != 0:
	a.append(min(p["['A', 'B']"]) + min(p["['A', 'C']"]))
if len(a) == 0:
	print(-1)
else:
	print(min(a))


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial
from collections import defaultdict, Counter, OrderedDict, deque
from itertools import combinations, groupby, zip_longest, permutations
from bisect import *
from functools import reduce
from operator import mul
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
tup = lambda : map(int, sys.stdin.buffer.readline().split())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().strip('\n')
grid = lambda r: [lint() for i in range(r)]
stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
localsys = 0
start_time = time.time()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
d = {}
input = sys.stdin.readline
for _ in range(int(input())):
	(i, j) = input().split()
	i = int(i)
	j = ''.join(sorted(j))
	if j in d:
		d[j] = min(i, d[j])
	else:
		d[j] = i
m = maxx
if 'A' in d and 'B' in d and ('C' in d):
	m = d['A'] + d['B'] + d['C']
for i in d:
	x = i
	if 'A' in x and 'B' in x and ('C' in x):
		m = min(m, d[x])
	for j in d:
		X = x + j
		if 'A' in X and 'B' in X and ('C' in X):
			m = min(m, d[x] + d[j])
print(m) if m != maxx else print(-1)
if localsys:
	print('\n\nTime Elased :', time.time() - start_time, 'seconds')


n = int(input())
d = {'A': 10 ** 8, 'B': 10 ** 8, 'C': 10 ** 8, 'AB': 10 ** 8, 'AC': 10 ** 8, 'BC': 10 ** 8, 'ABC': 10 ** 8}
for i in range(n):
	(x, s) = input().split()[:2]
	x = int(x)
	s = ''.join(sorted(s))
	d[s] = min(x, d[s])
ans = min(d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['AB'] + d['BC'], d['AB'] + d['AC'], d['BC'] + d['AC'], d['ABC'])
if ans == 10 ** 8:
	print(-1)
else:
	print(ans)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
n = I()
d = defaultdict(int)
d = {'A': 10 ** 10, 'B': 10 ** 10, 'C': 10 ** 10, 'AB': 10 ** 10, 'AC': 10 ** 10, 'BC': 10 ** 10, 'ABC': 10 ** 10}
for i in range(n):
	(c, v) = input().split()
	v = list(v)
	v.sort()
	s = ''
	for j in v:
		s += j
	d[s] = min(d[s], int(c))
ans = min(d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['AB'] + d['AC'], d['AB'] + d['BC'], d['AC'] + d['BC'], d['ABC'])
if ans > 3 * 100000:
	print(-1)
else:
	print(ans)


n = int(input())
cost = {}
for _ in range(n):
	(c, s) = input().split()
	s = ''.join(sorted(s))
	c = int(c)
	if s in cost:
		cost[s] = min(cost[s], c)
	else:
		cost[s] = c
inf = float('inf')

def inc(c, excl=None):
	min_ = inf
	for k in cost:
		if c in k and k != excl:
			min_ = min(min_, cost[k])
	return min_
best = 0
best = [inc('ABC'), inc('AB') + min(inc('C'), inc('BC'), inc('AC'), inc('ABC')), inc('BC') + min(inc('A'), inc('AC'), inc('AB'), inc('ABC')), inc('AC') + min(inc('B'), inc('AB'), inc('BC'), inc('ABC')), inc('A') + inc('B') + inc('C')]
best = min(best)
if best == inf:
	print(-1)
else:
	print(best)


d = {}
d['A'] = []
d['B'] = []
d['C'] = []
d['AB'] = []
d['AC'] = []
d['BC'] = []
d['ABC'] = []
ans = 10 ** 18
for _ in range(int(input())):
	(c, s) = input().split()
	c = int(c)
	if len(s) == 3:
		d['ABC'].append(c)
	elif len(s) == 2:
		if 'A' in s and 'B' in s:
			d['AB'].append(c)
		elif 'A' in s and 'C' in s:
			d['AC'].append(c)
		elif 'B' in s and 'C' in s:
			d['BC'].append(c)
	elif s == 'A':
		d['A'].append(c)
	elif s == 'B':
		d['B'].append(c)
	else:
		d['C'].append(c)
	if len(d['ABC']) > 0:
		ans = min(ans, min(d['ABC']))
	if len(d['A']) > 0 and len(d['B']) > 0 and (len(d['C']) > 0):
		t = min(d['A']) + min(d['B']) + min(d['C'])
		ans = min(ans, t)
	if len(d['AB']) > 0 and len(d['BC']) > 0:
		t = min(d['AB']) + min(d['BC'])
		ans = min(ans, t)
	if len(d['AB']) > 0 and len(d['AC']) > 0:
		t = min(d['AB']) + min(d['AC'])
		ans = min(ans, t)
	if len(d['AC']) > 0 and len(d['BC']) > 0:
		t = min(d['AC']) + min(d['BC'])
		ans = min(ans, t)
	if len(d['AC']) > 0 and len(d['B']) > 0:
		t = min(d['AC']) + min(d['B'])
		ans = min(ans, t)
	if len(d['AB']) > 0 and len(d['C']) > 0:
		t = min(d['AB']) + min(d['C'])
		ans = min(ans, t)
	if len(d['BC']) > 0 and len(d['A']) > 0:
		t = min(d['BC']) + min(d['A'])
		ans = min(ans, t)
if ans == 10 ** 18:
	print(-1)
else:
	print(ans)


n = int(input())
c = []
s = []
sa = []
sb = []
sc = []
ka = kb = kc = 0
for i in range(n):
	(cc, ss) = [el for el in input().split()]
	c.append(int(cc))
	s.append(ss)
	if 'A' in ss:
		ka = 1
	if 'B' in ss:
		kb = 1
	if 'C' in ss:
		kc = 1
if ka == 0 or kb == 0 or kc == 0:
	print(-1)
	raise SystemExit()
ca = cb = cc = cab = cac = cbc = cabc = float('inf')
ia = ib = ic = iab = iac = ibc = iabc = 0
for i in range(0, n):
	if 'A' in s[i] and c[i] < ca:
		ca = c[i]
		ia = i
	if 'B' in s[i] and c[i] < cb:
		cb = c[i]
		ib = i
	if 'C' in s[i] and c[i] < cc:
		cc = c[i]
		ic = i
	if 'A' in s[i] and 'B' in s[i] and (c[i] < cab):
		cab = c[i]
		iab = i
	if 'A' in s[i] and 'C' in s[i] and (c[i] < cac):
		cac = c[i]
		iac = i
	if 'B' in s[i] and 'C' in s[i] and (c[i] < cbc):
		cbc = c[i]
		ibc = i
	if 'A' in s[i] and 'B' in s[i] and ('C' in s[i]) and (c[i] < cabc):
		cabc = c[i]
		iabc = i

def get_cost(*args):
	ka = kb = kc = cost = 0
	for i in args:
		if 'A' in s[i]:
			ka = 1
		if 'B' in s[i]:
			kb = 1
		if 'C' in s[i]:
			kc = 1
		cost += c[i]
	if ka == 0 or kb == 0 or kc == 0:
		return float('inf')
	else:
		return cost
cost = min(get_cost(ia, ib, ic), get_cost(ia, ib), get_cost(ia, ic), get_cost(ib, ic), get_cost(ia), get_cost(ib), get_cost(ic), get_cost(iab, ic), get_cost(iac, ib), get_cost(ibc, ia), get_cost(iabc))
print(cost)


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

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

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

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

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		d = {'A': Max, 'B': Max, 'C': Max, 'AB': Max, 'AC': Max, 'BC': Max, 'ABC': Max}
		for x in range(n):
			l = input().split(' ')
			temp = list(l[1])
			temp.sort()
			temp = ''.join(temp)
			d[temp] = min(d[temp], int(l[0]))
		ans = Max
		ans = min(ans, d['A'] + d['B'] + d['C'])
		ans = min(ans, d['A'] + d['BC'])
		ans = min(ans, d['B'] + d['AC'])
		ans = min(ans, d['C'] + d['AB'])
		ans = min(ans, d['AB'] + d['BC'])
		ans = min(ans, d['AC'] + d['BC'])
		ans = min(ans, d['AB'] + d['AC'])
		ans = min(ans, d['ABC'])
		if ans >= Max:
			print(-1)
		else:
			print(ans)
	except:
		pass
M = 998244353
P = 1000000007
Max = 100000000
for _ in range(1):
	main()


n = int(input())
A = []
B = []
C = []
AB = []
AC = []
BC = []
ABC = []
a = 0
b = 0
c = 0
ab = 0
ac = 0
bc = 0
abc = 0
for i in range(n):
	(l, s) = input().split()
	l = int(l)
	if len(s) == 1:
		if 'A' in s:
			A.append(l)
		if 'B' in s:
			B.append(l)
		if 'C' in s:
			C.append(l)
	elif len(s) == 2:
		if ('AB' in s) | ('BA' in s):
			AB.append(l)
		if ('AC' in s) | ('CA' in s):
			AC.append(l)
		if ('BC' in s) | ('CB' in s):
			BC.append(l)
	else:
		ABC.append(l)
A.append(99999999999999)
B.append(99999999999999)
C.append(99999999999999)
AB.append(99999999999999)
AC.append(99999999999999)
BC.append(99999999999999)
ABC.append(99999999999999)
A.sort()
AB.sort()
AC.sort()
BC.sort()
B.sort()
C.sort()
ABC.sort()
if min(A[0] + B[0] + C[0], ABC[0], AB[0] + AC[0], AB[0] + BC[0], AC[0] + BC[0], B[0] + AC[0], AB[0] + C[0], A[0] + BC[0]) == 99999999999999:
	print('-1')
else:
	print(min(A[0] + B[0] + C[0], ABC[0], AB[0] + AC[0], AB[0] + BC[0], AC[0] + BC[0], B[0] + AC[0], AB[0] + C[0], A[0] + BC[0]))


n = int(input())
dic = {}
dic.update({'A': 100001})
dic.update({'B': 100001})
dic.update({'C': 100001})
dic.update({'AB': 100001})
dic.update({'AC': 100001})
dic.update({'BC': 100001})
dic.update({'ABC': 100001})
for i in range(n):
	s = input().split()
	if 'A' in s[1] and 'B' in s[1] and ('C' in s[1]):
		if int(s[0]) < dic['ABC']:
			dic['ABC'] = int(s[0])
	elif 'A' in s[1] and 'B' in s[1]:
		if int(s[0]) < dic['AB']:
			dic['AB'] = int(s[0])
	elif 'A' in s[1] and 'C' in s[1]:
		if int(s[0]) < dic['AC']:
			dic['AC'] = int(s[0])
	elif 'C' in s[1] and 'B' in s[1]:
		if int(s[0]) < dic['BC']:
			dic['BC'] = int(s[0])
	elif 'A' in s[1]:
		if int(s[0]) < dic['A']:
			dic['A'] = int(s[0])
	elif 'B' in s[1]:
		if int(s[0]) < dic['B']:
			dic['B'] = int(s[0])
	elif 'C' in s[1]:
		if int(s[0]) < dic['C']:
			dic['C'] = int(s[0])
res1 = 500000
if dic['A'] < 100001 and dic['B'] < 100001 and (dic['C'] < 100001):
	res1 = dic['A'] + dic['B'] + dic['C']
res2 = 500000
if dic['AB'] < 100001 and dic['C'] < 100001:
	res2 = dic['AB'] + dic['C']
res3 = 500000
if dic['AC'] < 100001 and dic['B'] < 100001:
	res3 = dic['AC'] + dic['B']
res4 = 500000
if dic['BC'] < 100001 and dic['A'] < 100001:
	res4 = dic['BC'] + dic['A']
res5 = 500000
if dic['ABC'] < 100001:
	res5 = dic['ABC']
res6 = 500000
if dic['AB'] < 100001 and dic['BC'] < 100001:
	res6 = dic['AB'] + dic['BC']
res7 = 500000
if dic['AC'] < 100001 and dic['BC'] < 100001:
	res7 = dic['AC'] + dic['BC']
res8 = 500000
if dic['AC'] < 100001 and dic['AB'] < 100001:
	res4 = dic['AC'] + dic['AB']
mn1 = min(min(res1, res2), min(res3, res4))
mn2 = min(min(res5, res6), min(res7, res8))
mn = min(mn1, mn2)
if mn < 500000:
	print(mn)
else:
	print(-1)


import sys, string
res = {'A': [], 'B': [], 'C': [], 'AB': [], 'AC': [], 'BC': [], 'ABC': []}
n = int(sys.stdin.readline())
for _ in range(n):
	(a, b) = sys.stdin.readline().split()
	b = ''.join(sorted(b))
	res[b].append(int(a))
for i in res:
	res[i].sort()
ans = []
if res['A'] and res['B'] and res['C']:
	ans.append(res['A'][0] + res['B'][0] + res['C'][0])
if res['AB'] and res['C']:
	ans.append(res['AB'][0] + res['C'][0])
if res['AC'] and res['B']:
	ans.append(res['AC'][0] + res['B'][0])
if res['BC'] and res['A']:
	ans.append(res['BC'][0] + res['A'][0])
if res['AB'] and res['AC']:
	ans.append(res['AB'][0] + res['AC'][0])
if res['AB'] and res['BC']:
	ans.append(res['AB'][0] + res['BC'][0])
if res['AC'] and res['BC']:
	ans.append(res['AC'][0] + res['BC'][0])
if res['ABC']:
	ans.append(res['ABC'][0])
if ans:
	print(min(ans))
else:
	print(-1)


n = int(input())
a = [[0 for i in range(2)] for j in range(1111)]
ans = 1000000000
mi = 1000000000
for i in range(0, n):
	t = input()
	a[i] = t.split(' ')
	a[i][0] = int(a[i][0])
	if 'C' in a[i][1]:
		mi = min(mi, a[i][0])
	if len(a[i][1]) == 3:
		ans = min(ans, a[i][0])
for i in range(0, n):
	tc = 0
	ret = 0
	if 'A' in a[i][1]:
		ret = ret + a[i][0]
		if 'C' in a[i][1]:
			tc = 1
		if 'B' in a[i][1]:
			if tc == 1:
				ans = min(ans, ret)
			else:
				ans = min(ans, ret + mi)
			continue
		for j in range(0, n):
			if 'B' in a[j][1]:
				ret = ret + a[j][0]
				if 'C' in a[j][1] or tc == 1:
					ans = min(ans, ret)
				else:
					ans = min(ans, ret + mi)
				ret = ret - a[j][0]
if ans > 100000000:
	ans = -1
print(ans)


from sys import stdin
input = lambda : stdin.readline()
from math import ceil, sqrt, gcd
a = []
b = []
c = []
ab = []
bc = []
ac = []
abc = []
for i in range(int(input())):
	(n, s) = input().split()
	if {'A'} == set(list(s)):
		a.append(int(n))
	elif {'B'} == set(list(s)):
		b.append(int(n))
	elif {'C'} == set(list(s)):
		c.append(int(n))
	elif {'B', 'C'} == set(list(s)):
		bc.append(int(n))
	elif {'A', 'B'} == set(list(s)):
		ab.append(int(n))
	elif {'A', 'C'} == set(list(s)):
		ac.append(int(n))
	elif {'A', 'B', 'C'} == set(list(s)):
		abc.append(int(n))
abc.sort()
bc.sort()
ac.sort()
ab.sort()
c.sort()
a.sort()
b.sort()
m = 1000000000000000000
if len(a) > 0 and len(b) > 0 and (len(c) > 0):
	m = min(m, a[0] + b[0] + c[0])
if len(ab) > 0 and len(c) > 0:
	m = min(m, ab[0] + c[0])
if len(bc) > 0 and len(a) > 0:
	m = min(m, bc[0] + a[0])
if len(ac) > 0 and len(b) > 0:
	m = min(m, ac[0] + b[0])
if len(abc) > 0:
	m = min(abc[0], m)
if len(ab) > 0 and len(bc) > 0:
	m = min(m, ab[0] + bc[0])
if len(ab) > 0 and len(ac) > 0:
	m = min(m, ab[0] + ac[0])
if len(ac) > 0 and len(bc) > 0:
	m = min(m, ac[0] + bc[0])
if m == 1000000000000000000:
	print(-1)
else:
	print(m)


A = [10 ** 10] * 7
n = int(input())
for i in range(n):
	(price, vito) = input().split()
	price = int(price)
	if 'A' in vito and 'B' in vito and ('C' in vito):
		if price < A[6]:
			A[6] = price
	elif 'B' in vito and 'C' in vito:
		if price < A[5]:
			A[5] = price
	elif 'A' in vito and 'B' in vito:
		if price < A[4]:
			A[4] = price
	elif 'A' in vito and 'C' in vito:
		if price < A[3]:
			A[3] = price
	elif 'C' in vito:
		if price < A[2]:
			A[2] = price
	elif 'B' in vito:
		if price < A[1]:
			A[1] = price
	elif price < A[0]:
		A[0] = price
A[6] = min(A[6], A[5] + A[0], A[4] + A[2], A[3] + A[1], A[0] + A[1] + A[2], A[3] + A[5], A[3] + A[4], A[4] + A[5])
if A[6] == 10 ** 10:
	print(-1)
else:
	print(A[6])


x = int(input())
j = {}
a = []
for i in range(x):
	x = [str(i) for i in input().split()]
	if 'A' in x[1] and 'B' in x[1] and ('C' in x[1]):
		x[1] = 'ABC'
	elif 'B' in x[1] and 'C' in x[1]:
		x[1] = 'BC'
	elif 'B' in x[1] and 'A' in x[1]:
		x[1] = 'AB'
	elif 'A' in x[1] and 'C' in x[1]:
		x[1] = 'AC'
	if x[1] not in a:
		a += [x[1]]
		h = (x[1], int(x[0]))
		j.update([h])
	elif j[x[1]] > int(x[0]):
		j[x[1]] = int(x[0])
f = None
l = None
try:
	f = j['AB'] + j['AC']
	if l == None or l > f:
		l = f
except Exception:
	pass
try:
	f = j['AB'] + j['BC']
	if l == None or l > f:
		l = f
except Exception:
	pass
try:
	f = j['BC'] + j['AC']
	if l == None or l > f:
		l = f
except Exception:
	pass
try:
	f = j['A'] + j['B'] + j['C']
	if l == None or l > f:
		l = f
except Exception:
	pass
try:
	f = j['A'] + j['BC']
	if l == None or f < l:
		l = f
except Exception:
	pass
try:
	f = j['ABC']
	if l == None or f < l:
		l = f
except Exception:
	pass
try:
	f = j['AB'] + j['C']
	if l == None or f < l:
		l = f
except Exception:
	pass
try:
	f = j['AC'] + j['B']
	if l == None or f < l:
		l = f
except Exception:
	pass
if f == None:
	print(-1)
else:
	print(l)


d = [int(1e+18)] * 8
n = int(input())
d[0] = 0
for i in range(0, n):
	(c, s) = input().split()
	c = int(c)
	a = 0
	for j in range(0, len(s)):
		a |= 1 << ord(s[j]) - ord('A')
	for j in range(0, 8):
		d[a | j] = min(d[a | j], c + d[j])
print(-1 if d[7] >= 1e+18 else d[7])


import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')

def st():
	return list(sys.stdin.readline().strip())

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

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

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

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

def prl(n):
	return sys.stdout.write(str(n) + ' ')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	n = inp()
	d = defaultdict(lambda : INF)
	for i in range(n):
		(a, s) = input().split()
		a = int(a)
		s = ''.join(sorted(list(s)))
		d[s] = min(a, d[s])
	ans = INF
	ans = min(ans, d['A'] + d['B'] + d['C'])
	ans = min(ans, d['ABC'])
	ans = min(ans, d['A'] + d['BC'])
	ans = min(ans, d['AB'] + d['C'])
	ans = min(ans, d['AC'] + d['B'])
	ans = min(ans, d['AC'] + d['AB'])
	ans = min(ans, d['AB'] + d['BC'])
	ans = min(ans, d['AC'] + d['BC'])
	pr(ans if ans != INF else -1)
for _ in range(1):
	solve()


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))

def fn(s):
	res = 0
	if 'A' in s:
		res += 1 << 2
	if 'B' in s:
		res += 1 << 1
	if 'C' in s:
		res += 1 << 0
	return res
for _ in range(1):
	n = nmbr()
	PI = float('inf')
	dp = [PI] * 8
	dp[0] = 0
	for i in range(1, n + 1):
		(c, s) = input().split()
		c = int(c)
		mask = fn(s)
		for m in range(8):
			dp[m | mask] = min(dp[m | mask], dp[m] + c)
	print(dp[7] if dp[7] != PI else -1)


n = int(input())
a = []
for i in range(n):
	k = input().split()
	k[0] = int(k[0])
	a.append(k)
Ma = Mb = Mc = 10 ** 6
ans = 10 ** 6
for i in range(n):
	if 'A' in a[i][1]:
		Ma = min(Ma, a[i][0])
	if 'B' in a[i][1]:
		Mb = min(Mb, a[i][0])
	if 'C' in a[i][1]:
		Mc = min(Mc, a[i][0])
	if len(a[i][1]) == 3:
		ans = min(ans, a[i][0])
ans = min(ans, Ma + Mb + Mc)
for i in range(n):
	for j in range(i + 1, n):
		if 'A' in a[i][1] + a[j][1] and 'B' in a[i][1] + a[j][1] and ('C' in a[i][1] + a[j][1]):
			ans = min(ans, a[i][0] + a[j][0])
if ans >= 10 ** 6:
	ans = -1
print(ans)


c = int(input())
vabc = [1000000000.0]
va = [1000000000.0]
vb = [1000000000.0]
vc = [1000000000.0]
vab = [1000000000.0]
vbc = [1000000000.0]
vac = [1000000000.0]
flaga = 0
flagb = 0
flagc = 0
for i in range(c):
	(n, vit) = input().split()
	vit = sorted(vit)
	if len(vit) == 3:
		vabc.append(int(n))
		(flaga, flagb, flagc) = (1, 1, 1)
	elif len(vit) == 2:
		if vit[0] == 'A':
			if vit[1] == 'B':
				vab.append(int(n))
				(flaga, flagb) = (1, 1)
			else:
				vac.append(int(n))
				(flaga, flagc) = (1, 1)
		else:
			vbc.append(int(n))
			(flagb, flagc) = (1, 1)
	elif vit[0] == 'A':
		va.append(int(n))
		flaga = 1
	elif vit[0] == 'B':
		vb.append(int(n))
		flagb = 1
	else:
		flagc = 1
		vc.append(int(n))
if flaga == 1 and flagb == 1 and (flagc == 1):
	vab = sorted(vab)
	vac = sorted(vac)
	vbc = sorted(vbc)
	va = sorted(va)
	vb = sorted(vb)
	vc = sorted(vc)
	vabc = sorted(vabc)
	res = min(vabc[0], min(vab[0] + vc[0], min(vbc[0] + va[0], min(vac[0] + vb[0], min(va[0] + vb[0] + vc[0], min(vab[0] + vbc[0], min(vab[0] + vac[0], vac[0] + vbc[0])))))))
	print(res)
else:
	print('-1')


import sys

def __starting_point():
	n = int(next(sys.stdin))
	optimal = {ele: float('inf') for ele in [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (1, 1, 1)]}
	optimal[0, 0, 0] = 0
	for _ in range(n):
		(price, vitamin) = next(sys.stdin).strip().split()
		price = int(price)
		state = (int('A' in vitamin), int('B' in vitamin), int('C' in vitamin))
		for a in range(2):
			for b in range(2):
				for c in range(2):
					if optimal[a, b, c] < float('inf'):
						new_state = (a | state[0], b | state[1], c | state[2])
						optimal[new_state] = min(optimal[new_state], optimal[a, b, c] + price)
	print(-1 if optimal[1, 1, 1] == float('inf') else optimal[1, 1, 1])
__starting_point()


ni = int(input())
(one, two, three) = (dict(), dict(), 10 ** 6)
st = ''
for _ in range(ni):
	(n, s) = input().split()
	n = int(n)
	s = ''.join(sorted(s))
	st += s
	if len(s) == 1:
		if s not in one:
			one[s] = n
		else:
			one[s] = min(one[s], n)
	elif len(s) == 2:
		if s not in two:
			two[s] = n
		else:
			two[s] = min(two[s], n)
	else:
		three = min(three, n)
first = list(one.values())
second = sorted(list(two.values()))
if len(set(st)) < 3:
	print(-1)
else:
	ans = three
	if len(first) == 3:
		ans = min(ans, sum(list(one.values())))
	if len(second) >= 2:
		ans = min(ans, second[0] + second[1])
	if 'A' in one and 'BC' in two:
		ans = min(ans, one['A'] + two['BC'])
	if 'B' in one and 'AC' in two:
		ans = min(ans, one['B'] + two['AC'])
	if 'C' in one and 'AB' in two:
		ans = min(ans, one['C'] + two['AB'])
	print(ans)


list_keys = ['A', 'B', 'C', 'AB', 'BC', 'AC', 'ABC', '1', '2']

def sortStr(s):
	res = ''
	s = sorted(s)
	for c in s:
		res += c
	return res

def check(s1, s2, s3):
	s = s1 + s2 + s3
	return 'A' in s and 'B' in s and ('C' in s)
val = {}
INF = 1000000000.0
for key in list_keys:
	val[key] = INF
val['1'] = 0
val['2'] = 0
n = int(input())
for i in range(n):
	(v, s) = input().split()
	v = int(v)
	s = sortStr(s)
	if val[s] > v:
		val[s] = v
res = INF
for key1 in list_keys:
	for key2 in list_keys:
		for key3 in list_keys:
			if check(key1, key2, key3):
				res = min(res, val[key1] + val[key2] + val[key3])
if res == INF:
	res = -1
print(res)


import math
import sys
from bisect import bisect_right, bisect_left, insort_right
from collections import Counter, defaultdict
from heapq import heappop, heappush
from itertools import accumulate
from sys import stdout
R = lambda : map(int, input().split())
n = int(input())
dp = [math.inf] * 10
for _ in range(n):
	(c, s) = input().split()
	c = int(c)
	k = 0
	for x in s:
		k |= 1 << ord(x) - ord('A')
	dp[k] = min(dp[k], c)
res = math.inf
for i in range(8):
	if i == 7:
		res = min(res, dp[i])
	for j in range(i):
		if i | j == 7:
			res = min(res, dp[i] + dp[j])
		for k in range(j):
			if i | j | k == 7:
				res = min(res, dp[i] + dp[j] + dp[k])
print(res if res < math.inf else -1)


n = int(input())
m = [tuple(input().split()) for _ in range(n)]
for (i, e) in enumerate(m):
	if 'A' in e[1]:
		if 'B' in e[1]:
			if 'C' in e[1]:
				m[i] = (int(e[0]), 0)
			else:
				m[i] = (int(e[0]), -3)
		elif 'C' in e[1]:
			m[i] = (int(e[0]), -2)
		else:
			m[i] = (int(e[0]), 1)
	elif 'B' in e[1]:
		if 'C' in e[1]:
			m[i] = (int(e[0]), -1)
		else:
			m[i] = (int(e[0]), 2)
	else:
		m[i] = (int(e[0]), 3)
c = {0: [], 1: [], 2: [], 3: [], -1: [], -2: [], -3: []}
for (i, e) in enumerate(m):
	c[e[1]].append(i)
ct = 0
mi = 1000000.0
for i in m:
	if i[1] == 0:
		if i[0] < mi:
			mi = i[0]
	else:
		rem = 0 - i[1]
		if len(c[rem]) > 0:
			for j in c[rem]:
				if i[0] + m[j][0] < mi:
					mi = i[0] + m[j][0]
		if i[1] > 0:
			tmp = i[0]
			check = 1
			for j in range(1, 4):
				if j != i[1] and len(c[j]) > 0:
					mino = 1000000.0
					check += 1
					for k in c[j]:
						if m[k][0] < mino:
							mino = m[k][0]
					tmp += mino
			if check == 3 and tmp < mi:
				mi = tmp
		else:
			tmp = [i[0], i[0], i[0]]
			for j in range(-3, 0):
				if j != i[1] and len(c[j]) > 0:
					mino = 1000000.0
					for k in c[j]:
						if m[k][0] < mino:
							mino = m[k][0]
					tmp[abs(j) - 1] += mino
			check = 1000000.0
			for yy in tmp:
				if yy != i[0] and yy < check:
					check = yy
			if yy != 1000000.0 and check < mi:
				mi = check
if mi == 1000000.0:
	print(-1)
else:
	print(mi)


from collections import Counter
from itertools import permutations
nu = int(input())
d = {}
for i in range(nu):
	(n, s) = map(str, input().split())
	n = int(n)
	k = s
	k = list(k)
	k.sort()
	k = ''.join(k)
	s = k
	if s in d:
		d[k] = min(d[k], n)
	else:
		d[k] = n
j = {}
c = 0
cost = []
s = 0
j[1] = 0
j[2] = 0
j[3] = 0
for i in d:
	if len(i) == 1:
		c += 1
		s = s + d[i]
	elif len(i) == 3:
		cost.append(d[i])
	elif i.startswith('A') and i.endswith('B'):
		j[1] = d[i]
	elif i.startswith('A') and i.endswith('C'):
		j[2] = d[i]
	elif i.startswith('B') and i.endswith('C'):
		j[3] = d[i]
if j[1] != 0 and j[2] != 0:
	cost.append(j[1] + j[2])
if j[2] != 0 and j[3] != 0:
	cost.append(j[3] + j[2])
if j[1] != 0 and j[3] != 0:
	cost.append(j[1] + j[3])
if c == 3:
	cost.append(s)
if j[1] != 0 and 'C' in d:
	cost.append(j[1] + d['C'])
if j[3] != 0 and 'A' in d:
	cost.append(j[3] + d['A'])
if j[2] != 0 and 'B' in d:
	cost.append(j[2] + d['B'])
if len(cost) == 0:
	exit(print('-1'))
print(min(cost))


n = int(input())
csi = [input().split() for i in range(n)]
prices = [1000000000] * 3
for i in range(n):
	if 'A' in csi[i][1]:
		prices[0] = min(int(csi[i][0]), prices[0])
	if 'B' in csi[i][1]:
		prices[1] = min(int(csi[i][0]), prices[1])
	if 'C' in csi[i][1]:
		prices[2] = min(int(csi[i][0]), prices[2])
ans = sum(prices)
for i in range(n):
	if len(csi[i][1]) == 2:
		if not 'A' in csi[i][1]:
			ans = min(int(csi[i][0]) + prices[0], ans)
		if not 'B' in csi[i][1]:
			ans = min(int(csi[i][0]) + prices[1], ans)
		if not 'C' in csi[i][1]:
			ans = min(int(csi[i][0]) + prices[2], ans)
	if len(csi[i][1]) == 3:
		ans = min(ans, int(csi[i][0]))
if ans >= 1000000000:
	ans = -1
print(ans)


from itertools import product
INF = 10 ** 10

def main():
	n = int(input())
	juices = []
	for i in range(n):
		l = input().split()
		c = int(l[0])
		b = sorted(l[1])
		juices.append((c, b))
	mins = {}
	key = lambda p: ''.join(sorted(list(set(p))))
	for p in product('ABC', 'ABC', 'ABC'):
		mins[key(p)] = INF
	for j in juices:
		for p in product(j[1], j[1], j[1]):
			k = key(p)
			if mins[k] > j[0]:
				mins[k] = j[0]
	mq = mins.items()
	best = INF
	for (j1, c1) in mq:
		if key(j1) == 'ABC' and c1 < best:
			best = c1
	for (j1, c1) in mq:
		for (j2, c2) in mq:
			s = c1 + c2
			if key(j1 + j2) == 'ABC' and s < best:
				best = s
	for (j1, c1) in mq:
		for (j2, c2) in mq:
			for (j3, c3) in mq:
				s = c1 + c2 + c3
				if key(j1 + j2 + j3) == 'ABC' and s < best:
					best = s
	if best == INF:
		print(-1)
	else:
		print(best)
main()


def tobin(s):
	result = 0
	for c in s:
		b = ord(c) - ord('A')
		result |= 1 << b
	return result

def dp(i, value):
	if value == 7:
		return 0
	if i == -1:
		return float('inf')
	(c, s) = items[i]
	first = dp(i - 1, value)
	second = c + dp(i - 1, value | s)
	return min(first, second)
n = int(input())
items = []
dp = [float('inf')] * 7 + [0]
for i in range(n):
	(c, s) = input().split()
	(c, s) = (int(c), tobin(s))
	for (i, u) in enumerate(dp):
		dp[i] = min(dp[i], c + dp[i | s])
value = dp[0]
print(value if value != float('inf') else -1)


n = int(input())
a = []
for i in range(n):
	(p, v) = input().split()
	p = int(p)
	c = 0
	if 'A' in v:
		c += 1
	if 'B' in v:
		c += 2
	if 'C' in v:
		c += 4
	a.append([p, c])
halat = [1000000] * 8
for i in range(n):
	if a[i][0] < halat[a[i][1]]:
		halat[a[i][1]] = a[i][0]
javab = 1000000
javab = min(javab, halat[1] + halat[2] + halat[4])
javab = min(javab, halat[7])
javab = min(javab, halat[1] + halat[6])
javab = min(javab, halat[2] + halat[5])
javab = min(javab, halat[4] + halat[3])
javab = min(javab, halat[3] + halat[5])
javab = min(javab, halat[3] + halat[6])
javab = min(javab, halat[5] + halat[6])
if javab == 1000000:
	print(-1)
else:
	print(javab)


n = int(input())
c11 = []
na1 = []
for _ in range(n):
	(co, na) = input().split()
	co = int(co)
	na = ''.join(sorted(na))
	c11.append(co)
	na1.append(na)
a1 = [300001]
b1 = [300001]
c1 = [300001]
d1 = [300001]
e1 = [300001]
f1 = [300001]
g1 = [300001]
for i in range(n):
	if na1[i] == 'A':
		a1.append(c11[i])
	elif na1[i] == 'B':
		b1.append(c11[i])
	elif na1[i] == 'C':
		c1.append(c11[i])
	elif na1[i] == 'AB':
		d1.append(c11[i])
	elif na1[i] == 'BC':
		e1.append(c11[i])
	elif na1[i] == 'AC':
		f1.append(c11[i])
	elif na1[i] == 'ABC':
		g1.append(c11[i])
a = min(a1)
b = min(b1)
c = min(c1)
ab = min(d1)
bc = min(e1)
ac = min(f1)
abc = min(g1)
s = ''
for i in na1:
	s += i
if ''.join(sorted(list(set(s)))) != 'ABC':
	print('-1')
	exit(0)
ans = min(a + b + c, abc, ab + bc, ac + bc, ab + ac, ab + c, bc + a, ac + b)
print(ans)


price = [10 ** 9] * 8
price[0] = 0
v = ['A', 'B', 'C']
n = int(input())
for i in range(n):
	(x, y) = input().strip().split()
	x = int(x)
	y = list(y)
	st = 0
	for ch in y:
		pos = v.index(ch)
		st = st | 1 << pos
	for i in range(8):
		price[i | st] = min(price[i | st], price[i] + x)
print(price[-1] if price[-1] != 10 ** 9 else -1)


from sys import stdin, stdout
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
PI = float('inf')

def fn(pos, mask):
	if mask == 7:
		return 0
	if pos == 0:
		return PI
	inc = l[pos - 1][0] + fn(pos - 1, mask | l[pos - 1][1])
	skip = fn(pos - 1, mask)
	return min(inc, skip)
for _ in range(1):
	n = nmbr()
	l = []
	ans = PI
	for i in range(n):
		(nn, ss) = input().split()
		nn = int(nn)
		mask = 0
		if 'A' in ss:
			mask |= 1 << 0
		if 'B' in ss:
			mask |= 1 << 1
		if 'C' in ss:
			mask |= 1 << 2
		l += [[nn, mask]]
	dp = [[PI for i in range(8)] for i in range(n + 1)]
	dp[0][0] = 0
	for i in range(n):
		for j in range(7):
			dp[i + 1][j | l[i][1]] = min(dp[i + 1][j | l[i][1]], l[i][0] + dp[i][j])
			dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
	for i in range(n + 1):
		ans = min(dp[i][7], ans)
	print(ans if ans != PI else -1)


n = int(input())
y = lambda x: [int(x[0]), ''.join(sorted(list(x[1])))]
juices = [y(input().split(' ')) for i in range(n)]
d = {'A': 10000000, 'B': 10000000, 'C': 10000000, 'AB': 10000000, 'BC': 10000000, 'AC': 10000000, 'ABC': 10000000}
for i in juices:
	d[i[1]] = min(i[0], d[i[1]])
i1 = d['A'] + d['B'] + d['C']
i2 = d['A'] + d['BC']
i3 = d['ABC']
i4 = d['B'] + d['AC']
i5 = d['C'] + d['AB']
i6 = d['AB'] + d['BC']
i7 = d['AC'] + d['BC']
i8 = d['AC'] + d['AB']
ans = min(i1, i2, i3, i4, i5, i6, i7, i8)
if ans > 300000:
	print(-1)
else:
	print(ans)


n = int(input())
A = set()
B = set()
C = set()
costs = []
for _ in range(n):
	(cost, typ) = map(str, input().split())
	costs.append(int(cost))
	if 'A' in typ:
		A.add(_)
	if 'B' in typ:
		B.add(_)
	if 'C' in typ:
		C.add(_)
ans = 10 ** 10
if not len(A) or not len(B) or (not len(C)):
	ans = -1
else:
	minCost = min([costs[x] for x in C])
	for x in A:
		for y in B:
			cost = costs[x] + costs[y]
			if x == y:
				cost -= costs[y]
			if x not in C and y not in C:
				cost += minCost
			ans = min(ans, cost)
print(ans)


import math
a = [math.inf]
b = [math.inf]
c = [math.inf]
ab = [math.inf]
bc = [math.inf]
ac = [math.inf]
abc = [math.inf]
n = int(input())
for i in range(n):
	s = input().split()
	s[0] = int(s[0])
	if 'A' in s[1]:
		a.append(s[0])
		if 'B' in s[1]:
			b.append(s[0])
			if 'C' in s[1]:
				c.append(s[0])
				abc.append(s[0])
				ab.append(s[0])
				bc.append(s[0])
				ac.append(s[0])
			else:
				ab.append(s[0])
		elif 'C' in s[1]:
			c.append(s[0])
			ac.append(s[0])
		else:
			a.append(s[0])
	elif 'B' in s[1]:
		b.append(s[0])
		if 'C' in s[1]:
			c.append(s[0])
			bc.append(s[0])
		else:
			b.append(s[0])
	elif 'C' in s[1]:
		c.append(s[0])
	else:
		pass
a = min(a)
b = min(b)
c = min(c)
ab = min(ab)
bc = min(bc)
ac = min(ac)
abc = min(abc)
x = min(a + b + c, ab + c, bc + a, ac + b, abc)
if x < math.inf:
	print(x)
else:
	print(-1)


n = int(input())
cs = []
cp = 10 ** 100
(osa, osb, osc, twbc, twac, twab, trs) = ([], [], [], [], [], [], [])
for i in range(n):
	a1 = input().split()
	a = int(a1[0])
	if len(a1[1]) == 1:
		if a1[1] == 'A':
			osa.append(a)
		if a1[1] == 'B':
			osb.append(a)
		if a1[1] == 'C':
			osc.append(a)
	if len(a1[1]) == 2:
		if 'A' not in a1[1]:
			twbc.append(a)
		if 'B' not in a1[1]:
			twac.append(a)
		if 'C' not in a1[1]:
			twab.append(a)
	if len(a1[1]) == 3:
		trs.append(a)
osa.sort()
osb.sort()
osc.sort()
twbc.sort()
twac.sort()
twab.sort()
trs.sort()
osa.append(10 ** 50)
osb.append(10 ** 50)
osc.append(10 ** 50)
twbc.append(10 ** 50)
twac.append(10 ** 50)
twab.append(10 ** 50)
trs.append(10 ** 50)
cp = min(cp, osa[0] + osb[0] + osc[0])
cp = min(cp, osa[0] + twbc[0], osb[0] + twac[0], osc[0] + twab[0], twab[0] + twbc[0], twab[0] + twac[0], twbc[0] + twac[0])
cp = min(cp, trs[0])
if cp > 10 ** 30:
	print(-1)
else:
	print(cp)


arr = [1000000 for i in range(8)]
t = int(input())
for i in range(t):
	instr = input().split()
	pls = 0
	if instr[1].count('A'):
		pls += 1
	if instr[1].count('B'):
		pls += 2
	if instr[1].count('C'):
		pls += 4
	if int(instr[0]) < arr[pls]:
		arr[pls] = int(instr[0])
if arr[1] + arr[2] + arr[4] > arr[7]:
	ans = arr[7]
else:
	ans = arr[1] + arr[2] + arr[4]
for i in range(1, 7):
	for j in range(1, 7):
		if arr[i] + arr[j] < ans and i | j == 7:
			ans = arr[i] + arr[j]
if ans == 1000000:
	print(-1)
else:
	print(ans)


n = int(input())
c = [None for _ in range(n)]
vitss = [None for _ in range(n)]
for i in range(n):
	(ci, vits) = input().split()
	c[i] = int(ci)
	vitss[i] = vits

def aux(n, r):
	for vit in vitss[n]:
		r = r.replace(vit, '')
	return r
memo = {}

def solve(depth, x, req_vits):
	if (depth, x, req_vits) not in memo:
		res = 1000000000000
		for i in range(x, n):
			r = req_vits
			r = aux(i, r)
			if r != '':
				if depth < 2 and i + 1 < n:
					res = min(res, c[i] + solve(depth + 1, i + 1, r))
			else:
				res = min(res, c[i])
		memo[depth, x, req_vits] = res
		return res
	else:
		return memo[depth, x, req_vits]
if n > 2:
	if solve(0, 0, 'ABC') == 1000000000000:
		print(-1)
	else:
		print(solve(0, 0, 'ABC'))
elif n == 2:
	if solve(1, 0, 'ABC') == 1000000000000:
		print(-1)
	else:
		print(solve(1, 0, 'ABC'))
elif solve(2, 0, 'ABC') == 1000000000000:
	print(-1)
else:
	print(solve(2, 0, 'ABC'))


inf = 10 ** 10
n = int(input())
costs = {'A': inf, 'B': inf, 'C': inf, 'AB': inf, 'AC': inf, 'BC': inf, 'ABC': inf}
for i in range(n):
	(c, v) = input().split()
	lv = list(v)
	lv.sort()
	vs = ''.join(lv)
	costs[vs] = min(costs[vs], int(c))
costs['AB'] = min(costs['AB'], costs['A'] + costs['B'])
costs['AC'] = min(costs['AC'], costs['A'] + costs['C'])
costs['BC'] = min(costs['BC'], costs['B'] + costs['C'])
costs['ABC'] = min(costs['ABC'], costs['A'] + costs['B'] + costs['C'])
costs['ABC'] = min(costs['ABC'], costs['AB'] + costs['BC'])
costs['ABC'] = min(costs['ABC'], costs['AB'] + costs['AC'])
costs['ABC'] = min(costs['ABC'], costs['AC'] + costs['BC'])
costs['ABC'] = min(costs['ABC'], costs['A'] + costs['BC'])
costs['ABC'] = min(costs['ABC'], costs['B'] + costs['AC'])
costs['ABC'] = min(costs['ABC'], costs['C'] + costs['AB'])
r = costs['ABC'] if costs['ABC'] < inf else -1
print(r)


n = int(input())
c = []
s = ''
d = {}
(d['A'], d['B'], d['C'], d['AB'], d['BC'], d['AC'], d['ABC']) = (1000001, 1000001, 1000001, 1000001, 1000001, 1000001, 1000001)
for i in range(n):
	(u, v) = [i for i in input().split()]
	c.append(int(u))
	k = ''.join(sorted(v))
	s += k
	if d[k] > int(u):
		d[k] = int(u)
if 'A' in s and 'B' in s and ('C' in s):
	print(min(d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['ABC'], d['AB'] + d['AC'], d['BC'] + d['AC'], d['AB'] + d['BC']))
else:
	print(-1)


from sys import stdin, stdout
n = int(stdin.readline())
mp = {}
for i in range(n):
	(c, s) = stdin.readline().strip().split()
	s = ''.join(sorted(list(s)))
	c = int(c)
	if not s in mp:
		mp[s] = c
	else:
		mp[s] = min(mp[s], c)
ans = float('inf')
for v in mp:
	for c in mp:
		for g in mp:
			if len({v, c, g}) != 3:
				continue
			if len(set(list(v)) | set(list(c)) | set(list(g))) != 3:
				continue
			ans = min(ans, mp[v] + mp[c] + mp[g])
for c in mp:
	for g in mp:
		if len({c, g}) != 2:
			continue
		if len(set(list(c)) | set(list(g))) != 3:
			continue
		ans = min(ans, mp[c] + mp[g])
for v in mp:
	if len(set(list(v))) != 3:
		continue
	ans = min(ans, mp[v])
if ans == float('inf'):
	stdout.write('-1\n')
else:
	stdout.write(str(ans))


n = int(input())
d = {'ABC': 100000000, 'A': 100000000, 'B': 100000000, 'C': 100000000, 'AB': 100000000, 'BC': 100000000, 'AC': 100000000}
for i in range(n):
	n = input().split(' ')
	juice = int(n[0])
	vit = n[1]
	vit = ''.join(sorted(vit))
	if d.__contains__(vit):
		if d.get(vit) > juice:
			d.update({vit: juice})
	else:
		d.update({vit: juice})
min_cost = d.get('ABC')
if min_cost > d.get('A') + d.get('B') + d.get('C'):
	min_cost = d.get('A') + d.get('B') + d.get('C')
if min_cost > d.get('AB') + min(min(d.get('BC'), d.get('AC')), d.get('C')):
	min_cost = d.get('AB') + min(min(d.get('BC'), d.get('AC')), d.get('C'))
if min_cost > d.get('BC') + min(min(d.get('AB'), d.get('AC')), d.get('A')):
	min_cost = d.get('BC') + min(min(d.get('AB'), d.get('AC')), d.get('A'))
if min_cost > d.get('AC') + min(min(d.get('AB'), d.get('BC')), d.get('B')):
	min_cost = d.get('AC') + min(min(d.get('AB'), d.get('BC')), d.get('B'))
if min_cost >= 100000000:
	print('-1')
else:
	print(min_cost)


from math import log2
pricey = 9 ** 99
x = set()
match = ['A', 'B', 'C', 'AB', 'BC', 'AC', 'ABC']
n = int(input())
cheapest = [100001] * 7
for i in range(n):
	(p, s) = input().split()
	p = int(p)
	for c in s:
		x.add(c)
	s = ''.join(sorted(list(s)))
	j = match.index(s)
	cheapest[j] = min(cheapest[j], p)
if len(x) < 3:
	print(-1)
else:
	for i in range(1, 128):
		truth = [int(i) for i in '0' * (6 - int(log2(i))) + bin(i)[2:]]
		price = 0
		has = set()
		for (j, b) in enumerate(truth):
			if b:
				if cheapest[j] > 100000:
					break
				else:
					price += cheapest[j]
					for c in match[j]:
						has.add(c)
		else:
			if len(has) == 3:
				pricey = min(price, pricey)
	print(pricey)


m = float('+inf')
dict = {'A': m, 'B': m, 'C': m, 'AB': m, 'BC': m, 'AC': m, 'ABC': m}
n = int(input())
for i in range(n):
	k = input().split()
	k[0] = int(k[0])
	if 'A' in k[1]:
		dict['A'] = min(dict['A'], k[0])
	if 'B' in k[1]:
		dict['B'] = min(dict['B'], k[0])
	if 'C' in k[1]:
		dict['C'] = min(dict['C'], k[0])
	if 'A' in k[1] and 'B' in k[1]:
		dict['AB'] = min(dict['AB'], k[0])
	if 'A' in k[1] and 'C' in k[1]:
		dict['AC'] = min(dict['AC'], k[0])
	if 'C' in k[1] and 'B' in k[1]:
		dict['BC'] = min(dict['BC'], k[0])
	if 'A' in k[1] and 'B' in k[1] and ('C' in k[1]):
		dict['ABC'] = min(dict['ABC'], k[0])
a = min(dict['ABC'], dict['A'] + dict['B'] + dict['C'], dict['AB'] + dict['C'], dict['BC'] + dict['A'], dict['B'] + dict['AC'])
print(-1 if a == m else a)


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, **kwargs, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())
(N,) = getIntList()
dp = [10 ** 10 for i in range(10)]
dp[0] = 0
for _ in range(N):
	g = input().split()
	p = int(g[0])
	v = g[1]
	t = 0
	c = 0
	for x in 'ABC':
		if x in v:
			t = t | 1 << c
		c += 1
	for i in range(8):
		if dp[i] + p < dp[i | t]:
			dp[i | t] = dp[i] + p
if dp[7] == 10 ** 10:
	print(-1)
else:
	print(dp[7])


t = int(input())
m = 10000000
d = {}
d['A'] = m
d['B'] = m
d['C'] = m
d['AB'] = m
d['BC'] = m
d['AC'] = m
d['ABC'] = m
for i in range(t):
	(n, s) = input().split(' ')
	s = list(s)
	s.sort()
	s = ''.join(s)
	d[s] = min(d[s], int(n))
a = min(d['A'] + d['B'] + d['C'], d['AB'] + d['C'], d['BC'] + d['A'], d['AC'] + d['B'], d['AB'] + d['BC'], d['AB'] + d['AC'], d['BC'] + d['AC'], d['ABC'])
if a >= m:
	print('-1')
else:
	print(a)


n = int(input())
mxN = 10 ** 9 + 7
d = {'A': mxN, 'B': mxN, 'C': mxN, 'AB': mxN, 'BC': mxN, 'AC': mxN, 'ABC': mxN}
for i in range(n):
	(k, s) = input().split()
	s = ''.join(sorted(list(s)))
	d[s] = min(d[s], int(k))
ans = min(d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['ABC'], d['AB'] + d['BC'], d['AB'] + d['AC'], d['BC'] + d['AC'])
if ans >= mxN:
	ans = -1
print(ans)


n = int(input())
m = float('Inf')
d = {'A': m, 'B': m, 'C': m, 'AB': m, 'AC': m, 'BC': m, 'ABC': m}
A = 0
B = 0
C = 0
for i in range(n):
	(c, s) = input().split()
	c = int(c)
	s = s.strip()
	s = list(s)
	s.sort()
	s = ''.join(s)
	d[s] = min(d[s], c)
	if 'A' in s:
		A = 1
	if 'B' in s:
		B = 1
	if 'C' in s:
		C = 1
if A * B * C == 0:
	print(-1)
else:
	a = d['A']
	b = d['B']
	c = d['C']
	ab = d['AB']
	ac = d['AC']
	bc = d['BC']
	abc = d['ABC']
	print(min(a + b + c, ab + c, ab + ac, ab + bc, ac + b, ac + ab, ac + bc, bc + a, bc + ab, bc + ac, abc))


n = int(input())
a = []
b = []
for i in range(n):
	s = input().split()
	a.append(int(s[0]))
	b.append(s[1])
A = 10 ** 6
B = 10 ** 6
C = 10 ** 6
T = 10 ** 6
K = 10 ** 6
for i in range(n):
	if 'A' in b[i]:
		A = min(A, a[i])
	if 'B' in b[i]:
		B = min(B, a[i])
	if 'C' in b[i]:
		C = min(C, a[i])
	if 'A' in b[i] and 'B' in b[i] and ('C' in b[i]):
		T = min(T, a[i])
for i in range(n):
	for j in range(i + 1, n):
		if 'A' in b[i] + b[j] and 'B' in b[i] + b[j] and ('C' in b[i] + b[j]):
			K = min(K, a[i] + a[j])
ans = min(K, T, A + B + C)
if ans >= 10 ** 6:
	print(-1)
else:
	print(ans)


from itertools import combinations

def main():
	n = int(input())
	dict = {}
	for l in range(1, 7):
		for cc in combinations('ABC', l):
			dict[''.join(cc)] = 999999999999999999
	for i in range(n):
		(c, s) = input().split()
		c = int(c)
		s = ''.join(sorted(s))
		dict[s] = min(dict[s], c)
	result = min(dict['A'] + dict['B'] + dict['C'], dict['A'] + dict['BC'], dict['AB'] + dict['C'], dict['ABC'], dict['AC'] + dict['B'], dict['AC'] + dict['AB'], dict['AB'] + dict['BC'], dict['AC'] + dict['BC'])
	print(result if result < 999999999999999999 else -1)
main()


import sys
n = int(input())
CS = [list(input().split()) for i in range(n)]
LIST = [10 ** 6] * 7
for cs in CS:
	if 'A' in cs[1]:
		LIST[0] = min(LIST[0], int(cs[0]))
	if 'B' in cs[1]:
		LIST[1] = min(LIST[1], int(cs[0]))
	if 'C' in cs[1]:
		LIST[2] = min(LIST[2], int(cs[0]))
	if 'A' in cs[1] and 'B' in cs[1]:
		LIST[3] = min(LIST[3], int(cs[0]))
	if 'B' in cs[1] and 'C' in cs[1]:
		LIST[4] = min(LIST[4], int(cs[0]))
	if 'C' in cs[1] and 'A' in cs[1]:
		LIST[5] = min(LIST[5], int(cs[0]))
	if 'A' in cs[1] and 'B' in cs[1] and ('C' in cs[1]):
		LIST[6] = min(LIST[6], int(cs[0]))
if LIST[0] == 10 ** 6 or LIST[1] == 10 ** 6 or LIST[2] == 10 ** 6:
	print(-1)
else:
	print(min(LIST[0] + LIST[1] + LIST[2], LIST[0] + LIST[4], LIST[1] + LIST[5], LIST[2] + LIST[3], LIST[6]))


from sys import exit

def bin_seq(k, seq):
	if k == 0:
		bin2.append(seq)
		return
	bin_seq(k - 1, seq + [0])
	bin_seq(k - 1, seq + [1])
n = int(input())
d = dict()
for i in range(n):
	(c, s) = input().split()
	kek = ''.join(sorted(s))
	d[kek] = min(d.get(kek, float('inf')), int(c))
lol = list(d.keys())
bin2 = []
bin_seq(len(lol), [])
min_ = float('inf')
for x in bin2:
	arr = [False] * 3
	val = 0
	for i in range(len(x)):
		if x[i] == 1:
			val += d[lol[i]]
			for p in lol[i]:
				arr[ord(p) - ord('A')] = True
	if False in arr:
		continue
	min_ = min(min_, val)
if min_ == float('inf'):
	print(-1)
else:
	print(min_)


z = [1000000000] * 7
for i in range(int(input())):
	(a, b) = map(str, input().split())
	a = int(a)
	b = ''.join(sorted(b))
	if b == 'A':
		z[0] = min(z[0], a)
	elif b == 'B':
		z[1] = min(z[1], a)
	elif b == 'C':
		z[2] = min(z[2], a)
	elif b == 'AB':
		z[3] = min(z[3], a)
	elif b == 'BC':
		z[4] = min(z[4], a)
	elif b == 'AC':
		z[5] = min(z[5], a)
	else:
		z[6] = min(z[6], a)
z[3] = min(z[3], z[0] + z[1])
z[4] = min(z[4], z[1] + z[2])
z[5] = min(z[5], z[0] + z[2])
z[6] = min(z[6], z[3] + z[4], z[4] + z[5], z[5] + z[3])
z[6] = min(z[6], z[0] + z[4], z[1] + z[5], z[2] + z[3])
if z[6] == 1000000000:
	print(-1)
else:
	print(z[6])


def main():
	n = int(input())
	c = []
	s = []
	minA = 999999
	minB = 999999
	minC = 999999
	minAll = 999999
	answer = 999999
	for i in range(n):
		inp = input().split()
		c.append(int(inp[0]))
		s.append(inp[1])
		if s[i] == 'A' and c[i] < minA:
			minA = c[i]
		if s[i] == 'B' and c[i] < minB:
			minB = c[i]
		if s[i] == 'C' and c[i] < minC:
			minC = c[i]
		if s[i].find('A') >= 0 and s[i].find('B') >= 0 and (s[i].find('C') >= 0):
			if c[i] < minAll:
				minAll = c[i]
	if minA != 999999 and minB != 999999 and (minC != 999999):
		answer = minA + minB + minC
	if minAll < answer:
		answer = minAll
	strS = str(s)
	if strS.find('A') == -1 or strS.find('B') == -1 or strS.find('C') == -1:
		print(-1)
		exit(0)
	for i in range(n):
		for j in range(i + 1, n):
			if c[i] + c[j] < answer:
				string = s[i] + s[j]
				if string.find('A') > -1 and string.find('B') > -1 and (string.find('C') > -1):
					answer = c[i] + c[j]
	if answer == 999999:
		answer = -1
	print(answer)
main()


t = int(input())
ab = [300001]
bc = [300001]
ac = [300001]
a = [300001]
b = [300001]
c = [300001]
sol = []
abc = [300001]
p = 0
q = 0
r = 0
for i in range(t):
	inp = list(input().split())
	inp[0] = int(inp[0])
	if inp[1] == 'AB' or inp[1] == 'BA':
		if ab[0] > inp[0]:
			ab[0] = inp[0]
		p += 1
		q += 1
	elif inp[1] == 'AC' or inp[1] == 'CA':
		if ac[0] > inp[0]:
			ac[0] = inp[0]
		p += 1
		r += 1
	elif inp[1] == 'BC' or inp[1] == 'CB':
		if bc[0] > inp[0]:
			bc[0] = inp[0]
		q += 1
		r += 1
	elif inp[1] == 'A':
		if a[0] > inp[0]:
			a[0] = inp[0]
		p += 1
	elif inp[1] == 'B':
		if b[0] > inp[0]:
			b[0] = inp[0]
		q += 1
	elif inp[1] == 'C':
		if c[0] > inp[0]:
			c[0] = inp[0]
		r += 1
	else:
		if abc[0] > inp[0]:
			abc[0] = inp[0]
		p += 1
		q += 1
		r += 1
if p < 1 or q < 1 or r < 1:
	print(-1)
else:
	sol.append(a[0] + b[0] + c[0])
	sol.append(a[0] + bc[0])
	sol.append(ac[0] + bc[0])
	sol.append(ab[0] + bc[0])
	sol.append(b[0] + ac[0])
	sol.append(c[0] + ab[0])
	sol.append(ac[0] + ab[0])
	sol.append(abc[0])
	print(min(sol))


n = int(input())
abc = []
ab = []
bc = []
ca = []
a = []
b = []
c = []
for i in range(n):
	(c1, s) = input().split()
	if 'A' in s and 'B' in s and ('C' in s):
		abc.append(int(c1))
	elif 'A' in s and 'B' in s:
		ab.append(int(c1))
	elif 'B' in s and 'C' in s:
		bc.append(int(c1))
	elif 'C' in s and 'A' in s:
		ca.append(int(c1))
	elif 'A' in s:
		a.append(int(c1))
	elif 'B' in s:
		b.append(int(c1))
	else:
		c.append(int(c1))
abc.sort()
ab.sort()
bc.sort()
ca.sort()
a.sort()
b.sort()
c.sort()
d = []
if len(abc) > 0:
	d.append(abc[0])
if len(ab) > 0:
	if len(bc) > 0:
		d.append(ab[0] + bc[0])
	if len(ca) > 0:
		d.append(ab[0] + ca[0])
	if len(c) > 0:
		d.append(ab[0] + c[0])
if len(bc) > 0:
	if len(ab) > 0:
		d.append(bc[0] + ab[0])
	if len(ca) > 0:
		d.append(bc[0] + ca[0])
	if len(a) > 0:
		d.append(bc[0] + a[0])
if len(ca) > 0:
	if len(ab) > 0:
		d.append(ca[0] + ab[0])
	if len(bc) > 0:
		d.append(ca[0] + bc[0])
	if len(b) > 0:
		d.append(ca[0] + b[0])
if len(a) > 0 and len(b) > 0 and (len(c) > 0):
	d.append(a[0] + b[0] + c[0])
if len(d) == 0:
	print(-1)
else:
	print(min(d))


d = {'A': 100000000.0, 'B': 100000000.0, 'C': 100000000.0, 'AB': 100000000.0, 'BC': 100000000.0, 'AC': 100000000.0, 'ABC': 100000000.0}
for _ in range(int(input())):
	(a, b) = input().split()
	a = int(a)
	b = ''.join(sorted(list(b)))
	d[b] = min(d.get(b), a)
ans = min(d['A'] + d['B'] + d['C'], d['AB'] + d['BC'], d['BC'] + d['AC'], d['AB'] + d['AC'], d['ABC'], d['BC'] + d['A'], d['AB'] + d['C'], d['AC'] + d['B'])
if ans >= 100000000.0:
	print(-1)
else:
	print(ans)


l = []
for i in range(int(input())):
	ln = input().split(' ')
	ln[0] = int(ln[0])
	l.append(ln)
combos = [['A', 'BC'], ['B', 'AC'], ['C', 'AB'], ['A', 'B', 'C'], ['ABC'], ['AB', 'BC'], ['AB', 'AC'], ['AC', 'BC']]
sm = 10000000000
for i in combos:
	mins = [1000000000] * len(i)
	for j in l:
		if ''.join(sorted(j[1])) in i:
			ind = i.index(''.join(sorted(j[1])))
			mins[ind] = min(mins[ind], j[0])
	sm = min(sm, sum(mins))
if sm > 10000000:
	print(-1)
else:
	print(sm)


n = int(input())
arr = []
for i in range(n):
	(a, b) = map(str, input().split())
	a = int(a)
	''.join(sorted(b))
	arr.append((a, b))
arr.sort()
s1 = ''
flag = 0
i = 1
ans = 0
fans = 10000000000000000000000000
arrz = [0] * n
ans = arr[0][0]
arrz[0] = 1
s1 += arr[0][1]
if 'A' in s1 and 'B' in s1 and ('C' in s1):
	fans = min(ans, fans)
while i < n:
	flag = 0
	for k in range(i):
		if arr[i][1] in arr[k][1]:
			flag = 1
			break
	if flag == 0:
		ans += arr[i][0]
		s1 += arr[i][1]
		arrz[i] = 1
		for k in range(i):
			if arr[k][1] in arr[i][1] and arrz[k] == 1:
				ans -= arr[k][0]
				arrz[k] = 0
	if 'A' in s1 and 'B' in s1 and ('C' in s1):
		fans = min(ans, fans)
	i += 1
if fans == 10000000000000000000000000:
	print(-1)
else:
	print(fans)


n = int(input())
one = {}
two = {}
three = {}

def compare(num, vit, Dict):
	if vit not in Dict:
		Dict[vit] = num
	elif num < Dict[vit]:
		Dict[vit] = num
	return Dict
for i in range(n):
	IN = input().split()
	num = int(IN[0])
	vit = ''.join(sorted(IN[1]))
	len_vit = len(vit)
	if len_vit == 1:
		one = compare(num, vit, one)
	elif len_vit == 2:
		two = compare(num, vit, two)
	else:
		three = compare(num, vit, three)
Min = None
try:
	Min = one['A'] + one['B'] + one['C']
except:
	None
if Min == None and 'ABC' in three:
	Min = three['ABC']
elif 'ABC' in three:
	if three['ABC'] < Min:
		Min = three['ABC']
if 'A' in one and 'BC' in two:
	val = one['A'] + two['BC']
	if Min == None:
		Min = val
		minset = ['A', 'BC']
	elif val < Min:
		Min = val
if 'B' in one and 'AC' in two:
	val = one['B'] + two['AC']
	if Min == None:
		Min = val
		minset = ['B', 'AC']
	elif val < Min:
		Min = val
if 'C' in one and 'AB' in two:
	val = one['C'] + two['AB']
	if Min == None:
		Min = val
		minset = ['C', 'AB']
	elif val < Min:
		Min = val
KEYS = list(two.keys())
K = len(KEYS)
for i in range(K):
	for j in range(i + 1, K):
		key_i = KEYS[i]
		key_j = KEYS[j]
		val = two[key_i] + two[key_j]
		if Min == None:
			Min = val
			minset = [key_i, key_j]
		elif val < Min:
			Min = val
			minset = [key_i, key_j]
if Min == None:
	print(-1)
else:
	print(Min)


t = int(input())
cost = {}
for _ in range(t):
	(val, name) = input().split()
	if name in cost:
		cost[name] = min(cost[name], int(val))
	else:
		cost[name] = int(val)
minCostA = float('inf')
minCostB = float('inf')
minCostC = float('inf')
minCostAB = float('inf')
minCostBC = float('inf')
minCostAC = float('inf')
minCostABC = float('inf')
for i in cost:
	c = cost[i]
	if i == 'A':
		minCostA = min(minCostA, c)
	elif i == 'B':
		minCostB = min(minCostB, c)
	elif i == 'C':
		minCostC = min(minCostC, c)
	elif i == 'AB' or i == 'BA':
		minCostAB = min(minCostAB, c)
	elif i == 'BC' or i == 'CB':
		minCostBC = min(minCostBC, c)
	elif i == 'AC' or i == 'CA':
		minCostAC = min(minCostAC, c)
	elif ''.join(sorted(i)) == 'ABC':
		minCostABC = min(minCostABC, c)
final = min(minCostA + minCostB + minCostC, minCostA + minCostBC, minCostB + minCostAC, minCostC + minCostAB, minCostAB + minCostBC, minCostAB + minCostAC, minCostBC + minCostAB, minCostBC + minCostAC, minCostABC)
if final == float('inf'):
	print(-1)
else:
	print(final)


def solve(arr):
	s = []
	for i in arr:
		s += list(i)
	s = set(s)
	if len(s) == 3:
		return True
	else:
		return False
n = int(input())
arr = []
d = {'A': 10 ** 100, 'B': 10 ** 100, 'C': 10 ** 100, 'AB': 10 ** 100, 'AC': 10 ** 100, 'BC': 10 ** 100, 'ABC': 10 ** 100}
for _ in range(n):
	(a, b) = input().strip().split()
	a = int(a)
	b = ''.join(sorted(list(b)))
	d[b] = min(d[b], a)
minn = 10 ** 110
for i in d.keys():
	for j in d.keys():
		for k in d.keys():
			if solve([i, j, k]):
				minn = min(minn, d[i] + d[j] + d[k])
for i in d.keys():
	for j in d.keys():
		if solve([i, j]):
			minn = min(minn, d[i] + d[j])
minn = min(minn, d['ABC'])
if minn >= 10 ** 100:
	print(-1)
else:
	print(minn)


n = int(input())

def missins(duo):
	pres = {'A', 'B', 'C'}
	for i in duo:
		pres.discard(i)
	return list(pres)[0]
vits = []
for i in range(n):
	vits.append(input().split())
l = ['A', 'B', 'C', 'all']
d = {x: float('inf') for x in l}
duos = {}
for (i, j) in vits:
	i = int(i)
	if len(j) == 1:
		d[j] = min(d[j], i)
	elif len(j) == 3:
		d['all'] = min(d['all'], i)
	elif j in duos:
		duos[j] = min(duos[j], i)
	else:
		duos[j] = i
dl = list(duos.keys())
s = 0
for i in 'ABC':
	s += d[i]
d['all'] = min(d['all'], s)
for i in range(len(dl)):
	miss = missins(dl[i])
	d['all'] = min(d['all'], d[miss] + duos[dl[i]])
	for j in range(i + 1, len(dl)):
		if miss in dl[j]:
			d['all'] = min(d['all'], duos[dl[i]] + duos[dl[j]])
if d['all'] == float('inf'):
	print(-1)
else:
	print(d['all'])


from sys import stdin, stdout
from itertools import combinations
(solo_mins, duo_mins, trio_min) = ({'A': 10 ** 10, 'B': 10 ** 10, 'C': 10 ** 10}, {'AB': 10 ** 10, 'BC': 10 ** 10, 'AC': 10 ** 10}, 10 ** 10)
minimum = 10 ** 10
for _ in range(int(stdin.readline())):
	(cost, vita) = stdin.readline().split()
	cost = int(cost)
	vita = sorted(vita)
	vita = ''.join(vita)
	if vita in ['A', 'B', 'C']:
		solo_mins[vita] = min(solo_mins[vita], cost)
	elif vita in ['AB', 'BC', 'AC']:
		duo_mins[vita] = min(duo_mins[vita], cost)
	else:
		trio_min = min(trio_min, cost)
minis = 10 ** 10
minis = min(minis, solo_mins['A'] + solo_mins['B'] + solo_mins['C'])
minis = min(minis, solo_mins['A'] + duo_mins['BC'])
minis = min(minis, solo_mins['B'] + duo_mins['AC'])
minis = min(minis, solo_mins['C'] + duo_mins['AB'])
minis = min(minis, duo_mins['AB'] + duo_mins['BC'])
minis = min(minis, duo_mins['AC'] + duo_mins['BC'])
minis = min(minis, duo_mins['AB'] + duo_mins['AC'])
minis = min(minis, trio_min)
if minis == 10 ** 10:
	stdout.write('-1')
else:
	stdout.write(str(minis))


n = int(input())
d = {'A': 0, 'B': 0, 'C': 0, 'AB': 0, 'AC': 0, 'BC': 0, 'ABC': 0}
for i in range(n):
	(c, s) = input().split(' ')
	c = int(c)
	s = str(s)
	if s.count('A') > 0:
		if s.count('B') > 0:
			if s.count('C') > 0:
				if d['ABC'] == 0:
					d['ABC'] = c
				elif c < d['ABC']:
					d['ABC'] = c
			elif d['AB'] == 0:
				d['AB'] = c
			elif c < d['AB']:
				d['AB'] = c
		elif s.count('C') > 0:
			if d['AC'] == 0:
				d['AC'] = c
			elif c < d['AC']:
				d['AC'] = c
		elif d['A'] == 0:
			d['A'] = c
		elif c < d['A']:
			d['A'] = c
	elif s.count('B') > 0:
		if s.count('C') > 0:
			if d['BC'] == 0:
				d['BC'] = c
			elif c < d['BC']:
				d['BC'] = c
		elif d['B'] == 0:
			d['B'] = c
		elif c < d['B']:
			d['B'] = c
	elif d['C'] == 0:
		d['C'] = c
	elif c < d['C']:
		d['C'] = c
if d['A'] == 0 and d['AB'] == 0 and (d['AC'] == 0) and (d['ABC'] == 0):
	print(-1)
	exit()
if d['B'] == 0 and d['AB'] == 0 and (d['BC'] == 0) and (d['ABC'] == 0):
	print(-1)
	exit()
if d['C'] == 0 and d['AC'] == 0 and (d['BC'] == 0) and (d['ABC'] == 0):
	print(-1)
	exit()
l = list()
if d['A'] != 0 and d['B'] != 0 and (d['C'] != 0):
	l.append(d['A'] + d['B'] + d['C'])
if d['A'] != 0 and d['BC'] != 0:
	l.append(d['A'] + d['BC'])
if d['B'] != 0 and d['AC'] != 0:
	l.append(d['B'] + d['AC'])
if d['C'] != 0 and d['AB'] != 0:
	l.append(d['C'] + d['AB'])
if d['AB'] != 0 and d['AC'] != 0:
	l.append(d['AB'] + d['AC'])
if d['AB'] != 0 and d['BC'] != 0:
	l.append(d['AB'] + d['BC'])
if d['AC'] != 0 and d['BC'] != 0:
	l.append(d['AC'] + d['BC'])
if d['ABC'] != 0:
	l.append(d['ABC'])
print(min(l))


n = int(input())
isC = False
isB = False
isA = False
d = {}
for i in range(n):
	line = input()
	c = int(line.split(' ')[0])
	s = line.split(' ')[1]
	if 'A' in s:
		isA = True
	if 'B' in s:
		isB = True
	if 'C' in s:
		isC = True
	name = ''.join(sorted(s))
	try:
		d[name] = min(c, d[name])
	except:
		d[name] = c
if not isA or not isB or (not isC):
	print(-1)
else:
	arr = []
	try:
		arr.append(d['A'] + d['B'] + d['C'])
	except:
		pass
	try:
		arr.append(d['A'] + d['BC'])
	except:
		pass
	try:
		arr.append(d['AB'] + d['C'])
	except:
		pass
	try:
		arr.append(d['AC'] + d['B'])
	except:
		pass
	try:
		arr.append(d['AB'] + d['BC'])
	except:
		pass
	try:
		arr.append(d['AC'] + d['BC'])
	except:
		pass
	try:
		arr.append(d['AB'] + d['AC'])
	except:
		pass
	try:
		arr.append(d['ABC'])
	except:
		pass
	try:
		arr.append(d['ABC'])
	except:
		pass
	print(min(arr))


m = 10 ** 9 + 7
d = {'A': m, 'B': m, 'C': m, 'AB': m, 'AC': m, 'BC': m, 'ABC': m}
for _ in range(int(input())):
	(n, s) = input().split()
	s = list(s)
	s.sort()
	n = int(n)
	for i in range(len(s)):
		d[s[i]] = min(n, d[s[i]])
	d[''.join(s)] = min(n, d[''.join(s)])
ans = 10 ** 9 + 7
ans = min(ans, d['A'] + d['BC'])
ans = min(ans, d['B'] + d['AC'])
ans = min(ans, d['C'] + d['AB'])
ans = min(ans, d['ABC'])
ans = min(ans, d['A'] + d['B'] + d['C'])
if ans >= 10 ** 9 + 7:
	print(-1)
else:
	print(ans)


from math import *
N = int(input())
DP = [inf] * 8
DP[0] = 0
for n in range(N):
	(v, T) = input().split()
	cur = 0
	for t in T:
		if t == 'A':
			cur |= 1
		elif t == 'B':
			cur |= 2
		else:
			cur |= 4
	DP[cur] = min(DP[cur], int(v))
for mask1 in range(8):
	for mask2 in range(8):
		DP[mask1 | mask2] = min(DP[mask1 | mask2], DP[mask1] + DP[mask2])
if DP[-1] == inf:
	print(-1)
else:
	print(DP[-1])


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for _ in range(1):
	n = nmbr()
	l = []
	mna = mnb = mnc = ans = float('inf')
	for _ in range(n):
		(c, s) = input().split()
		c = int(c)
		s = ''.join(sorted(list(s)))
		if s == 'A':
			mna = min(mna, c)
		elif s == 'B':
			mnba = min(mnb, c)
		elif s == 'C':
			mnc = min(mnc, c)
		elif s == 'ABC':
			ans = min(ans, c)
		l += [[c, s]]
	for i in range(n):
		for j in range(i + 1, n):
			c = l[i][0] + l[j][0]
			s = l[i][1] + l[j][1]
			s = set(s)
			if len(s) == 1:
				continue
			elif len(s) == 2:
				if 'A' not in s:
					ans = min(ans, c + mna)
				elif 'B' not in s:
					ans = min(ans, c + mnb)
				elif 'C' not in s:
					ans = min(ans, c + mnc)
			else:
				ans = min(ans, c)
	print(ans if ans != float('inf') else -1)


from typing import List
JMIXTURES = 7

def joice_embedding(juice: str) -> int:
	if juice == 'A':
		return 0
	elif juice == 'B':
		return 1
	elif juice == 'C':
		return 2
	elif juice == 'AB' or juice == 'BA':
		return 3
	elif juice == 'AC' or juice == 'CA':
		return 4
	elif juice == 'BC' or juice == 'CB':
		return 5
	else:
		return 6

def solv(price: List[int], vitamis: List[str]) -> int:
	best_price = [float('inf')] * JMIXTURES
	curr_prices = [float('inf')] * JMIXTURES
	for i in range(len(price)):
		vit = joice_embedding(vitamis[i])
		curr_prices[vit] = price[i]
		for j in range(JMIXTURES):
			best_price[j] = min(best_price[j], curr_prices[j])
		for (j, (vit1, vit2)) in zip(range(3, 6), ((0, 1), (0, 2), (1, 2))):
			best_price[j] = min(best_price[j], curr_prices[vit1] + curr_prices[vit2])
		best_price[6] = min(best_price[6], curr_prices[0] + curr_prices[1] + curr_prices[2])
		for (j, vit1) in zip(range(3), (5, 4, 3)):
			best_price[6] = min(best_price[6], best_price[j] + best_price[vit1])
		for (m1, m2) in ((3, 4), (3, 5), (4, 5)):
			best_price[6] = min(best_price[6], best_price[m1] + best_price[m2])
	return best_price[6]
LINES = int(input())
prices = []
vitamins = []
for _ in range(LINES):
	(price, mixture) = input().split()
	prices.append(int(price))
	vitamins.append(mixture)
rez = solv(prices, vitamins)
print(-1 if rez == float('inf') else rez)


n = int(input())
choices = {}

def get_price(pick, choices):
	if pick not in choices:
		return -1
	return choices[pick]
for _ in range(n):
	(price, line) = input().split()
	price = int(price)
	line = ''.join(sorted(line))
	if line not in choices:
		choices[line] = price
	else:
		choices[line] = min(choices[line], price)
cheapest = float('inf')
for path in [['A', 'BC'], ['AB', 'C'], ['AC', 'B'], ['A', 'B', 'C'], ['ABC'], ['AB', 'BC'], ['AB', 'AC'], ['AC', 'BC']]:
	cost = 0
	failed = False
	for option in path:
		price = get_price(option, choices)
		if price == -1:
			failed = True
			break
		else:
			cost += price
	if not failed:
		cheapest = min(cheapest, cost)
if cheapest != float('inf'):
	print(cheapest)
else:
	print(-1)


from sys import stdout, stdin
import math
n = int(input())
text = stdin.read()
(a, b, c) = [[], [], []]
d = {i: [] for i in range(1, 8)}
if not 'A' in text or not 'B' in text or (not 'C' in text):
	print(-1)
else:
	l = text.split('\n')
	for z in l:
		try:
			(c, t) = z.split()
			d[4 * ('A' in t) + 2 * ('B' in t) + ('C' in t)].append(int(c))
		except:
			continue
	p = {k: min(l) if l else 333333 for (k, l) in d.items()}
	print(min(p[7], min(p[1], p[3], p[5]) + p[6], min(p[2], p[3], p[6]) + p[5], min(p[4], p[5], p[6]) + p[3], min(p[1], p[3], p[5]) + min(p[2], p[3], p[6]) + min(p[4], p[6], p[5])))


from sys import stdin
input = stdin.readline
n = int(input())
lst = [0] * n
dp = {'A': float('inf'), 'B': float('inf'), 'C': float('inf'), 'AB': float('inf'), 'BC': float('inf'), 'AC': float('inf'), 'ABC': float('inf')}
for i in range(n):
	(a, b) = input().strip().split()
	s = ''.join(sorted(b))
	dp[s] = min(int(a), dp.get(s))
ans = float('inf')
ans = min(ans, dp.get('A') + dp.get('B') + dp.get('C'))
ans = min(ans, dp.get('A') + dp.get('BC'))
ans = min(ans, dp.get('AB') + dp.get('C'))
ans = min(ans, dp.get('AC') + dp.get('B'))
ans = min(ans, dp.get('AC') + dp.get('BC'))
ans = min(ans, dp.get('AB') + dp.get('BC'))
ans = min(ans, dp.get('AB') + dp.get('AC'))
ans = min(ans, dp.get('ABC'))
if ans == float('inf'):
	print(-1)
else:
	print(ans)


n = int(input())
d = dict()
for i in range(n):
	(a, b) = map(str, input().split())
	b = list(b)
	b.sort()
	b = ''.join(b)
	if b in d:
		d[b] = min(d[b], int(a))
	else:
		d[b] = int(a)
a = 999999 if 'A' not in d else d['A']
b = 999999 if 'B' not in d else d['B']
ab = 999999 if 'AB' not in d else d['AB']
c = 999999 if 'C' not in d else d['C']
ac = 999999 if 'AC' not in d else d['AC']
bc = 999999 if 'BC' not in d else d['BC']
abc = 999999 if 'ABC' not in d else d['ABC']
ans = min(min(min(min(abc, a + b + c), min(ab + bc, ab + c)), min(ac + bc, ac + b)), min(ab + ac, bc + a))
if ans >= 999999:
	ans = -1
print(ans)


n = int(input())
inf = 100000000000000
a = inf
b = inf
c = inf
ab = inf
ac = inf
bc = inf
abc = inf
for i in range(n):
	(x, y) = map(str, input().split())
	x = int(x)
	if 'A' in y:
		a = min(x, a)
	if 'B' in y:
		b = min(x, b)
	if 'C' in y:
		c = min(x, c)
	if 'A' in y and 'B' in y:
		ab = min(ab, x)
	if 'A' in y and 'C' in y:
		ac = min(ac, x)
	if 'B' in y and 'C' in y:
		bc = min(bc, x)
	if 'A' in y and 'B' in y and ('C' in y):
		abc = min(abc, x)
ab = min(ab, a + b)
bc = min(bc, b + c)
ac = min(ac, a + c)
abc = min(abc, ab + c, a + bc, b + ac)
if abc == inf:
	abc = -1
print(abc)


n = int(input())
m = {'A': 10 ** 6, 'B': 10 ** 6, 'C': 10 ** 6, 'ABC': 10 ** 6}
prices = []
for i in range(n):
	(price, vit) = input().split()
	price = int(price)
	vit = ''.join(sorted(vit))
	if vit in m:
		m[vit] = min(m[vit], price)
	else:
		m[vit] = price
ans = 10 ** 6
ans = min(m['A'] + m['B'] + m['C'], m['ABC'])
whole = set('ABC')
keys = list(m.keys())
for i in range(len(keys) - 1):
	for j in range(i + 1, len(keys)):
		if whole == set(keys[i] + keys[j]):
			ans = min(ans, m[keys[i]] + m[keys[j]])
print(ans if ans != 10 ** 6 else -1)


from collections import defaultdict
import math
al = {}
a = int(input())
al = defaultdict(lambda : math.inf, al)
for i in range(a):
	(x, y) = input().split()
	y = sorted(y)
	if 'A' in y:
		al['A'] = min(al['A'], int(x))
	if 'B' in y:
		al['B'] = min(al['B'], int(x))
	if 'C' in y:
		al['C'] = min(al['C'], int(x))
	if 'A' in y and 'B' in y:
		al['AB'] = min(al['AB'], int(x))
	if 'C' in y and 'B' in y:
		al['BC'] = min(al['BC'], int(x))
	if 'A' in y and 'B' in y and ('C' in y):
		al['ABC'] = min(al['ABC'], int(x))
	if 'A' in y and 'C' in y:
		al['AC'] = min(al['AC'], int(x))
temp = min(al['A'] + al['BC'], al['B'] + al['AC'], al['C'] + al['AB'], al['ABC'], al['A'] + al['B'] + al['C'])
if temp == math.inf:
	print(-1)
else:
	print(temp)


from math import inf

def main():
	count = int(input())
	arr = [inf for x in range(8)]
	arr[0] = 0
	for x in range(count):
		string = input().split()
		value = int(string[0])
		types = list(string[1])
		store = 0
		for y in types:
			if y == 'A':
				store += 1
			elif y == 'B':
				store += 2
			elif y == 'C':
				store += 4
		for y in range(8):
			arr[y | store] = min(arr[y | store], value + arr[y])
	if arr[7] == inf:
		print(-1)
	else:
		print(arr[7])
main()


from collections import defaultdict
n = int(input())
my_dick = dict()
my_dick = defaultdict(lambda : 10000000000.0 + 1)
for i in range(n):
	x = list(input().split())
	x[0] = int(x[0])
	my_dick[''.join(sorted(x[1]))] = min(my_dick[''.join(sorted(x[1]))], x[0])
ok = True
vit = ['A', 'B', 'C', 'AB', 'BC', 'AC', 'ABC']
ans = 0
if not ok:
	print(-1)
else:
	for i in range(3):
		ans += my_dick[vit[i]]
	ans = min(ans, my_dick['A'] + my_dick['BC'], my_dick['B'] + my_dick['AC'], my_dick['C'] + my_dick['AB'], my_dick['ABC'])
	ans = min(ans, my_dick['AB'] + my_dick['BC'], my_dick['AC'] + my_dick['AB'], my_dick['BC'] + my_dick['AC'])
	if ans >= 10000000000.0 + 1:
		print(-1)
	else:
		print(ans)


n = int(input())
d = {'A': 10000000, 'B': 1000000000, 'C': 1000000000, 'AB': 1000000000, 'BC': 1000000000, 'AC': 10000000000, 'ABC': 10000000000}
for i in range(n):
	(a1, a2) = map(str, input().strip().split())
	l1 = list(a2)
	l1.sort()
	a2 = ''.join(l1)
	a1 = int(a1)
	d[a2] = min(a1, d[a2])
o = min(d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['AB'] + d['BC'], d['AC'] + d['AB'], d['AC'] + d['BC'], d['ABC'])
if o > 300000:
	print(-1)
else:
	print(o)


import math
n = int(input())
minA = minB = minC = minAB = minAC = minBC = minABC = 300005
a = []
s = []
for i in range(n):
	(ai, si) = input().split()
	ai = int(ai)
	a.append(ai)
	s.append(si)
	if si == 'A':
		minA = min(ai, minA)
	elif si == 'B':
		minB = min(ai, minB)
	elif si == 'C':
		minC = min(ai, minC)
for i in range(n):
	if s[i] == 'AB' or s[i] == 'BA':
		minAB = min(a[i], minAB)
	elif s[i] == 'AC' or s[i] == 'CA':
		minAC = min(a[i], minAC)
	elif s[i] == 'BC' or s[i] == 'CB':
		minBC = min(a[i], minBC)
minAB = min(minAB, minA + minB)
minAC = min(minAC, minA + minC)
minBC = min(minBC, minB + minC)
for i in range(n):
	if s[i] == 'ABC' or s[i] == 'ACB' or s[i] == 'BCA' or (s[i] == 'BAC') or (s[i] == 'CAB') or (s[i] == 'CBA'):
		minABC = min(a[i], minABC)
minABC = min(minABC, minAB + minC, minAC + minB, minBC + minA, minA + minB + minC, minAB + minAC, minAB + minBC, minAC + minBC)
if minABC == 300005:
	print('-1')
else:
	print(minABC)


import math
n = int(input())
map = {}
map['ABC'] = math.inf
map['AB'] = math.inf
map['AC'] = math.inf
map['BC'] = math.inf
map['A'] = math.inf
map['B'] = math.inf
map['C'] = math.inf
for i in range(0, n):
	data_input = input()
	data_input = data_input.split(' ')
	cost = int(data_input[0])
	vitamin = data_input[1]
	if 'A' in vitamin and 'B' in vitamin and ('C' in vitamin) and (cost < map['ABC']):
		map['ABC'] = cost
	if 'A' in vitamin and 'B' in vitamin and (cost < map['AB']):
		map['AB'] = cost
	if 'A' in vitamin and 'C' in vitamin and (cost < map['AC']):
		map['AC'] = cost
	if 'B' in vitamin and 'C' in vitamin and (cost < map['BC']):
		map['BC'] = cost
	if 'A' in vitamin and cost < map['A']:
		map['A'] = cost
	if 'B' in vitamin and cost < map['B']:
		map['B'] = cost
	if 'C' in vitamin and cost < map['C']:
		map['C'] = cost
res = []
res.append(map['ABC'])
res.append(map['AB'] + map['C'])
res.append(map['AC'] + map['B'])
res.append(map['BC'] + map['A'])
res.append(map['A'] + map['B'] + map['C'])
res.sort()
if res[0] == math.inf:
	print(-1)
else:
	print(res[0])


import sys
n = int(input())
liste = []
for i in range(n):
	dizi = list(input('').split())
	liste.append(dizi)
dizi1 = []
dizi2 = []
dizi3 = []
for i in liste:
	i[1] = ''.join(sorted(i[1]))
	if len(i[1]) == 1:
		dizi1.append([int(i[0]), i[1]])
	if len(i[1]) == 2:
		dizi2.append([int(i[0]), i[1]])
	if len(i[1]) == 3:
		dizi3.append([int(i[0]), i[1]])
ab = sys.maxsize
ac = sys.maxsize
bc = sys.maxsize
for i in dizi2:
	if i[1] == 'AB':
		if i[0] < ab:
			ab = i[0]
	if i[1] == 'AC':
		if i[0] < ac:
			ac = i[0]
	if i[1] == 'BC':
		if i[0] < bc:
			bc = i[0]
mina = sys.maxsize
minc = sys.maxsize
minb = sys.maxsize
for i in dizi1:
	if i[1] == 'A':
		if i[0] < mina:
			mina = i[0]
	if i[1] == 'B':
		if i[0] < minb:
			minb = i[0]
	if i[1] == 'C':
		if i[0] < minc:
			minc = i[0]
a = False
min3 = sys.maxsize
for i in dizi3:
	if i[0] < min3:
		min3 = i[0]
sonuc = sys.maxsize
if mina != sys.maxsize and minb != sys.maxsize and (minc != sys.maxsize):
	if sonuc > mina + minb + minc:
		sonuc = mina + minb + minc
		a = True
if ab != sys.maxsize:
	if ac != sys.maxsize:
		if sonuc > ab + ac:
			sonuc = ab + ac
			a = True
if ab != sys.maxsize:
	if bc != sys.maxsize:
		if sonuc > ab + bc:
			sonuc = ab + bc
			a = True
if ac != sys.maxsize:
	if bc != sys.maxsize:
		if sonuc > ac + bc:
			sonuc = ac + bc
			a = True
if min3 != sys.maxsize:
	if sonuc > min3:
		sonuc = min3
		a = True
if mina != sys.maxsize and bc != sys.maxsize:
	if sonuc > mina + bc:
		sonuc = mina + bc
		a = True
if minb != sys.maxsize and ac != sys.maxsize:
	if sonuc > minb + ac:
		sonuc = minb + ac
		a = True
if minc != sys.maxsize and ab != sys.maxsize:
	if sonuc > minc + ab:
		sonuc = minc + ab
		a = True
if a:
	print(sonuc)
else:
	print(-1)


import math

def main():
	n = int(input())
	val = []
	best = [-1] * 8
	minimizer = -1
	for i in range(n):
		temp = input().split()
		charss = [char for char in str(temp[1])]
		g = 0
		for char in charss:
			if char == 'A':
				g = g + 1
			if char == 'B':
				g = g + 2
			if char == 'C':
				g = g + 4
			if best[g] == -1 or best[g] > int(temp[0]):
				best[g] = int(temp[0])
	for a in [1, 3, 5, 7]:
		if best[a] == -1:
			continue
		if a == 7 and (minimizer == -1 or minimizer > best[a]):
			minimizer = best[a]
		if a == 3 and best[4] != -1 and (minimizer == -1 or minimizer > best[a] + best[4]):
			minimizer = best[a] + best[4]
		for b in [2, 6]:
			if best[b] == -1:
				continue
			if ((a == 3 or a == 1) and b == 6 or a == 5) and (minimizer == -1 or minimizer > best[a] + best[b]):
				minimizer = best[a] + best[b]
			if best[4] != -1 and (minimizer == -1 or minimizer > best[4] + best[a] + best[b]):
				minimizer = best[a] + best[b] + best[4]
	print(minimizer)
main()


import sys
input = sys.stdin.readline
n = int(input())
v_dic = {}
v_in_list = []
vitamins = ['A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']
for vitamin in vitamins:
	v_dic[vitamin] = 10 ** 6
for _ in range(n):
	(c, v) = map(str, input().split())
	v_list = [*v]
	v_in_list += v_list
	if len(v_list) > 1:
		v_list.sort()
		v = ''.join(v_list)
	else:
		v = str(v)
	v_dic[v] = min(v_dic[v], int(c))
if len(set(v_in_list)) == 3:
	ans = min(v_dic['A'] + v_dic['B'] + v_dic['C'], v_dic['A'] + v_dic['B'] + v_dic['AC'], v_dic['A'] + v_dic['AB'] + v_dic['C'], v_dic['A'] + v_dic['BC'], v_dic['B'] + v_dic['A'] + v_dic['BC'], v_dic['B'] + v_dic['AB'] + v_dic['C'], v_dic['B'] + v_dic['AC'], v_dic['C'] + v_dic['A'] + v_dic['BC'], v_dic['C'] + v_dic['AC'] + v_dic['B'], v_dic['C'] + v_dic['AB'], v_dic['AB'] + v_dic['AC'], v_dic['AB'] + v_dic['BC'], v_dic['AC'] + v_dic['BC'], v_dic['ABC'])
else:
	ans = -1
print(ans)


n = input()
n = int(n)
a = float('inf')
b = float('inf')
c = float('inf')
abc = float('inf')
res = float('inf')
arr = []
while n:
	n -= 1
	(cost, s) = input().split()
	cost = int(cost)
	if s == 'A':
		a = min(cost, a)
	elif s == 'B':
		b = min(cost, b)
	elif s == 'C':
		c = min(cost, c)
	elif 'A' in s and 'B' in s and ('C' in s):
		abc = min(cost, abc)
	arr.append([cost, s])
res = min(res, a + b + c, abc)
for (cost1, s1) in arr:
	if 'A' in s1:
		for (cost2, s2) in arr:
			if 'B' in s2:
				if 'C' in s1 or 'C' in s2:
					res = min(res, cost1 + cost2)
for (cost1, s1) in arr:
	if 'A' in s1:
		for (cost2, s2) in arr:
			if 'C' in s2:
				if 'B' in s1 or 'B' in s2:
					res = min(res, cost1 + cost2)
if res == float('inf'):
	res = -1
print(res)


import math
from itertools import compress, product
(n, dic) = (int(input()), {})
for _ in range(n):
	(price, vitamins) = input().split()
	price = int(price)
	vitamins = tuple(sorted(list(vitamins)))
	try:
		x = dic[vitamins]
		dic[vitamins] = min(x, price)
	except KeyError:
		dic[vitamins] = price
data = list(dic.keys())
selector = list(product([0, 1], repeat=len(dic)))
power_set = [list(compress(data, select)) for select in selector]
ans = math.inf
for x in power_set:
	res = 0
	(A, B, C) = (0, 0, 0)
	for e in x:
		if 'A' in e:
			A = 1
		if 'B' in e:
			B = 1
		if 'C' in e:
			C = 1
		res += dic[tuple(e)]
	if A and B and C:
		ans = min(ans, res)
print(ans if ans != math.inf else -1)


n = int(input())
d = {'A': 100001, 'B': 100001, 'C': 100001, 'AB': 100001, 'BC': 100001, 'AC': 100001, 'ABC': 100001}
for _ in range(n):
	(c, s) = input().split()
	c = int(c)
	temp = ''.join(sorted(s))
	if c < d[temp]:
		d[temp] = c
for item in d:
	if d[item] == 100001:
		d[item] = 300001
ans = min(d['A'] + d['B'] + d['C'], d['ABC'], d['AB'] + d['C'], d['BC'] + d['A'], d['AC'] + d['B'], d['AB'] + d['BC'], d['BC'] + d['AC'], d['AB'] + d['AC'])
if ans == 300001:
	print(-1)
else:
	print(ans)


n = int(input())
a = [[10 ** 6] for i in range(7)]
for i in range(n):
	b = input().split()
	c = int(b[0])
	s = b[1]
	if s == 'A':
		a[0].append(c)
	elif s == 'B':
		a[1].append(c)
	elif s == 'C':
		a[2].append(c)
	elif s == 'AB' or s == 'BA':
		a[3].append(c)
	elif s == 'BC' or s == 'CB':
		a[4].append(c)
	elif s == 'AC' or s == 'CA':
		a[5].append(c)
	elif len(s) == 3:
		a[6].append(c)
res = min(min(a[0]) + min(a[1]) + min(a[2]), min(a[3]) + min(a[2]), min(a[4]) + min(a[0]), min(a[5]) + min(a[1]), min(a[6]), min(a[3]) + min(a[4]), min(a[3]) + min(a[5]), min(a[4]) + min(a[5]))
if res < 10 ** 6:
	print(res)
else:
	print(-1)


def dp(a):
	d = {}
	for i in a:
		x = i[1]
		l = ''.join(sorted(list(set(x))))
		d[l] = min(d.get(l, float('inf')), i[0])
		for j in set(x):
			d[j] = min(d.get(j, float('inf')), i[0])
	ans = []
	for word in d.keys():
		t = d[word]
		for l in 'ABC':
			if l not in word:
				t += d.get(l, float('inf'))
		ans.append(t)
	rr = min(ans)
	if rr == float('inf'):
		return -1
	return rr
blanck = []
for i in range(int(input())):
	(a, b) = map(str, input().strip().split())
	blanck.append((int(a), b))
print(dp(blanck))


n = int(input())
costs = [10 ** 18 for i in range(1 << 3)]
for i in range(n):
	(cs, s) = input().split()
	cs = int(cs)
	msk = 0
	if 'A' in s:
		msk |= 1
	if 'B' in s:
		msk |= 2
	if 'C' in s:
		msk |= 4
	costs[msk] = min(costs[msk], cs)
for msk in [3, 5, 6, 7]:
	for m1 in range(8):
		for m2 in range(8):
			if m1 | m2 == msk:
				costs[msk] = min(costs[msk], costs[m1] + costs[m2])
if costs[7] == 10 ** 18:
	print(-1)
else:
	print(costs[7])


n = int(input())
dic = {'A': 'A', 'B': 'B', 'C': 'C', 'AB': 'AB', 'BA': 'AB', 'AC': 'AC', 'CA': 'AC', 'BC': 'BC', 'CB': 'BC', 'ABC': 'ABC', 'ACB': 'ABC', 'BAC': 'ABC', 'BCA': 'ABC', 'CAB': 'ABC', 'CBA': 'ABC'}
fun = {'A': [], 'B': [], 'C': [], 'AB': [], 'AC': [], 'BC': [], 'ABC': []}
for _ in range(n):
	s = input().split()
	fun[dic[s[1]]].append(int(s[0]))
ans = []
if len(fun['ABC']) > 0:
	ans.append(min(fun['ABC']))
if len(fun['AB']) > 0 and len(fun['BC']) > 0:
	ans.append(min(fun['AB']) + min(fun['BC']))
if len(fun['AB']) > 0 and len(fun['AC']) > 0:
	ans.append(min(fun['AB']) + min(fun['AC']))
if len(fun['AC']) > 0 and len(fun['BC']) > 0:
	ans.append(min(fun['AC']) + min(fun['BC']))
if len(fun['A']) > 0 and len(fun['B']) > 0 and (len(fun['C']) > 0):
	ans.append(min(fun['A']) + min(fun['B']) + min(fun['C']))
if len(fun['A']) > 0 and len(fun['BC']) > 0:
	ans.append(min(fun['A']) + min(fun['BC']))
if len(fun['B']) > 0 and len(fun['AC']) > 0:
	ans.append(min(fun['B']) + min(fun['AC']))
if len(fun['C']) > 0 and len(fun['AB']) > 0:
	ans.append(min(fun['C']) + min(fun['AB']))
try:
	print(min(ans))
except:
	print(-1)


from functools import reduce
n = int(input())
a = []
s = set()
d = dict()
m = 900000000
for i in range(n):
	(price, v) = input().split()
	price = int(price)
	s |= set(v)
	v = ''.join(tuple(sorted(v)))
	if not d.get(v):
		d[v] = price
	elif d[v] > price:
		d[v] = price
if len(s) < 3:
	print(-1)
else:
	if 'ABC' in d.keys() and d['ABC'] < m:
		m = d['ABC']
	if 'C' in d.keys() and 'B' in d.keys() and ('A' in d.keys()):
		m = min(d['C'] + d['A'] + d['B'], m)
	if 'C' in d.keys() and 'AB' in d.keys():
		m = min(d['C'] + d['AB'], m)
	if 'A' in d.keys() and 'BC' in d.keys():
		m = min(d['A'] + d['BC'], m)
	if 'B' in d.keys() and 'AC' in d.keys():
		m = min(d['B'] + d['AC'], m)
	if 'AC' in d.keys() and 'AB' in d.keys():
		m = min(d['AC'] + d['AB'], m)
	if 'BC' in d.keys() and 'AB' in d.keys():
		m = min(d['BC'] + d['AB'], m)
	if 'AC' in d.keys() and 'BC' in d.keys():
		m = min(d['AC'] + d['BC'], m)
	print(m)


n = int(input())
map_s = {'C': 4, 'B': 2, 'A': 1}
g = []
g_C = []
g_B = []
g_A = []
g_AC = []
g_AB = []
g_BC = []
for _ in range(n):
	(c, s) = input().split()
	c = int(c)
	value = sum((map_s[s] for s in s))
	g.append((value, c))
	if value & 4:
		g_C.append((value, c))
	if value & 2:
		g_B.append((value, c))
	if value & 1:
		g_A.append((value, c))
	if value & 6:
		g_BC.append((value, c))
	if value & 3:
		g_AB.append((value, c))
	if value & 5:
		g_AC.append((value, c))
min_C = min((item[1] for item in g_C)) if g_C else 10 ** 6
min_B = min((item[1] for item in g_B)) if g_B else 10 ** 6
min_A = min((item[1] for item in g_A)) if g_A else 10 ** 6
min_AC = 10 ** 6
for i in range(len(g_AC)):
	item = g_AC[i]
	if item[0] & 5 == 5:
		min_AC = min(min_AC, item[1])
		continue
	if item[0] & 4:
		min_AC = min(min_AC, item[1] + min_A)
	if item[0] & 1:
		min_AC = min(min_AC, item[1] + min_C)
min_AB = 10 ** 6
for i in range(len(g_AB)):
	item = g_AB[i]
	if item[0] & 3 == 3:
		min_AB = min(min_AB, item[1])
		continue
	if item[0] & 2:
		min_AB = min(min_AB, item[1] + min_A)
	if item[0] & 1:
		min_AB = min(min_AB, item[1] + min_B)
min_BC = 10 ** 6
for i in range(len(g_BC)):
	item = g_BC[i]
	if item[0] & 5 == 5:
		min_BC = min(min_BC, item[1])
		continue
	if item[0] & 4:
		min_BC = min(min_BC, item[1] + min_B)
	if item[0] & 2:
		min_BC = min(min_BC, item[1] + min_C)
res = 10 ** 6
for item in g:
	if item[0] & 7 == 7:
		res = min(res, item[1])
		continue
	if item[0] & 6 == 6:
		res = min(res, item[1] + min_A)
		continue
	if item[0] & 5 == 5:
		res = min(res, item[1] + min_B)
		continue
	if item[0] & 3 == 3:
		res = min(res, item[1] + min_C)
		continue
	if item[0] & 1:
		res = min(res, item[1] + min_BC)
	if item[0] & 2:
		res = min(res, item[1] + min_AC)
	if item[0] & 4:
		res = min(res, item[1] + min_AB)
print(res if res < 10 ** 6 else -1)


n = int(input())
la = []
lb = []
lc = []
lab = []
lbc = []
lac = []
labc = []
q = []
while n:
	(x, y) = input().split()
	if y.find('A') != -1:
		la.append(int(x))
	if y.find('B') != -1:
		lb.append(int(x))
	if y.find('C') != -1:
		lc.append(int(x))
	if y.find('AB') != -1 or y.find('BA') != -1:
		lab.append(int(x))
	if y.find('BC') != -1 or y.find('CB') != -1:
		lbc.append(int(x))
	if y.find('AC') != -1 or y.find('CA') != -1:
		lac.append(int(x))
	try:
		if set([y[0], y[1], y[2]]) == set(['A', 'B', 'C']):
			labc.append(int(x))
	except:
		pass
	n = n - 1
la.sort()
lb.sort()
lc.sort()
lab.sort()
lbc.sort()
lac.sort()
labc.sort()
try:
	q.append(la[0] + lb[0] + lc[0])
except:
	pass
try:
	q.append(lab[0] + lc[0])
except:
	pass
try:
	q.append(la[0] + lbc[0])
except:
	pass
try:
	q.append(lb[0] + lac[0])
except:
	pass
try:
	q.append(lab[0] + lbc[0])
except:
	pass
try:
	q.append(lab[0] + lac[0])
except:
	pass
try:
	q.append(lbc[0] + lac[0])
except:
	pass
try:
	q.append(labc[0])
except:
	pass
if len(q) == 0:
	print(-1)
else:
	print(min(q))


v = {}
for i in range(int(input())):
	(n, s) = input().split()
	s = ''.join(sorted([c for c in s]))
	if s not in v:
		v[s] = int(n)
	else:
		v[s] = min(v[s], int(n))
res = 10 ** 9
if 'A' in v and 'B' in v and ('C' in v):
	res = min(res, v['A'] + v['B'] + v['C'])
if 'AB' in v and 'C' in v:
	res = min(res, v['AB'] + v['C'])
if 'A' in v and 'BC' in v:
	res = min(res, v['A'] + v['BC'])
if 'AC' in v and 'B' in v:
	res = min(res, v['AC'] + v['B'])
if 'AB' in v and 'AC' in v:
	res = min(res, v['AB'] + v['AC'])
if 'AB' in v and 'BC' in v:
	res = min(res, v['AB'] + v['BC'])
if 'AC' in v and 'BC' in v:
	res = min(res, v['AC'] + v['BC'])
if 'ABC' in v:
	res = min(res, v['ABC'])
print(res if res != 10 ** 9 else -1)


n = int(input())
d = {'A': 1000000, 'B': 1000000, 'C': 1000000, 'AB': 1000000, 'BC': 1000000, 'AC': 1000000, 'ABC': 1000000}
for _ in range(n):
	(x, y) = input().split()
	x = int(x)
	if y in ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']:
		d['ABC'] = min(x, d['ABC'])
	elif y in ['AB', 'BA']:
		d['AB'] = min(x, d['AB'])
	elif y in ['BC', 'CB']:
		d['BC'] = min(x, d['BC'])
	elif y in ['AC', 'CA']:
		d['AC'] = min(x, d['AC'])
	else:
		d[y] = min(d[y], x)
l = [d['A'] + d['B'] + d['C'], d['ABC']]
xl = ['AB', 'BC', 'AC']
for i in xl:
	if i == 'AB':
		l.append(d[i] + d['C'])
		for j in xl:
			if i != j:
				l.append(d[i] + d[j])
	elif i == 'BC':
		l.append(d[i] + d['A'])
		for j in xl:
			if i != j:
				l.append(d[i] + d[j])
	elif i == 'AC':
		l.append(d[i] + d['B'])
		for j in xl:
			if i != j:
				l.append(d[i] + d[j])
m = min(l)
if m >= 1000000:
	print(-1)
else:
	print(m)


A = []
B = []
C = []
AB = []
BC = []
CA = []
ABC = []
final = []
for i in range(int(input())):
	t = list(input().split())
	if t[1] == 'A':
		A.append(int(t[0]))
	elif t[1] == 'B':
		B.append(int(t[0]))
	elif t[1] == 'C':
		C.append(int(t[0]))
	elif t[1] == 'AB' or t[1] == 'BA':
		AB.append(int(t[0]))
	elif t[1] == 'BC' or t[1] == 'CB':
		BC.append(int(t[0]))
	elif t[1] == 'CA' or t[1] == 'AC':
		CA.append(int(t[0]))
	elif len(t[1]) == 3:
		ABC.append(int(t[0]))
if len(A) > 0 and len(B) > 0 and (len(C) > 0):
	final.append(min(A) + min(B) + min(C))
if len(AB) > 0 and len(C) > 0:
	final.append(min(AB) + min(C))
if len(A) > 0 and len(BC) > 0:
	final.append(min(BC) + min(A))
if len(B) > 0 and len(CA) > 0:
	final.append(min(CA) + min(B))
if len(ABC) > 0:
	final.append(min(ABC))
if len(AB) > 0 and len(BC) > 0:
	final.append(min(AB) + min(BC))
if len(CA) > 0 and len(AB) > 0:
	final.append(min(CA) + min(AB))
if len(BC) > 0 and len(CA) > 0:
	final.append(min(BC) + min(CA))
if len(final) > 0:
	print(min(final))
else:
	print(-1)


n = int(input())
l = []
for i in range(n):
	(c, s) = map(str, input().split())
	l.append([int(c), s])
abc = a = b = c = ab = ac = bc = 1000000000
for i in range(n):
	comp = ''.join(sorted(set(l[i][1])))
	if comp == 'A':
		abc = min(bc + l[i][0], b + c + l[i][0], abc)
		a = min(a, l[i][0])
	elif comp == 'B':
		abc = min(ac + l[i][0], a + c + l[i][0], abc)
		b = min(b, l[i][0])
	elif comp == 'C':
		abc = min(ab + l[i][0], a + b + l[i][0], abc)
		c = min(c, l[i][0])
	elif comp == 'AB':
		abc = min(c + l[i][0], abc)
		ab = min(ab, l[i][0])
		a = min(a, l[i][0])
		b = min(b, l[i][0])
	elif comp == 'AC':
		abc = min(b + l[i][0], abc)
		ac = min(ac, l[i][0])
		a = min(a, l[i][0])
		c = min(c, l[i][0])
	elif comp == 'BC':
		abc = min(a + l[i][0], abc)
		bc = min(bc, l[i][0])
		b = min(b, l[i][0])
		c = min(c, l[i][0])
	elif comp == 'ABC':
		abc = min(l[i][0], abc)
		a = min(a, l[i][0])
		b = min(b, l[i][0])
		c = min(c, l[i][0])
		ac = min(ac, l[i][0])
		ab = min(ab, l[i][0])
		bc = min(bc, l[i][0])
print(abc if abc != 1000000000 else -1)


l1 = {}
l2 = {}
l3 = {}
(ta, tb, tc) = (0, 0, 0)
for _ in range(int(input())):
	(k, p) = input().split()
	p = ''.join(sorted(p))
	k = int(k)
	if 'A' in p:
		ta += 1
	if 'B' in p:
		tb += 1
	if 'C' in p:
		tc += 1
	if len(p) == 3:
		if p in l3:
			l3[p] = min(k, l3[p])
		else:
			l3[p] = k
	elif len(p) == 2:
		if p in l2:
			l2[p] = min(k, l2[p])
		else:
			l2[p] = k
	elif p in l1:
		l1[p] = min(k, l1[p])
	else:
		l1[p] = k
s = []
if ta > 0 and tb > 0 and (tc > 0):
	if len(list(l1.values())) == 3:
		s.append(sum(list(l1.values())))
	if len(l3.values()) == 1:
		s.append(sum(list(l3.values())))
	for (i, j) in l2.items():
		if 'A' in i and 'B' in i and ('C' in l1):
			s.append(j + l1['C'])
		if 'C' in i and 'B' in i and ('A' in l1):
			s.append(j + l1['A'])
		if 'A' in i and 'C' in i and ('B' in l1):
			s.append(j + l1['B'])
		if 'AB' in l2 and 'BC' in l2:
			s.append(l2['AB'] + l2['BC'])
		if 'AB' in l2 and 'AC' in l2:
			s.append(l2['AB'] + l2['AC'])
		if 'BC' in l2 and 'AC' in l2:
			s.append(l2['BC'] + l2['AC'])
	print(min(s))
else:
	print(-1)


def ersort(k):
	k = list(k)
	k.sort()
	e = ''
	for j in k:
		e += j
	return e
n = int(input())
d = dict()
d.update({'ABC': 10 ** 9})
d.update({'AC': 10 ** 9})
d.update({'AB': 10 ** 9})
d.update({'BC': 10 ** 9})
d.update({'C': 10 ** 9})
d.update({'B': 10 ** 9})
d.update({'A': 10 ** 9})
for i in range(n):
	(a, k) = map(str, input().split())
	k = ersort(k)
	d[k] = min(d[k], int(a))
a = d['A'] + d['B'] + d['C']
b = d['AB'] + min(d['C'], d['BC'], d['AC'])
d1 = d['AC'] + min(d['B'], d['BC'], d['AB'])
e = d['BC'] + min(d['A'], d['AB'], d['AC'])
c = d['ABC']
pri = min(a, b, c, d1, e)
if pri <= 10 ** 8:
	print(pri)
else:
	print(-1)


def solve(c, n, d):
	p = min(d['BC'])
	q = min(d['AC'])
	r = min(d['AB'])
	x = min(d['A']) + min(p, min(d['B']) + min(d['C']))
	y = min(d['B']) + min(q, min(d['A']) + min(d['C']))
	z = min(d['C']) + min(r, min(d['A']) + min(d['B']))
	t = min(p + q, q + r, r + p)
	return min(x, y, z, min(d['ABC']), t)
n = int(input())
d = {'A': [float('inf')], 'B': [float('inf')], 'C': [float('inf')], 'AB': [float('inf')], 'BC': [float('inf')], 'AC': [float('inf')], 'ABC': [float('inf')]}
for i in range(n):
	(v, c) = map(str, input().split())
	c = ''.join(sorted(c))
	d[c].append(int(v))
ans = solve(c, n, d)
if ans == float('inf'):
	print(-1)
else:
	print(ans)


ones = 3 * [1000000]
doubles = 3 * [1000000]
triple = 1000000
for i in range(int(input())):
	x = list(input().split())
	if len(x[1]) == 3 and int(x[0]) < triple:
		triple = int(x[0])
	elif len(x[1]) == 2:
		if (x[1] == 'AB' or x[1] == 'BA') and int(x[0]) < doubles[0]:
			doubles[0] = int(x[0])
		elif (x[1] == 'BC' or x[1] == 'CB') and int(x[0]) < doubles[1]:
			doubles[1] = int(x[0])
		elif (x[1] == 'AC' or x[1] == 'CA') and int(x[0]) < doubles[2]:
			doubles[2] = int(x[0])
	elif x[1] == 'A' and int(x[0]) < ones[0]:
		ones[0] = int(x[0])
	elif x[1] == 'B' and int(x[0]) < ones[1]:
		ones[1] = int(x[0])
	elif x[1] == 'C' and int(x[0]) < ones[2]:
		ones[2] = int(x[0])
ans = min(triple, sum(doubles[:2]), sum(doubles[1:]), doubles[0] + doubles[-1], doubles[0] + ones[2], doubles[1] + ones[0], doubles[2] + ones[1], sum(ones))
if ans == 1000000:
	print(-1)
else:
	print(ans)


import time
max = 300000 + 5
ABC = max
AC = max
AB = max
BC = max
A = max
B = max
C = max
n = int(input())
for i in range(n):
	(c, s) = (i for i in input().split())
	c = int(c)
	if len(s) == 3:
		if c < ABC:
			ABC = c
	elif len(s) == 2:
		if 'A' in s:
			if 'B' in s:
				if c < AB:
					AB = c
			elif c < AC:
				AC = c
		elif c < BC:
			BC = c
	elif s == 'A':
		if c < A:
			A = c
	elif s == 'B':
		if c < B:
			B = c
	elif c < C:
		C = c
start = time.time()
ans = min(ABC, AB + C, AC + B, BC + A, AB + BC, AB + AC, BC + AC, A + B + C)
if ans == max:
	print(-1)
else:
	print(ans)
finish = time.time()


from queue import PriorityQueue as PQ
n = int(input())
todos = PQ()
precos = {'A': float('inf'), 'B': float('inf'), 'C': float('inf'), 'AB': float('inf'), 'BC': float('inf'), 'AC': float('inf'), 'ABC': float('inf')}
somatotal = float('inf')
i = 0
while i < n:
	(valor, tipo) = input().split()
	valor = int(valor)
	tipos = [i for i in tipo]
	tipos.sort()
	tipo = ''.join(tipos)
	if valor < precos[tipo]:
		precos[tipo] = valor
	i += 1
teste = []
teste.append(precos['A'] + precos['B'] + precos['C'])
teste.append(precos['AB'] + precos['C'])
teste.append(precos['AC'] + precos['B'])
teste.append(precos['BC'] + precos['A'])
teste.append(precos['AB'] + precos['BC'])
teste.append(precos['AC'] + precos['BC'])
teste.append(precos['AB'] + precos['AC'])
teste.append(precos['ABC'])
valormin = min(teste)
if valormin == float('inf'):
	print(-1)
else:
	print(valormin)


import math
n = int(input())
inf = float('inf')
a = [inf]
b = [inf]
c = [inf]
ab = [inf]
ac = [inf]
bc = [inf]
abc = [inf]
for i in range(n):
	(x, s) = input().split()
	x = int(x)
	s_ = []
	for j in s:
		s_.append(j)
	s = sorted(s_)
	s = ''.join(s)
	if s == 'A':
		a.append(x)
	elif s == 'B':
		b.append(x)
	elif s == 'C':
		c.append(x)
	elif s == 'AB':
		ab.append(x)
	elif s == 'AC':
		ac.append(x)
	elif s == 'BC':
		bc.append(x)
	elif s == 'ABC':
		abc.append(x)
ans = float('inf')
a = min(a)
b = min(b)
c = min(c)
ab = min(ab)
ac = min(ac)
bc = min(bc)
abc = min(abc)
ans = min(a + b + c, a + bc, b + ac, c + ab, ab + ac, ab + bc, ac + bc, abc)
if ans == inf:
	print(-1)
else:
	print(ans)


n = int(input())
c = []
s = []
for _ in range(n):
	ln = input().split()
	c.append(int(ln[0]))
	s.append(str(ln[1]))
dp = [1000000000.0 for _ in range(8)]
dp[0] = 0
for i in range(n):
	k = 0
	if 'A' in s[i]:
		k += 1
	if 'B' in s[i]:
		k += 2
	if 'C' in s[i]:
		k += 4
	for j in range(8):
		dp[j | k] = min(dp[j | k], dp[j] + c[i])
if dp[7] == 1000000000.0:
	print(-1)
else:
	print(dp[7])


dic = {'A': [], 'B': [], 'C': [], 'AB': [], 'BC': [], 'AC': [], 'ABC': []}
for _ in range(int(input())):
	(p, v) = map(str, input().split())
	if len(v) != 1:
		dic[''.join(sorted(v))].append(int(p))
	for t in list(v):
		dic[t].append(int(p))
if any([len(dic[x]) == 0 for x in ['A', 'B', 'C']]):
	print(-1)
else:
	fp = sum([min(dic[x]) for x in ['A', 'B', 'C']])
	cl = [['AB', 'C'], ['BC', 'A'], ['AC', 'B']]
	sp = []
	for m in cl:
		if len(dic[m[0]]) != 0:
			sp.append(min(dic[m[0]]) + min(dic[m[1]]))
	if sum(sp) == 0:
		sp = -1
	else:
		sp = min(sp)
	tp = -1
	if len(dic['ABC']) != 0:
		tp = min(dic['ABC'])
	print(min(filter(lambda x: x > 0, [fp, sp, tp])))


from sys import stdin

def main():
	(s, inf) = (input(), 10 ** 9)
	l = [[inf] for _ in range(8)]
	for s in stdin.read().splitlines():
		(p, s) = s.split()
		l[('A' in s) + ('B' in s) * 2 + ('C' in s) * 4].append(int(p))
	(a, b, ab, c, ac, bc, abc) = map(min, l[1:])
	r = min(a + b + c, ab + c, ac + b, bc + a, ab + ac, ab + bc, ac + bc, abc)
	print(r if r < inf else -1)
main()


INF = 10 ** 10

def find(p, cmb):
	for i in p:
		flag = False
		for j in cmb:
			if j not in i[1]:
				flag = True
				break
		if not flag:
			return i[0]
	return INF
n = int(input())
p = []
for i in range(n):
	(x, r) = input().split()
	x = int(x)
	p.append((x, r))
p.sort()
mn = find(p, 'A') + find(p, 'B') + find(p, 'C')
mn = min(mn, find(p, 'AB') + find(p, 'C'))
mn = min(mn, find(p, 'AC') + find(p, 'B'))
mn = min(mn, find(p, 'BC') + find(p, 'A'))
mn = min(mn, find(p, 'ABC'))
if mn >= INF:
	print(-1)
else:
	print(mn)


n = int(input())
k = 10000000000.0
d = {'A': k, 'B': k, 'C': k, 'AB': k, 'BA': k, 'AC': k, 'CA': k, 'BC': k, 'CB': k, 'ABC': k, 'ACB': k, 'BAC': k, 'BCA': k, 'CAB': k, 'CBA': k}
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	d[b] = min(d[b], a)
mthree = min(d['ABC'], d['ACB'], d['BAC'], d['BCA'], d['CAB'], d['CBA'])
abc = d['AB'] + d['C']
bac = d['BA'] + d['C']
acb = d['AC'] + d['B']
cab = d['CA'] + d['B']
bca = d['BC'] + d['A']
cba = d['CB'] + d['A']
mtwo = min(abc, bac, acb, cab, bca, cba)
l = [0] * 24
l[0] = d['AB'] + d['AC']
l[1] = d['AB'] + d['CA']
l[2] = d['AB'] + d['BC']
l[3] = d['AB'] + d['CB']
l[4] = d['BA'] + d['AC']
l[5] = d['BA'] + d['CA']
l[6] = d['BA'] + d['BC']
l[7] = d['BA'] + d['CB']
l[8] = d['AC'] + d['AB']
l[9] = d['AC'] + d['BA']
l[10] = d['AC'] + d['BC']
l[11] = d['AC'] + d['CB']
l[12] = d['CA'] + d['AB']
l[13] = d['CA'] + d['BA']
l[14] = d['CA'] + d['BC']
l[15] = d['CA'] + d['CB']
l[16] = d['BC'] + d['AB']
l[17] = d['BC'] + d['BA']
l[18] = d['BC'] + d['AC']
l[19] = d['BC'] + d['CA']
l[20] = d['CB'] + d['AB']
l[21] = d['CB'] + d['BA']
l[22] = d['CB'] + d['AC']
l[23] = d['CB'] + d['CA']
mfour = min(l)
mone = d['A'] + d['B'] + d['C']
mn = min(mone, mtwo, mthree, mfour)
if mn < 10000000000.0:
	print(mn)
else:
	print(-1)


import math
n = int(input())
pA = -1
pB = -1
pC = -1
pAB = -1
pBC = -1
pAC = -1
pABC = -1
for i in range(n):
	v = input()
	(p, v) = (int(v.split(' ')[0]), v.split(' ')[1])
	if len(v) == 3:
		if p < pABC or pABC < 0:
			pABC = p
	elif len(v) == 2:
		if v in ['AB', 'BA']:
			if p < pAB or pAB < 0:
				pAB = p
		elif v in ['AC', 'CA']:
			if p < pAC or pAC < 0:
				pAC = p
		elif v in ['CB', 'BC']:
			if p < pBC or pBC < 0:
				pBC = p
	else:
		if v == 'A':
			if p < pA or pA < 0:
				pA = p
		if v == 'B':
			if p < pB or pB < 0:
				pB = p
		if v == 'C':
			if p < pC or pC < 0:
				pC = p
if pA > 0 and pB > 0 and (pC > 0) and (pA + pB + pC < pABC or pABC < 0):
	pABC = pA + pB + pC
if pA > 0 and pBC > 0 and (pA + pBC < pABC or pABC < 0):
	pABC = pA + pBC
if pAB > 0 and pC > 0 and (pAB + pC < pABC or pABC < 0):
	pABC = pAB + pC
if pAC > 0 and pB > 0 and (pAC + pB < pABC or pABC < 0):
	pABC = pAC + pB
if pAB > 0 and pBC > 0 and (pAB + pBC < pABC or pABC < 0):
	pABC = pAB + pBC
if pAB > 0 and pAC > 0 and (pAB + pAC < pABC or pABC < 0):
	pABC = pAB + pAC
if pAC > 0 and pBC > 0 and (pAC + pBC < pABC or pABC < 0):
	pABC = pAC + pBC
print(pABC)


n = int(input())
A = []
(z1, z2, z3, z4, z5, z6, z7) = [10 ** 9] * 7
kop = set()
for _ in range(n):
	(c, s) = input().split()
	c = int(c)
	s = ''.join(sorted(list(set(s))))
	for _ in s:
		kop.add(_)
	if s == 'A':
		z1 = min(z1, c)
	elif s == 'B':
		z2 = min(z2, c)
	elif s == 'C':
		z3 = min(z3, c)
	elif s == 'AB':
		z4 = min(z4, c)
	elif s == 'AC':
		z5 = min(z5, c)
	elif s == 'BC':
		z6 = min(z6, c)
	elif s == 'ABC':
		z7 = min(z7, c)
if len(kop) == 3:
	print(min(z7, z1 + z2 + z3, z1 + z6, z2 + z5, z3 + z4, z4 + z5, z4 + z6, z5 + z6))
else:
	print(-1)


n = int(input())
d = [10 ** 9] * 8
for i in range(n):
	(c, s) = input().split()
	c = int(c)
	key = 0
	for char in s:
		key += 2 ** (ord(char) - ord('A'))
	d[key] = min(d[key], c)
ans = min(d[7], d[1] + d[2] + d[4], d[3] + d[4], d[6] + d[1], d[5] + d[2], d[3] + d[6], d[3] + d[5], d[5] + d[6])
if ans == 10 ** 9:
	print(-1)
else:
	print(ans)


d = {}
d['A'] = d['B'] = d['C'] = d['BC'] = d['AB'] = d['AC'] = d['ABC'] = 1000 * 1000 * 1000
for i in range(int(input())):
	(price, vit) = input().split()
	price = int(price)
	vit = ''.join(sorted(vit))
	if vit in d:
		d[vit] = min(d[vit], price)
	else:
		d[vit] = price
res = min(d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['AB'] + d['BC'], d['AC'] + d['BC'], d['AB'] + d['AC'], d['ABC'])
if res == 1000 * 1000 * 1000:
	print(-1)
else:
	print(res)


n = int(input().rstrip())
c = [0] * n
s = [''] * n
for _ in range(n):
	cs = input().rstrip().split(' ')
	c[_] = int(cs[0])
	s[_] = cs[1]
A = B = C = AB = AC = BC = ABC = 100001
for i in range(n):
	if 'A' in s[i]:
		A = min(A, c[i])
		if 'B' in s[i]:
			AB = min(AB, c[i])
		if 'C' in s[i]:
			AC = min(AC, c[i])
		if 'B' in s[i] and 'C' in s[i]:
			ABC = min(ABC, c[i])
	elif 'B' in s[i]:
		B = min(B, c[i])
		if 'C' in s[i]:
			BC = min(BC, c[i])
	else:
		C = min(C, c[i])
result = 300001
if A != 100001:
	if BC != 100001:
		result = min(result, A + BC)
	if B != 100001 and C != 100001:
		result = min(result, A + B + C)
if B != 100001 and AC != 100001:
	result = min(result, B + AC)
if C != 100001 and AB != 100001:
	result = min(result, C + AB)
if AB != 100001 and AC != 100001:
	if AC != 100001:
		result = min(result, AB + AC)
	if BC != 100001:
		result = min(result, AB + BC)
if AC != 100001 and BC != 100001:
	result = min(result, AB + BC)
if ABC != 100001:
	result = min(result, ABC)
if result == 300001:
	print(-1)
else:
	print(result)


def sumc(s):
	c = 0
	for i in s:
		c += ord(i)
	return c

def vitamins(l):
	hm = {sumc('A'): 100000000000, sumc('B'): 100000000000, sumc('C'): 100000000000, sumc('AB'): 100000000000, sumc('AC'): 100000000000, sumc('BC'): 100000000000, sumc('ABC'): 100000000000}
	for i in l:
		if hm[sumc(i[1])] > int(i[0]):
			hm[sumc(i[1])] = int(i[0])
	mn = 10000000
	x = min(hm[sumc('A')] + hm[sumc('B')] + hm[sumc('C')], hm[sumc('AB')] + hm[sumc('C')], hm[sumc('A')] + hm[sumc('BC')], hm[sumc('AC')] + hm[sumc('B')], hm[sumc('ABC')], hm[sumc('AB')] + hm[sumc('AC')], hm[sumc('AB')] + hm[sumc('BC')], hm[sumc('AC')] + hm[sumc('BC')])
	if x < mn:
		return x
	else:
		return -1
n = int(input())
lt = []
for _ in range(n):
	lt.append(input().split())
print(vitamins(lt))


from itertools import permutations
n = int(input())
dic = {}
for _ in range(n):
	(a, b) = input().split()
	if b == 'A' or b == 'B' or b == 'C':
		if b in dic:
			if dic[b] > int(a):
				dic[b] = int(a)
		else:
			dic[b] = int(a)
	elif b == 'AB' or b == 'BA':
		if 'AB' in dic:
			if dic['AB'] > int(a):
				dic['AB'] = int(a)
		else:
			dic['AB'] = int(a)
	elif b == 'BC' or b == 'CB':
		if 'BC' in dic:
			if dic['BC'] > int(a):
				dic['BC'] = int(a)
		else:
			dic['BC'] = int(a)
	elif b == 'AC' or b == 'CA':
		if 'AC' in dic:
			if dic['AC'] > int(a):
				dic['AC'] = int(a)
		else:
			dic['AC'] = int(a)
	elif 'ABC' in dic:
		if dic['ABC'] > int(a):
			dic['ABC'] = int(a)
	else:
		dic['ABC'] = int(a)
abc = 100000000
if set(('A', 'B', 'C')).issubset(dic):
	if dic['A'] + dic['B'] + dic['C'] < abc:
		abc = dic['A'] + dic['B'] + dic['C']
if set(('AB', 'C')).issubset(dic):
	if dic['AB'] + dic['C'] < abc:
		abc = dic['AB'] + dic['C']
if set(('AB', 'AC')).issubset(dic):
	if dic['AB'] + dic['AC'] < abc:
		abc = dic['AB'] + dic['AC']
if set(('AB', 'BC')).issubset(dic):
	if dic['AB'] + dic['BC'] < abc:
		abc = dic['AB'] + dic['BC']
if set(('BC', 'A')).issubset(dic):
	if dic['BC'] + dic['A'] < abc:
		abc = dic['BC'] + dic['A']
if set(('BC', 'AB')).issubset(dic):
	if dic['AB'] + dic['BC'] < abc:
		abc = dic['AB'] + dic['BC']
if set(('BC', 'AC')).issubset(dic):
	if dic['AC'] + dic['BC'] < abc:
		abc = dic['AC'] + dic['BC']
if set(('AC', 'B')).issubset(dic):
	if dic['AC'] + dic['B'] < abc:
		abc = dic['AC'] + dic['B']
if set(('AC', 'BC')).issubset(dic):
	if dic['AC'] + dic['BC'] < abc:
		abc = dic['AC'] + dic['BC']
if set(('AC', 'BC')).issubset(dic):
	if dic['AC'] + dic['BC'] < abc:
		abc = dic['AC'] + dic['BC']
if 'ABC' in dic:
	if dic['ABC'] < abc:
		abc = dic['ABC']
if abc == 100000000:
	abc = -1
print(abc)


class CodeforcesTask1042BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.juices = []

	def read_input(self):
		self.n = int(input())
		for _ in range(self.n):
			inp = input().split(' ')
			inp[1] = list(inp[1])
			inp[1].sort()
			inp[1] = ''.join(inp[1])
			self.juices.append([int(inp[0]), inp[1]])

	def process_task(self):
		pricing = {}
		schemes = ['A', 'B', 'C', 'AB', 'BC', 'AC', 'ABC']
		for juice in self.juices:
			if not juice[1] in pricing:
				pricing[juice[1]] = juice[0]
			else:
				pricing[juice[1]] = min(pricing[juice[1]], juice[0])
		for s in schemes:
			if s not in pricing:
				pricing[s] = 10 ** 7
		prices = []
		for i in range(1, 2 ** 7):
			mask = '0' * (7 - len(bin(i)[2:])) + bin(i)[2:]
			now = ''
			price = 0
			for x in range(7):
				if mask[x] == '1':
					now += schemes[x]
					price += pricing[schemes[x]]
			if 'A' in now and 'B' in now and ('C' in now):
				prices.append(price)
		res = min(prices)
		if res >= 10 ** 7:
			res = -1
		self.result = str(res)

	def get_result(self):
		return self.result
Solution = CodeforcesTask1042BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


from collections import Counter
arr = ['A', 'B', 'C', 'AB', 'BC', 'AC', 'ABC']
n = int(input())
h = Counter()
for i in arr:
	h[i] = 10 ** 6
while n:
	n -= 1
	(x, s) = input().split()
	x = int(x)
	s = ''.join(sorted(s))
	h[s] = min(h[s], x)
res = min(h['A'] + h['B'] + h['C'], h['AB'] + h['C'], h['A'] + h['BC'], h['AC'] + h['B'], h['ABC'], h['AB'] + h['BC'], h['AB'] + h['AC'], h['BC'] + h['AC'])
print(res if res < 10 ** 6 else -1)


import collections
n = int(input())
cost = []
vit = []
for _ in range(n):
	(c, v) = input().split()
	cost.append(int(c))
	vv = 0
	if 'A' in v:
		vv += 1
	if 'B' in v:
		vv += 2
	if 'C' in v:
		vv += 4
	vit.append(vv)
Q = collections.deque([(0, 0)])
d = collections.defaultdict(lambda : float('inf'))
res = float('inf')
for i in range(n):
	for _ in range(len(Q)):
		(c, v) = Q.popleft()
		Q.append((c, v))
		new_v = v | vit[i]
		if new_v == v or c + cost[i] >= d[new_v]:
			continue
		d[new_v] = c + cost[i]
		if new_v == 7:
			res = min(res, d[new_v])
		Q.append((d[new_v], new_v))
print(res if res != float('inf') else -1)


a = [0] * 7
for i in range(int(input())):
	b = input().split()
	c = int(b[0])
	b1 = ''.join(sorted(b[1]))
	d = {'A': 0, 'B': 1, 'C': 2, 'AB': 3, 'AC': 4, 'BC': 5, 'ABC': 6}[b1]
	if a[d] == 0 or c < a[d]:
		a[d] = c
e = ['A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']
f = -1
for i in range(7):
	for j in range(i + 1, 7):
		for k in range(j + 1, 7):
			if 'A' in e[i] + e[j] + e[k] and 'B' in e[i] + e[j] + e[k] and ('C' in e[i] + e[j] + e[k]) and a[i] and a[j] and a[k]:
				if f == -1 or a[i] + a[j] + a[k] < f:
					f = a[i] + a[j] + a[k]
for i in range(7):
	for j in range(i + 1, 7):
		if 'A' in e[i] + e[j] and 'B' in e[i] + e[j] and ('C' in e[i] + e[j]) and a[i] and a[j]:
			if f == -1 or a[i] + a[j] < f:
				f = a[i] + a[j]
if a[6]:
	if f == -1 or a[6] < f:
		f = a[6]
print(f)


num_pills = int(input())
cheap = {}
inf = 10 ** 9 + 10
for i in ['A', 'B', 'C', 'AB', 'BC', 'AC', 'ABC']:
	cheap[i] = inf
for _ in range(num_pills):
	(cost, vits) = input().split()
	cost = int(cost)
	vits = ''.join(sorted(vits))
	cheap[vits] = min(cheap[vits], cost)
cheapest = inf
for i in ['A,BC', 'B,AC', 'C,AB', 'ABC', 'A,B,C', 'AB,AC', 'BC,AC', 'AB,BC']:
	total = sum((cheap[component] for component in i.split(',')))
	cheapest = min(cheapest, total)
if cheapest == inf:
	print(-1)
else:
	print(cheapest)


def mi():
	return map(int, input().split())
n = int(input())
a = [0] * n
for i in range(n):
	a[i] = list(input().split())
	a[i][0] = int(a[i][0])
(A, B, C, AB, AC, BC, ABC) = (10000000000.0, 10000000000.0, 10000000000.0, 10000000000.0, 10000000000.0, 10000000000.0, 10000000000.0)
for i in range(n):
	a[i][1] = ''.join(sorted(list(a[i][1])))
	if a[i][1] == 'A':
		A = min(A, a[i][0])
	if a[i][1] == 'AB':
		AB = min(AB, a[i][0])
	if a[i][1] == 'AC':
		AC = min(AC, a[i][0])
	if a[i][1] == 'BC':
		BC = min(BC, a[i][0])
	if a[i][1] == 'ABC':
		ABC = min(ABC, a[i][0])
	if a[i][1] == 'B':
		B = min(B, a[i][0])
	if a[i][1] == 'C':
		C = min(C, a[i][0])
t = [AB + C, AB + AC, AB + BC, AB + ABC, BC + A, BC + AB, BC + AC, BC + ABC, AC + B, AC + AB, AC + BC, AC + ABC, ABC, A + B + C]
lol = min(t)
if lol == 10000000000.0:
	print(-1)
else:
	print(lol)


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for _ in range(1):
	n = nmbr()
	l = []
	cc = []
	PI = float('inf')
	for i in range(n):
		(c, s) = input().split()
		mask = 0
		if 'A' in s:
			mask = mask | 1 << 2
		if 'B' in s:
			mask = mask | 1 << 1
		if 'C' in s:
			mask = mask | 1 << 0
		l += [mask]
		cc += [int(c)]
	dp = [PI] * 8
	dp[0] = 0
	for i in range(n):
		for mask in range(1, 8):
			dp[mask] = min(dp[mask], cc[i] + dp[mask - (l[i] & mask)])
	print(-1 if dp[7] == PI else dp[7])


n = int(input())
vitamins = {}
for i in range(n):
	(price, tmp) = input().split()
	tmp = list(tmp)
	tmp.sort()
	vit = ''.join(tmp)
	price = int(price)
	if vit in vitamins:
		vitamins[vit] = min(vitamins[vit], price)
	else:
		vitamins[vit] = price
answ = 1000000
for i in vitamins:
	tmp = [0, 0]
	if i == 'A':
		tmp[0] += vitamins['A']
		tmp[1] = 1
		if 'BC' in vitamins:
			tmp[0] += vitamins['BC']
			answ = min(answ, tmp[0])
			tmp = [vitamins['A'], 0]
		if 'B' in vitamins:
			tmp[0] += vitamins['B']
			tmp[1] += 1
		else:
			continue
		if 'C' in vitamins:
			tmp[0] += vitamins['C']
			tmp[1] += 1
			answ = min(answ, tmp[0])
	if i == 'AB':
		tmp[0] += vitamins['AB']
		tmp[1] = 1
		if 'C' in vitamins:
			tmp[0] += vitamins['C']
			answ = min(answ, tmp[0])
			tmp = [vitamins['AB'], 0]
		if 'BC' in vitamins:
			tmp[0] += vitamins['BC']
			answ = min(answ, tmp[0])
			tmp = [vitamins['AB'], 0]
		if 'AC' in vitamins:
			tmp[0] += vitamins['AC']
			answ = min(answ, tmp[0])
			tmp = [vitamins['AB'], 0]
		if 'ABC' in vitamins:
			tmp[0] += vitamins['ABC']
			answ = min(answ, tmp[0])
			tmp = [vitamins['AB'], 0]
	if i == 'ABC':
		answ = min(answ, vitamins['ABC'])
	if i == 'AC':
		tmp[0] = vitamins['AC']
		if 'B' in vitamins:
			tmp[0] += vitamins['B']
			answ = min(tmp[0], answ)
			tmp = [vitamins['AC'], 0]
		if 'AB' in vitamins:
			tmp[0] += vitamins['AB']
			answ = min(tmp[0], answ)
			tmp = [vitamins['AC'], 0]
		if 'ABC' in vitamins:
			tmp[0] += vitamins['ABC']
			answ = min(tmp[0], answ)
			tmp = [vitamins['AC'], 0]
		if 'BC' in vitamins:
			tmp[0] += vitamins['BC']
			answ = min(tmp[0], answ)
			tmp = [vitamins['AC'], 0]
	if i == 'B':
		tmp[0] += vitamins['B']
		if 'AB' in vitamins:
			tmp[0] += vitamins['AB']
			answ = min(answ, tmp[0])
			tmp = [vitamins['B'], 0]
		if 'A' in vitamins:
			tmp[0] += vitamins['A']
		else:
			continue
		if 'C' in vitamins:
			tmp[0] += vitamins['C']
			answ = min(answ, tmp[0])
	if i == 'BC':
		tmp[0] = vitamins['BC']
		if 'A' in vitamins:
			tmp[0] += vitamins['A']
			answ = min(answ, tmp[0])
			tmp = [vitamins['BC'], 0]
		if 'AB' in vitamins:
			tmp[0] += vitamins['AB']
			answ = min(answ, tmp[0])
			tmp = [vitamins['BC'], 0]
		if 'AC' in vitamins:
			tmp[0] += vitamins['AC']
			answ = min(answ, tmp[0])
			tmp = [vitamins['BC'], 0]
		if 'ABC' in vitamins:
			tmp[0] += vitamins['ABC']
			answ = min(answ, tmp[0])
			tmp = [vitamins['BC'], 0]
	if i == 'C':
		tmp[0] = vitamins['C']
		if 'AB' in vitamins:
			tmp[0] += vitamins['AB']
			answ = min(tmp[0], answ)
			tmp[0] = vitamins['AB']
		if 'A' in vitamins:
			tmp[0] += vitamins['A']
		else:
			continue
		if 'B' in vitamins:
			tmp[0] += vitamins['B']
			answ = min(answ, tmp[0])
if answ == 1000000:
	print(-1)
else:
	print(answ)


n = int(input())
A = set()
B = set()
C = set()
cost = {}
for i in range(n):
	l = input().split()
	c = int(l[0])
	vits = l[1]
	cost[i] = c
	for k in vits:
		if k == 'A':
			A.add(i)
		elif k == 'B':
			B.add(i)
		else:
			C.add(i)
minmoney = 300001
abc = A & B & C
ab = A & B
bc = B & C
ac = A & C
a = A
b = B
c = C
minc = mina = minb = 300001
for x in a:
	mina = min(mina, cost[x])
for x in b:
	minb = min(minb, cost[x])
for x in c:
	minc = min(minc, cost[x])
minab = minbc = minac = 300001
for x in ab:
	minab = min(minab, cost[x])
for x in bc:
	minbc = min(minbc, cost[x])
for x in ac:
	minac = min(minac, cost[x])
minabc = 300001
for x in abc:
	minabc = min(minabc, cost[x])
minmoney = min(minabc, minab + minc, minac + minb, minbc + mina, mina + minb + minc)
if minmoney > 300000:
	print(-1)
else:
	print(minmoney)


from collections import defaultdict
n = int(input())
juices = defaultdict(int)
for i in range(n):
	(price, tmp) = map(str, input().split())
	price = int(price)
	tmp = list(tmp)
	tmp.sort()
	tmp = ''.join(tmp)
	if juices[tmp] == 0:
		juices[tmp] = price
	else:
		juices[tmp] = min(juices[tmp], price)
ans = 1000000
juices = sorted(juices.items(), key=lambda x: x[1])
for (k1, v1) in juices:
	if k1 == 'ABC':
		ans = min(ans, v1)
		continue
	for (k2, v2) in juices:
		tmp2 = set(list(k1) + list(k2))
		tmp2 = list(tmp2)
		tmp2.sort()
		if ''.join(tmp2) == 'ABC':
			ans = min(ans, v2 + v1)
			continue
		for (k3, v3) in juices:
			tmp3 = set(list(k1) + list(k2) + list(k3))
			tmp3 = list(tmp3)
			tmp3.sort()
			if ''.join(tmp3) == 'ABC':
				ans = min(ans, v1 + v2 + v3)
if ans == 1000000:
	ans = -1
print(ans)


import sys
t = int(sys.stdin.readline())
arr1 = []
arr2 = []
x = 10 ** 5 + 1
amin = x
bmin = x
cmin = x
for i in range(t):
	ip = sys.stdin.readline().strip().split()
	arr1.append(int(ip[0]))
	arr2.append(list(ip[1]))
	continue
f1 = 0
f2 = 0
f3 = 0
abcmin = 10 ** 6
n = t
for i in range(n):
	fl1 = 0
	fl2 = 0
	fl3 = 0
	for j in arr2[i]:
		if j == 'A':
			amin = min(amin, arr1[i])
			f1 = 1
			fl1 = 1
		if j == 'B':
			bmin = min(bmin, arr1[i])
			f2 = 1
			fl2 = 1
		if j == 'C':
			cmin = min(cmin, arr1[i])
			f3 = 1
			fl3 = 1
	if fl1 and fl2 and fl3:
		abcmin = min(abcmin, arr1[i])
if not (f1 and f2 and f3):
	print(-1)
	exit(0)
op = min(amin + bmin + cmin, abcmin)
for i in range(n):
	for j in range(n):
		fl1 = 0
		fl2 = 0
		fl3 = 0
		for k in arr2[j]:
			if k == 'A':
				fl1 = 1
			if k == 'B':
				fl2 = 1
			if k == 'C':
				fl3 = 1
		for k in arr2[i]:
			if k == 'A':
				fl1 = 1
			if k == 'B':
				fl2 = 1
			if k == 'C':
				fl3 = 1
		if fl1 and fl2 and fl3:
			op = min(op, arr1[i] + arr1[j])
print(op)


dic = {}
dic['ABC'] = dic['AB'] = dic['BC'] = dic['AC'] = dic['A'] = dic['B'] = dic['C'] = float('inf')
for i in range(int(input())):
	(c, s) = input().split()
	c = int(c)
	s = ''.join(sorted(s))
	dic[s] = min(dic[s], c)
x = ['ABC', 'AB', 'BC', 'AC', 'A', 'B', 'C']
ans = min(dic['ABC'], dic['A'] + dic['B'] + dic['C'])
for i in x:
	for j in x:
		if i == j:
			continue
		if len(set(list(i + j))) == 3:
			ans = min(ans, dic[i] + dic[j])
if ans == float('inf'):
	print(-1)
else:
	print(ans)


def f(n, order, i):
	pass
maxn = 100000005
n = int(input())
a = []
for i in range(n):
	(price, vit) = input().split(' ')
	a.append((price, vit))
ans = []
for i in range(n + 1):
	ans.append([])
	for j in range(8):
		if j == 0:
			ans[i].append(0)
		else:
			ans[i].append(maxn)
minn = maxn
for i in range(0, n):
	(price, vit) = a[i]
	vit_sum = 0
	for c in vit:
		vit_sum += 1 << ord(c) - ord('A')
	for state in range(8):
		ans[i + 1][state | vit_sum] = min(min(ans[i][state] + int(price), ans[i][state | vit_sum]), ans[i + 1][state | vit_sum])
		ans[i + 1][state] = min(ans[i][state], ans[i + 1][state])
		if state == 7:
			minn = min(ans[i + 1][7], minn)
if minn == maxn:
	print(-1)
else:
	print(minn)


n = int(input())
d = dict()
for i in range(n):
	(b, a) = input().split()
	b = int(b)
	if a not in d.keys():
		d[a] = b
	else:
		d[a] = min(d[a], b)
a = []
for (key, val) in d.items():
	a.append([key, val])
a.append(['', 0])
a.append(['', 0])
ans = 228 ** 337
for i in range(len(a)):
	for j in range(i + 1, len(a)):
		for k in range(j + 1, len(a)):
			s = a[i][0] + a[j][0] + a[k][0]
			if 'A' in s and 'B' in s and ('C' in s):
				ans = min(ans, a[i][1] + a[j][1] + a[k][1])
if ans == 228 ** 337:
	ans = -1
print(ans)


n = int(input())
vitamins_prices = {}
all_vitamins = ['A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']
ord_a = ord('A')
ord_c = ord('C')
for i in range(n):
	(current_price, current_vitamins) = input().split()
	current_price = int(current_price)
	current_vitamins_set = set(current_vitamins)
	for vit in all_vitamins:
		vit_set = set(vit)
		if current_vitamins_set < vit_set:
			continue
		elif current_vitamins_set == vit_set:
			vitamins_prices[vit] = min(vitamins_prices.get(vit, current_price), current_price)
		elif vit not in vitamins_prices:
			continue
		mixed = ''.join(sorted(vit_set | current_vitamins_set))
		vit_price = vitamins_prices.get(vit, current_price)
		mixed_price = vitamins_prices.get(mixed, vit_price + current_price)
		vitamins_prices[mixed] = min(mixed_price, vit_price + current_price)
print(vitamins_prices.get('ABC', -1))


n = int(input())
inf = 10 ** 6
abc = [inf] * 7
u = []

def rf(x, y):
	if x > y:
		return y
	return x
for i in range(n):
	(k, s) = input().split()
	k = int(k)
	s = list(s)
	s.sort()
	a = 'A' in s
	b = 'B' in s
	c = 'C' in s
	if a:
		abc[0] = rf(abc[0], k)
	if b:
		abc[1] = rf(abc[1], k)
	if c:
		abc[2] = rf(abc[2], k)
	if a and b:
		abc[3] = rf(abc[3], k)
	if c and b:
		abc[4] = rf(abc[4], k)
	if a and c:
		abc[5] = rf(abc[5], k)
	if a and b and c:
		abc[6] = rf(abc[6], k)
a1 = [0] * 5
a1[0] = abc[0] + abc[1] + abc[2]
a1[1] = abc[0] + abc[4]
a1[2] = abc[1] + abc[5]
a1[3] = abc[2] + abc[3]
a1[4] = abc[6]
ans = min(a1)
if ans == inf:
	print(-1)
else:
	print(ans)


from math import inf
n = int(input())
ans = [inf] * 7
for w in range(n):
	(c, v) = input().split()
	c = int(c)
	if v == 'A':
		ans[0] = min(ans[0], c)
	elif v == 'B':
		ans[1] = min(ans[1], c)
	elif v == 'C':
		ans[2] = min(ans[2], c)
	elif v == 'AB' or v == 'BA':
		ans[3] = min(ans[3], c)
	elif v == 'BC' or v == 'CB':
		ans[4] = min(ans[4], c)
	elif v == 'CA' or v == 'AC':
		ans[5] = min(ans[5], c)
	else:
		ans[6] = min(ans[6], c)
p = min(ans[0] + ans[4], ans[1] + ans[5], ans[2] + ans[3], ans[6], ans[1] + ans[2] + ans[0], ans[3] + ans[4], ans[4] + ans[5], ans[3] + ans[5])
if p == inf:
	print(-1)
else:
	print(p)


from collections import defaultdict
from sys import stdin, stdout
import math
import bisect
n = int(input())
val = defaultdict(lambda : math.inf)
for _ in range(n):
	(a, b) = input().split()
	a = int(a)
	b = ''.join(sorted(b))
	val[b] = min(val[b], a)
t = []
t.append(val['A'] + val['B'] + val['C'])
t.append(val['ABC'])
t.append(val['AB'] + val['BC'])
t.append(val['AC'] + val['BC'])
t.append(val['AC'] + val['AB'])
t.append(val['AB'] + val['ABC'])
t.append(val['AC'] + val['ABC'])
t.append(val['BC'] + val['ABC'])
t.append(val['AB'] + val['C'])
t.append(val['AC'] + val['B'])
t.append(val['A'] + val['BC'])
if min(t) == math.inf:
	print('-1')
else:
	print(min(t))


import math
n = int(input())
a = []
for i in range(n):
	(c, s) = input().split()
	a.append((int(c), s))
p = q = r = t = 0
(x, y, z) = (0, 0, 0)
i = 0
while i < len(a):
	c = a[i][0]
	s = a[i][1]
	if 'A' in s and (x > c or x == 0):
		x = c
	if 'B' in s and (y > c or y == 0):
		y = c
	if 'C' in s and (z > c or z == 0):
		z = c
	if 'A' in s and 'B' in s and (p > c or p == 0):
		p = c
	if 'B' in s and 'C' in s and (q > c or q == 0):
		q = c
	if 'A' in s and 'C' in s and (r > c or r == 0):
		r = c
	if len(s) == 3 and (t > c or t == 0):
		t = c
	i += 1
f = []
m = -1
if x and y and z:
	f.append(x + y + z)
if p and z:
	f.append(p + z)
if q and x:
	f.append(q + x)
if r and y:
	f.append(r + y)
if t:
	f.append(t)
if len(f) > 0:
	m = min(f)
print(m)


from collections import Counter
n = int(input())
a = [1000000.0]
b = [1000000.0]
c = [1000000.0]
ab = [1000000.0]
bc = [1000000.0]
ca = [1000000.0]
abc = [1000000.0]
for i in range(n):
	(x, y) = map(str, input().split())
	x = int(x)
	t = Counter(y)
	if t['A'] and t['B'] and t['C']:
		abc.append(x)
	elif t['A'] and t['B'] and (t['C'] == 0):
		ab.append(x)
	elif t['A'] == 0 and t['B'] and t['C']:
		bc.append(x)
	elif t['A'] and t['B'] == 0 and t['C']:
		ca.append(x)
	elif t['A'] and t['B'] == 0 and (t['C'] == 0):
		a.append(x)
	elif t['A'] == 0 and t['B'] and (t['C'] == 0):
		b.append(x)
	elif t['A'] == 0 and t['B'] == 0 and t['C']:
		c.append(x)
(ma, mb, mc, mab, mbc, mca, mabc) = (min(a), min(b), min(c), min(ab), min(bc), min(ca), min(abc))
ans = min(ma + mbc, mb + mca, mc + mab, ma + mb + mc, mabc, ma + mb + mbc, ma + mb + mca, ma + mc + mab, ma + mc + mbc, ma + mab + mbc, ma + mab + mca, ma + mbc + mca, mb + mc + mab, mb + mc + mca, mb + mab + mbc, mb + mab + mca, mb + mbc + mca, mc + mab + mbc, mc + mab + mca, mc + mbc + mca, mab + mbc, mab + mca, mbc + mca)
print(ans if ans != 1000000.0 else -1)


n = int(input())
a = 300001
b = 300001
c = 300001
ab = 300001
bc = 300001
ac = 300001
abc = 300001
for i in range(n):
	w = list(map(str, input().split(' ')))
	q = int(w[0])
	if len(w[1]) == 3:
		abc = q if q < abc else abc
		ab = q if q < ab else ab
		bc = q if q < bc else bc
		ac = q if q < ac else ac
		a = q if q < a else a
		b = q if q < b else b
		c = q if q < c else c
	elif len(w[1]) >= 2:
		if w[1] == 'AB' or w[1] == 'BA':
			ab = q if q < ab else ab
			a = q if q < a else a
			b = q if q < b else b
		if w[1] == 'BC' or w[1] == 'CB':
			bc = q if q < bc else bc
			c = q if q < c else c
			b = q if q < b else b
		if w[1] == 'AC' or w[1] == 'CA':
			ac = q if q < ac else ac
			a = q if q < a else a
			c = q if q < c else c
	else:
		a = q if q < a and w[1] == 'A' else a
		b = q if q < b and w[1] == 'B' else b
		c = q if q < c and w[1] == 'C' else c
if abc == 300001 and ab == 300001 and (ac == 300001) and (a == 300001) or (abc == 300001 and ab == 300001 and (bc == 300001) and (b == 300001)) or (abc == 300001 and bc == 300001 and (ac == 300001) and (c == 300001)):
	print(-1)
else:
	print(min(abc, ab + c, ab + ac, ab + bc, bc + a, bc + ac, ac + b, a + b + c, a + b + ac, a + b + bc, a + c + ab, a + c + bc, b + c + ab, b + c + ac))


n = int(input())
a = [1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000]
for i in range(n):
	s = input()
	k = s.split(' ')
	q = int(k[0])
	p = k[1]
	p = sorted(p)
	p = ''.join(p)
	if p == 'A' or p == 'B' or p == 'C':
		a[int(ord(p[0]) - ord('A'))] = min(a[int(ord(p[0]) - ord('A'))], q)
	elif p == 'AB':
		a[3] = min(a[3], q)
	elif p == 'AC':
		a[4] = min(a[4], q)
	elif p == 'BC':
		a[5] = min(a[5], q)
	elif p == 'ABC':
		a[6] = min(a[6], q)
x = a[0] + a[1] + a[2]
y1 = a[4] + a[5]
y2 = a[3] + a[4]
y3 = a[3] + a[5]
y4 = a[0] + a[5]
y5 = a[1] + a[4]
y6 = a[2] + a[3]
z = a[6]
ans = min(x, y1, y2, y3, y4, y5, y6, z)
if ans == 1000000:
	print(-1)
else:
	print(ans)


mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
l = ['A', 'B', 'C', 'AC', 'BC', 'AB', 'ABC']
vl = [1000000] * 7
n = ii()
for _ in range(n):
	(n, s) = input().split()
	n = int(n)
	s = ''.join((i for i in sorted(s)))
	ind = l.index(s)
	vl[ind] = min(vl[ind], n)
if vl[0] == 1000000 and vl[3] == 1000000 and (vl[5] == 1000000) and (vl[6] == 1000000):
	exit(print(-1))
if vl[1] == 1000000 and vl[4] == 1000000 and (vl[5] == 1000000) and (vl[6] == 1000000):
	exit(print(-1))
if vl[2] == 1000000 and vl[3] == 1000000 and (vl[4] == 1000000) and (vl[6] == 1000000):
	exit(print(-1))
print(min(vl[0] + vl[1] + vl[3], vl[0] + vl[1] + vl[4], vl[0] + vl[1] + vl[2], vl[1] + vl[2] + vl[3], vl[1] + vl[2] + vl[5], vl[3] + vl[4], vl[3] + vl[5], vl[4] + vl[5], vl[6], vl[0] + vl[4], vl[1] + vl[3], vl[2] + vl[5]))


a = []
b = []
c = []
abc = []
ab = []
bc = []
ac = []
final = []
n = int(input())
for i in range(0, n):
	(x, y) = input().split()
	if y == 'A':
		a.append(int(x))
	if y == 'B':
		b.append(int(x))
	if y == 'C':
		c.append(int(x))
	if y == 'ABC' or y == 'BAC' or y == 'BCA' or (y == 'ACB') or (y == 'CAB') or (y == 'CBA'):
		abc.append(int(x))
	if y == 'AB' or y == 'BA':
		ab.append(int(x))
	if y == 'CA' or y == 'AC':
		ac.append(int(x))
	if y == 'BC' or y == 'CB':
		bc.append(int(x))
if len(a) != 0 and len(b) != 0 and (len(c) != 0):
	final.append(min(a) + min(b) + min(c))
if len(a) != 0 and len(bc) != 0:
	final.append(min(a) + min(bc))
if len(abc) != 0:
	final.append(min(abc))
if len(b) != 0 and len(ac) != 0:
	final.append(min(b) + min(ac))
if len(c) != 0 and len(ab) != 0:
	final.append(min(c) + min(ab))
if len(ab) != 0:
	if len(bc) != 0:
		final.append(min(ab) + min(bc))
	if len(ac) != 0:
		final.append(min(ab) + min(ac))
if len(bc) != 0:
	if len(ab) != 0:
		final.append(min(ab) + min(bc))
	if len(ac) != 0:
		final.append(min(bc) + min(ac))
if len(ac) != 0:
	if len(bc) != 0:
		final.append(min(ac) + min(bc))
	if len(ab) != 0:
		final.append(min(ab) + min(ac))
if len(final) != 0:
	print(min(final))
else:
	print(-1)


n = int(input())
d = {'A': 10000000, 'B': 10000000, 'C': 10000000, 'AB': 10000000, 'AC': 10000000, 'BC': 10000000, 'ABC': 10000000}
while n > 0:
	(c, v) = [x for x in input().split()]
	c = int(c)
	v = ''.join(sorted(v))
	if d[v] > c:
		d[v] = c
	n -= 1
d['AB'] = min(d['ABC'], d['AB'], d['A'] + d['B'])
d['AC'] = min(d['ABC'], d['AC'], d['A'] + d['C'])
d['BC'] = min(d['ABC'], d['BC'], d['B'] + d['C'])
d['A'] = min(d['A'], d['AB'], d['AC'])
d['B'] = min(d['B'], d['AB'], d['BC'])
d['C'] = min(d['C'], d['BC'], d['AC'])
d['ABC'] = min(d['ABC'], d['AB'] + d['C'], d['BC'] + d['A'], d['AC'] + d['B'])
print(d['ABC'] if d['ABC'] < 10000000 else -1)


class Juice:

	def __init__(self, arg):
		self.price = int(arg[0])
		self.vitamin = 0
		for i in range(0, len(arg[1])):
			if arg[1][i] == 'A':
				self.vitamin += 1
			elif arg[1][i] == 'B':
				self.vitamin += 2
			else:
				self.vitamin += 4
			continue

class Solution:

	def __init__(self):
		self.juices = []
		self.prices = [0, 400000, 400000, 400000, 400000, 400000, 400000, 400000]

	def _input_(self):
		n = int(input())
		for i in range(0, n):
			self.juices.append(Juice(input().split(' ')))

	def run(self):
		self._input_()
		for i in range(0, 8):
			for j in self.juices:
				if self.prices[i | j.vitamin] > self.prices[i] + j.price:
					self.prices[i | j.vitamin] = self.prices[i] + j.price
				continue
			continue
		if self.prices[7] == 400000:
			print(-1)
		else:
			print(self.prices[7])
		pass
Solution().run()


dic = {'A': 10 ** 6, 'B': 10 ** 6, 'C': 10 ** 6, 'AB': 10 ** 6, 'AC': 10 ** 6, 'BC': 10 ** 6, 'ABC': 10 ** 6}
n = int(input())
for i in range(n):
	x = input().split(' ')
	key = ''.join(sorted(list(x[1])))
	if int(x[0]) < dic[key]:
		dic[key] = int(x[0])
mins = [dic['A'] + dic['B'] + dic['C'], dic['A'] + dic['BC'], dic['B'] + dic['AC'], dic['C'] + dic['AB'], dic['AB'] + dic['AC'], dic['AB'] + dic['BC'], dic['AC'] + dic['BC'], dic['ABC']]
if min(mins) >= 10 ** 6:
	print(-1)
else:
	print(min(mins))


n = int(input())
dict1 = {}
for i in range(n):
	(a, b) = map(str, input().split())
	b = ''.join(sorted(b))
	a = int(a)
	try:
		dict1[b].append(a)
	except:
		KeyError
		dict1[b] = [a]
ans = 10 ** 19
if 'ABC' in dict1.keys():
	ans = min(ans, min(dict1['ABC']))
if 'A' in dict1.keys() and 'B' in dict1.keys() and ('C' in dict1.keys()):
	ans = min(ans, min(dict1['A']) + min(dict1['B']) + min(dict1['C']))
try:
	ans = min(ans, min(dict1['AB']) + min(dict1['BC']))
except:
	KeyError
try:
	ans = min(ans, min(dict1['AB']) + min(dict1['C']))
except:
	KeyError
try:
	ans = min(ans, min(dict1['AB']) + min(dict1['AC']))
except:
	KeyError
try:
	ans = min(ans, min(dict1['AC']) + min(dict1['B']))
except:
	KeyError
try:
	ans = min(ans, min(dict1['AC']) + min(dict1['BC']))
except:
	KeyError
try:
	ans = min(ans, min(dict1['BC']) + min(dict1['A']))
except:
	KeyError
if ans == 10 ** 19:
	print(-1)
else:
	print(ans)


n = int(input())
d = [300001 for i in range(8)]
for i in range(n):
	(p, s) = input().split()
	s = 1 * ('A' in s) + 2 * ('B' in s) + 4 * ('C' in s)
	d[s] = min(d[s], int(p))
t = min(d[1] + d[2] + d[4], d[2] + d[5], d[3] + d[4], d[1] + d[6], d[3] + d[5], d[3] + d[6], d[5] + d[6], d[7])
print(t if t < 300001 else -1)


n = int(input())
d = dict()
for el1 in 'ABC':
	d[el1] = float('inf')
	for el2 in 'ABC':
		d[el1 + el2] = float('inf')
		for el3 in 'ABC':
			d[el1 + el2 + el3] = float('inf')
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	b = list(b)
	b.sort()
	b = ''.join(b)
	for i in range(len(b)):
		d[b[i]] = min(d[b[i]], a)
		for j in range(i + 1, len(b)):
			d[b[i] + b[j]] = min(d[b[i] + b[j]], a)
	d[b] = min(d[b], a)
for el1 in 'ABC':
	for el2 in 'ABC':
		if el2 >= el1:
			d[el1 + el2] = min(d[el1 + el2], d[el1] + d[el2])
for el1 in 'ABC':
	for el2 in 'ABC':
		if el2 >= el1:
			for el3 in 'ABC':
				if el3 >= el2:
					d[el1 + el2 + el3] = min(d[el1 + el2 + el3], d[el1 + el2] + d[el3], d[el1 + el3] + d[el2], d[el2 + el3] + d[el1])
print(d['ABC'] if d['ABC'] != float('inf') else -1)


d = {'A': [], 'B': [], 'C': [], 'AB': [], 'AC': [], 'BC': [], 'ABC': []}
for _ in range(int(input())):
	(c, v) = map(str, input().split())
	c = int(c)
	v = sorted(v)
	x = ''
	for i in v:
		x += i
	d[x].append(c)
for i in d:
	if len(d[i]) == 0:
		d[i].append(10 ** 6)
m = min(d['A']) + min(d['B']) + min(d['C'])
n = min(d['AB']) + min(d['C'])
o = min(d['BC']) + min(d['A'])
p = min(d['AC']) + min(d['B'])
r = min(d['AB']) + min(d['BC'])
s = min(d['AC']) + min(d['BC'])
t = min(d['AB']) + min(d['AC'])
q = min(m, n, o, p, min(d['ABC']), r, s, t)
if q >= 10 ** 6:
	print(-1)
else:
	print(q)


num_juices = int(input())
min_a = min_b = min_c = min_ab = min_ac = min_bc = min_abc = 500000
for _ in range(num_juices):
	(cost, juice) = input().split()
	cost = int(cost)
	juice = ''.join(sorted(juice))
	if juice == 'A':
		min_a = min(min_a, cost)
	elif juice == 'B':
		min_b = min(min_b, cost)
	elif juice == 'C':
		min_c = min(min_c, cost)
	elif juice == 'AB':
		min_a = min(min_a, cost)
		min_b = min(min_b, cost)
		min_ab = min(min_ab, cost)
	elif juice == 'AC':
		min_a = min(min_a, cost)
		min_c = min(min_c, cost)
		min_ac = min(min_ac, cost)
	elif juice == 'BC':
		min_b = min(min_b, cost)
		min_c = min(min_c, cost)
		min_bc = min(min_bc, cost)
	else:
		min_abc = min(min_abc, cost)
min_ab = min(min_ab, min_a + min_b)
min_ac = min(min_ac, min_a + min_c)
min_bc = min(min_bc, min_b + min_c)
min_abc = min(min_abc, min(min_ab + min_c, min(min_ac + min_b, min_bc + min_a)))
if min_abc > 300000:
	min_abc = -1
print(min_abc)


n = int(input())
posibles = ['A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']
jugos = [100001] * 7
found = [False] * 7
for _ in range(n):
	lista = input().split()
	c = int(lista[0])
	jugo = ''.join(sorted(lista[1]))
	i = posibles.index(jugo)
	jugos[i] = min(jugos[i], c)
	found[i] = True
minimo = 10 ** 8 + 1
if found[0] and found[1] and found[2]:
	minimo = min(minimo, jugos[0] + jugos[1] + jugos[2])
if found[0] and found[5]:
	minimo = min(minimo, jugos[0] + jugos[5])
if found[1] and found[4]:
	minimo = min(minimo, jugos[1] + jugos[4])
if found[2] and found[3]:
	minimo = min(minimo, jugos[2] + jugos[3])
if found[3] and found[4]:
	minimo = min(minimo, jugos[3] + jugos[4])
if found[3] and found[5]:
	minimo = min(minimo, jugos[3] + jugos[5])
if found[4] and found[5]:
	minimo = min(minimo, jugos[4] + jugos[5])
if found[6]:
	minimo = min(minimo, jugos[6])
if minimo == 10 ** 8 + 1:
	print(-1)
else:
	print(minimo)


n = int(input())
p = [list(input().split()) for i in range(n)]
word = [(''.join(sorted(list(j))), int(i)) for (i, j) in p]
word.sort()
a = list()
b = list()
c = list()
ab = list()
ac = list()
bc = list()
abc = list()
for (i, j) in word:
	if 'A' == i:
		a.append((j, i))
	elif 'B' == i:
		b.append((j, i))
	elif 'C' == i:
		c.append((j, i))
	elif 'AB' == i:
		ab.append((j, i))
	elif 'BC' == i:
		bc.append((j, i))
	elif 'AC' == i:
		ac.append((j, i))
	elif 'ABC' == i:
		abc.append((j, i))
a.sort()
b.sort()
c.sort()
ab.sort()
bc.sort()
ac.sort()
abc.sort()
na = len(a)
nb = len(b)
nc = len(c)
if (na is 0 or nb is 0 or nc is 0) and (len(bc) is 0 or len(a) is 0) and (len(ac) is 0 or len(b) is 0) and (len(ab) is 0 or len(c) is 0) and ((len(ab) is 0 or len(bc) is 0) and (len(bc) is 0 or len(ac) is 0) and (len(ac) is 0 or len(ab) is 0)) and (len(abc) is 0):
	print('-1')
else:
	m = list()
	if na is not 0 and nb is not 0 and (nc is not 0):
		m.append(a[0][0] + b[0][0] + c[0][0])
	if len(ab) is not 0 and len(bc) is not 0:
		m.append(ab[0][0] + bc[0][0])
	if len(ac) is not 0 and len(ab) is not 0:
		m.append(ac[0][0] + ab[0][0])
	if len(bc) is not 0 and len(ac) is not 0:
		m.append(bc[0][0] + ac[0][0])
	if len(bc) is not 0 and len(a) is not 0:
		m.append(bc[0][0] + a[0][0])
	if len(c) is not 0 and len(ab) is not 0:
		m.append(c[0][0] + ab[0][0])
	if len(ac) is not 0 and len(b) is not 0:
		m.append(ac[0][0] + b[0][0])
	if len(abc) is not 0:
		m.append(abc[0][0])
	print(min(m))


n = int(input())
v = [0] + [10 ** 9] * 7
for _ in range(n):
	(c, s) = input().split()
	c = int(c)
	s = sum((1 << ord(x) - ord('A') for x in s))
	for i in range(8):
		v[i | s] = min(v[i | s], v[i] + c)
if v[7] == 10 ** 9:
	v[7] = -1
print(v[7])


inf = float('inf')

def allpres(s, x):
	for i in x:
		if i not in s:
			return False
	return True

def mp(arr, x):
	for (i, j) in arr:
		if allpres(j, x):
			return i
	return inf
r = lambda : list(map(int, input().split()))
t = int(input())
arr = []
for _ in range(t):
	(c, s) = input().split()
	c = int(c)
	arr.append([c, s])
arr.sort(key=lambda x: x[0])
ans = inf
x = mp(arr, 'A')
y = mp(arr, 'B')
z = mp(arr, 'C')
ans = min(ans, x + y + z)
x = mp(arr, 'AB')
y = mp(arr, 'C')
ans = min(ans, x + y)
x = mp(arr, 'BC')
y = mp(arr, 'A')
ans = min(ans, x + y)
x = mp(arr, 'AC')
y = mp(arr, 'B')
ans = min(ans, x + y)
x = mp(arr, 'ABC')
ans = min(ans, x)
print(ans if ans != inf else -1)


def f(n):
	if n == 0:
		return 0
	for k in range(1, 8):
		for j in range(1, 8):
			if j | k == n and j != n and (k != n):
				a[n] = min(a[n], f(j) + f(k))
	return a[n]
n = int(input())
a = 8 * [100000000.0]
for _ in range(n):
	(val, s) = input().split()
	s = sum(map(lambda x: 1 << ord(x) - ord('A'), s))
	a[s] = min(a[s], int(val))
out = f(7)
if out == 100000000.0:
	print(-1)
else:
	print(out)


def get(k):
	return d.get(k, float('inf'))
n = int(input())
d = {}
for _ in range(n):
	(c, s) = input().split()
	c = int(c)
	s = ''.join(sorted(list(s)))
	d[s] = min(d.get(s, float('inf')), c)
cost = float('inf')
cost = min(cost, get('A') + get('B') + get('C'))
cost = min(cost, get('A') + get('BC'))
cost = min(cost, get('B') + get('AC'))
cost = min(cost, get('C') + get('AB'))
cost = min(cost, get('AB') + get('BC'))
cost = min(cost, get('AB') + get('AC'))
cost = min(cost, get('AC') + get('BC'))
cost = min(cost, get('ABC'))
if cost == float('inf'):
	cost = -1
print(cost)


n = int(input())
d = {'A': float('inf'), 'B': float('inf'), 'C': float('inf'), 'AB': float('inf'), 'BC': float('inf'), 'AC': float('inf'), 'ABC': float('inf')}
for i in range(n):
	(x, y) = input().split()
	x = int(x)
	y = sorted(y)
	y = ''.join((str(x) for x in y))
	if x < d[y]:
		d[y] = x
z = list(d.values())
a = []
a.append(z[0] + z[1] + z[2])
a.append(z[0] + z[4])
a.append(z[1] + z[5])
a.append(z[2] + z[3])
a.append(z[3] + z[4])
a.append(z[4] + z[5])
a.append(z[5] + z[3])
a.append(z[6])
if min(a) == float('inf'):
	print(-1)
else:
	print(min(a))


n = int(input())
di = {'a': 1, 'b': 2, 'ab': 3, 'c': 4, 'ac': 5, 'bc': 6, 'abc': 7}
cl = [None] * 8
for i in range(n):
	(c, s) = input().split()
	c = int(c)
	s = ''.join(sorted(s.lower()))
	ii = di[s]
	if cl[ii] is None or c < cl[ii]:
		cl[ii] = c
cl[0] = 0
mcost = sum([c for c in cl if c is not None])
fail = True
for i in range(8):
	for j in range(8):
		for k in range(8):
			if i | j | k != 7 or None in [cl[i], cl[j], cl[k]]:
				continue
			fail = False
			c = cl[i] + cl[j] + cl[k]
			if c < mcost:
				mcost = c
print(-1 if fail else mcost)


n = int(input())
sum = 0
prices = []
vitamins = []
words = dict()
for i in range(n):
	(cost, vitamin) = [str(i) for i in input().split()]
	prices.append(int(cost))
	vitamin = ''.join(sorted(vitamin))
	vitamins.append(vitamin)
for i in range(n):
	words[vitamins[i]] = min(words.get(vitamins[i], 1000000), prices[i])
sum = min(words.get('A', 1000000) + words.get('B', 1000000) + words.get('C', 1000000), words.get('AB', 1000000) + words.get('C', 1000000), words.get('AC', 1000000) + words.get('B', 1000000), words.get('AB', 1000000) + words.get('AC', 1000000), words.get('AB', 1000000) + words.get('BC', 1000000), words.get('BC', 1000000) + words.get('AC', 1000000), words.get('AC', 1000000) + words.get('BC', 1000000), words.get('BC', 1000000) + words.get('A', 1000000), words.get('ABC', 1000000))
if sum != 1000000:
	print(sum)
else:
	print(-1)


n = int(input())
m = float('inf')
d = {'A': m, 'B': m, 'C': m, 'AB': m, 'BC': m, 'AC': m, 'ABC': m}
ans = m
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	b = ''.join(sorted(list(b)))
	d[b] = min(d[b], a)
ans = min(ans, d['A'] + d['B'] + d['C'])
ans = min(ans, d['AB'] + d['BC'])
ans = min(ans, d['AC'] + d['BC'])
ans = min(ans, d['AB'] + d['AC'])
ans = min(ans, d['A'] + d['BC'])
ans = min(ans, d['B'] + d['AC'])
ans = min(ans, d['C'] + d['AB'])
ans = min(ans, d['ABC'])
if ans == m:
	print(-1)
else:
	print(ans)


n = int(input())
mt = 333333
sp = {'A': mt, 'B': mt, 'C': mt, 'AB': mt, 'AC': mt, 'BC': mt, 'ABC': mt}
for i in range(n):
	(c, a) = input().split()
	a = ''.join(sorted(list(a)))
	sp[a] = min(sp[a], int(c))
t = min(mt, sp['A'] + sp['B'] + sp['C'])
t = min(t, sp['AB'] + sp['C'])
t = min(t, sp['AB'] + sp['AC'])
t = min(t, sp['AB'] + sp['BC'])
t = min(t, sp['BC'] + sp['A'])
t = min(t, sp['BC'] + sp['AB'])
t = min(t, sp['BC'] + sp['AC'])
t = min(t, sp['AC'] + sp['B'])
t = min(t, sp['ABC'])
if t >= mt:
	print(-1)
else:
	print(t)


from collections import defaultdict
price = defaultdict(lambda : float('inf'))
n = int(input())
for _ in range(n):
	(p, vit) = input().split()
	vit = ''.join((i for i in sorted(vit)))
	price[vit] = min(price[vit], int(p))
	for v in vit:
		price[v] = min(price[v], int(p))
res = price['ABC']
res = min(price['A'] + price['B'] + price['C'], res)
res = min(price['A'] + price['BC'], res)
res = min(price['B'] + price['AC'], res)
res = min(price['C'] + price['AB'], res)
if res == float('inf'):
	print(-1)
else:
	print(res)


d = {'A': 1000000000, 'B': 1000000000, 'C': 1000000000, 'AB': 1000000000, 'BC': 1000000000, 'AC': 1000000000, 'ABC': 1000000000}
e = {'A': 0, 'B': 0, 'C': 0}
for _ in ' ' * int(input()):
	(a, b) = map(str, input().split())
	b = ''.join(sorted(b.replace(' ', '')))
	d[b] = min(d[b], int(a))
	for i in e:
		if i in b:
			e[i] += 1
if min(e.values()) < 1:
	exit(print(-1))
d['AB'] = min(d['AB'], d['A'] + d['B'])
d['BC'] = min(d['BC'], d['C'] + d['B'])
d['AC'] = min(d['AC'], d['A'] + d['C'])
print(min(d['ABC'], d['AB'] + d['C'], d['AC'] + d['B'], d['BC'] + d['A'], d['AB'] + d['BC'], d['AB'] + d['AC'], d['AC'] + d['BC']))


a = int(input())
d = 1000000000000000000
E = {'A': d, 'B': d, 'C': d, 'AB': d, 'BA': d, 'CA': d, 'AC': d, 'BC': d, 'CB': d, 'ACB': d, 'BAC': d, 'BCA': d, 'CAB': d, 'CBA': d, 'ABC': d}
for _ in range(a):
	(b, c) = input().split()
	if E[c] > int(b):
		E[c] = int(b)
f = min(E['A'] + E['B'] + E['C'], E['A'] + E['CB'], E['A'] + E['BC'], E['B'] + E['AC'], E['B'] + E['CA'], E['C'] + E['AB'], E['C'] + E['BA'], E['ABC'], E['ACB'], E['BAC'], E['BCA'], E['CAB'], E['CBA'], E['AB'] + E['AC'], E['AB'] + E['CB'], E['BA'] + E['BC'], E['BA'] + E['AC'], E['AC'] + E['CB'], E['CA'] + E['BA'], E['CA'] + E['CB'], E['BC'] + E['AC'], E['BC'] + E['AB'], E['CB'] + E['AC'], E['BA'] + E['CB'], E['AB'] + E['CA'])
if f == d:
	print(-1)
else:
	print(f)


n = int(input())
costs = []
A = 600000
B = 600000
C = 600000
AB = 600000
AC = 600000
BC = 600000
ABC = 600000
for i in range(n):
	words = []
	for word in input().split():
		words.append(word)
	cost = int(words[0])
	words[1] = ''.join(sorted(words[1]))
	if words[1] == 'A':
		A = min(A, cost)
	if words[1] == 'B':
		B = min(B, cost)
	if words[1] == 'C':
		C = min(C, cost)
	if words[1] == 'AB':
		AB = min(AB, cost)
	if words[1] == 'BC':
		BC = min(BC, cost)
	if words[1] == 'AC':
		AC = min(AC, cost)
	if words[1] == 'ABC':
		ABC = min(ABC, cost)
answer = min(ABC, AB + C, AC + B, BC + A, A + B + C, AB + BC, AB + AC, BC + AC)
if answer == 600000:
	answer = -1
print(answer)


n = int(input())
tt = {'A': 0, 'B': 1, 'C': 2, 'AB': 3, 'BA': 3, 'AC': 4, 'CA': 4, 'BC': 5, 'CB': 5, 'ABC': 6, 'ACB': 6, 'BAC': 6, 'BCA': 6, 'CBA': 6, 'CAB': 6}
vit = [float('INF') for i in range(7)]
for i in range(n):
	az = str(input())
	i = 0
	while i < len(az) and az[i] != ' ':
		i += 1
	cost = int(az[:i])
	t = tt[az[i + 1:]]
	vit[t] = min(vit[t], cost)
ans = min([vit[0] + vit[1] + vit[2], vit[3] + vit[2], vit[4] + vit[1], vit[5] + vit[0], vit[6], vit[3] + vit[5], vit[3] + vit[4], vit[4] + vit[5]])
if ans != float('INF'):
	print(ans)
else:
	print(-1)


def solve(n, seq):
	abc = [False] * 3
	combination = ['A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC']
	index = 0
	innerIndex = 0
	ans = 0
	temp = 0
	price = {x: 300001 for x in combination}
	for (p, vit) in seq:
		if 'A' in vit:
			abc[0] = True
		if 'B' in vit:
			abc[1] = True
		if 'C' in vit:
			abc[2] = True
		if len(vit) == 1:
			if p < price[vit]:
				price[vit] = p
		else:
			vitamin = sorted(list(vit))
			string = ''.join(vitamin)
			if p < price[string]:
				price[string] = p
	if False in abc:
		return -1
	combination1 = combination[:3]
	combination2 = combination[3:6]
	ans = price['A'] + price['B'] + price['C']
	temp = 0
	index = 0
	innerIndex = 0
	innerIndex2 = 0
	while index < 3 - 1:
		innerIndex = index + 1
		while innerIndex < 3:
			string = combination1[index] + combination1[innerIndex]
			innerIndex2 = 0
			while innerIndex2 < 3:
				temp = 0
				if combination2[innerIndex2] != string:
					temp += price[combination2[innerIndex2]] + price[combination1[innerIndex]] + price[combination1[index]]
					if temp < ans:
						ans = temp
				innerIndex2 += 1
			innerIndex += 1
		index += 1
	index = 0
	while index < 3:
		innerIndex = 0
		while innerIndex < 3:
			temp = 0
			if combination1[index] not in combination2[innerIndex]:
				temp += price[combination1[index]] + price[combination2[innerIndex]]
				if temp < ans:
					ans = temp
			innerIndex += 1
		index += 1
	index = 0
	while index < 3:
		innerIndex = 0
		while innerIndex < 3:
			temp = 0
			if combination2[index] != combination2[innerIndex]:
				temp += price[combination2[index]] + price[combination2[innerIndex]]
				if temp < ans:
					ans = temp
			innerIndex += 1
		index += 1
	return min(ans, price['ABC'])
n = int(input())
seq = []
for x in range(n):
	temp = [0] * 2
	(x, y) = input().split()
	temp[0] = int(x)
	temp[1] = y
	seq.append(temp)
print(solve(n, seq))


juices = dict()
(HA, HB, HC) = (1 << 2, 1 << 1, 1)
import math
for k in range(1, (HA | HB | HC) + 1):
	juices[k] = math.inf
n = int(input())
for i in range(n):
	(costs, tp) = input().split()
	cost = int(costs)
	(ha, hb, hc) = map(lambda x: 1 if x else 0, ['A' in tp, 'B' in tp, 'C' in tp])
	d = ha << 2 | hb << 1 | hc
	v = juices.get(d)
	if v is None:
		v = math.inf
	if cost < v:
		juices[d] = cost
for key1 in juices:
	for key2 in juices:
		cmb = key1 | key2
		v = juices[key1] + juices[key2]
		if v < juices[cmb]:
			juices[cmb] = v
v = juices[HA | HB | HC]
if v == math.inf:
	print(-1)
else:
	print(v)


tmp = float('inf')
d = {'A': tmp, 'B': tmp, 'C': tmp, 'AB': tmp, 'AC': tmp, 'BC': tmp, 'ABC': tmp}
seen = {'A': 0, 'B': 0, 'C': 0}
for i in range(int(input())):
	(c, s) = map(str, input().split())
	t = ''.join(sorted(s))
	d[t] = min(int(c), d[t])
	for j in s:
		seen[j] = 1
if sum(seen.values()) != 3:
	print(-1)
else:
	print(min([d['ABC'], d['A'] + d['B'] + d['C'], d['A'] + d['BC'], d['B'] + d['AC'], d['C'] + d['AB'], d['AB'] + d['AC'], d['AB'] + d['BC'], d['AC'] + d['BC']]))


n = int(input())
juices = dict()
minA = 100001
minB = 100001
minC = 100001
minAB = 100001
minBC = 100001
minCA = 100001
minABC = 100001
for _ in range(n):
	taken = str(input())
	taken = taken.split()
	price = int(taken[0])
	keys = str(taken[1])
	if 'A' in keys:
		if price < minA:
			minA = price
		if 'B' in keys:
			if price < minAB:
				minAB = price
		if 'C' in keys:
			if price < minCA:
				minCA = price
		if 'B' in keys and 'C' in keys:
			if price < minABC:
				minABC = price
	if 'B' in keys:
		if price < minB:
			minB = price
		if 'C' in keys:
			if price < minBC:
				minBC = price
	if 'C' in keys:
		if price < minC:
			minC = price
results = list()
if minABC != 100001:
	results.append(minABC)
if minAB != 100001 and minC != 100001:
	results.append(minAB + minC)
if minAB != 100001 and minBC != 100001:
	results.append(minAB + minBC)
if minAB != 100001 and minCA != 100001:
	results.append(minAB + minCA)
if minAB != 100001 and minABC != 100001:
	results.append(minAB + minABC)
if minBC != 100001 and minA != 100001:
	results.append(minBC + minA)
if minBC != 100001 and minAB != 100001:
	results.append(minBC + minAB)
if minBC != 100001 and minCA != 100001:
	results.append(minBC + minCA)
if minBC != 100001 and minABC != 100001:
	results.append(minBC + minABC)
if minCA != 100001 and minB != 100001:
	results.append(minCA + minB)
if minCA != 100001 and minAB != 100001:
	results.append(minCA + minAB)
if minCA != 100001 and minBC != 100001:
	results.append(minCA + minBC)
if minCA != 100001 and minABC != 100001:
	results.append(minCA + minABC)
if minA != 100001 and minB != 100001 and (minC != 100001):
	results.append(minA + minB + minC)
if results:
	print(min(results))
else:
	print(-1)


n = int(input())
price = []
vita = []
dmin = {}
dmin['A'] = 9999999
dmin['B'] = 9999999
dmin['C'] = 9999999
dmin['AB'] = 9999999
dmin['AC'] = 9999999
dmin['BC'] = 9999999
dmin['ABC'] = 9999999
for i in range(n):
	(p, v) = input().split()
	p = int(p)
	v = ''.join(sorted(v))
	if dmin[v] > p:
		dmin[v] = p
	vita.append((p, v))
th1 = dmin['A'] + dmin['B'] + dmin['C']
th2 = dmin['A'] + dmin['BC']
th3 = dmin['B'] + dmin['AC']
th4 = dmin['C'] + dmin['AB']
th5 = dmin['ABC']
th6 = dmin['AB'] + dmin['BC']
th7 = dmin['AC'] + dmin['BC']
th8 = dmin['AB'] + dmin['AC']
res = min(th1, th2, th3, th4, th5, th6, th7, th8)
if res == 9999999:
	print(-1)
else:
	print(res)


n = int(input().strip())
d = {'A': 1000000, 'B': 1000000, 'C': 1000000, 'AB': 1000000, 'BC': 1000000, 'AC': 1000000, 'ABC': 1000000}
for i in range(n):
	(c, s) = input().split()
	cst = int(c)
	l = list(s)
	l.sort()
	x = ''.join(l)
	d[x] = min(cst, d[x])
tc = min(d['A'] + d['B'] + d['C'], d['AB'] + d['BC'], d['AC'] + d['BC'], d['AC'] + d['AB'], d['ABC'], d['A'] + d['BC'], d['AB'] + d['C'], d['B'] + d['AC'])
if tc > 300000:
	print(-1)
else:
	print(tc)