#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const long long INF = 9e18;
struct AC_Automata {
  int root, tree[N][14], tot;
  int fail[N];
  long long val[N];
  int qu[N], head, tail;
  int newnode() {
    for (int i = 0; i < 14; i++) tree[tot][i] = 0;
    fail[tot] = val[tot] = 0;
    return tot++;
  }
  void init() {
    tot = 0;
    root = newnode();
  }
  void insert(string s, long long _val) {
    int cur = root;
    for (int i = 0; s[i]; i++) {
      int tmp = s[i] - 'a';
      if (!tree[cur][tmp]) tree[cur][tmp] = newnode();
      cur = tree[cur][tmp];
    }
    val[cur] += _val;
  }
  void build() {
    head = 1, tail = 0;
    for (int i = 0; i < 14; i++)
      if (tree[0][i]) qu[++tail] = tree[0][i];
    while (head <= tail) {
      int cur = qu[head++];
      for (int i = 0; i < 14; i++) {
        int tmp = tree[cur][i];
        if (tmp)
          fail[tmp] = tree[fail[cur]][i], qu[++tail] = tmp;
        else
          tree[cur][i] = tree[fail[cur]][i];
      }
    }
    for (int i = 1; i <= tail; i++) val[qu[i]] += val[fail[qu[i]]];
  }
} ac;
int main() {
  ios::sync_with_stdio(false);
  ac.init();
  int k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    string s;
    int val;
    cin >> s >> val;
    ac.insert(s, val);
  }
  ac.build();
  string s;
  cin >> s;
  auto split = [&](string s) -> vector<string> {
    vector<string> v;
    string t = "";
    for (int i = 0; s[i]; i++) {
      if (s[i] == '?')
        v.push_back(t), t = "";
      else
        t.push_back(s[i]);
    }
    v.push_back(t);
    return v;
  };
  vector<string> v = split(s);
  int sz = v.size();
  vector<vector<int>> trans(sz, vector<int>(ac.tot));
  vector<vector<long long>> sum(sz, vector<long long>(ac.tot));
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < ac.tot; j++) {
      int tmp = j;
      long long tot = 0;
      for (auto x : v[i]) {
        tmp = ac.tree[tmp][x - 'a'];
        tot += ac.val[tmp];
      }
      trans[i][j] = tmp;
      sum[i][j] = tot;
    }
  }
  long long ma = -INF;
  vector<vector<long long>> dp(1 << 14, vector<long long>(ac.tot, -INF));
  dp[0][trans[0][0]] = sum[0][0];
  for (int i = 0; i < (1 << 14); i++) {
    bitset<16> st(i);
    int id = st.count() + 1;
    if (id >= sz) {
      ma = max(ma, *max_element(dp[i].begin(), dp[i].end()));
      continue;
    }
    for (int j = 0; j < ac.tot; j++) {
      if (dp[i][j] == -INF) continue;
      for (int k = 0; k < 14; k++) {
        if (st[k]) continue;
        int pos = ac.tree[j][k], nxt = trans[id][pos];
        long long val = ac.val[pos] + sum[id][pos];
        dp[i | (1 << k)][nxt] = max(dp[i | (1 << k)][nxt], dp[i][j] + val);
      }
    }
  }
  printf("%lld\n", ma);
  return 0;
}
