#include <bits/stdc++.h>
const long long inf = 1e18;
const long long big = 1e17;
char s[400005];
int p[1005], n, k, link[1005], ch[1005][14], tl, cnt;
long long mark[1005], f[1 << 14][1005], g[1 << 14][1005], w[1005], ans = -inf;
void transit(int l, int r) {
  if (l > r) return;
  for (int i = 0; i <= tl; ++i) {
    p[i] = i;
    w[i] = 0;
  }
  for (int i = l; i <= r; ++i)
    for (int j = 0; j <= tl; ++j) {
      w[j] += mark[ch[p[j]][s[i] - 'a']];
      p[j] = ch[p[j]][s[i] - 'a'];
    }
  std::memcpy(g, f, sizeof(f));
  for (int mask = 0; mask < (1 << 14); ++mask)
    for (int i = 0; i <= tl; ++i) f[mask][i] = -inf;
  for (int mask = 0; mask < (1 << 14); ++mask)
    for (int i = 0; i <= tl; ++i)
      f[mask][p[i]] = std::max(f[mask][p[i]], g[mask][i] + w[i]);
}
void insert(int len, int w) {
  int rt = 0;
  for (int i = 1; i <= len; ++i) {
    if (!ch[rt][s[i] - 'a']) ch[rt][s[i] - 'a'] = ++tl;
    rt = ch[rt][s[i] - 'a'];
  }
  mark[rt] += w;
}
void build() {
  std::queue<int> q;
  std::memset(link, -1, sizeof(link));
  link[0] = 0;
  for (int i = 0; i < 14; ++i)
    if (ch[0][i]) {
      q.push(ch[0][i]);
      link[ch[0][i]] = 0;
    }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    mark[u] += mark[link[u]];
    for (int i = 0; i < 14; ++i) {
      if (ch[u][i]) {
        link[ch[u][i]] = ch[link[u]][i];
        q.push(ch[u][i]);
      } else
        ch[u][i] = ch[link[u]][i];
    }
  }
}
int main() {
  scanf("%d", &k);
  for (int i = 1; i <= k; ++i) {
    scanf("%s", s + 1);
    int len = std::strlen(s + 1);
    int w;
    scanf("%d", &w);
    insert(len, w);
  }
  build();
  scanf("%s", s + 1);
  n = std::strlen(s + 1);
  std::vector<int> v;
  for (int i = 1; i <= n; ++i)
    if (s[i] == '?') {
      v.push_back(i);
      cnt++;
    }
  v.push_back(n + 1);
  for (int mask = 0; mask < (1 << 14); ++mask)
    for (int i = 0; i <= tl; ++i) f[mask][i] = -inf;
  f[0][0] = 0;
  int last = 0;
  for (int x : v) {
    transit(last + 1, x - 1);
    last = x;
    if (x > n) break;
    std::memcpy(g, f, sizeof(g));
    for (int mask = 0; mask < (1 << 14); ++mask)
      for (int i = 0; i <= tl; ++i) f[mask][i] = -inf;
    for (int mask = (1 << 14) - 1; mask >= 0; --mask) {
      for (int k = 0; k <= tl; ++k) {
        if (g[mask][k] < -big) continue;
        for (int j = 0; j < 14; ++j) {
          if (mask & (1 << j)) continue;
          f[mask + (1 << j)][ch[k][j]] = std::max(f[mask + (1 << j)][ch[k][j]],
                                                  g[mask][k] + mark[ch[k][j]]);
        }
      }
    }
  }
  for (int mask = 0; mask < (1 << 14); ++mask)
    for (int k = 0; k <= tl; ++k) ans = std::max(ans, f[mask][k]);
  printf("%lld", ans);
  return 0;
}
