#include <bits/stdc++.h>
using namespace std;
int read() {
  int out = 0, sgn = 1;
  char jp = getchar();
  while (jp != '-' && (jp < '0' || jp > '9')) jp = getchar();
  if (jp == '-') sgn = -1, jp = getchar();
  while (jp >= '0' && jp <= '9') out = out * 10 + jp - '0', jp = getchar();
  return out * sgn;
}
const int N = 1e3 + 10, K = 14;
const long long inf = 1e18;
int n, m, ch[N][K], idx = 0, fail[N];
char buf[N * N];
long long val[N];
void ins(int w) {
  int u = 0, len = strlen(buf + 1);
  for (int i = 1; i <= len; ++i) {
    int c = buf[i] - 'a';
    if (!ch[u][c]) ch[u][c] = ++idx;
    u = ch[u][c];
  }
  val[u] += w;
}
queue<int> q;
void getfail() {
  for (int i = 0; i < K; ++i)
    if (ch[0][i]) q.push(ch[0][i]);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < K; ++i)
      if (ch[u][i]) {
        q.push(ch[u][i]);
        fail[ch[u][i]] = ch[fail[u]][i];
        int x = ch[u][i], fa = ch[fail[u]][i];
        val[x] += val[fa];
      } else
        ch[u][i] = ch[fail[u]][i];
  }
}
int pos[N], k = 0, f[N][K + 1];
long long g[N][K + 1], dp[2][1 << K][N];
bool in(int S, int x) { return S >> x & 1; }
void upd(long long &x, long long y) { x = max(x, y); }
void cl(int id) {
  for (int S = 0; S < (1 << K); ++S)
    for (int x = 0; x <= idx; ++x) dp[id][S][x] = -inf;
}
int main() {
  m = read();
  for (int i = 1; i <= m; ++i) {
    scanf("%s", buf + 1);
    ins(read());
  }
  getfail();
  scanf("%s", buf + 1);
  n = strlen(buf + 1);
  pos[0] = 0;
  for (int i = 1; i <= n; ++i)
    if (buf[i] == '?') pos[++k] = i;
  pos[k + 1] = n + 1;
  for (int x = 0; x <= idx; ++x)
    for (int i = 0; i <= k; ++i) {
      int u = x;
      long long w = 0;
      for (int j = pos[i] + 1; j <= pos[i + 1] - 1; ++j)
        u = ch[u][buf[j] - 'a'], w += val[u];
      f[x][i] = u, g[x][i] = w;
    }
  int id = 0;
  cl(id);
  dp[id][0][0] = 0;
  for (int i = 0; i < k; ++i) {
    cl(id ^ 1);
    for (int x = 0; x <= idx; ++x) {
      int y = f[x][i];
      long long w = g[x][i];
      for (int S = 0; S < (1 << K); ++S)
        if (dp[id][S][x] > -inf)
          for (int c = 0; c < K; ++c)
            if (!in(S, c))
              upd(dp[id ^ 1][S | 1 << c][ch[y][c]],
                  dp[id][S][x] + w + val[ch[y][c]]);
    }
    id ^= 1;
  }
  long long ans = -inf;
  for (int x = 0; x <= idx; ++x)
    for (int S = 0; S < (1 << K); ++S)
      if (dp[id][S][x] > -inf) upd(ans, dp[id][S][x] + g[x][k]);
  printf("%lld\n", ans);
  return 0;
}
