#include <bits/stdc++.h>
using namespace std;
string s;
long long k, n;
long long line;
long long dp[101][101][26];
long long a[26][26];
const long long inf = 1e9;
long long solve(long long idx, long long change, long long lst) {
  if (idx == n) return 0;
  long long &ans = dp[idx][change][lst];
  if (ans != -inf) return ans;
  long long cur = s[idx] - 'a';
  if (change > 0) {
    for (long long ch = 0; ch < 26; ch++) {
      if (ch == cur) {
        if (idx == 0) {
          ans = max(ans, solve(idx + 1, change, cur));
        } else {
          ans = max(ans, a[lst][ch] + solve(idx + 1, change, cur));
        }
      } else {
        if (idx == 0) {
          ans = max(ans, solve(idx + 1, change - 1, ch));
        } else {
          ans = max(ans, a[lst][ch] + solve(idx + 1, change - 1, ch));
        }
      }
    }
  } else {
    if (idx == 0) {
      ans = max(ans, solve(idx + 1, change, cur));
    } else {
      ans = max(ans, solve(idx + 1, change, cur) + a[lst][cur]);
    }
  }
  return ans;
}
void solve() {
  cin >> s >> k >> line;
  n = s.size();
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= k; j++)
      for (long long k1 = 0; k1 <= 25; k1++) dp[i][j][k1] = -inf;
  for (long long i = 0; i < line; i++) {
    char c1, c2;
    long long bon;
    cin >> c1 >> c2 >> bon;
    a[c1 - 'a'][c2 - 'a'] = bon;
  }
  long long ans = solve(0, k, 0);
  cout << ans;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) solve();
}
