#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-5;
map<pair<int, string>, int> DP;
int A[256][256];
int calc_score(string s) {
  int L = s.length();
  int sum0 = 0;
  for (int i = 0; i < (int)(L - 1); i++) {
    sum0 += A[(int)s[i]][(int)s[i + 1]];
  }
  return sum0;
}
int search(int K, string subs) {
  char c;
  int L = subs.length();
  if (L == 1) return 0;
  if (DP.find(make_pair(K, subs)) != DP.end()) return DP[make_pair(K, subs)];
  if (K == 0) {
    int sum = calc_score(subs);
    return DP[make_pair(K, subs)] = sum;
  }
  if (L == 2) {
    int tmp[3];
    char c2;
    char c1 = subs[0];
    tmp[0] = tmp[1] = A[(int)c1][(int)subs[1]];
    for (c2 = 'a'; c2 <= 'z'; c2++) {
      int count = (c2 == subs[1]);
      tmp[count] = max(tmp[count], A[(int)c1][(int)c2]);
    }
    return DP[make_pair(K, subs)] = max(tmp[0], tmp[1]);
  }
  char oc = subs[1];
  int flag = 0;
  int sum0 = 0;
  for (c = 'a'; c <= 'z'; c++) {
    int num = 1;
    int sum;
    if (c == oc) num = 0;
    subs[1] = c;
    int new_k = min(L - 2, K - num);
    sum = search(new_k, subs.substr(1));
    sum += A[(int)subs[0]][(int)c];
    if (flag == 0 || sum > sum0) {
      sum0 = sum;
      flag = 1;
    }
  }
  subs[1] = oc;
  return DP[make_pair(K, subs)] = sum0;
}
int main(void) {
  string s;
  int K, n;
  memset(A, 0, sizeof(A));
  cin >> s >> K;
  cin >> n;
  for (int i = 0; i < (int)(n); i++) {
    char c1, c2;
    int point;
    cin >> c1 >> c2 >> point;
    A[(int)c1][(int)c2] = point;
  }
  int L = s.length();
  int sum0 = calc_score(s);
  if (K == 0 || L == 1) {
    cout << sum0 << endl;
    return 0;
  }
  string subs = s;
  char oc = subs[0];
  int ret = sum0;
  for (char c = 'a'; c <= 'z'; c++) {
    int num = 1;
    int sum;
    if (c == oc) num = 0;
    subs[0] = c;
    int new_k = min(L - 1, K - num);
    sum = search(new_k, subs);
    if (sum > ret) {
      ret = sum;
    }
  }
  cout << ret << endl;
  return 0;
}
