#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
const int kind = 14;
struct AC {
  int son[1010 + 5][kind], fail[1010 + 5], v[1010 + 5];
  int q[1010 + 5];
  int tot;
  int newnode() {
    tot++;
    memset(son[tot], 0, sizeof son[tot]);
    fail[tot] = 0;
    v[tot] = 0;
    return tot;
  }
  void init() {
    tot = 0;
    newnode();
  }
  void insert(char *s, int val) {
    int p = 1, l = strlen(s);
    for (int i = 0; i <= l - 1; i++) {
      int c = s[i] - 'a';
      if (!son[p][c]) son[p][c] = newnode();
      p = son[p][c];
    }
    v[p] += val;
  }
  void build() {
    int rear = -1;
    q[++rear] = 1;
    for (int front = 0; front <= rear; front++) {
      int now = q[front];
      for (int i = 0; i <= kind - 1; i++) {
        int v = son[now][i];
        if (v) {
          int p = fail[now];
          while (p && !son[p][i]) p = fail[p];
          fail[v] = p ? son[p][i] : 1;
          q[++rear] = v;
        }
      }
    }
    for (int front = 0; front <= rear; front++) {
      int now = q[front];
      v[now] += v[fail[now]];
    }
    for (int front = 0; front <= rear; front++) {
      int now = q[front];
      for (int i = 0; i <= kind - 1; i++)
        if (son[now][i] == 0) {
          if (son[fail[now]][i])
            son[now][i] = son[fail[now]][i];
          else
            son[now][i] = 1;
        }
    }
  }
  pair<int, long long> go(int now, char *s, int l) {
    long long res = 0;
    for (int i = 1; i <= l; i++) {
      int c = s[i] - 'a';
      now = son[now][c];
      res += v[now];
    }
    return make_pair(now, res);
  }
} ac;
char s[400000 + 5];
pair<int, long long> tran[1010 + 5][1010 + 5];
long long dp[1 << 14][1010 + 5];
int main() {
  ac.init();
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    int val;
    scanf("%d", &val);
    ac.insert(s, val);
  }
  ac.build();
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int N = -1, prev = 0;
  s[n + 1] = '?';
  for (int i = 1; i <= n + 1; i++)
    if (s[i] == '?') {
      N++;
      for (int j = 1; j <= ac.tot; j++)
        tran[N][j] = ac.go(j, s + prev, i - prev - 1);
      prev = i;
    }
  memset(dp, 0x80, sizeof dp);
  dp[0][tran[0][1].first] = tran[0][1].second;
  int B = 1 << 14;
  for (int msk = 1; msk <= B - 1; msk++) {
    int cnt = __builtin_popcount(msk);
    if (cnt > N) continue;
    for (int c = 0; c <= 13; c++)
      if (msk & (1 << c)) {
        int omsk = msk ^ (1 << c);
        for (int j = 1; j <= ac.tot; j++)
          if (dp[omsk][j] > -1e18) {
            long long tmp = dp[omsk][j];
            int now = ac.son[j][c];
            tmp += ac.v[now] + tran[cnt][now].second;
            now = tran[cnt][now].first;
            chmax(dp[msk][now], tmp);
          }
      }
  }
  long long ans = -1e18;
  for (int msk = 0; msk <= B - 1; msk++)
    if (__builtin_popcount(msk) == N)
      for (int j = 1; j <= ac.tot; j++) chmax(ans, dp[msk][j]);
  printf("%lld\n", ans);
  return 0;
}
