#include <bits/stdc++.h>
using namespace std;
const int maxn = 400005;
const int maxm = 1005;
const long long inf = 1e18;
int n, m;
long long f[maxm][1 << 14], ans = -inf;
char s[maxn];
vector<int> vec;
struct Trie {
  int fail, ch[14];
  long long num;
} trie[maxm];
int cnt;
int bitcnt(int x) {
  int ret = 0;
  while (x) ret++, x -= ((x) & (-x));
  return ret;
}
void build(char *s, long long c) {
  int len = strlen(s + 1), u = 0;
  for (int i = 1; i <= len; i++) {
    if (!trie[u].ch[s[i] - 'a']) trie[u].ch[s[i] - 'a'] = ++cnt;
    u = trie[u].ch[s[i] - 'a'];
  }
  trie[u].num += c;
}
void get_fail() {
  queue<int> que;
  for (int i = 0; i < 14; i++)
    if (trie[0].ch[i]) {
      trie[trie[0].ch[i]].fail = 0;
      que.push(trie[0].ch[i]);
    }
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    trie[u].num += trie[trie[u].fail].num;
    for (int i = 0; i < 14; i++) {
      if (trie[u].ch[i]) {
        trie[trie[u].ch[i]].fail = trie[trie[u].fail].ch[i];
        que.push(trie[u].ch[i]);
      } else
        trie[u].ch[i] = trie[trie[u].fail].ch[i];
    }
  }
}
int main() {
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    long long c;
    scanf("%s%lld", s + 1, &c);
    build(s, c);
  }
  get_fail();
  scanf("%s", s + 1);
  n = strlen(s + 1);
  vec.push_back(0);
  for (int i = 1; i <= n; i++)
    if (s[i] == '?') vec.push_back(i);
  for (int u = 0; u <= cnt; u++)
    for (int S = 0; S <= (1 << 14) - 1; S++) f[u][S] = -inf;
  f[0][0] = 0;
  for (int i = 0; i < vec.size() - 1; i++) {
    for (int u = 0; u <= cnt; u++) {
      int v = u;
      long long sum = 0;
      for (int j = vec[i] + 1; j <= vec[i + 1] - 1; j++) {
        v = trie[v].ch[s[j] - 'a'];
        sum += trie[v].num;
      }
      for (int S = 0; S <= (1 << 14) - 1; S++) {
        if (bitcnt(S) != i || f[u][S] == -inf) continue;
        for (int c = 0; c <= 13; c++) {
          if (S >> c & 1) continue;
          int now = trie[v].ch[c];
          f[now][S | (1 << c)] =
              max(f[now][S | (1 << c)], f[u][S] + sum + trie[now].num);
        }
      }
    }
  }
  for (int u = 0; u <= cnt; u++) {
    int v = u;
    long long sum = 0;
    for (int j = vec[vec.size() - 1] + 1; j <= n; j++) {
      v = trie[v].ch[s[j] - 'a'];
      sum += trie[v].num;
    }
    for (int S = 0; S <= (1 << 14) - 1; S++) {
      if (bitcnt(S) != vec.size() - 1) continue;
      ans = max(ans, f[u][S] + sum);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
