#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 2, K = 1e3 + 2;
char a[N];
vector<int> cnt[15];
long long int dp[K][1 << 14], d[K], ans = -1ll << 62;
int n, tot = 1, len, now, ch[K][14], val[K], fa[K], t[K];
inline void cmax(long long int& x, long long int y) {
  if (x < y) x = y;
}
inline void input() {
  scanf("%d", &n);
  int x, u, *v;
  for (int i = 1; i <= n; i++) {
    scanf("%s%d", a + 1, &x), len = strlen(a + 1), u = 1;
    for (int j = 1; j <= len; j++)
      v = &ch[u][a[j] - 'a'], !*v && (*v = ++tot), u = *v;
    val[u] += x;
  }
}
inline void init() {
  for (int i = 0; i <= 13; i++) ch[0][i] = 1;
  int u, v;
  for (queue<int> q({1}); !q.empty(); q.pop()) {
    v = fa[u = q.front()], val[u] += val[v], t[u] = u;
    for (int i = 0; i <= 13; i++)
      if (ch[u][i])
        fa[ch[u][i]] = ch[v][i], q.push(ch[u][i]);
      else
        ch[u][i] = ch[v][i];
  }
  for (int i = 0; i <= (1 << 14) - 1; i++)
    cnt[__builtin_popcount(i)].push_back(i);
  memset(dp, -127, sizeof(dp)), dp[1][0] = 0;
}
inline void upd(int x, int y, long long int w) { cmax(dp[x][y], w + val[x]); }
inline void work() {
  scanf("%s", a + 1), n = strlen(a + 1);
  for (int i = 1; i <= n; i++)
    if (a[i] == '?') {
      for (int v : cnt[now])
        for (int j = 1; j <= tot; j++)
          for (int k = 0; k <= 13; k++)
            if (!(v >> k & 1)) upd(ch[t[j]][k], 1 << k | v, dp[j][v] + d[j]);
      for (int j = 1; j <= tot; j++) t[j] = j, d[j] = 0;
      now++;
    } else
      for (int j = 1; j <= tot; j++)
        t[j] = ch[t[j]][a[i] - 'a'], d[j] += val[t[j]];
  for (int v : cnt[now])
    for (int i = 1; i <= tot; i++) cmax(ans, dp[i][v] + d[i]);
  cout << ans;
}
int main() {
  input();
  init();
  work();
  return 0;
}
