#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
vector<map<char, int> > g;
vector<int> f;
vector<long long> w;
int add(string s) {
  int R = 0;
  for (char c : s) {
    if (!g[R].count(c))
      g[R][c] = (int)g.size(), g.push_back({}), f.push_back(0), w.push_back(0);
    R = g[R][c];
  }
  return R;
}
int FSM() {
  queue<int> qu;
  qu.push(0);
  while (qu.size()) {
    int u = qu.front();
    qu.pop();
    for (auto it : g[u]) {
      char c = it.first;
      int v = it.second;
      for (int z = f[u]; z >= 0; z = f[z])
        if (g[z].count(c)) {
          f[v] = g[z][c];
          break;
        }
      w[v] += w[f[v]];
      qu.push(v);
    }
  }
  return 1;
}
int nxt[1005][20];
long long val[1005][20];
long long tmp[1005];
long long dp[1005][1 << 14];
int go[1005][14];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  g.push_back({});
  f.push_back(-1);
  w.push_back(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string S;
    cin >> S;
    int W;
    cin >> W;
    w[add(S)] += W;
  }
  FSM();
  for (int i = 0; i < (int)g.size(); ++i)
    for (int j = 0; j < 14; ++j)
      for (int x = i; x >= 0; x = f[x])
        if (g[x].count('a' + j)) {
          go[i][j] = g[x]['a' + j];
          break;
        }
  string S;
  cin >> S;
  vector<int> pos;
  for (int i = 0; i < (int)S.size(); ++i)
    if (S[i] == '?') pos.emplace_back(i);
  int m = (int)pos.size();
  n = (int)S.size();
  for (int i = 0; i <= m; ++i) {
    int l = (i == 0 ? 0 : pos[i - 1] + 1);
    int r = (i == m ? n - 1 : pos[i] - 1);
    for (int u = 0; u < (int)g.size(); ++u) {
      nxt[u][i] = u;
      for (int j = l; j <= r; ++j) {
        int v = go[nxt[u][i]][S[j] - 'a'];
        nxt[u][i] = v;
        val[u][i] += w[v];
      }
    }
  }
  for (int i = 0; i < (1 << 14); ++i)
    for (int j = 0; j < (int)g.size(); ++j) dp[j][i] = -1e18;
  dp[0][0] = 0;
  long long ans = -1e18;
  for (int mask = 0; mask < (1 << 14); ++mask) {
    int c = __builtin_popcount(mask);
    if (c > m) continue;
    for (int u = 0; u < (int)g.size(); ++u) tmp[u] = -1e18;
    for (int u = 0; u < (int)g.size(); ++u)
      tmp[nxt[u][c]] = max(tmp[nxt[u][c]], dp[u][mask] + val[u][c]);
    if (c == m) {
      long long nxt = *max_element(tmp, tmp + (int)g.size());
      if (ans < nxt) ans = nxt;
      continue;
    }
    for (int u = 0; u < (int)g.size(); ++u)
      for (int i = 0; i < 14; ++i) {
        if (mask >> i & 1) continue;
        int v = go[u][i];
        int t = mask ^ (1 << i);
        if (dp[v][t] < tmp[u] + w[v]) dp[v][t] = tmp[u] + w[v];
      }
  }
  cout << ans << endl;
}
