#include <bits/stdc++.h>
using namespace std;
int score[30][30];
int dp[110][110][30];
int main(int argc, char** argv) {
  cin.sync_with_stdio(false);
  cout << setprecision(16);
  memset(score, 0, sizeof(score));
  string s;
  int K, M, a, N, b, i, j;
  char ch1, ch2;
  for ((ch1) = 0; (ch1) < (int)(110); (ch1)++)
    for ((ch2) = 0; (ch2) < (int)(110); (ch2)++)
      for ((a) = 0; (a) < (int)(30); (a)++) dp[ch1][ch2][a] = INT_MIN;
  cin >> s >> K >> M;
  N = s.length();
  for ((i) = 0; (i) < (int)(M); (i)++) {
    cin >> ch1 >> ch2;
    cin >> score[ch1 - 'a'][ch2 - 'a'];
  }
  for ((i) = 0; (i) < (int)(26); (i)++) {
    dp[1][(s[0] == i + 'a') ? 0 : 1][i] = 0;
  }
  for (i = 1; i < N; i++)
    for ((j) = 0; (j) < (int)(min(K, i) + 1); (j)++)
      for ((a) = 0; (a) < (int)(26); (a)++)
        if (dp[i][j][a] != INT_MIN)
          for ((b) = 0; (b) < (int)(26); (b)++) {
            int j2 = j + ((s[i] == 'a' + b) ? 0 : 1);
            int tmp = dp[i][j][a] + score[a][b];
            dp[i + 1][j2][b] = max(dp[i + 1][j2][b], tmp);
          }
  int ans = INT_MIN;
  for ((i) = 0; (i) < (int)(K + 1); (i)++)
    for ((j) = 0; (j) < (int)(26); (j)++) ans = max(ans, dp[N][i][j]);
  cout << ans;
  return 0;
}
