#include <bits/stdc++.h>
using namespace std;
int n, k, m;
int trans[28][28];
string s, s1, s2;
int dp[110][28][110];
bool avi[110][28][110];
int main() {
  cin >> s;
  n = s.length();
  cin >> k;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int c;
    cin >> s1 >> s2 >> c;
    trans[s1[0] - 'a'][s2[0] - 'a'] = c;
  }
  for (int i = 0; i < 110; i++)
    for (int j = 0; j < 28; j++)
      for (int k = 0; k < 110; k++) dp[i][j][k] = -0x3f3f3f3f;
  for (int i = 0; i < 26; i++) {
    dp[0][i][i != (s[0] - 'a')] = 0;
    avi[0][i][i != s[0] - 'a'] = 1;
  }
  for (int i = 1; i < n; i++) {
    for (int p = 0; p <= i; p++) {
      for (int c = 0; c < 26; c++) {
        if (avi[i - 1][c][p]) {
          for (int d = 0; d < 26; d++) {
            avi[i][d][p + (d != s[i] - 'a')] = 1;
            dp[i][d][p + (d != s[i] - 'a')] = max(
                dp[i][d][p + (d != s[i] - 'a')], dp[i - 1][c][p] + trans[c][d]);
          }
        }
      }
    }
  }
  int ans = -0x3f3f3f3f;
  for (int p = 0; p <= k; p++)
    for (int c = 0; c < 26; c++)
      if (avi[n - 1][c][p]) ans = max(ans, dp[n - 1][c][p]);
  printf("%d\n", ans);
  return 0;
}
