#include <bits/stdc++.h>
using namespace std;
int cst[26][26];
string s;
int k;
int dt[100][26][101];
int dp(int id, int al, int kl) {
  if (dt[id][al][kl]) return dt[id][al][kl];
  if (id == s.size() - 1) {
    dt[id][al][kl] = 1001;
  } else if (kl == 0) {
    dt[id][al][kl] =
        dp(id + 1, s[id + 1] - 'a', kl) + cst[s[id] - 'a'][s[id + 1] - 'a'];
  } else {
    int nk;
    if (al == s[id] - 'a')
      nk = kl;
    else
      nk = kl - 1;
    if (nk == 0) {
      dt[id][al][kl] =
          dp(id + 1, s[id + 1] - 'a', nk) + cst[al][s[id + 1] - 'a'];
    } else {
      int ma = 0;
      for (int(i) = (0); (i) <= (25); ++(i)) {
        int tp = dp(id + 1, i, nk) + cst[al][i];
        if (tp > ma) ma = tp;
      }
      dt[id][al][kl] = ma;
    }
  }
  return dt[id][al][kl];
}
int main(int argc, char **argv) {
  cin >> s >> k;
  int n, c;
  cin >> n;
  char a, b;
  for (int(i) = (0); (i) <= (25); ++(i))
    for (int(j) = (0); (j) <= (25); ++(j)) cst[i][j] = 1001;
  for (int(i) = (1); (i) <= (n); ++(i)) {
    cin >> a >> b >> c;
    cst[a - 'a'][b - 'a'] = c + 1001;
  }
  if (s.size() <= 1) {
    cout << 0 << endl;
    return 0;
  }
  int mx = 0;
  if (k == 0)
    mx = dp(0, s[0] - 'a', k);
  else {
    for (int(i) = (0); (i) <= (25); ++(i)) {
      int tp = dp(0, i, k);
      if (tp > mx) mx = tp;
    }
  }
  int vl = s.size();
  cout << mx - (vl * 1001) << endl;
  return 0;
}
