#include <bits/stdc++.h>
long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
using namespace std;
class pa3 {
 public:
  long long x;
  long long y, z;
  pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {}
  bool operator<(const pa3& p) const {
    if (x != p.x) return x < p.x;
    if (y != p.y) return y < p.y;
    return z < p.z;
  }
  bool operator>(const pa3& p) const {
    if (x != p.x) return x > p.x;
    if (y != p.y) return y > p.y;
    return z > p.z;
  }
  bool operator==(const pa3& p) const {
    return x == p.x && y == p.y && z == p.z;
  }
  bool operator!=(const pa3& p) const {
    return !(x == p.x && y == p.y && z == p.z);
  }
};
class pa4 {
 public:
  long long x;
  long long y, z, w;
  pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0)
      : x(x), y(y), z(z), w(w) {}
  bool operator<(const pa4& p) const {
    if (x != p.x) return x < p.x;
    if (y != p.y) return y < p.y;
    if (z != p.z) return z < p.z;
    return w < p.w;
  }
  bool operator>(const pa4& p) const {
    if (x != p.x) return x > p.x;
    if (y != p.y) return y > p.y;
    if (z != p.z) return z > p.z;
    return w > p.w;
  }
  bool operator==(const pa4& p) const {
    return x == p.x && y == p.y && z == p.z && w == p.w;
  }
};
class pa2 {
 public:
  long long x, y;
  pa2(long long x = 0, long long y = 0) : x(x), y(y) {}
  pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); }
  pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); }
  bool operator<(const pa2& p) const { return y != p.y ? y < p.y : x < p.x; }
  bool operator>(const pa2& p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const pa2& p) const {
    return abs(x - p.x) == 0 && abs(y - p.y) == 0;
  }
  bool operator!=(const pa2& p) const {
    return !(abs(x - p.x) == 0 && abs(y - p.y) == 0);
  }
};
string itos(long long i) {
  ostringstream s;
  s << i;
  return s.str();
}
long long Gcd(long long v, long long b) {
  if (v == 0) return b;
  if (b == 0) return v;
  if (v > b) return Gcd(b, v);
  if (v == b) return b;
  if (b % v == 0) return v;
  return Gcd(v, b % v);
}
long long mod;
long long extgcd(long long a, long long b, long long& x, long long& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
pair<long long, long long> operator+(const pair<long long, long long>& l,
                                     const pair<long long, long long>& r) {
  return {l.first + r.first, l.second + r.second};
}
pair<long long, long long> operator-(const pair<long long, long long>& l,
                                     const pair<long long, long long>& r) {
  return {l.first - r.first, l.second - r.second};
}
long long beki(long long wa, long long rr, long long warukazu) {
  if (rr == 0) return 1 % warukazu;
  if (rr == 1) return wa % warukazu;
  wa %= warukazu;
  if (rr % 2 == 1)
    return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu;
  long long zx = beki(wa, rr / 2, warukazu);
  return (zx * zx) % warukazu;
}
long long pr[1000100];
long long inv[1000110];
long long comb(long long nn, long long rr) {
  if (rr < 0 || rr > nn || nn < 0) return 0;
  long long r = pr[nn] * inv[rr];
  r %= mod;
  r *= inv[nn - rr];
  r %= mod;
  return r;
}
void gya(long long ert) {
  pr[0] = 1;
  for (long long i = 1; i <= ert; i++) {
    pr[i] = ((long long)pr[i - 1] * i) % mod;
  }
  inv[ert] = beki(pr[ert], mod - 2, mod);
  for (long long i = ert - 1; i >= 0; i--) {
    inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
  }
}
long long cost[1020];
long long tugi[2000][15];
vector<long long> G[2020];
struct aho {
  typedef struct trie {
    long long moji;
    long long id;
    long long depth;
    long long P;
    trie* parent;
    trie* flink;
    trie* outer;
    trie* child[14];
  } trie;

 private:
 public:
  long long cd = 0;
  trie* new_trie() {
    trie* p = new trie;
    p->P = -1;
    p->id = cd;
    cd++;
    p->flink = NULL;
    p->parent = NULL;
    p->outer = NULL;
    for (long long i = 0; i < 14; i++) p->child[i] = NULL;
    return p;
  }
  trie* ROOT;
  aho() {
    ROOT = new_trie();
    ROOT->moji = -1;
    ROOT->depth = 0;
  }
  void add_string(string str, long long str_id, long long hoge) {
    trie* trie_now = ROOT;
    long long l = str.length();
    for (long long i = 0; i < l; i++) {
      long long tugi = str[i] - 'a';
      if (trie_now->child[tugi] == NULL) {
        trie* p_new = new_trie();
        p_new->moji = tugi;
        p_new->parent = trie_now;
        p_new->depth = i + 1;
        trie_now->child[tugi] = p_new;
      }
      trie_now = trie_now->child[tugi];
    }
    cost[trie_now->id] = hoge;
    trie_now->P = str_id;
  }
  void debug() { debug_dfs(ROOT); }
  void debug_dfs(trie* e) {
    cout << "c ";
    for (long long i = 0; i < 14; i++)
      if (e->child[i] != NULL) cout << i << " ";
    cout << endl;
    for (long long i = 0; i < 14; i++)
      if (e->child[i] != NULL) debug_dfs(e->child[i]);
  }
  queue<trie*> queue_trie;
  void shori() {
    ROOT->parent = ROOT;
    ROOT->flink = ROOT;
    queue_trie.push(ROOT);
    while (queue_trie.size()) {
      trie* trie_now = queue_trie.front();
      queue_trie.pop();
      for (long long i = 0; i < 14; i++)
        if (trie_now->child[i] != NULL) queue_trie.push(trie_now->child[i]);
      if (trie_now == ROOT) continue;
      if (trie_now->parent == ROOT) {
        trie_now->flink = ROOT;
        continue;
      }
      trie* itr = (trie_now->parent)->flink;
      while (1) {
        if (itr == ROOT) {
          if (ROOT->child[trie_now->moji] == NULL) {
            trie_now->flink = ROOT;
            break;
          } else {
            trie_now->flink = ROOT->child[trie_now->moji];
            break;
          }
        } else {
          if (itr->child[trie_now->moji] != NULL) {
            trie_now->flink = itr->child[trie_now->moji];
            break;
          } else
            itr = itr->flink;
        }
      }
    }
    while (queue_trie.size()) queue_trie.pop();
    queue_trie.push(ROOT);
    while (queue_trie.size()) {
      trie* trie_now = queue_trie.front();
      queue_trie.pop();
      for (long long i = 0; i < 14; i++)
        if (trie_now->child[i] != NULL) queue_trie.push(trie_now->child[i]);
      if (trie_now == ROOT) {
        trie_now->outer = ROOT;
      } else if (trie_now->flink->P < 0)
        trie_now->outer = trie_now->flink->outer;
      else {
        trie_now->outer = trie_now->flink;
      }
      if (trie_now != ROOT) G[trie_now->outer->id].push_back(trie_now->id);
      for (long long i = 0; i < 14; i++) {
        if (trie_now->child[i] != NULL)
          tugi[trie_now->id][i] = trie_now->child[i]->id;
        else {
          trie* hoge = trie_now;
          while (hoge->child[i] == NULL) {
            hoge = hoge->flink;
            if (hoge == ROOT) break;
          }
          if (hoge->child[i] != NULL) {
            hoge = hoge->child[i];
          }
          tugi[trie_now->id][i] = hoge->id;
        }
      }
    }
  }
  void string_find(string str) {
    trie* trie_now = ROOT;
    long long l = str.length();
    for (long long i = 0; i < l; i++) {
      long long moji = str[i] - 'a';
      while (trie_now->child[moji] == NULL) {
        trie_now = trie_now->flink;
        if (trie_now == ROOT) break;
      }
      if (trie_now->child[moji] != NULL) {
        trie_now = trie_now->child[moji];
      }
      trie* tr = trie_now;
      while (tr != NULL) {
        tr = tr->outer;
      }
    }
  }
  long long YARU(long long id, long long iti) { return 0; }
};
void dfs_G(long long r) {
  for (auto v : G[r]) {
    cost[v] += cost[r];
    dfs_G(v);
  }
}
long long dp[1030][1 << 14];
long long memo[1020];
pair<long long, long long> yomi[1030][20];
vector<string> ve;
void moto(long long r, long long d) {
  long long f = 0;
  long long rr = r;
  for (auto c : ve[d]) {
    r = tugi[r][c - 'a'];
    f += cost[r];
  }
  yomi[rr][d] = make_pair(r, f);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  aho A;
  long long n;
  cin >> n;
  map<string, long long> ma;
  for (long long i = 0; i < n; i++) {
    string ss;
    long long f;
    cin >> ss >> f;
    ma[ss] += f;
  }
  long long gf = 1;
  for (auto i : ma) {
    if (i.second != 0) {
      A.add_string(i.first, gf, i.second);
      gf++;
    }
  }
  A.shori();
  long long V = A.cd;
  dfs_G(0);
  string s;
  cin >> s;
  string t = "";
  for (auto c : s) {
    if (c == '?') {
      ve.push_back(t);
      t = "";
    } else
      t += c;
  }
  ve.push_back(t);
  long long d = (long long)ve.size() - 1;
  long long inff = 1000000007 * 1000000000ll;
  for (long long i = 0; i < V; i++) {
    for (long long j = 0; j < (1 << 14); j++) dp[i][j] = -inff;
    for (long long j = 0; j < 20; j++) yomi[i][j] = make_pair(-1, 0);
  }
  dp[0][0] = 0;
  long long ans = -inff;
  for (long long i = 0; i < V; i++) memo[i] = -inff;
  for (long long r = 0; r < (1 << 14); r++) {
    long long g = __builtin_popcount(r);
    if (g > d) continue;
    for (long long i = 0; i < V; i++)
      if (dp[i][r] != -inff) {
        if (yomi[i][g] == (pair<long long, long long>)make_pair(-1, 0))
          moto(i, g);
        memo[yomi[i][g].first] =
            max(memo[yomi[i][g].first], dp[i][r] + yomi[i][g].second);
      }
    if (g < d) {
      for (long long i = 0; i < V; i++)
        if (memo[i] != -inff) {
          for (long long j = 0; j < 14; j++)
            if (!(r & (1 << j))) {
              dp[tugi[i][j]][r ^ (1 << j)] =
                  max(dp[tugi[i][j]][r ^ (1 << j)], memo[i] + cost[tugi[i][j]]);
            }
          memo[i] = -inff;
        }
    } else {
      for (long long i = 0; i < V; i++) {
        ans = max(ans, memo[i]);
        memo[i] = -inff;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Your challenge is to write a CPP solution to the following problem:

You are given a string S and an array of strings [t_1, t_2, ..., t_k]. Each string t_i consists of lowercase Latin letters from a to n; S consists of lowercase Latin letters from a to n and no more than 14 question marks.

Each string t_i has its cost c_i — an integer number. The value of some string T is calculated as ∑_{i = 1}^{k} F(T, t_i) ⋅ c_i, where F(T, t_i) is the number of occurences of string t_i in T as a substring. For example, F(aaabaaa, aa) = 4.

You have to replace all question marks in S with pairwise distinct lowercase Latin letters from a to n so the value of S is maximum possible.

Input

The first line contains one integer k (1 ≤ k ≤ 1000) — the number of strings in the array [t_1, t_2, ..., t_k].

Then k lines follow, each containing one string t_i (consisting of lowercase Latin letters from a to n) and one integer c_i (1 ≤ |t_i| ≤ 1000, -10^6 ≤ c_i ≤ 10^6). The sum of lengths of all strings t_i does not exceed 1000.

The last line contains one string S (1 ≤ |S| ≤ 4 ⋅ 10^5) consisting of lowercase Latin letters from a to n and question marks. The number of question marks in S is not greater than 14.

Output

Print one integer — the maximum value of S after replacing all question marks with pairwise distinct lowercase Latin letters from a to n.

Examples

Input


4
abc -10
a 1
b 1
c 3
?b?


Output


5


Input


2
a 1
a 1
?


Output


2


Input


3
a 1
b 3
ab 4
ab


Output


8


Input


1
a -1
?????????????


Output


0


Input


1
a -1
??????????????


Output


-1

### Response

```cpp
#include <bits/stdc++.h>
long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
using namespace std;
class pa3 {
 public:
  long long x;
  long long y, z;
  pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {}
  bool operator<(const pa3& p) const {
    if (x != p.x) return x < p.x;
    if (y != p.y) return y < p.y;
    return z < p.z;
  }
  bool operator>(const pa3& p) const {
    if (x != p.x) return x > p.x;
    if (y != p.y) return y > p.y;
    return z > p.z;
  }
  bool operator==(const pa3& p) const {
    return x == p.x && y == p.y && z == p.z;
  }
  bool operator!=(const pa3& p) const {
    return !(x == p.x && y == p.y && z == p.z);
  }
};
class pa4 {
 public:
  long long x;
  long long y, z, w;
  pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0)
      : x(x), y(y), z(z), w(w) {}
  bool operator<(const pa4& p) const {
    if (x != p.x) return x < p.x;
    if (y != p.y) return y < p.y;
    if (z != p.z) return z < p.z;
    return w < p.w;
  }
  bool operator>(const pa4& p) const {
    if (x != p.x) return x > p.x;
    if (y != p.y) return y > p.y;
    if (z != p.z) return z > p.z;
    return w > p.w;
  }
  bool operator==(const pa4& p) const {
    return x == p.x && y == p.y && z == p.z && w == p.w;
  }
};
class pa2 {
 public:
  long long x, y;
  pa2(long long x = 0, long long y = 0) : x(x), y(y) {}
  pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); }
  pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); }
  bool operator<(const pa2& p) const { return y != p.y ? y < p.y : x < p.x; }
  bool operator>(const pa2& p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const pa2& p) const {
    return abs(x - p.x) == 0 && abs(y - p.y) == 0;
  }
  bool operator!=(const pa2& p) const {
    return !(abs(x - p.x) == 0 && abs(y - p.y) == 0);
  }
};
string itos(long long i) {
  ostringstream s;
  s << i;
  return s.str();
}
long long Gcd(long long v, long long b) {
  if (v == 0) return b;
  if (b == 0) return v;
  if (v > b) return Gcd(b, v);
  if (v == b) return b;
  if (b % v == 0) return v;
  return Gcd(v, b % v);
}
long long mod;
long long extgcd(long long a, long long b, long long& x, long long& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
pair<long long, long long> operator+(const pair<long long, long long>& l,
                                     const pair<long long, long long>& r) {
  return {l.first + r.first, l.second + r.second};
}
pair<long long, long long> operator-(const pair<long long, long long>& l,
                                     const pair<long long, long long>& r) {
  return {l.first - r.first, l.second - r.second};
}
long long beki(long long wa, long long rr, long long warukazu) {
  if (rr == 0) return 1 % warukazu;
  if (rr == 1) return wa % warukazu;
  wa %= warukazu;
  if (rr % 2 == 1)
    return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu;
  long long zx = beki(wa, rr / 2, warukazu);
  return (zx * zx) % warukazu;
}
long long pr[1000100];
long long inv[1000110];
long long comb(long long nn, long long rr) {
  if (rr < 0 || rr > nn || nn < 0) return 0;
  long long r = pr[nn] * inv[rr];
  r %= mod;
  r *= inv[nn - rr];
  r %= mod;
  return r;
}
void gya(long long ert) {
  pr[0] = 1;
  for (long long i = 1; i <= ert; i++) {
    pr[i] = ((long long)pr[i - 1] * i) % mod;
  }
  inv[ert] = beki(pr[ert], mod - 2, mod);
  for (long long i = ert - 1; i >= 0; i--) {
    inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
  }
}
long long cost[1020];
long long tugi[2000][15];
vector<long long> G[2020];
struct aho {
  typedef struct trie {
    long long moji;
    long long id;
    long long depth;
    long long P;
    trie* parent;
    trie* flink;
    trie* outer;
    trie* child[14];
  } trie;

 private:
 public:
  long long cd = 0;
  trie* new_trie() {
    trie* p = new trie;
    p->P = -1;
    p->id = cd;
    cd++;
    p->flink = NULL;
    p->parent = NULL;
    p->outer = NULL;
    for (long long i = 0; i < 14; i++) p->child[i] = NULL;
    return p;
  }
  trie* ROOT;
  aho() {
    ROOT = new_trie();
    ROOT->moji = -1;
    ROOT->depth = 0;
  }
  void add_string(string str, long long str_id, long long hoge) {
    trie* trie_now = ROOT;
    long long l = str.length();
    for (long long i = 0; i < l; i++) {
      long long tugi = str[i] - 'a';
      if (trie_now->child[tugi] == NULL) {
        trie* p_new = new_trie();
        p_new->moji = tugi;
        p_new->parent = trie_now;
        p_new->depth = i + 1;
        trie_now->child[tugi] = p_new;
      }
      trie_now = trie_now->child[tugi];
    }
    cost[trie_now->id] = hoge;
    trie_now->P = str_id;
  }
  void debug() { debug_dfs(ROOT); }
  void debug_dfs(trie* e) {
    cout << "c ";
    for (long long i = 0; i < 14; i++)
      if (e->child[i] != NULL) cout << i << " ";
    cout << endl;
    for (long long i = 0; i < 14; i++)
      if (e->child[i] != NULL) debug_dfs(e->child[i]);
  }
  queue<trie*> queue_trie;
  void shori() {
    ROOT->parent = ROOT;
    ROOT->flink = ROOT;
    queue_trie.push(ROOT);
    while (queue_trie.size()) {
      trie* trie_now = queue_trie.front();
      queue_trie.pop();
      for (long long i = 0; i < 14; i++)
        if (trie_now->child[i] != NULL) queue_trie.push(trie_now->child[i]);
      if (trie_now == ROOT) continue;
      if (trie_now->parent == ROOT) {
        trie_now->flink = ROOT;
        continue;
      }
      trie* itr = (trie_now->parent)->flink;
      while (1) {
        if (itr == ROOT) {
          if (ROOT->child[trie_now->moji] == NULL) {
            trie_now->flink = ROOT;
            break;
          } else {
            trie_now->flink = ROOT->child[trie_now->moji];
            break;
          }
        } else {
          if (itr->child[trie_now->moji] != NULL) {
            trie_now->flink = itr->child[trie_now->moji];
            break;
          } else
            itr = itr->flink;
        }
      }
    }
    while (queue_trie.size()) queue_trie.pop();
    queue_trie.push(ROOT);
    while (queue_trie.size()) {
      trie* trie_now = queue_trie.front();
      queue_trie.pop();
      for (long long i = 0; i < 14; i++)
        if (trie_now->child[i] != NULL) queue_trie.push(trie_now->child[i]);
      if (trie_now == ROOT) {
        trie_now->outer = ROOT;
      } else if (trie_now->flink->P < 0)
        trie_now->outer = trie_now->flink->outer;
      else {
        trie_now->outer = trie_now->flink;
      }
      if (trie_now != ROOT) G[trie_now->outer->id].push_back(trie_now->id);
      for (long long i = 0; i < 14; i++) {
        if (trie_now->child[i] != NULL)
          tugi[trie_now->id][i] = trie_now->child[i]->id;
        else {
          trie* hoge = trie_now;
          while (hoge->child[i] == NULL) {
            hoge = hoge->flink;
            if (hoge == ROOT) break;
          }
          if (hoge->child[i] != NULL) {
            hoge = hoge->child[i];
          }
          tugi[trie_now->id][i] = hoge->id;
        }
      }
    }
  }
  void string_find(string str) {
    trie* trie_now = ROOT;
    long long l = str.length();
    for (long long i = 0; i < l; i++) {
      long long moji = str[i] - 'a';
      while (trie_now->child[moji] == NULL) {
        trie_now = trie_now->flink;
        if (trie_now == ROOT) break;
      }
      if (trie_now->child[moji] != NULL) {
        trie_now = trie_now->child[moji];
      }
      trie* tr = trie_now;
      while (tr != NULL) {
        tr = tr->outer;
      }
    }
  }
  long long YARU(long long id, long long iti) { return 0; }
};
void dfs_G(long long r) {
  for (auto v : G[r]) {
    cost[v] += cost[r];
    dfs_G(v);
  }
}
long long dp[1030][1 << 14];
long long memo[1020];
pair<long long, long long> yomi[1030][20];
vector<string> ve;
void moto(long long r, long long d) {
  long long f = 0;
  long long rr = r;
  for (auto c : ve[d]) {
    r = tugi[r][c - 'a'];
    f += cost[r];
  }
  yomi[rr][d] = make_pair(r, f);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  aho A;
  long long n;
  cin >> n;
  map<string, long long> ma;
  for (long long i = 0; i < n; i++) {
    string ss;
    long long f;
    cin >> ss >> f;
    ma[ss] += f;
  }
  long long gf = 1;
  for (auto i : ma) {
    if (i.second != 0) {
      A.add_string(i.first, gf, i.second);
      gf++;
    }
  }
  A.shori();
  long long V = A.cd;
  dfs_G(0);
  string s;
  cin >> s;
  string t = "";
  for (auto c : s) {
    if (c == '?') {
      ve.push_back(t);
      t = "";
    } else
      t += c;
  }
  ve.push_back(t);
  long long d = (long long)ve.size() - 1;
  long long inff = 1000000007 * 1000000000ll;
  for (long long i = 0; i < V; i++) {
    for (long long j = 0; j < (1 << 14); j++) dp[i][j] = -inff;
    for (long long j = 0; j < 20; j++) yomi[i][j] = make_pair(-1, 0);
  }
  dp[0][0] = 0;
  long long ans = -inff;
  for (long long i = 0; i < V; i++) memo[i] = -inff;
  for (long long r = 0; r < (1 << 14); r++) {
    long long g = __builtin_popcount(r);
    if (g > d) continue;
    for (long long i = 0; i < V; i++)
      if (dp[i][r] != -inff) {
        if (yomi[i][g] == (pair<long long, long long>)make_pair(-1, 0))
          moto(i, g);
        memo[yomi[i][g].first] =
            max(memo[yomi[i][g].first], dp[i][r] + yomi[i][g].second);
      }
    if (g < d) {
      for (long long i = 0; i < V; i++)
        if (memo[i] != -inff) {
          for (long long j = 0; j < 14; j++)
            if (!(r & (1 << j))) {
              dp[tugi[i][j]][r ^ (1 << j)] =
                  max(dp[tugi[i][j]][r ^ (1 << j)], memo[i] + cost[tugi[i][j]]);
            }
          memo[i] = -inff;
        }
    } else {
      for (long long i = 0; i < V; i++) {
        ans = max(ans, memo[i]);
        memo[i] = -inff;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

```