#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1e18;   long long k, n, c;   string s;   long long p[26][26];   long long f[105][105][26];   char x, y;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> s >> k >> n;     for (long long i = 1; i <= n; i++) {       cin >> x >> y >> c;       p[x -  a ][y -  a ] = c;     }     s =  *  + s;     for (long long i = 0; i < s.size(); i++)       for (long long j = 0; j <= k; j++)         for (long long v = 0; v < 26; v++) f[i][j][v] = -MAX;     for (long long i = 0; i < 26; i++) {       if (i == s[1] -  a )         f[1][0][i] = 0;       else         f[1][1][i] = 0;     }     for (long long i = 2; i < s.size(); i++) {       for (long long j = 0; j <= min(i - 1, k); j++) {         for (long long v = 0; v < 26; v++) {           for (long long u = 0; u < 26; u++) {             long long ok = (s[i] -  a  != v);             f[i][j + ok][v] = max(f[i][j + ok][v], f[i - 1][j][u] + p[u][v]);           }         }       }     }     long long ans = -MAX;     for (long long i = 0; i <= k; i++)       for (long long j = 0; j < 26; j++) ans = max(f[s.size() - 1][i][j], ans);     cout << ans;   }   