#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const double pi = 3.141592653589793238462;
const int nmax = 2000000;
const int hash_base = 119;
const long long inf = 2e9;
string s;
int k, dp[111][111][255], cost_matrix[255][255], m, ans = -inf;
int main() {
  cin >> s >> k >> m;
  for (int i = 1; i <= m; i++) {
    char a, b;
    int cost;
    scanf("\n%c %c %d", &a, &b, &cost);
    cost_matrix[a][b] = cost;
  };
  for (int i = 1; i < s.size(); i++)
    for (int j = 0; j <= k; j++)
      for (char symb2 = 'a'; symb2 <= 'z'; symb2++) {
        dp[j][i][symb2] = -inf;
        for (char symb1 = 'a'; symb1 <= 'z'; symb1++) {
          if (symb2 != s[i] && symb1 != s[i - 1] && j < 2) continue;
          if ((symb2 != s[i] || symb1 != s[i - 1]) && j < 1) continue;
          if (symb2 == s[i])
            dp[j][i][symb2] =
                max(dp[j][i][symb2],
                    dp[j][i - 1][symb1] + cost_matrix[symb1][symb2]);
          else
            dp[j][i][symb2] =
                max(dp[j][i][symb2],
                    dp[j - 1][i - 1][symb1] + cost_matrix[symb1][symb2]);
        };
      };
  int Answer = -inf;
  for (int i = 0; i <= k; i++)
    for (int j = 'a'; j <= 'z'; j++)
      if (!(i == 0 && j != s[s.size() - 1]))
        Answer = max(Answer, dp[i][s.size() - 1][j]);
  cout << Answer << endl;
  return 0;
}
