#include <bits/stdc++.h>
using namespace std;
int c[26][26];
int dp[26][101][101];
string s;
int solve(char lastChar, int op, int indx) {
  if (indx == s.size()) return 0;
  if (dp[lastChar][op][indx] != -1) return dp[lastChar][op][indx];
  int ans = -10000000;
  if (indx == 0) {
    if (op == 0)
      return dp[lastChar][op][indx] = solve(s[indx] - 'a', 0, indx + 1);
    for (int i = 0; i < 26; i++) {
      ans = max(ans, solve(i, op - (s[indx] - 'a' != i), indx + 1));
    }
    return dp[lastChar][op][indx] = ans;
  }
  if (op == 0) {
    return dp[lastChar][op][indx] =
               solve(s[indx] - 'a', 0, indx + 1) + c[lastChar][s[indx] - 'a'];
  }
  for (int i = 0; i < 26; i++) {
    ans = max(ans,
              solve(i, op - (s[indx] - 'a' != i), indx + 1) + c[lastChar][i]);
  }
  return dp[lastChar][op][indx] = ans;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> s;
  int k;
  cin >> k;
  int n;
  cin >> n;
  char a, b;
  int cost;
  while (n--) {
    cin >> a >> b >> cost;
    c[a - 'a'][b - 'a'] = cost;
  }
  memset(dp, -1, sizeof dp);
  cout << solve(0, k, 0);
}
