#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
const int M = (1 << 14) + 5;
char s[N];
const int L = 1e3 + 5;
int ch[L][14], fa[L], val[L], ct;
int q[L], l, r;
long long dp[2][L][M];
int tran[20][L];
long long sum[20][L];
int cal(int s) {
  int c = 0;
  for (int i = 0; i < 14; i++)
    if (s >> i & 1) c++;
  return c;
}
void init();
void work();
int ca = 1;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  work();
  return 0;
}
void work() {
  int K;
  cin >> K;
  for (int i = 1; i <= K; i++) {
    cin >> s + 1;
    int p = 0, c;
    for (int j = 1; s[j]; j++) {
      c = s[j] - 'a';
      if (!ch[p][c]) ch[p][c] = ++ct;
      p = ch[p][c];
    }
    cin >> c;
    val[p] += c;
  }
  for (int i = 0; i < 14; i++)
    if (ch[0][i]) q[r++] = ch[0][i];
  while (l < r) {
    int u = q[l++];
    val[u] += val[fa[u]];
    for (int i = 0; i < 14; i++)
      if (ch[u][i]) {
        fa[ch[u][i]] = ch[fa[u]][i];
        q[r++] = ch[u][i];
      } else
        ch[u][i] = ch[fa[u]][i];
  }
  cin >> s + 1;
  int n = strlen(s + 1);
  int cnt = 0, last = 1;
  int tot = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == '?') {
      cnt++;
      tot++;
      for (int j = 0; j <= ct; j++) {
        int p = j;
        for (int k = last; k < i; k++) {
          p = ch[p][s[k] - 'a'];
          sum[cnt][j] += val[p];
        }
        tran[cnt][j] = p;
      }
      last = i + 1;
    }
  if (last <= n) {
    cnt++;
    for (int j = 0; j <= ct; j++) {
      int p = j;
      for (int k = last; k <= n; k++) {
        p = ch[p][s[k] - 'a'];
        sum[cnt][j] += val[p];
      }
      tran[cnt][j] = p;
    }
  }
  memset(dp, 0xcf, sizeof(dp));
  int cur = 0, pre = 1;
  last = 1;
  cnt = 0;
  dp[cur][0][0] = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == '?') {
      cnt++;
      cur ^= 1, pre = cur ^ 1;
      memset(dp[cur], 0xcf, sizeof(dp[cur]));
      for (int j = 0; j <= ct; j++) {
        for (int k = 0; k < (1 << 14); k++)
          if (dp[pre][j][k] != 0xcfcfcfcfcfcfcfcf) {
            dp[cur][tran[cnt][j]][k] =
                max(dp[cur][tran[cnt][j]][k], dp[pre][j][k] + sum[cnt][j]);
          }
      }
      cur ^= 1, pre = cur ^ 1;
      memset(dp[cur], 0xcf, sizeof(dp[cur]));
      for (int j = 0; j <= ct; j++) {
        for (int k = 0; k < (1 << 14); k++)
          if (dp[pre][j][k] != 0xcfcfcfcfcfcfcfcf) {
            for (int t = 0; t < 14; t++)
              if (!(k >> t & 1)) {
                dp[cur][ch[j][t]][k | (1 << t)] =
                    max(dp[cur][ch[j][t]][k | (1 << t)],
                        dp[pre][j][k] + val[ch[j][t]]);
              }
          }
      }
      last = i + 1;
    }
  if (last <= n) {
    cur ^= 1, pre = cur ^ 1;
    memset(dp[cur], 0xcf, sizeof(dp[cur]));
    cnt++;
    for (int j = 0; j <= ct; j++) {
      for (int k = 0; k < (1 << 14); k++)
        if (dp[pre][j][k] != 0xcfcfcfcfcfcfcfcf) {
          dp[cur][tran[cnt][j]][k] =
              max(dp[cur][tran[cnt][j]][k], dp[pre][j][k] + sum[cnt][j]);
        }
    }
  }
  long long ans = 0xcfcfcfcfcfcfcfcf;
  for (int j = 0; j <= ct; j++) {
    for (int k = 0; k < (1 << 14); k++) {
      ans = max(ans, dp[cur][j][k]);
    }
  }
  cout << ans;
}
