#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 5, MAXM = 4e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() { return getchar(); }
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45) ny = -1;
  if (nc < 0) return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
long long f[1 << 14][MAXN], sum[MAXN], val[15][MAXN];
int tot = 1, pcnt[1 << 14], ch[14][MAXN], fail[MAXN], to[15][MAXN];
char s[MAXM];
inline void GetFail() {
  for (int i = (0); i <= (13); i++) ch[i][0] = 1;
  queue<int> q;
  q.push(1);
  for (; !q.empty();) {
    int x = q.front();
    q.pop();
    sum[x] += sum[fail[x]];
    for (int c = (0); c <= (13); c++)
      if (!ch[c][x])
        ch[c][x] = ch[c][fail[x]];
      else
        fail[ch[c][x]] = ch[c][fail[x]], q.push(ch[c][x]);
  }
}
inline void upd(long long& x, long long y) { x < y ? x = y : 0; }
int pos[MAXN];
int main() {
  for (int k = read(); k--;) {
    scanf("%s", s + 1);
    int c = read(), p = 1;
    for (int i = 1; s[i]; i++) {
      if (!ch[s[i] - 'a'][p]) ch[s[i] - 'a'][p] = ++tot;
      p = ch[s[i] - 'a'][p];
    }
    sum[p] += c;
  }
  GetFail();
  memset(f[0], -0x3f, sizeof(f[0])), f[0][1] = 0;
  scanf("%s", s + 1);
  int n = strlen(s + 1), cnt = 0;
  for (int i = (1); i <= (n); i++)
    if (s[i] == '?') pos[++cnt] = i;
  pos[cnt + 1] = n + 1;
  for (int t = (0); t <= (cnt); t++)
    for (int i = (1); i <= (tot); i++) {
      int p = i;
      long long tmp = sum[p];
      for (int j = (pos[t] + 1); j <= (pos[t + 1] - 1); j++)
        p = ch[s[j] - 'a'][p], tmp += sum[p];
      to[t][i] = p, val[t][i] = tmp;
    }
  memset(f, -0x3f, sizeof(f)), f[0][to[0][1]] = val[0][1];
  for (int i = 0; i < 1 << 14; i++) {
    pcnt[i] = pcnt[i >> 1] + (i & 1);
    for (int j = 0; j < 14; j++)
      if (~i >> j & 1) {
        int nxt = i | (1 << j), o = pcnt[i] + 1;
        for (int t = (1); t <= (tot); t++) {
          int now = ch[j][t];
          upd(f[nxt][to[o][now]], f[i][t] + val[o][now]);
        }
      }
  }
  long long ans = -1e18;
  for (int i = 0; i < 1 << 14; i++)
    if (pcnt[i] == cnt)
      for (int t = (1); t <= (tot); t++) ans = max(ans, f[i][t]);
  cout << ans << '\n';
  return 0;
}
