#include <bits/stdc++.h>
using namespace std;
const int inf_int = 1e9 + 100;
const long long inf_ll = 1e18;
const double pi = 3.1415926535898;
bool debug = 0;
const int MAXN = 2e5 + 100;
const int LOG = 21;
const int mod = 998244353;
const int MX = (1e7 + 100);
int go[1024][26];
int sufLink[1024];
int leaf[1024];
int top = 1;
void build() {
  memset(sufLink, -1, sizeof sufLink);
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < 26; ++i) {
      int to = go[v][i];
      int suf = sufLink[v] == -1 ? 0 : go[sufLink[v]][i];
      if (to >= 0) {
        sufLink[to] = suf;
        q.push(to);
      } else {
        go[v][i] = suf;
      }
    }
    if (sufLink[v] >= 0) leaf[v] += leaf[sufLink[v]];
  }
}
long long dp[2][1 << 14][1024];
inline pair<int, long long> process_string(int v, const string &a) {
  long long res = 0;
  for (char c : a) {
    v = go[v][c];
    res += leaf[v];
  }
  return {v, res};
}
long long calc_cost[1024];
int calc_pos[1024];
void solve() {
  memset(go, -1, sizeof go);
  int k;
  cin >> k;
  for (int i = 1; i <= k; ++i) {
    string a;
    cin >> a;
    int v = 0;
    for (int c : a) {
      c = c - 'a';
      if (go[v][c] == -1) {
        go[v][c] = top++;
      }
      v = go[v][c];
    }
    int cost;
    cin >> cost;
    leaf[v] += cost;
  }
  build();
  string t;
  cin >> t;
  for (char &c : t) {
    if (c == '?') {
      continue;
    }
    c = c - 'a';
  }
  vector<int> pos;
  for (int i = 0; i < (int(t.size())); ++i) {
    if (t[i] == '?') {
      pos.push_back(i);
    }
  }
  long long ans = 0;
  if (pos.empty()) {
    int v = 0;
    for (char c : t) {
      v = go[v][c];
      ans += leaf[v];
    }
    cout << ans << "\n";
    return;
  }
  int st = pos[0];
  for (int i = 0; i < (1 << 14); ++i) {
    fill(dp[0][i], dp[0][i] + 1024, -inf_ll);
    fill(dp[1][i], dp[1][i] + 1024, -inf_ll);
  }
  auto x = process_string(0, t.substr(0, st));
  dp[0][0][x.first] = x.second;
  42;
  for (int i = 0; i < (int(pos.size())); ++i) {
    for (int mask = 0; mask < (1 << 14); ++mask) {
      for (int v = 0; v < top; ++v) {
        if (dp[0][mask][v] != -inf_ll) {
          long long val = dp[0][mask][v];
          for (int c = 0; c < 14; ++c) {
            if (!(mask & (1 << c))) {
              int to = go[v][c];
              dp[1][mask | (1 << c)][to] =
                  max(dp[1][mask | (1 << c)][to], val + leaf[to]);
            }
          }
          dp[0][mask][v] = -inf_ll;
        }
      }
    }
    string to_nxt = "";
    if (i + 1 < (int(pos.size()))) {
      to_nxt = t.substr(pos[i] + 1, pos[i + 1] - pos[i] - 1);
    } else {
      to_nxt = t.substr(pos[i] + 1, (int(t.size())) - pos[i] - 1);
    }
    42;
    for (int v = 0; v < top; ++v) {
      auto x = process_string(v, to_nxt);
      calc_cost[v] = x.second;
      calc_pos[v] = x.first;
    }
    for (int mask = 0; mask < (1 << 14); ++mask) {
      for (int v = 0; v < top; ++v) {
        if (dp[1][mask][v] != -inf_ll) {
          dp[0][mask][calc_pos[v]] =
              max(dp[0][mask][calc_pos[v]], dp[1][mask][v] + calc_cost[v]);
          dp[1][mask][v] = -inf_ll;
        }
      }
    }
  }
  ans = -inf_ll;
  for (int mask = 0; mask < (1 << 14); ++mask) {
    for (int v = 0; v < top; ++v) {
      ans = max(ans, dp[0][mask][v]);
    }
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(2);
  int t = 1;
  while (t--) {
    solve();
  }
  42;
}
