#include <bits/stdc++.h>
using namespace std;
int totalCases, testNum;
string s;
int len;
int xyc[27][27];
int DP[105][105][30];
int K;
void preprocess() {
  memset(xyc, 0, sizeof(xyc));
  memset(DP, -1, sizeof(DP));
}
bool input() {
  cin >> s >> K;
  len = s.length();
  int n, c;
  char x, y;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> c;
    xyc[x - 96][y - 96] = c;
  }
  return true;
}
int f(int i, int k, int prevVal) {
  if (k < 0) return -10000000;
  if (DP[i][k][prevVal] != -1) return DP[i][k][prevVal];
  if (i == len) return 0;
  int maxFound = -10000000;
  for (int val = 1; val <= 26; val++) {
    int cons = xyc[prevVal][val] + f(i + 1, k - (val != (s[i] - 96)), val);
    maxFound = max(maxFound, cons);
  }
  DP[i][k][prevVal] = maxFound;
  return maxFound;
}
void solve() { cout << f(0, K, 0) << endl; }
int main() {
  preprocess();
  totalCases = 1;
  for (testNum = 1; testNum <= totalCases; testNum++) {
    if (!input()) break;
    solve();
  }
}
