#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > v(14 + 1);
int go[1001][14], fail[1001], weight[1001], nxt, maxLength;
pair<int, long long> a[1001];
long long dp[1001][(1 << 14)];
const long long negativeInfinity = LLONG_MIN / 2;
void AddStringToAhoCorasick(string s, int val) {
  int i = 0;
  for (auto c : s) {
    int k = c - 'a';
    if (go[i][k] == 0) {
      go[i][k] = ++nxt;
    }
    i = go[i][k];
  }
  maxLength = max(maxLength, (int)s.size());
  weight[i] += val;
}
void BuildAhoCorasick() {
  queue<int> Q;
  for (int x = 0; x < 14; ++x) {
    if (go[0][x]) {
      Q.push(go[0][x]);
    }
  }
  while (!Q.empty()) {
    const int i = Q.front();
    Q.pop();
    weight[i] += weight[fail[i]];
    for (int x = 0; x < 14; ++x) {
      const int j = go[i][x];
      if (j) {
        fail[j] = go[fail[i]][x];
        Q.push(j);
      } else {
        go[i][x] = go[fail[i]][x];
      }
    }
  }
}
void ResetTransitions() {
  for (int i = 0; i <= nxt; ++i) {
    a[i].first = -1;
  }
}
inline pair<int, long long> GetTransitionedNode(int i, const string &s) {
  int j = i;
  long long val = 0;
  for (auto c : s) {
    j = go[j][c - 'a'];
    val += weight[j];
  }
  a[i] = make_pair(j, val);
  return a[i];
}
void ApplyStringTransitions(const string &s, const int n, const int qCount) {
  for (int i = 0; i <= n; ++i) {
    int j;
    long long val;
    tie(j, val) = GetTransitionedNode(i, s);
    for (int mask : v[qCount]) {
      if (dp[i][mask] < negativeInfinity) {
        continue;
      }
      val += dp[i][mask];
      for (int x = 0; x < 14; ++x) {
        if ((mask & (1 << x)) > 0) {
          continue;
        }
        const int k = go[j][x], nmask = mask | (1 << x);
        dp[k][nmask] = max(dp[k][nmask], val + weight[k]);
      }
      val -= dp[i][mask];
    }
  }
}
int Initialise() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    int val;
    cin >> s >> val;
    AddStringToAhoCorasick(s, val);
  }
  BuildAhoCorasick();
  for (int mask = 0; mask < (1 << 14); ++mask) {
    v[__builtin_popcount(mask)].push_back(mask);
  }
  return nxt;
}
int main() {
  int n = Initialise();
  string t;
  cin >> t;
  int cur = 0;
  for (int i = 0; i <= n; ++i) {
    for (int mask = 0; mask < (1 << 14); ++mask) {
      dp[i][mask] = LLONG_MIN;
    }
  }
  dp[0][0] = 0;
  string s;
  int qCount = 0;
  for (auto c : t) {
    if (c != '?') {
      s.push_back(c);
      continue;
    }
    ApplyStringTransitions(s, n, qCount);
    s.clear();
    ++qCount;
  }
  long long ans = LLONG_MIN;
  for (int i = 0; i <= n; ++i) {
    int j;
    long long val;
    tie(j, val) = GetTransitionedNode(i, s);
    for (int mask : v[qCount]) {
      if (dp[i][mask] < negativeInfinity) {
        continue;
      }
      ans = max(ans, val + dp[i][mask]);
    }
  }
  cout << ans << '\n';
  return 0;
}
