#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
using ll = long long int;
class TC {
 public:
  void solve() {
    int n;
    cin >> n;
    vector<int> A, B, C, three;
    vector<pair<int, string> > two;
    ll ans = 1e15;
    for (int i = 0; i < n; i++) {
      int c;
      string s;
      cin >> c >> s;
      sort(s.begin(), s.end());
      if (s == "A") {
        A.push_back(c);
      } else if (s == "B") {
        B.push_back(c);
      } else if (s == "C") {
        C.push_back(c);
      } else {
        int sz = (int)s.size();
        if (sz == 2) {
          two.push_back(make_pair(c, s));
        } else if (sz == 3) {
          three.push_back(c);
        }
      }
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.begin(), C.end());
    sort(three.begin(), three.end());
    if (three.size() > 0) {
      ans = min(ans, (ll)three[0]);
    }
    if (A.size() > 0 and B.size() > 0 and C.size() > 0) {
      ans = min(ans, (ll)A[0] + B[0] + C[0]);
    }
    for (int i = 0; i < two.size(); i++) {
      for (int j = i + 1; j < two.size(); j++) {
        bool a, b, c;
        a = b = c = false;
        string s = two[i].second + two[j].second;
        for (auto cc : s) {
          if (cc == 'A')
            a = true;
          else if (cc == 'B')
            b = true;
          else if (cc == 'C')
            c = true;
          else
            assert(false);
        }
        if (a and b and c) {
          ans = min(ans, (ll)two[i].first + two[j].first);
        }
      }
    }
    for (auto x : two) {
      string s = x.second;
      ll val = x.first;
      if (s == "AB") {
        if (C.size() > 0) {
          ans = min(ans, val + C[0]);
        }
      } else if (s == "AC") {
        if (B.size() > 0) {
          ans = min(ans, val + B[0]);
        }
      } else if (s == "BC") {
        if (A.size() > 0) {
          ans = min(ans, val + A[0]);
        }
      }
    }
    if (ans == (ll)1e15) {
      ans = -1;
    }
    cout << ans << endl;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  int testcases = 1;
  for (int i = 1; i <= testcases; i++) {
    TC tc;
    tc.solve();
  }
  return 0;
}
