human
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 ≤ n ≤ 1 000) — the number of juices.

Each of the next n lines contains an integer c_i (1 ≤ c_i ≤ 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".

Write me a Python program to print -1 if there is no way to obtain all three vitamins?
assistant
#Here is Python program to print -1 if there is no way to obtain all three vitamins.

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

if __name__ == "__main__":
    Solution = CodeforcesTask1042BSolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())