#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 10000000000000000LL;
void relax(long long &a, long long b) { a = max(a, b); }
struct {
  int to[26];
  int up;
  int sum;
  int nxt[26];
  vector<int> child;
} T[1005];
int tsz = 1;
void dfs(int v) {
  for (int u : T[v].child) {
    T[u].sum += T[v].sum;
    dfs(u);
  }
}
long long dp[2][1005][1 << 14];
const int MX = 400005;
char input[MX];
int main() {
  int k;
  ignore = scanf("%d", &k);
  for (int rep = 0; rep < k; rep++) {
    static char s[1005];
    ignore = scanf(" %s", s);
    int n = strlen(s);
    int cur = 0;
    for (int i = 0; i < n; i++) {
      if (T[cur].to[s[i] - 'a'] == 0) {
        T[cur].to[s[i] - 'a'] = tsz++;
      }
      cur = T[cur].to[s[i] - 'a'];
    }
    int c;
    ignore = scanf("%d", &c);
    T[cur].sum += c;
  }
  {
    static int Q[1005];
    int L = 0, R = 0;
    for (int i = 0; i < 26; i++) {
      if (T[0].to[i]) Q[R++] = T[0].to[i];
    }
    while (L < R) {
      int cur = Q[L++];
      for (int i = 0; i < 26; i++) {
        int v = T[cur].to[i];
        if (!v) continue;
        int p = T[cur].up;
        while (p && !T[p].to[i]) p = T[p].up;
        if (T[p].to[i]) p = T[p].to[i];
        T[v].up = p;
        Q[R++] = v;
      }
    }
    for (int v = 0; v < tsz; v++) {
      for (int i = 0; i < 26; i++) {
        int x = v;
        while (x && !T[x].to[i]) x = T[x].up;
        if (T[x].to[i]) x = T[x].to[i];
        T[v].nxt[i] = x;
      }
    }
    for (int v = 1; v < tsz; v++) {
      T[T[v].up].child.push_back(v);
    }
  }
  dfs(0);
  fill_n(&dp[0][0][0], 1005 * (1 << 14), -INF);
  dp[0][0][0] = 0;
  ignore = scanf(" %s", input);
  int len = strlen(input);
  int taken = 0;
  int pp = 0;
  for (int i = 0; i < len; pp ^= 1) {
    fill_n(&dp[pp ^ 1][0][0], 1005 * (1 << 14), -INF);
    if (input[i] == '?') {
      for (int msk = 0; msk < (1 << 14); msk++) {
        if (__builtin_popcount(msk) != taken) continue;
        for (int t = 0; t < 14; t++) {
          if (msk >> t & 1) continue;
          for (int v = 0; v < tsz; v++) {
            int x = T[v].nxt[t];
            relax(dp[pp ^ 1][x][msk | (1 << t)], dp[pp][v][msk] + T[x].sum);
          }
        }
      }
      taken++;
      i++;
    } else {
      int j = i;
      while (j < len && input[j] != '?') j++;
      for (int v = 0; v < tsz; v++) {
        int x = v;
        long long s = 0;
        for (int t = i; t < j; t++) {
          x = T[x].nxt[input[t] - 'a'];
          s += T[x].sum;
        }
        for (int msk = 0; msk < (1 << 14); msk++) {
          relax(dp[pp ^ 1][x][msk], dp[pp][v][msk] + s);
        }
      }
      i = j;
    }
  }
  long long ans = -INF;
  for (int msk = 0; msk < (1 << 14); msk++) {
    for (int v = 0; v < tsz; v++) {
      ans = max(ans, dp[pp][v][msk]);
    }
  }
  printf("%lld\n", ans);
}
