#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using lld = long double;
using l64 = int64_t;
const ll MOD = 1000000007;
const ll N = 1001;
ll cost[28][28];
ll dp[N][N][28];
ll n, k, q;
string ss;
ll fun(ll i, ll j, ll prev) {
  if (i == n) return 0;
  auto &res = dp[i][j][prev];
  if (res != -1) return res;
  ll ans = -INT_MAX / 2;
  for (ll o = 0; o < 26; o++) {
    if (o == ss[i] - 'a')
      ans = max(ans, cost[prev][o] + fun(i + 1, j, o));
    else if (j > 0)
      ans = max(ans, cost[prev][o] + fun(i + 1, j - 1, o));
  }
  return res = ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> ss;
  cin >> k;
  cin >> q;
  n = ss.size();
  char a, b;
  ll c;
  memset(dp, -1, sizeof(dp));
  memset(cost, 0, sizeof(cost));
  for (ll i = 0; i < q; i++) {
    cin >> a >> b;
    cin >> c;
    cost[a - 'a'][b - 'a'] = c;
  }
  cout << fun(0, k, 27);
  return 0;
}
