#include <bits/stdc++.h>
using namespace std;
int main() {
  int bonus[26][26], changes, i, j, k, l;
  for (i = 0; i < 26; i++)
    for (j = 0; j < 26; j++) bonus[i][j] = 0;
  string sname, t;
  cin >> sname;
  vector<int> name(sname.size());
  for (i = 0; i < name.size(); i++) name[i] = sname[i] - 'a';
  cin >> changes >> k;
  for (; k; k--) {
    cin >> t;
    i = t[0] - 'a';
    cin >> t;
    j = t[0] - 'a';
    cin >> bonus[i][j];
  }
  int dp[101][26][101];
  for (i = 0; i < 101; i++)
    for (j = 0; j < 26; j++)
      for (k = 0; k < 101; k++) dp[i][j][k] = -10000000;
  for (i = 0; i < 26; i++) dp[0][i][0 + (i == name[0] ? 0 : 1)] = 0;
  for (i = 1; i < name.size(); i++) {
    for (j = 0; j < 26; j++) {
      for (k = 0; k < 26; k++) {
        for (l = 0; l <= changes; l++) {
          if (dp[i - 1][k][l] > -10000000) {
            dp[i][j][l + (j == name[i] ? 0 : 1)] =
                max<int>(dp[i][j][l + (j == name[i] ? 0 : 1)],
                         dp[i - 1][k][l] + bonus[k][j]);
          }
        }
      }
    }
  }
  int ans = -10000000;
  for (j = 0; j < 26; j++)
    for (k = 0; k <= changes; k++)
      ans = max<int>(ans, dp[name.size() - 1][j][k]);
  cout << ans << endl;
  return 0;
}
