#include <bits/stdc++.h>
using namespace std;
int dp[255][105][105];
int c[255][255];
int cc[1005];
int N;
string s;
int f(char l, int i, int k) {
  if (i == N) return 0;
  if (dp[l][i][k] != -1) return dp[l][i][k];
  int r = f(s[i], i + 1, k) + c[l][s[i]];
  if (k) {
    for (char x = 'a'; x <= 'z'; x++) r = max(r, f(x, i + 1, k - 1) + c[l][x]);
  }
  return dp[l][i][k] = r;
}
int main() {
  int k, m;
  char x, y;
  int z;
  cin >> s >> k >> m;
  N = s.size();
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    c[x][y] = z;
  }
  memset(dp, -1, sizeof(dp));
  cout << f('#', 0, k) << endl;
  return 0;
}
