#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const double EPS = 1e-9;
int k;
int dp[200][26][101];
int score[26][26];
bool used[200][26][101];
int main() {
  memset(dp, -1, sizeof(dp));
  memset(dp, 0, sizeof(score));
  memset(used, false, sizeof(used));
  string s;
  cin >> s >> k;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char c1, c2;
    int d;
    cin >> c1 >> c2 >> d;
    score[c1 - 'a'][c2 - 'a'] = d;
  }
  if (s == "") {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < 26; i++) {
    if (i == s[0] - 'a') {
      dp[0][i][k] = 0;
      used[0][i][k] = true;
    } else {
      if (k > 0) {
        dp[0][i][k - 1] = 0;
        used[0][i][k - 1] = true;
      }
    }
  }
  for (int i = 1; i < (int)s.size(); i++) {
    for (int j = 0; j < 26; j++) {
      for (int l = 0; l <= k; l++) {
        for (int m = 0; m < 26; m++) {
          if (!used[i - 1][m][l]) continue;
          if (j == s[i] - 'a') {
            if (!used[i][j][l] || dp[i][j][l] < dp[i - 1][m][l] + score[m][j]) {
              dp[i][j][l] = dp[i - 1][m][l] + score[m][j];
              used[i][j][l] = true;
            }
          } else {
            if (l > 0 && (!used[i][j][l - 1] ||
                          dp[i][j][l - 1] < dp[i - 1][m][l] + score[m][j])) {
              dp[i][j][l - 1] = dp[i - 1][m][l] + score[m][j];
              used[i][j][l - 1] = true;
            }
          }
        }
      }
    }
  }
  int res = -INF;
  for (int i = 0; i <= k; i++) {
    for (int j = 0; j < 26; j++) {
      if (used[(int)s.size() - 1][j][i])
        res = max(res, dp[(int)s.size() - 1][j][i]);
    }
  }
  cout << res << endl;
  return 0;
}
