#include <bits/stdc++.h>
using namespace std;
int bon[200][200];
string s;
int calc[27][101][101];
int getans(char prevlet, int cloc, int cchange) {
  if (cchange < 0) return -10000000;
  if (cloc == s.size()) return 0;
  if (prevlet != 0)
    if (calc[prevlet - 'a'][cloc][cchange] != -1)
      return calc[prevlet - 'a'][cloc][cchange];
  char x;
  int ans = -1000000;
  for (x = 'a'; x <= 'z'; x++)
    ans = max(ans,
              bon[prevlet][x] + getans(x, cloc + 1, cchange - (s[cloc] != x)));
  if (prevlet != 0)
    return calc[prevlet - 'a'][cloc][cchange] = ans;
  else
    return ans;
}
int main() {
  int n, k, b;
  char x, y;
  cin >> s >> k;
  cin >> n;
  memset(calc, -1, sizeof(calc));
  while (n--) {
    scanf(" %c %c %d", &x, &y, &b);
    bon[x][y] = b;
  }
  cout << getans(0, 0, k) << endl;
  return 0;
}
