#include <bits/stdc++.h>
using namespace std;
const int inf = 214748364;
string s;
int K, n, c[105][105], dp[105][35][105];
int main() {
  memset(c, 0, sizeof c);
  cin >> s >> K >> n;
  for (int i = 0; i < n; i++) {
    char x, y;
    cin >> x >> y;
    cin >> c[x - 'a'][y - 'a'];
  }
  for (int i = 0; i < 105; i++) {
    for (int j = 0; j < 35; j++) {
      for (int k = 0; k < 105; k++) {
        dp[i][j][k] = -inf;
      }
    }
  }
  dp[0][26][K] = 0;
  for (int i = 0; s[i]; i++) {
    for (int j = 0; j <= 26; j++) {
      for (int k = 0; k <= K; k++) {
        for (int o = 0; o < 26; o++) {
          int x;
          if (s[i] - 'a' == o) {
            x = k;
          } else {
            x = k - 1;
          }
          if (x >= 0) {
            dp[i + 1][o][x] = max(dp[i + 1][o][x], dp[i][j][k] + c[j][o]);
          }
        }
      }
    }
  }
  int ans = -inf;
  for (int j = 0; j <= 26; j++) {
    for (int k = 0; k <= K; k++) {
      ans = max(ans, dp[s.size()][j][k]);
    }
  }
  cout << ans << endl;
  return 0;
}
