#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int oo = 1e9;
const int MOD = 998244353;
const static int alpha = 26;
int trie[1010][alpha], fail[1010], nodes;
long long cost_word[1010];
void add(string &s, int cost) {
  int cur = 0;
  for (char c : s) {
    int x = c - 'a';
    if (!trie[cur][x]) trie[cur][x] = ++nodes;
    cur = trie[cur][x];
  }
  cost_word[cur] += cost;
}
void build() {
  queue<int> q;
  q.push(0);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < alpha; ++i) {
      int v = trie[u][i];
      if (!v)
        trie[u][i] = trie[fail[u]][i];
      else
        q.push(v);
      if (!u || !v) continue;
      fail[v] = trie[fail[u]][i];
      cost_word[v] += cost_word[fail[v]];
    }
  }
}
int n;
string s;
map<long long, pair<long long, long long> > memo[1010][alpha];
pair<long long, long long> advance(int at, char c, int node) {
  int pat = at;
  if (memo[node][c - 'a'].count(pat)) {
    return memo[node][c - 'a'][pat];
  }
  pair<long long, long long> ans = {node, 0};
  if (at < n && s[at] == '?') {
    ans.first = trie[ans.first][c - 'a'];
    ans.second += cost_word[ans.first];
    at++;
  }
  while (at < n && s[at] != '?') {
    ans.first = trie[ans.first][s[at] - 'a'];
    ans.second += cost_word[ans.first];
    at++;
  }
  return memo[node][c - 'a'][pat] = ans;
}
vector<int> pos;
long long dp[1 << 14][1010];
long long go(int mask, int node, int i) {
  if (i == pos.size()) return 0;
  long long &r = dp[mask][node];
  if (r == -1e18) {
    for (char c = 'a'; c <= 'n'; c++) {
      if (mask >> (c - 'a') & 1) continue;
      pair<long long, long long> got = advance(pos[i], c, node);
      r = max(r, go(mask | (1 << (c - 'a')), got.first, i + 1) + got.second);
    }
  }
  return r;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    string s;
    int c;
    cin >> s >> c;
    add(s, c);
  }
  build();
  for (int i = 0; i < (1 << 14); i++) {
    for (int j = 0; j < 1010; j++) {
      dp[i][j] = -1e18;
    }
  }
  cin >> s;
  n = s.size();
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '?') pos.push_back(i);
  }
  long long ans = -1e18;
  if (s[0] == '?') {
    ans = go(0, 0, 0);
  } else {
    pair<long long, long long> got = advance(0, s[0], 0);
    ans = go(0, got.first, 0) + got.second;
  }
  cout << ans << '\n';
}
