#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define ZERO_IQ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(x, y) cerr<<(#x)<<" " <<(#y)<<" is " << (x) <<" "<< (y) << endl
#define debug2(x, y, z) cerr<<(#x)<<" " <<(#y)<<" " <<(#z)<<" is " << (x) <<" "<< (y) <<" "<<(z) << endl
#define watch(x) cerr<<(#x)<<" is " << (x) << endl
string s;
int arr[30][30];
ll dp[101][101][27];

ll solve(int x, int l, int prev) {
    if (x == s.size()) return 0;
    if (dp[x][l][prev] != -1) return dp[x][l][prev];
    ll res = solve(x + 1, l, s[x] - 'a') + arr[prev][s[x] - 'a'];
    for (char i = 'a'; i <= 'z'; i++) {
        if (i != s[x] && l > 0) res = max((ll) res, solve(x + 1, l - 1, i - 'a') + arr[prev][i - 'a']);
    }
    return dp[x][l][prev] = res;
}

int main() {
    memset(dp, -1, sizeof dp);
    int a, n;
    cin >> s >> a >> n;
    for (int i = 0; i < n; i++) {
        char q, qq;
        cin >> q >> qq;
        cin >> arr[q - 'a'][qq - 'a'];
    }
    cout << solve(0, a, 29) << endl;
    return 0;
}


// practice with Dukkha
#include <algorithm>
#include <iostream>
#include <cstring>

using namespace std;

const int K = 100, N = 100, A = 26, INF = 0x3f3f3f3f;

char aa[N + 1];
int cc[A][A];
int dp[K + 1][A], dq[K + 1][A];

int main() {
	int k, n; cin >> aa >> k >> n;
	while (n--) {
		static char sa[2], sb[2];
		int c; cin >> sa >> sb >> c;
		int a = sa[0] - 'a';
		int b = sb[0] - 'a';
		cc[a][b] = c;
	}
	n = strlen(aa);
	for (int i = 0; i < n; i++) {
		int a_ = aa[i] - 'a';
		if (i == 0) {
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++)
					dp[h][a] = -INF;
			dp[0][a_] = 0;
			for (int a = 0; a < A; a++)
				if (a != a_)
					dp[1][a] = 0;
		} else {
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++)
					dq[h][a] = -INF;
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++) {
					int x = dp[h][a];
					if (x == -INF)
						continue;
					for (int b = 0; b < A; b++) {
						int y = x + cc[a][b];
						if (b == a_)
							dq[h][b] = max(dq[h][b], y);
						else if (h < k)
							dq[h + 1][b] = max(dq[h + 1][b], y);
					}
				}
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++)
					dp[h][a] = dq[h][a];
		}
	}
	int ans = -INF;
	for (int h = 0; h <= k; h++)
		for (int a = 0; a < A; a++)
			ans = max(ans, dp[h][a]);
	cout << ans << '\n';
	return 0;
}


// practice with Dukkha
#include <algorithm>
#include <iostream>
#include <cstring>

using namespace std;

const int K = 100, N = 100, A = 26;

char aa[N + 1];
int cc[A][A];
int dp[K + 1][A], dq[K + 1][A];

int main() {
	int k, n; cin >> aa >> k >> n;
	while (n--) {
		static char sa[2], sb[2];
		int c; cin >> sa >> sb >> c;
		int a = sa[0] - 'a';
		int b = sb[0] - 'a';
		cc[a][b] = c;
	}
	n = strlen(aa);
	for (int i = 0; i < n; i++) {
		int a_ = aa[i] - 'a';
		if (i == 0) {
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++)
					dp[h][a] = -1;
			dp[0][a_] = 0;
			for (int a = 0; a < A; a++)
				if (a != a_)
					dp[1][a] = 0;
		} else {
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++)
					dq[h][a] = -1;
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++) {
					int x = dp[h][a];
					if (x == -1)
						continue;
					for (int b = 0; b < A; b++) {
						int y = x + cc[a][b];
						if (b == a_)
							dq[h][b] = max(dq[h][b], y);
						else if (h < k)
							dq[h + 1][b] = max(dq[h + 1][b], y);
					}
				}
			for (int h = 0; h <= k; h++)
				for (int a = 0; a < A; a++)
					dp[h][a] = dq[h][a];
		}
	}
	int ans = 0;
	for (int h = 0; h <= k; h++)
		for (int a = 0; a < A; a++)
			ans = max(ans, dp[h][a]);
	cout << ans << '\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cout << #x << ": " << x << endl
#define repn(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) for(int i = 0; i < (a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
const int INF = 1e9;

int cost[30][30];
string s;
int L;
int dp[105][105][30], v[105][105][30];

int dfs(int p, int k, int l){
	if(k < 0) return -INF;
	if(p == L) return 0;
	if(v[p][k][l]) return dp[p][k][l];
	int res = 0;
	rep(c, 26){
		res = max(res, cost[l][c] + dfs(p + 1, k - (s[p] == 'a' + c ? 0 : 1), c));
	}
	v[p][k][l] = 1;
	return dp[p][k][l] = res;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int K;
	cin >> s >> K;
	L = s.length();
	int n;
	cin >> n;
	memset(cost, 0, sizeof(cost));
	rep(i, n){
		char a, b;
		cin >> a >> b;
		cin >> cost[a - 'a'][b - 'a'];
	}
	cout << dfs(0, K, 26) << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cout << #x << ": " << x << endl
#define repn(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) for(int i = 0; i < (a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
const int inf = 1 << 29;

int cost[30][30];
string s;
int L;
int dp[105][105][30], v[105][105][30];

int dfs(int p, int k, int l){
	if(k < 0) return -inf;
	if(p == L) return 0;
	if(v[p][k][l]) return dp[p][k][l];
	int res = -inf;
	rep(c, 26){
		res = max(res, cost[l][c] + dfs(p + 1, k - (s[p] == 'a' + c ? 0 : 1), c));
	}
	v[p][k][l] = 1;
	return dp[p][k][l] = res;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int K;
	cin >> s >> K;
	L = s.length();
	int n;
	cin >> n;
	memset(cost, 0, sizeof(cost));
	rep(i, n){
		char a, b;
		cin >> a >> b;
		cin >> cost[a - 'a'][b - 'a'];
	}
	memset(v, 0, sizeof(v));
	cout << dfs(0, K, 26) << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cout << #x << ": " << x << endl
#define repn(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) for(int i = 0; i < (a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
const int INF = 1e9;

int cost[30][30];
string s;
int L;
int dp[105][105][30], v[105][105][30];

int dfs(int p, int k, int l){
	if(k < 0) return -INF;
	if(p == L) return 0;
	if(v[p][k][l]) return dp[p][k][l];
	rep(c, 26){
		dp[p][k][l] = max(dp[p][k][l], cost[l][c] + dfs(p + 1, k - (s[p] == 'a' + c ? 0 : 1), c));
	}
	v[p][k][l] = 1;
	return dp[p][k][l];
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int K;
	cin >> s >> K;
	L = s.length();
	int n;
	cin >> n;
	rep(i, n){
		char a, b;
		cin >> a >> b;
		cin >> cost[a - 'a'][b - 'a'];
	}
	cout << dfs(0, K, 26) << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cout << #x << ": " << x << endl
#define repn(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) for(int i = 0; i < (a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
const ll inf = 1e18;

ll cost[30][30];
string s;
int L;
ll dp[105][105][30], v[105][105][30];

ll dfs(int p, int k, int l){
	if(k < 0) return -inf;
	if(p == L) return 0;
	if(v[p][k][l]) return dp[p][k][l];
	ll res = 0;
	rep(c, 26){
		res = max(res, cost[l][c] + dfs(p + 1, k - (s[p] == 'a' + c ? 0 : 1), c));
	}
	v[p][k][l] = 1;
	return dp[p][k][l] = res;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int K;
	cin >> s >> K;
	L = s.length();
	int n;
	cin >> n;
	memset(cost, 0, sizeof(cost));
	rep(i, n){
		char a, b;
		cin >> a >> b;
		cin >> cost[a - 'a'][b - 'a'];
	}
	memset(v, 0, sizeof(v));
	cout << dfs(0, K, 26) << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cout << #x << ": " << x << endl
#define repn(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) for(int i = 0; i < (a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
const int INF = INT_MAX;

int cost[30][30];
string s;
int L;
int dp[105][105][30], v[105][105][30];

int dfs(int p, int k, int l){
	if(k < 0) return -INF;
	if(p == L) return 0;
	if(v[p][k][l]) return dp[p][k][l];
	int res = 0;
	rep(c, 26){
		res = max(res, cost[l][c] + dfs(p + 1, k - (s[p] == 'a' + c ? 0 : 1), c));
	}
	v[p][k][l] = 1;
	return dp[p][k][l] = res;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int K;
	cin >> s >> K;
	L = s.length();
	int n;
	cin >> n;
	memset(cost, 0, sizeof(cost));
	rep(i, n){
		char a, b;
		cin >> a >> b;
		cin >> cost[a - 'a'][b - 'a'];
	}
	cout << dfs(0, K, 26) << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
#define MOD 1000000007
#define int long long
#define IOS                               \
        ios_base::sync_with_stdio(false); \
        cin.tie();                        \
        cout.tie()
#define ld long double
using namespace std;
#define min1(x, y, z) min(x, min(y, z))
#define max1(x, y, z) max(x, max(y, z))
const int maxn = 2e5 + 5;
const int inf = 9e18;
const double pi = acos(-1);

string s;
int k;
map<pair<char, char>, int> mp;

int cache[105][105][105];

int dp(int idx, int k, int prev)
{
        int n = s.length();

        char ch = prev + 'a';
        //cout << idx << " " << k << " " << ch << endl;

        if (k < 0)
                return -inf;
        if (idx >= n)
                return 0;

        if (cache[idx][k][prev] != -1)
        {
                return cache[idx][k][prev];
        }

        int ans = 0;
        for (char c = 'a'; c <= 'z'; c++)
        {
                for (char d = 'a'; d <= 'z'; d++)
                {
                        if (s[idx] == c && s[idx + 1] == d)
                        {
                                ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k, d - 'a'));
                        }
                        else if (s[idx] == c || s[idx + 1] == d)
                        {
                                ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k - 1, d - 'a'));
                        }
                        else
                        {
                                ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k - 2, d - 'a'));
                        }
                }
        }

        return cache[idx][k][prev] = ans;
}

int32_t main()
{
        IOS;

        cin >> s >> k;

        int n;
        cin >> n;

        for (int i = 0; i < n; i++)
        {
                char x, y;
                cin >> x >> y;
                int c;
                cin >> c;
                mp[{x, y}] = c;
        }

        memset(cache, -1, sizeof(cache));
        cout << dp(0, k, '.') << endl;

        return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define int long long
#define IOS                               \
        ios_base::sync_with_stdio(false); \
        cin.tie();                        \
        cout.tie()
#define ld long double
using namespace std;
#define min1(x, y, z) min(x, min(y, z))
#define max1(x, y, z) max(x, max(y, z))
const int maxn = 2e5 + 5;
const int inf = 9e18;
const double pi = acos(-1);

string s;
int k;
map<pair<char, char>, int> mp;

int cache[105][105][150];

int dp(int idx, int k, int prev)
{
        int n = s.length();

        char ch = prev + 'a';

        if (k < 0)
                return -inf;
        if (idx >= n)
                return 0;

        if (cache[idx][k][prev] != -1)
        {
                return cache[idx][k][prev];
        }

        //cout << idx << " " << k << " " << ch << endl;

        int ans = 0;
        for (char c = 'a'; c <= 'z'; c++)
        {
                for (char d = 'a'; d <= 'z'; d++)
                {
                        if (idx == n - 1 && k > 0)
                        {
                                ans = max(ans, mp[{prev + 'a', c}]);
                        }
                        else
                        {
                                if (s[idx] == c && s[idx + 1] == d)
                                {
                                        ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k, d - 'a'));
                                }
                                else if (s[idx] == c || s[idx + 1] == d)
                                {
                                        ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k - 1, d - 'a'));
                                }
                                else
                                {
                                        ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k - 2, d - 'a'));
                                }
                        }
                }
        }

        return cache[idx][k][prev] = ans;
}

int32_t main()
{
        IOS;

        cin >> s >> k;

        int n;
        cin >> n;

        for (int i = 0; i < n; i++)
        {
                char x, y;
                cin >> x >> y;
                int c;
                cin >> c;
                mp[{x, y}] = c;
        }

        memset(cache, -1, sizeof(cache));
        cout << dp(0, k, 149) << endl;

        return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define int long long
#define IOS                               \
        ios_base::sync_with_stdio(false); \
        cin.tie();                        \
        cout.tie()
#define ld long double
using namespace std;
#define min1(x, y, z) min(x, min(y, z))
#define max1(x, y, z) max(x, max(y, z))
const int maxn = 2e5 + 5;
const int inf = 9e18;
const double pi = acos(-1);

string s;
int k;
map<pair<char, char>, int> mp;

int cache[105][105][105];

int dp(int idx, int k, int prev)
{
        int n = s.length();

        if (k < 0)
                return -inf;
        if (idx >= n)
                return 0;

        if (cache[idx][k][prev] != -1)
        {
                return cache[idx][k][prev];
        }

        int ans = 0;
        for (char c = 'a'; c <= 'z'; c++)
        {
                for (char d = 'a'; d <= 'z'; d++)
                {
                        if (s[idx] == c && s[idx + 1] == d)
                        {
                                ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k, d - 'a'));
                        }
                        else if (s[idx] == c || s[idx + 1] == d)
                        {
                                ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k - 1, d - 'a'));
                        }
                        else
                        {
                                ans = max(ans, mp[{prev + 'a', c}] + mp[{c, d}] + dp(idx + 2, k - 2, d - 'a'));
                        }
                }
        }

        return cache[idx][k][prev] = ans;
}

int32_t main()
{
        IOS;

        cin >> s >> k;

        int n;
        cin >> n;

        for (int i = 0; i < n; i++)
        {
                char x, y;
                cin >> x >> y;
                int c;
                cin >> c;
                mp[{x, y}] = c;
        }

        memset(cache, -1, sizeof(cache));
        cout << dp(0, k, '.' - 'a') << endl;

        return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define int long long
#define IOS                               \
        ios_base::sync_with_stdio(false); \
        cin.tie();                        \
        cout.tie()
#define ld long double
using namespace std;
#define min1(x, y, z) min(x, min(y, z))
#define max1(x, y, z) max(x, max(y, z))
const int maxn = 2e5 + 5;
const int inf = 9e18;
const double pi = acos(-1);

string s;
int k;
map<pair<char, char>, int> mp;

int cache[105][105][150];

int dp(int idx, int k, int prev)
{
        int n = s.length();

        if (k < 0)
                return -inf;
        if (idx >= n)
                return 0;

        if (cache[idx][k][prev] != -1)
        {
                return cache[idx][k][prev];
        }

        int ans = dp(idx + 1, k, s[idx]) + mp[{prev, s[idx]}];
        for (char c = 'a'; c <= 'z'; c++)
        {
                ans = max(ans, mp[{prev, c}] + dp(idx + 1, k - 1, c));
        }

        return cache[idx][k][prev] = ans;
}

int32_t main()
{
        IOS;

        cin >> s >> k;

        int n;
        cin >> n;

        for (int i = 0; i < n; i++)
        {
                char x, y;
                cin >> x >> y;
                int c;
                cin >> c;
                mp[{x, y}] = c;
        }

        memset(cache, -1, sizeof(cache));
        cout << dp(0, k, 149) << endl;

        return 0;
}


//
//#include <bits/stdc++.h>
//
//#define ln                '\n'
//#define all(dat)           dat.begin(), dat.end()
//#define loop(i, to)        for (int i = 0; i < to; ++i)
//#define cont(i, to)        for (int i = 1; i <= to; ++i)
//#define circ(i, fr, to)    for (int i = fr; i <= to; ++i)
//#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
//
//typedef long long          num;
//
//using namespace std;
//
//const int nsz = 1e5, inf = 0x3f3f3f3f;
//bool vis[2 * nsz + 5];
//int sz, n, m, dis[2 * nsz + 5];
//map<vector<int>, int> to;
//vector<pair<int, int>> g[nsz + 5];
//
//struct node {
//    int id;
//    vector<int> u;
//
//    node() {}
//
//    node(int id, vector<int> u) {
//        this->id = id;
//        this->u = u;
//    }
//};
//node dat[2 * nsz + 5];
//
//int inline id(vector<int> tmp) {
//    if (to[tmp])  return to[tmp];
//    return to[tmp] = ++sz;
//}
//
//bool inline visited(int id) {
//    for (int u : dat[id].u) {
//        if (!vis[u])  return 0;
//    }
//    return 1;
//}
//
//int inline val(int id) {
//    int res = 0;
//    for (int u : dat[id].u) {
//        res += dis[u];
//    }
//    return res;
//}
//
//void inline dijkstra(int s = 1) {
//    memset(dis, inf, sizeof(dis));
//    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
//    dis[s] = 0;
//    q.push({0, s});
//    for (; !q.empty();) {
//        int u = q.top().second;
//        q.pop();
//        if (vis[u])  continue;
//        vis[u] = 1;
//        loop (i, g[u].size()) {
//            int v = g[u][i].first, d = g[u][i].second;
//            if (vis[v])  continue;
//            if (dat[v].u.size() == 1) {
//                if (dis[v] > dis[u] + d) {
//                    dis[v] = dis[u] + d;
//                    q.push({dis[v], v});
//                }
//            } else {
//                if (!visited(v))  continue;
//                int dist = val(v);
//                if (dis[v] > dist) {
//                    dis[v] = dist;
//                    q.push({dis[v], v});
//                }
//            }
//        }
//    }
//}
//
//int main() {
//    cin >> m >> n;
//    int sid = id({});
//    dat[sid] = node(sid, {});
//    cont (i, m) {
//        int u, sz, d = 0;
//        vector<int> to;
//        cin >> u >> sz;
//        cont (j, sz) {
//            int v;
//            cin >> v;
//            if (v == -1) {
//                ++d;
//                continue;
//            }
//            to.push_back(v);
//        }
//        int id1 = id({u}), id2 = id(to);
//        dat[id1] = node(id1, {u});
//        dat[id2] = node(id2, to);
//        cout << id1 << " -> " << id2 << ' ' << d << ln;
//        g[id2].push_back({id1, d});
//    }
//    dijkstra();
//    cont (u, sz) {
//        cout << dis[u] << ' ';
//    }
//}

#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (int i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 100, asz = 26, inf = 0x3f3f3f3f;
int n, m, k, cur = 0, pre = 1, dp[nsz + 5][nsz + 5][asz], w[nsz + 5][nsz + 5], ans = -inf;
string s;

void inline upd(int &a, int b) {
    a = max(a, b);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> s >> k >> m;
    n = (int) s.size();
    s = ' ' + s;
    cont (i, m) {
        char a, b;
        int val;
        cin >> a >> b >> val;
        w[a - 'a'][b - 'a'] = val;
    }
    fill(dp[0][0], dp[0][0] + (nsz + 5) * (nsz + 5) * asz, -inf);
    loop (c, 26) {
        dp[pre][c != s[1] - 'a'][c] = 0;
    }
    cont (i, n) {
        circ (j, 0, k) {
            loop (c1, 26) {
                loop (c2, 26) {
                    if (dp[pre][j][c1] == -inf)  continue;
                    upd(dp[cur][j + (c2 != s[i + 1] - 'a')][c2], dp[pre][j][c1] + w[c1][c2]);
                }
            }
        }
        swap(pre, cur);
    }
    circ (j, 0, k) {
        loop (c, 26) {
            upd(ans, dp[pre][j][c]);
        }
    }
    cout << ans << ln;
}


#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (int i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 100, asz = 26, inf = 0x3f3f3f3f;
int n, m, k, dp[nsz + 5][nsz + 5][asz], w[nsz + 5][nsz + 5], ans = -inf;
string s;

void inline upd(int &a, int b) {
    a = max(a, b);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> s >> k >> m;
    n = s.size();
    s = ' ' + s;
    cont (i, m) {
        char a, b;
        int val;
        cin >> a >> b >> val;
        w[a - 'a'][b - 'a'] = val;
    }
    circ (i, 0, n) {
        circ (j, 0, k) {
            loop (c, 26) {
                dp[i][j][c] = -inf;
            }
        }
    }
    loop (c, 26) {
        dp[1][c != s[1] - 'a'][c] = 0;
    }
    cont (i, n) {
        circ (j, 0, k) {
            loop (c1, 26) {
                loop (c2, 26) {
                    if (dp[i][j][c1] == -inf)  continue;
                    upd(dp[i + 1][j + (c2 != s[i + 1] - 'a')][c2], dp[i][j][c1] + w[c1][c2]);
                }
            }
        }
    }
    circ (j, 0, k) {
        loop (c, 26) {
            upd(ans, dp[n][j][c]);
        }
    }
    cout << ans << ln;
}


#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (int i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 100, asz = 26, inf = 0x3f3f3f3f;
int n, m, k, dp[nsz + 5][nsz + 5][asz], w[nsz + 5][nsz + 5], ans = -inf;
string s;

void inline upd(int &a, int b) {
	a = max(a, b);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin >> s >> k >> m;
	n = s.size();
	s = ' ' + s;
	cont (i, m) {
		char a, b;
		int val;
		cin >> a >> b >> val;
		w[a - 'a'][b - 'a'] = val;
	}
	circ (i, 0, n) {
		circ (j, 0, k) {
			loop (c, 26) {
				dp[i][j][c] = -inf;
			}
		}
	}
	loop (c, 26) {
		dp[1][c != s[1] - 'a'][c] = 0;
	}
	cont (i, n) {
		circ (j, 0, k) {
			loop (c1, 26) {
				loop (c2, 26) {
					if (dp[i][j][c1] == -inf)  continue;
					upd(dp[i + 1][j + (c2 != s[i + 1] - 'a')][c2], dp[i][j][c1] + w[c1][c2]);
				}
			}
		}
	}
	circ (j, 0, k) {
		loop (c, 26) {
			upd(ans, dp[n][j][c]);
		}
	}
	cout << ans << ln;
}


#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (int i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 100, asz = 26;
int n, m, k, dp[nsz + 5][nsz + 5][asz], w[nsz + 5][nsz + 5], ans;
string s;

void inline upd(int &a, int b) {
	a = max(a, b);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin >> s >> k >> m;
	n = s.size();
	s = ' ' + s;
	cont (i, m) {
		char a, b;
		int val;
		cin >> a >> b >> val;
		w[a - 'a'][b - 'a'] = val;
	}
	memset(dp, -1, sizeof(dp));
	loop (c, 26) {
		dp[1][c != s[1] - 'a'][c] = 0;
	}
	cont (i, n) {
		circ (j, 0, k) {
			loop (c1, 26) {
				loop (c2, 26) {
					if (dp[i][j][c1] == -1)  continue;
					upd(dp[i + 1][j + (c2 != s[i + 1] - 'a')][c2], dp[i][j][c1] + w[c1][c2]);
				}
			}
		}
	}
	circ (j, 0, k) {
		loop (c, 26) {
			upd(ans, dp[n][j][c]);
		}
	}
	cout << ans << ln;
}


#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (int i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 100, asz = 26, inf = 0x3f3f3f3f;
int n, m, k, pre = 0, cur = 1, dp[2][nsz + 5][asz], w[nsz + 5][nsz + 5], ans = -inf;
string s;

void inline upd(int &a, int b) {
    a = max(a, b);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> s >> k >> m;
    n = (int) s.size();
    s = ' ' + s;
    cont (i, m) {
        char a, b;
        int val;
        cin >> a >> b >> val;
        w[a - 'a'][b - 'a'] = val;
    }
    fill(dp[0][0], dp[0][0] + sizeof(dp) / sizeof(int), -inf);
    loop (c, 26) {
        dp[pre][c != s[1] - 'a'][c] = 0;
    }
    cont (i, n - 1) {
        circ (j, 0, k) {
            loop (c1, 26) {
                if (dp[pre][j][c1] == -inf)  continue;
                loop (c2, 26) {
                    upd(dp[cur][j + (c2 != s[i + 1] - 'a')][c2], dp[pre][j][c1] + w[c1][c2]);
                }
            }
        }
        swap(pre, cur);
        fill(dp[cur][0], dp[cur][0] + sizeof(dp[cur]) / sizeof(int), -inf);
    }
    circ (j, 0, k) {
        loop (c, 26) {
            upd(ans, dp[pre][j][c]);
        }
    }
    cout << ans << ln;
}


#include <bits/stdc++.h>
#define For(i, a, b, c) for(int i = (a); i <= (b); i += (c))
#define Rof(i, a, b, c) for(int i = (a); i >= (b); i += (c))
#define Forn(i, n) For(i, 0, (n) - 1, +1)
#define For1(i, n) For(i, 1, (n), +1)
#define Forall(i, x) for(auto i = (x).begin(); i != (x).end(); i++)
#define size(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);

#define fst first
#define snd second
#define pb push_back
#define lower lower_bound
#define upper upper_bound
#define ENDL '\n'
using namespace std;

typedef long long lli;
typedef unsigned long long ulli;
typedef pair<int, int> ii;

template <class T>
struct rge{ T b, e; };

template <class T>
rge<T> range(T i, T j){
  return rge<T>{i, j};
};

template <class A, class B>
ostream & operator << (ostream &os, pair<A, B> p){
  return os << "(" << p.fst << ", " << p.snd << ")";
}

template <class T>
ostream & operator << (ostream &os, rge<T> d){
  os << "[";
  for(auto it = d.b; it != d.e; it++)
    os << (it == d.b ? "": ", ") << *it;
  return os << "]";
}

#define deb(x) cout << #x": " << (x) << '\n';

const int N = 105, K = 105;
const lli inf = 1e18;

lli dp[N][K][26];
lli cost[26][26];
string s;
int n, m, k;

template <class T>
void umax(T &a, T b){
  a = max(a, b);
}

int main(){ _
  cin >> s >> k;
  n = size(s);
  cin >> m;
  Forn(i, m){
    char x, y;
    lli c;
    cin >> x >> y >> c;
    cost[x - 'a'][y - 'a'] = c;
  }
  Forn(i, n - 1){
    Forn(change, min(i + 1, k) + 1){
      Forn(x, 26){
        if( change || (!change && char('a' + x) == s[i]) ){
          Forn(y, 26){
            int add = s[i + 1] != char('a' + y);
            umax(dp[i + 1][change + add][y], dp[i][change][x] + cost[x][y]);
          }
        }
      }
    }
  }
  lli ans = 0;
  Forn(i, n - 1){
    ans += cost[s[i] - 'a'][s[i + 1] - 'a'];
  }
  Forn(change, k + 1){
    Forn(y, 26){
      umax(ans, dp[n - 1][change][y]);
    }
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
#define For(i, a, b, c) for(int i = (a); i <= (b); i += (c))
#define Rof(i, a, b, c) for(int i = (a); i >= (b); i += (c))
#define Forn(i, n) For(i, 0, (n) - 1, +1)
#define For1(i, n) For(i, 1, (n), +1)
#define Forall(i, x) for(auto i = (x).begin(); i != (x).end(); i++)
#define size(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);

#define fst first
#define snd second
#define pb push_back
#define lower lower_bound
#define upper upper_bound
#define ENDL '\n'
using namespace std;

typedef long long lli;
typedef unsigned long long ulli;
typedef pair<int, int> ii;

template <class T>
struct rge{ T b, e; };

template <class T>
rge<T> range(T i, T j){
  return rge<T>{i, j};
};

template <class A, class B>
ostream & operator << (ostream &os, pair<A, B> p){
  return os << "(" << p.fst << ", " << p.snd << ")";
}

template <class T>
ostream & operator << (ostream &os, rge<T> d){
  os << "[";
  for(auto it = d.b; it != d.e; it++)
    os << (it == d.b ? "": ", ") << *it;
  return os << "]";
}

#define deb(x) cout << #x": " << (x) << '\n';

const int N = 105, K = 105;
const lli inf = 1e18;

lli dp[N][K][26];
lli cost[26][26];
string s;
int n, m, k;

template <class T>
void umax(T &a, T b){
  a = max(a, b);
}

int main(){ _
  cin >> s >> k;
  n = size(s);
  cin >> m;
  Forn(i, m){
    char x, y;
    lli c;
    cin >> x >> y >> c;
    cost[x - 'a'][y - 'a'] = c;
  }
  For1(i, n + 1){
    Forn(change, min(i + 1, k) + 1){
      Forn(y, 26){
        dp[i][change][y] = -inf;
      }
    }
  }
  Forn(i, n - 1){
    Forn(change, min(i + 1, k) + 1){
      Forn(x, 26){
        if( change != 0 || (!change && char('a' + x) == s[i]) ){
          Forn(y, 26){
            int add = s[i + 1] != char('a' + y);
            umax(dp[i + 1][change + add][y], dp[i][change][x] + cost[x][y]);
          }
        }
      }
    }
  }
  lli ans = 0;
  Forn(i, n - 1){
    ans += cost[s[i] - 'a'][s[i + 1] - 'a'];
  }
  Forn(change, k + 1){
    Forn(y, 26){
      umax(ans, dp[n - 1][change][y]);
    }
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
#define For(i, a, b, c) for(int i = (a); i <= (b); i += (c))
#define Rof(i, a, b, c) for(int i = (a); i >= (b); i += (c))
#define Forn(i, n) For(i, 0, (n) - 1, +1)
#define For1(i, n) For(i, 1, (n), +1)
#define Forall(i, x) for(auto i = (x).begin(); i != (x).end(); i++)
#define size(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);

#define fst first
#define snd second
#define pb push_back
#define lower lower_bound
#define upper upper_bound
#define ENDL '\n'
using namespace std;

typedef long long lli;
typedef unsigned long long ulli;
typedef pair<int, int> ii;

template <class T>
struct rge{ T b, e; };

template <class T>
rge<T> range(T i, T j){
  return rge<T>{i, j};
};

template <class A, class B>
ostream & operator << (ostream &os, pair<A, B> p){
  return os << "(" << p.fst << ", " << p.snd << ")";
}

template <class T>
ostream & operator << (ostream &os, rge<T> d){
  os << "[";
  for(auto it = d.b; it != d.e; it++)
    os << (it == d.b ? "": ", ") << *it;
  return os << "]";
}

#define deb(x) cout << #x": " << (x) << '\n';

const int N = 105, K = 105;
const lli inf = 1e18;

lli dp[N][K][26];
lli cost[26][26];
string s;
int n, m, k;

template <class T>
void umax(T &a, T b){
  a = max(a, b);
}

int main(){ _
  cin >> s >> k;
  n = size(s);
  cin >> m;
  Forn(i, m){
    char x, y;
    lli c;
    cin >> x >> y >> c;
    umax(cost[x - 'a'][y - 'a'], c);
  }
  Forn(i, n - 1){
    Forn(change, min(i + 1, k) + 1){
      Forn(x, 26){
        if( change || (!change && char('a' + x) == s[i]) ){
          Forn(y, 26){
            int add = s[i + 1] != char('a' + y);
            umax(dp[i + 1][change + add][y], dp[i][change][x] + cost[x][y]);
          }
        }
      }
    }
  }
  lli ans = -inf;
  Forn(change, k + 1){
    Forn(y, 26){
      umax(ans, dp[n - 1][change][y]);
    }
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
#define For(i, a, b, c) for(int i = (a); i <= (b); i += (c))
#define Rof(i, a, b, c) for(int i = (a); i >= (b); i += (c))
#define Forn(i, n) For(i, 0, (n) - 1, +1)
#define For1(i, n) For(i, 1, (n), +1)
#define Forall(i, x) for(auto i = (x).begin(); i != (x).end(); i++)
#define size(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);

#define fst first
#define snd second
#define pb push_back
#define lower lower_bound
#define upper upper_bound
#define ENDL '\n'
using namespace std;

typedef long long lli;
typedef unsigned long long ulli;
typedef pair<int, int> ii;

template <class T>
struct rge{ T b, e; };

template <class T>
rge<T> range(T i, T j){
  return rge<T>{i, j};
};

template <class A, class B>
ostream & operator << (ostream &os, pair<A, B> p){
  return os << "(" << p.fst << ", " << p.snd << ")";
}

template <class T>
ostream & operator << (ostream &os, rge<T> d){
  os << "[";
  for(auto it = d.b; it != d.e; it++)
    os << (it == d.b ? "": ", ") << *it;
  return os << "]";
}

#define deb(x) cout << #x": " << (x) << '\n';

const int N = 105, K = 105;
const lli inf = 1e18;

lli dp[N][K][26];
lli cost[26][26];
string s;
int n, m, k;

template <class T>
void umax(T &a, T b){
  a = max(a, b);
}

int main(){ _
  cin >> s >> k;
  n = size(s);
  cin >> m;
  Forn(i, m){
    char x, y;
    lli c;
    cin >> x >> y >> c;
    umax(cost[x - 'a'][y - 'a'], c);
  }
  Forn(i, n - 1){
    Forn(change, min(i + 1, k) + 1){
      Forn(x, 26){
        if( change || (!change && char('a' + x) == s[i]) ){
          Forn(y, 26){
            int add = s[i + 1] != char('a' + y);
            umax(dp[i + 1][change + add][y], dp[i][change][x] + cost[x][y]);
          }
        }
      }
    }
  }
  lli ans = -inf;
  For1(change, k){
    Forn(y, 26){
      umax(ans, dp[n - 1][change][y]);
    }
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>
#define For(i, a, b, c) for(int i = (a); i <= (b); i += (c))
#define Rof(i, a, b, c) for(int i = (a); i >= (b); i += (c))
#define Forn(i, n) For(i, 0, (n) - 1, +1)
#define For1(i, n) For(i, 1, (n), +1)
#define Forall(i, x) for(auto i = (x).begin(); i != (x).end(); i++)
#define size(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);

#define fst first
#define snd second
#define pb push_back
#define lower lower_bound
#define upper upper_bound
#define ENDL '\n'
using namespace std;

typedef long long lli;
typedef unsigned long long ulli;
typedef pair<int, int> ii;

template <class T>
struct rge{ T b, e; };

template <class T>
rge<T> range(T i, T j){
  return rge<T>{i, j};
};

template <class A, class B>
ostream & operator << (ostream &os, pair<A, B> p){
  return os << "(" << p.fst << ", " << p.snd << ")";
}

template <class T>
ostream & operator << (ostream &os, rge<T> d){
  os << "[";
  for(auto it = d.b; it != d.e; it++)
    os << (it == d.b ? "": ", ") << *it;
  return os << "]";
}

#define deb(x) cout << #x": " << (x) << '\n';

const int N = 105, K = 105;
const lli inf = 1e18;

lli dp[N][K][26];
lli cost[26][26];
string s;
int n, m, k;

template <class T>
void umax(T &a, T b){
  a = max(a, b);
}

int main(){ _
  cin >> s >> k;
  n = size(s);
  cin >> m;
  Forn(i, m){
    char x, y;
    lli c;
    cin >> x >> y >> c;
    umax(cost[x - 'a'][y - 'a'], c);
  }
  Forn(i, n - 1){
    Forn(change, min(i + 1, k) + 1){
      Forn(x, 26){
        if( change || (!change && char('a' + x) == s[i]) ){
          Forn(y, 26){
            int add = s[i + 1] != char('a' + y);
            umax(dp[i + 1][change + add][y], dp[i][change][x] + cost[x][y]);
          }
        }
      }
    }
  }
  lli ans = 0;
  Forn(i, n - 1){
    ans += cost[s[i] - 'a'][s[i + 1] - 'a'];
  }
  Forn(change, k + 1){
    Forn(y, 26){
      umax(ans, dp[n - 1][change][y]);
    }
  }
  cout << ans << '\n';
  return 0;
}


/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/

#include <bits/stdc++.h>
using namespace std;

#define     ff              first
#define     ss              second
#define     pb(x)           push_back(x)
#define     mp              make_pair
#define     sz(x)           x.size()
#define     mem(x ,y)       memset(x , y , sizeof x )
#define     all(a )         a.begin() , a.end()
#define     endl            "\n"
#define     pii             pair<int,int>
#define     rep(i ,n )      for(int i = 0 ; i < n ; i++ )
#define     repi(i ,n )     for(int i = 1 ; i <= n ; i++ )
#define     forn(i ,x,n )   for(int i = x ; i <= n ; i++ )
#define     ford(i ,x,n )   for(int i = x ; i >= n ; i-- )
#define     repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )
#define     dbg(args...)    do {   cerr << #args << ' ' ;  print(args); } while(0); cerr<< endl ;

typedef long long ll  ;
typedef unsigned long long  ull ;
typedef vector<int> vi  ;

const int  inf = 1e9+7  ;
const int  mod = 1e9+7;
const ll  INF = 1e17+7  ;

int setbit(int n, int pos  ) { return n = n|(1<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1<<pos))  ; }

template < typename T > T mul(T x, T y   ) { x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template < typename T > T add(T x, T y  ) { x %= mod ; y %= mod ; return (x+y)%mod ;  }
template < typename T > T modPow(T x, T k ,T mod ) { if(k == 0 ) return 1; if(k&1  ) { return ( (x%mod)*modPow(x,k-1, mod )%mod )%mod ; } else { T ret = modPow(x,k/2 , mod ) ; ret %= mod ; return (ret*ret )%mod; } }
template < typename T > T ext_gcd(T num1 , T num2, T &X , T &Y ) { T r2,r1, q2, q1 , x2, x1, y2 ,y1 , x,  y, r   ; x2 = 1; y2 = 0;  x1 = 0; y1 = 1; for( r2 =num1 ,r1 = num2 ;r1 != 0 ; y2 = y1, y1 = y,x2 = x1 ,x1 = x ,r2 = r1, r1 = r) {q1 = r2/r1; x = x2 - q1*x1; y = y2 - q1*y1; r = r2%r1 ; } X = x2 ; Y = y2 ; return r2 ;  }
template < typename T > void print(const T& v) {    cerr << v << ' ' ;}
template < typename T1, typename... T2 > void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}

template < typename T >
T  modInv( T x , T m = 1e9+7) {
    return modPow(x,m-2 , m) ;
    // long long ret, t ;//
    // ext_gcd( x, m ,ret ,t  ) ;
    // ret %= m ; if(ret<0) ret += m  ;
    // return ret;
}

// debuging ...
template<typename T >
void dbg_a(T a[] ,int n=10 ) {
    cerr << "[ "; rep(i,n )cerr<<a[i]<<' ' ; cerr<< " ]" <<  endl;
}

template < typename F,  typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "[ " << p.first << ", " << p.second << " ]";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "[ "; rep(i , sz( v)  )  os << v[i] << ' ' ; return os << " ]" ;
}

//int dx[] = {0 , 0 ,+1, -1 } ;
//int dy[] = {-1, +1,0 , 0 };

const int M = (int )2e6+5;
const int mx  = 4000+23  ;

////  code from here ...

int  n   , dp[102][102 ][26 ] ;
int cost[30][30] ;
string  s ;

int  func(int  id, int cnt, int pre )  {
    //dbg(id,cnt) ;
    if(cnt<0 ) return -inf;
    if(id<1) return 0  ;
    int &ans =dp[id][cnt][pre] ;
    if(ans!=-1)return ans  ;
    ans = -inf;
    rep(i,26) {

        ans = max( ans  , func(id-1, ((i==(s[id]-'a' ) )?cnt:cnt-1) , i  ) +cost[i][pre ] ) ;

    }

    return ans ;

}
void solve() {
    cerr<< "program running ... " <<  endl ;
    int q , x ,k;
    cin >> s >> k ;
    cin>> q ;
    rep(i, q) {
        string s1 , s2 ; cin>> s1 >>s2 >> x ;
        cost[s1[0]-'a'][s2[0]-'a' ] = x;

       // dbg( s1, s2, cost[s1[0]-'a'][s2[0]-'a' ] , x ) ;
    }
    s  = ' ' + s ;
    mem(dp,-1) ;
    //dbg(s ,k ,sz(s ) ) ;
    int ans = -inf ;
    rep(i ,26 ){
        ans = max(ans , func(sz(s)-2 , (s[sz(s)-1]-'a'==i)?k:k-1 ,  i  ) ) ;

    }
    cout << ans    << endl  ;

}

int main() {
    #ifndef ONLINE_JUDGE
       freopen("in.txt", "r", stdin);
        //    freopen("out.txt", "w", stdout);
    #endif
    ios ::sync_with_stdio(false);  cin.tie(0); cout.tie(0) ;

    int t=1 ,    c= 0 ;

    //  cin >> t;

    while(t-- ) {
        solve() ;

    }
    return 0 ;

}


/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/

#include <bits/stdc++.h>
using namespace std;

#define     ff              first
#define     ss              second
#define     pb(x)           push_back(x)
#define     mp              make_pair
#define     sz(x)           x.size()
#define     mem(x ,y)       memset(x , y , sizeof x )
#define     all(a )         a.begin() , a.end()
#define     endl            "\n"
#define     pii             pair<int,int>
#define     rep(i ,n )      for(int i = 0 ; i < n ; i++ )
#define     repi(i ,n )     for(int i = 1 ; i <= n ; i++ )
#define     forn(i ,x,n )   for(int i = x ; i <= n ; i++ )
#define     ford(i ,x,n )   for(int i = x ; i >= n ; i-- )
#define     repit(i, c)     for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )
#define     dbg(args...)    do {   cerr << #args << ' ' ;  print(args); } while(0); cerr<< endl ;

typedef long long ll  ;
typedef unsigned long long  ull ;
typedef vector<int> vi  ;

const int  inf = 1e9+7  ;
const int  mod = 1e9+7;
const ll  INF = 1e17+7  ;

int setbit(int n, int pos  ) { return n = n|(1<< pos) ; }
int resetbit(int n,int pos ) {  return n =  n & ~(1<<pos ); }
bool checkbit(int  n,int pos ) { return (bool ) (n&(1<<pos))  ; }

template < typename T > T mul(T x, T y   ) { x %= mod ; y %= mod ; return (x*y)%mod  ;  }
template < typename T > T add(T x, T y  ) { x %= mod ; y %= mod ; return (x+y)%mod ;  }
template < typename T > T modPow(T x, T k ,T mod ) { if(k == 0 ) return 1; if(k&1  ) { return ( (x%mod)*modPow(x,k-1, mod )%mod )%mod ; } else { T ret = modPow(x,k/2 , mod ) ; ret %= mod ; return (ret*ret )%mod; } }
template < typename T > T ext_gcd(T num1 , T num2, T &X , T &Y ) { T r2,r1, q2, q1 , x2, x1, y2 ,y1 , x,  y, r   ; x2 = 1; y2 = 0;  x1 = 0; y1 = 1; for( r2 =num1 ,r1 = num2 ;r1 != 0 ; y2 = y1, y1 = y,x2 = x1 ,x1 = x ,r2 = r1, r1 = r) {q1 = r2/r1; x = x2 - q1*x1; y = y2 - q1*y1; r = r2%r1 ; } X = x2 ; Y = y2 ; return r2 ;  }
template < typename T > void print(const T& v) {    cerr << v << ' ' ;}
template < typename T1, typename... T2 > void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}

template < typename T >
T  modInv( T x , T m = 1e9+7) {
    return modPow(x,m-2 , m) ;
    // long long ret, t ;//
    // ext_gcd( x, m ,ret ,t  ) ;
    // ret %= m ; if(ret<0) ret += m  ;
    // return ret;
}

// debuging ...
template<typename T >
void dbg_a(T a[] ,int n=10 ) {
    cerr << "[ "; rep(i,n )cerr<<a[i]<<' ' ; cerr<< " ]" <<  endl;
}

template < typename F,  typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "[ " << p.first << ", " << p.second << " ]";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "[ "; rep(i , sz( v)  )  os << v[i] << ' ' ; return os << " ]" ;
}

//int dx[] = {0 , 0 ,+1, -1 } ;
//int dy[] = {-1, +1,0 , 0 };

const int M = (int )2e6+5;
const int mx  = 4000+23  ;

////  code from here ...

int  n   , dp[102][102 ][26 ] ;
int cost[30][30] ;
string  s ;

int  func(int  id, int cnt, int pre )  {
    //dbg(id,cnt) ;
    if(cnt<0 ) return -inf;
    if(id<1) return 0  ;
    int &ans =dp[id][cnt][pre] ;
    if(ans!=-1)return ans  ;
    ans = 0;
    rep(i,26) {

        ans = max( ans  , func(id-1, ((i==(s[id]-'a' ) )?cnt:cnt-1) , i  ) +cost[i][pre ] ) ;

    }

    return ans ;

}
void solve() {
    cerr<< "program running ... " <<  endl ;
    int q , x ,k;
    cin >> s >> k ;
    cin>> q ;
    rep(i, q) {
        string s1 , s2 ; cin>> s1 >>s2 >> x ;
        cost[s1[0]-'a'][s2[0]-'a' ] = x;

       // dbg( s1, s2, cost[s1[0]-'a'][s2[0]-'a' ] , x ) ;
    }
    s  = ' ' + s ;
    mem(dp,-1) ;
    //dbg(s ,k ,sz(s ) ) ;
    int ans = 0 ;
    rep(i ,26 ){
        ans = max(ans , func(sz(s)-2 , (s[sz(s)-1]-'a'==i)?k:k-1 ,  i  ) ) ;

    }
    cout << ans    << endl  ;

}

int main() {
    #ifndef ONLINE_JUDGE
       freopen("in.txt", "r", stdin);
        //    freopen("out.txt", "w", stdout);
    #endif
    ios ::sync_with_stdio(false);  cin.tie(0); cout.tie(0) ;

    int t=1 ,    c= 0 ;

    //  cin >> t;

    while(t-- ) {
        solve() ;

    }
    return 0 ;

}


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ff first
#define ss second
#define eb emplace_back
#define MP make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define mset(m,v) memset(m,v,sizeof(m))
#define tr(c,it) for(auto it=c.begin();it!=c.end();it++)
#define fr(i,n) for(lli i=0;i<(n);i++)
#define inc(i,a,b) for(lli i=a;i<=b;i++)
#define dec(i,a,b) for(lli i=a;i>=b;i--)
#define fr2(i,n) for(int i=0;i<(n);i++)
#define inc2(i,a,b) for(int i=a;i<=b;i++)
#define dec2(i,a,b) for(int i=a;i>=b;i--)
#define ymin(a,b)  (a=min((a),(b)))
#define ymax(a,b)  (a=max((a),(b)))
#define sz(x) (lli)(x).size()
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr<< *it << " = " << a << endl;
	err(++it, args...);
}

typedef long long int lli;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<lli,lli> plli;
typedef vector<lli> vlli;
typedef vector<int> vi;
//typedef  vector<vector<lli> >ncr(1001,vector<lli>(1001,0))
typedef vector<plli> vplli;
long long MOD=1000000009;
#define addm(x,y) (x+y>=MOD? (x+y-MOD):(x+y))
lli prof[28][28];
lli dp[110][28][110];
string s;
lli k;
lli len;
lli func(lli ind,lli prev,lli change)
{
	if(change>k)
		return -1e15;
	if(ind>=len)
		return 0;
	if(dp[ind][prev][change]!=-1)
		return dp[ind][prev][change];

	dp[ind][prev][change]=func(ind+1,s[ind]-'a',change)+prof[prev][s[ind]-'a'];
	for(lli i=0;i<26;i++)
	{
		if(i!=s[ind]-'a')
		dp[ind][prev][change]=max(dp[ind][prev][change],func(ind+1,i,change+1)+prof[prev][i]);
	}
	return dp[ind][prev][change];
}
void solve()
{
	cin>>s>>k;
	len=s.length();
	lli q;
	cin>>q;
	mset(dp,-1);
	mset(prof,0);
	while(q--)
	{
		lli cost;
		char x,y;
		cin>>x>>y>>cost;
		prof[x-'a'][y-'a']=cost;
	}
	lli ans=func(0,27,0);
	cout<<ans<<endl;

}
int main()
{
		#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	IOS
	// clock_t start,end;
	// start=clock();
	// int t;
	// cin>>t;
	// for(int i=1;i<=t;i++)
	// {
	// 	cout<<"Case #"<<i<<": ";
	// 	solve();

	// }

	// end=clock();

	// double time_taken=double(end-start)/double(CLOCKS_PER_SEC);
	// cout<<fixed<<setprecision(10)<<time_taken<<" sec"<<endl;
	solve();

}


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ff first
#define ss second
#define eb emplace_back
#define MP make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define mset(m,v) memset(m,v,sizeof(m))
#define tr(c,it) for(auto it=c.begin();it!=c.end();it++)
#define fr(i,n) for(lli i=0;i<(n);i++)
#define inc(i,a,b) for(lli i=a;i<=b;i++)
#define dec(i,a,b) for(lli i=a;i>=b;i--)
#define fr2(i,n) for(int i=0;i<(n);i++)
#define inc2(i,a,b) for(int i=a;i<=b;i++)
#define dec2(i,a,b) for(int i=a;i>=b;i--)
#define ymin(a,b)  (a=min((a),(b)))
#define ymax(a,b)  (a=max((a),(b)))
#define sz(x) (lli)(x).size()
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr<< *it << " = " << a << endl;
	err(++it, args...);
}

typedef long long int lli;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<lli,lli> plli;
typedef vector<lli> vlli;
typedef vector<int> vi;
//typedef  vector<vector<lli> >ncr(1001,vector<lli>(1001,0))
typedef vector<plli> vplli;
long long MOD=1000000009;
#define addm(x,y) (x+y>=MOD? (x+y-MOD):(x+y))
lli prof[27][27];
lli dp[110][27][110];
string s;
lli k;
lli len;
lli func(lli ind,lli prev,lli change)
{
	if(change>k)
		return -1e15;
	if(ind>=len && change<=k)
		return 0;
	if(dp[ind][prev][change]!=-1)
		return dp[ind][prev][change];

	dp[ind][prev][change]=func(ind+1,s[ind]-'a',change)+prof[prev][s[ind]-'a'];
	for(lli i=0;i<26;i++)
	{
		if(i!=s[ind]-'a')
		dp[ind][prev][change]=max(dp[ind][prev][change],func(ind+1,i,change+1)+prof[prev][i]);
	}
	return dp[ind][prev][change];
}
void solve()
{
	cin>>s>>k;
	len=s.length();
	lli q;
	cin>>q;
	mset(dp,-1);
	mset(prof,0);
	while(q--)
	{
		lli cost;
		char x,y;
		cin>>x>>y>>cost;
		prof[x-'a'][y-'a']=cost;
	}
	lli ans=func(0,27,0);
	cout<<ans<<endl;

}
int main()
{
		#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	IOS
	// clock_t start,end;
	// start=clock();
	// int t;
	// cin>>t;
	// for(int i=1;i<=t;i++)
	// {
	// 	cout<<"Case #"<<i<<": ";
	// 	solve();

	// }

	// end=clock();

	// double time_taken=double(end-start)/double(CLOCKS_PER_SEC);
	// cout<<fixed<<setprecision(10)<<time_taken<<" sec"<<endl;
	solve();

}


// #include <bits/stdc++.h>
// using namespace std;

// #define fori(i,a,b)    for(int i=a;i<=b;i++)
// #define ford(i,a,b)   for(int i=a;i>=b;i--)
// #define all(a)        a.begin(), a.end()
// #define in(c,x)       ((c).find(x) != (c).end())
// #define cpresent(c,x) (find(all(c),x) != (c).end())
// #define pb             push_back
// #define sz(x)          (int)x.size()
// #define fill(a,v)      memset(a, v, sizeof a)
// #define fs first
// #define sc second
// #define en '\n'

// typedef long long int lli;
// typedef vector<int> vi;
// typedef vector<long long int> vlli;
// typedef pair< int, int > pii;
// typedef list<int> li;
// typedef map<int, int> mi;
// lli gcd(lli a,lli b){return (!b)?a:gcd(b,a%b);}

// int k1, n;
// int val[26][26];
// string s;
// int dp[101][101][26];

// void solve(){
//     cin>>s>>k1>>n;

//     fori(i, 1, n){
//         char x, y;
//         cin>>x>>y;

//         int x1=  x - 'a';
//         int y1=  y - 'a';

//         cin>>val[x1][y1];
//     }

//     fori(i, 0, sz(s)){
//         fori(k, 0, k1){
//             fori(c, 0, 25)
//             dp[i][k][c] = -1e8;
//         }
//     }

//     fori(i, 0, 25)
//     dp[0][1][i] = 0;

//     dp[0][0][s[0]-'a'] = 0;

//     fori(i, 0, sz(s)-2){
//         fori(k, 0, k1){
//             fori(c, 0, 25){
//                 fori(c1, 0, 25){
//                     int k2 = k;

//                     if(s[i+1]-'a' != c) k2 += 1;
// //                    if(k2 == -1) k2 = 0;

//                     if(k2 <= k1)
//                     dp[i+1][k2][c] = max(dp[i+1][k2][c], dp[i][k][c1] + val[c1][c]);
//                 }
//             }
//         }
//     }

//     int ans = -1e8;

//     fori(i, 0, 25)
//     ans = max(ans, dp[sz(s)-1][k1][i]);

//     cout<<ans;
// }

// int main(){
//     ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
//     if (fopen("input.txt", "r")) {
//         freopen("input.txt", "r", stdin);
//         freopen("output.txt", "w", stdout);
//     }

//     int t = 1;

//     while(t--){
//         solve();
//     }
// }

#include <bits/stdc++.h>
using namespace std;

#define fori(i,a,b)    for(int i=a;i<=b;i++)
#define ford(i,a,b)   for(int i=a;i>=b;i--)
#define all(a)        a.begin(), a.end()
#define in(c,x)       ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define pb             push_back
#define sz(x)          (int)x.size()
#define fill(a,v)      memset(a, v, sizeof a)
#define fs first
#define sc second
#define en '\n'

typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair< int, int > pii;
typedef list<int> li;
typedef map<int, int> mi;
lli gcd(lli a,lli b){return (!b)?a:gcd(b,a%b);}

int k1, n;
int val[26][26];
string s;
int dp[101][101][26];

void solve(){
    cin>>s>>k1>>n;

    fori(i, 1, n){
        char x, y;
        cin>>x>>y>>val[x - 'a'][y - 'a'];
    }

    int ns = sz(s);

    fori(i, 0, ns){
        fori(k, 0, k1){
            fori(c, 0, 25)
            dp[i][k][c] = -1e8;
        }
    }

    fori(i, 0, 25)
    dp[1][1][i] = 0;

    dp[1][0][s[0]-'a'] = 0;

    fori(i, 2, sz(s)){
        fori(c, 0, 25){
            int p = (s[i-1]-'a' != c);
            fori(k, p, k1){
                fori(c1, 0, 25)
                dp[i][k][c] = max(dp[i][k][c], dp[i-1][k-p][c1] + val[c1][c]);
            }
        }
    }

    int ans = -1e8;

    fori(i, 0, 25){
        fori(k, 0, k1){
            ans = max(ans, dp[sz(s)][k][i]);
        }
    }

    cout<<ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("input.txt", "r")) {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }

    int t = 1;

    while(t--){
        solve();
    }
}


// #include <bits/stdc++.h>
// using namespace std;

// #define fori(i,a,b)    for(int i=a;i<=b;i++)
// #define ford(i,a,b)   for(int i=a;i>=b;i--)
// #define all(a)        a.begin(), a.end()
// #define in(c,x)       ((c).find(x) != (c).end())
// #define cpresent(c,x) (find(all(c),x) != (c).end())
// #define pb             push_back
// #define sz(x)          (int)x.size()
// #define fill(a,v)      memset(a, v, sizeof a)
// #define fs first
// #define sc second
// #define en '\n'

// typedef long long int lli;
// typedef vector<int> vi;
// typedef vector<long long int> vlli;
// typedef pair< int, int > pii;
// typedef list<int> li;
// typedef map<int, int> mi;
// lli gcd(lli a,lli b){return (!b)?a:gcd(b,a%b);}

// int k1, n;
// int val[26][26];
// string s;
// int dp[101][101][26];

// void solve(){
//     cin>>s>>k1>>n;

//     fori(i, 1, n){
//         char x, y;
//         cin>>x>>y;

//         int x1=  x - 'a';
//         int y1=  y - 'a';

//         cin>>val[x1][y1];
//     }

//     fori(i, 0, sz(s)){
//         fori(k, 0, k1){
//             fori(c, 0, 25)
//             dp[i][k][c] = -1e8;
//         }
//     }

//     fori(i, 0, 25)
//     dp[0][1][i] = 0;

//     dp[0][0][s[0]-'a'] = 0;

//     fori(i, 0, sz(s)-2){
//         fori(k, 0, k1){
//             fori(c, 0, 25){
//                 fori(c1, 0, 25){
//                     int k2 = k;

//                     if(s[i+1]-'a' != c) k2 += 1;
// //                    if(k2 == -1) k2 = 0;

//                     if(k2 <= k1)
//                     dp[i+1][k2][c] = max(dp[i+1][k2][c], dp[i][k][c1] + val[c1][c]);
//                 }
//             }
//         }
//     }

//     int ans = -1e8;

//     fori(i, 0, 25)
//     ans = max(ans, dp[sz(s)-1][k1][i]);

//     cout<<ans;
// }

// int main(){
//     ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
//     if (fopen("input.txt", "r")) {
//         freopen("input.txt", "r", stdin);
//         freopen("output.txt", "w", stdout);
//     }

//     int t = 1;

//     while(t--){
//         solve();
//     }
// }

#include <bits/stdc++.h>
using namespace std;

#define fori(i,a,b)    for(int i=a;i<=b;i++)
#define ford(i,a,b)   for(int i=a;i>=b;i--)
#define all(a)        a.begin(), a.end()
#define in(c,x)       ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define pb             push_back
#define sz(x)          (int)x.size()
#define fill(a,v)      memset(a, v, sizeof a)
#define fs first
#define sc second
#define en '\n'

typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair< int, int > pii;
typedef list<int> li;
typedef map<int, int> mi;
lli gcd(lli a,lli b){return (!b)?a:gcd(b,a%b);}

int k1, n;
int val[26][26];
string s;
int dp[101][101][26];

void solve(){
    cin>>s>>k1>>n;

    fori(i, 1, n){
        char x, y;
        cin>>x>>y>>val[x - 'a'][y - 'a'];
    }

    int ns = sz(s);

    fori(i, 0, ns){
        fori(k, 0, k1){
            fori(c, 0, 25)
            dp[i][k][c] = -1e8;
        }
    }

    fori(i, 0, 25)
    dp[1][1][i] = 0;

    dp[1][0][s[0]-'a'] = 0;

    fori(i, 2, sz(s)){
        fori(c, 0, 25){
            int p = (s[i-1]-'a' != c);
            fori(k, p, k1){
                fori(c1, 0, 25)
                dp[i][k][c] = max(dp[i][k][c], dp[i-1][k-p][c1] + val[c1][c]);
            }
        }
    }

    int ans = -1e8;

    fori(i, 0, 25)
    ans = max(ans, dp[sz(s)][k1][i]);

    cout<<ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("input.txt", "r")) {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }

    int t = 1;

    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;

#define fori(i,a,b)    for(int i=a;i<=b;i++)
#define ford(i,a,b)   for(int i=a;i>=b;i--)
#define all(a)        a.begin(), a.end()
#define in(c,x)       ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define pb             push_back
#define sz(x)          (int)x.size()
#define fill(a,v)      memset(a, v, sizeof a)
#define fs first
#define sc second
#define en '\n'

typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair< int, int > pii;
typedef list<int> li;
typedef map<int, int> mi;
lli gcd(lli a,lli b){return (!b)?a:gcd(b,a%b);}

int k1, n;
int val[26][26];
string s;
int dp[101][101][26];

void solve(){
    cin>>s>>k1>>n;

    fori(i, 1, n){
        char x, y;
        cin>>x>>y;

        int x1=  x - 'a';
        int y1=  y - 'a';

        cin>>val[x1][y1];
    }

    fori(i, 0, sz(s)){
        fori(k, 0, k1){
            fori(c, 0, 25)
            dp[i][k][c] = -1e8;
        }
    }

    fori(i, 0, 25)
    dp[1][1][i] = 0;

    dp[1][0][s[0]-'a'] = 0;
    fori(i, 1, sz(s)){
        fori(k, 1, k1){
            fori(c, 0, 25){
                if((s[i-1]-'a') != c){
                    fori(c1, 0, 25)
                    dp[i][k][c] = max(dp[i][k][c], dp[i-1][k-1][c1] + val[c1][c]);
                } else {
                    fori(c1, 0, 25)
                    dp[i][k][c] = max(dp[i][k][c], dp[i-1][k][c1] + val[c1][c]);
                }
            }
        }
    }

    int ans = INT_MIN;

    fori(i, 0, 25){
        //cout<<i<<" "<<dp[sz(s)][k1][i]<<"\n";
        ans = max(ans, dp[sz(s)][k1][i]);
    }
    cout<<ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (fopen("input.txt", "r")) {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }

    int t = 1;

    while(t--){
        solve();
    }
}


#include "bits/stdc++.h"
using namespace std;

template <class T>
bool uax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
template <class T>
bool uin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}

const int nax = 101;
const int A = 26;
const int INF = 1e9 + 10;

int dp[nax][nax][A];
int w[A][A];

void build() {
  for (int i = 0; i < nax; ++i) {
    for (int j = 0; j < nax; ++j) {
      for (int k = 0; k < A; ++k) {
        dp[i][j][k] = -INF;
      }
    }
  }
}

int char_int(char c) { return (int)c - 'a'; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  build();
  int k;
  string s;
  cin >> s >> k;
  int n = s.length();
  uin(k, n);  // k <= n
  int qq;
  cin >> qq;
  for (int i = 0; i < qq; ++i) {
    char x, y;
    int profit;
    cin >> x >> y >> profit;
    uax(w[char_int(x)][char_int(y)], profit);
  }

  for (int i = 0; i < A; ++i) {
    if (i == char_int(s[0])) {
      dp[0][0][i] = 0;
    } else {
      dp[0][1][i] = 0;
    }
  }

  for (int x = 1; x < n; ++x) {
    for (int y = 1; y <= min(k, x + 1); ++y) {
      for (int z = 0; z < A; ++z) {
        for (int zz = 0; zz < A; ++zz) {
          if (zz == char_int(s[x])) {
            uax(dp[x][y][zz], dp[x - 1][y][z] + w[z][zz]);
          } else {
            uax(dp[x][y + 1][zz], dp[x - 1][y][z] + w[z][zz]);
          }
        }
      }
    }
  }

  int ans = -INF;
  for (int i = 0; i <= k; ++i) {
    for (int j = 0; j < A; ++j) {
      uax(ans, dp[n - 1][i][j]);
    }
  }
  cout << ans << '\n';
}


#include "bits/stdc++.h"
using namespace std;

template <class T>
bool uax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
template <class T>
bool uin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}

const int nax = 101;
const int A = 26;
const int INF = 1e9 + 10;

int dp[nax][nax][A];
int w[A][A];

void build() {
  for (int i = 0; i < nax; ++i) {
    for (int j = 0; j < nax; ++j) {
      for (int k = 0; k < A; ++k) {
        dp[i][j][k] = -INF;
      }
    }
  }
}

int char_int(char c) { return (int)c - 'a'; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  build();
  int k;
  string s;
  cin >> s >> k;
  int n = s.length();
  uin(k, n);  // k <= n
  int qq;
  cin >> qq;
  for (int i = 0; i < qq; ++i) {
    char x, y;
    int profit;
    cin >> x >> y >> profit;
    uax(w[char_int(x)][char_int(y)], profit);
  }

  for (int i = 0; i < A; ++i) {
    if (i == char_int(s[0])) {
      dp[0][0][i] = 0;
    } else {
      dp[0][1][i] = 0;
    }
  }
  for (int x = 1; s[x]; x++) {
    for (int y = 0; y <= x && y <= k; y++)
      for (int z = 0; z < 26; z++)
        for (int zz = 0; zz < 26; zz++) {
          if (zz == s[x] - 'a')
            uax(dp[x][y][zz], dp[x - 1][y][z] + w[z][zz]);
          else
            uax(dp[x][y + 1][zz], dp[x - 1][y][z] + w[z][zz]);
        }
  }
  int ans = -INF;
  for (int i = 0; i <= k; ++i) {
    for (int j = 0; j < A; ++j) {
      uax(ans, dp[n - 1][i][j]);
    }
  }
  cout << ans << '\n';
}


//Debug sometimes later
#include "bits/stdc++.h"
using namespace std;

template <class T>
bool uax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
template <class T>
bool uin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}

const int nax = 101;
const int A = 26;
const int INF = 1e9 + 10;

int dp[nax][nax][A];
int w[A][A];

void build() {
  for (int i = 0; i < nax; ++i) {
    for (int j = 0; j < nax; ++j) {
      for (int k = 0; k < A; ++k) {
        dp[i][j][k] = -INF;
      }
    }
  }
}

int char_int(char c) { return (int)c - 'a'; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  build();
  int k;
  string s;
  cin >> s >> k;
  int n = s.length();
  uin(k, n);  //k <= n
  int qq;
  cin >> qq;
  for (int i = 0; i < qq; ++i) {
    char x, y;
    int profit;
    cin >> x >> y >> profit;
    uax(w[char_int(x)][char_int(y)], profit);
    uax(w[char_int(y)][char_int(x)], profit);
  }
  int cum_profit = 0;
  for (int i = 1; i < n; ++i) {
    cum_profit += w[char_int(s[i - 1])][char_int(s[i])];
    dp[i][0][char_int(s[i])] = cum_profit;
  }

  for (int x = 1; x < n; ++x) {
    for (int y = 1; y <= min(k, x + 1); ++y) {
      for (int z = 0; z < A; ++z) {
        for (int xx = 0; xx < A; ++xx) {
          uax(dp[x][y][z], dp[x - 1][y - 1][xx] + w[z][xx]);
        }
      }
    }
  }

  int ans = cum_profit;
  for (int i = 0; i <= k; ++i) {
    for (int j = 0; j < A; ++j) {
      uax(ans, dp[n - 1][i][j]);
    }
  }
  cout << ans << '\n';
}


#include "bits/stdc++.h"
using namespace std;

template <class T>
bool uax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
template <class T>
bool uin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}

const int nax = 101;
const int A = 26;
const int INF = 1e9 + 10;

int dp[nax][nax][A];
int w[A][A];

void build() {
  for (int i = 0; i < nax; ++i) {
    for (int j = 0; j < nax; ++j) {
      for (int k = 0; k < A; ++k) {
        dp[i][j][k] = -INF;
      }
    }
  }
}

int char_int(char c) { return (int)c - 'a'; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  build();
  int k;
  string s;
  cin >> s >> k;
  int n = s.length();
  int qq;
  cin >> qq;
  for (int i = 0; i < qq; ++i) {
    char x, y;
    int profit;
    cin >> x >> y >> profit;
    w[char_int(x)][char_int(y)] = profit;
  }

  for (int i = 0; i < A; ++i) {
    if (i == char_int(s[0])) {
      dp[0][0][i] = 0;
    } else {
      dp[0][1][i] = 0;
    }
  }
  for (int x = 1; s[x]; x++) {
    for (int y = 0; y <= x && y <= k; y++) {
      for (int z = 0; z < A; z++) {
        for (int zz = 0; zz < A; zz++) {
          if (zz == char_int(s[x]))
            uax(dp[x][y][zz], dp[x - 1][y][z] + w[z][zz]);
          else
            uax(dp[x][y + 1][zz], dp[x - 1][y][z] + w[z][zz]);
        }
      }
    }
  }
  int ans = -INF;
  for (int i = 0; i <= k; ++i) {
    for (int j = 0; j < A; ++j) {
      uax(ans, dp[n - 1][i][j]);
    }
  }
  cout << ans << '\n';
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF (1<<28)
using namespace std;
// just testing

// not my solution I coudn't understand

int n,m,x,dp[105][105][30],ret,pr;
char s[105],s1[5],s2[5];
int v[300][300];

void upd(int &x, int y){
	x = max(x,y);
}

int main(){

	scanf("%s%d", s, &m);
	n = strlen(s);
	scanf("%d", &pr);
	while (pr--){
		scanf("%s%s%d", s1, s2, &x);
		v[s1[0]][s2[0]] = x;
	}

	memset(dp, -1000000, sizeof(dp));

	for (int i=0; i<=m; i++){
		for (char c='a'; c<='z'; c++){
			if (c==s[0] || i==1) dp[0][i][c-'a']=0;
		}
	}

	for (int i=1; i<n; i++){
		for (char pc='a'; pc<='z'; pc++){
			for (int j=0; j<=m; j++){
				for (char c='a'; c<='z'; c++) upd(dp[i][j+(c!=s[i])][c-'a'], dp[i-1][j][pc-'a'] + v[pc][c]);
			}
		}
	}

	ret = -INF;
	for (int j=0; j<=m; j++){
			for (char c='a'; c<='z'; c++) upd(ret, dp[n-1][j][c-'a']);
	}

	printf("%d\n",ret);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF (1<<28)
using namespace std;
// just testing

// not my solution I coudn't understand

int n,m,x,dp[105][105][30],ret,pr;
char s[105],s1[5],s2[5];
int v[300][300];

void upd(int &x, int y){
	x = max(x,y);
}

int main(){

	scanf("%s%d", s, &m);
	n = strlen(s);
	scanf("%d", &pr);
	while (pr--){
		scanf("%s%s%d", s1, s2, &x);
		v[s1[0]][s2[0]] = x;
	}

	memset(dp, -1, sizeof(dp));

	for (int i=0; i<=m; i++){
		for (char c='a'; c<='z'; c++){
			if (c==s[0] || i==1) dp[0][i][c-'a']=0;
		}
	}

	for (int i=1; i<n; i++){
		for (char pc='a'; pc<='z'; pc++){
			for (int j=0; j<=m; j++){
				for (char c='a'; c<='z'; c++) upd(dp[i][j+(c!=s[i])][c-'a'], dp[i-1][j][pc-'a'] + v[pc][c]);
			}
		}
	}

	ret = -INF;
	for (int j=0; j<=m; j++){
			for (char c='a'; c<='z'; c++) upd(ret, dp[n-1][j][c-'a']);
	}

	printf("%d\n",ret);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF (1<<28)
using namespace std;
// just testing

// not my solution I coudn't understand

int n,m,x,dp[105][105][30],ret,pr;
char s[105],s1[5],s2[5];
int v[300][300];

void upd(int &x, int y){
	x = max(x,y);
}

int main(){

	scanf("%s%d", s, &m);
	n = strlen(s);
	scanf("%d", &pr);
	while (pr--){
		scanf("%s%s%d", s1, s2, &x);
		v[s1[0]][s2[0]] = x;
	}

	memset(dp, 1000, sizeof(dp));

	for (int i=0; i<=m; i++){
		for (char c='a'; c<='z'; c++){
			if (c==s[0] || i==1) dp[0][i][c-'a']=0;
		}
	}

	for (int i=1; i<n; i++){
		for (char pc='a'; pc<='z'; pc++){
			for (int j=0; j<=m; j++){
				for (char c='a'; c<='z'; c++) upd(dp[i][j+(c!=s[i])][c-'a'], dp[i-1][j][pc-'a'] + v[pc][c]);
			}
		}
	}

	ret = -INF;
	for (int j=0; j<=m; j++){
			for (char c='a'; c<='z'; c++) upd(ret, dp[n-1][j][c-'a']);
	}

	printf("%d\n",ret);
	return 0;
}


//submitted by HimJ
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i,s,e) for(i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
const int mxn=1e5+1;
int himj[26][26];
ll dp[101][101][26];
int n;
string s;
ll solve(int i,int k,int prev)
{
    if(i==n)
        return 0;
    if(dp[i][k][prev]!=-1e16)
        return dp[i][k][prev];
    if(k==0)
        return dp[i][k][prev]=himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a');
    ll ans=-1e16;
    int j;
    fr(j,0,26)
    {
        if(s[i]-'a'==j)
            ans=max(ans,himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a'));
        else
        ans=max(ans,himj[prev][j]+solve(i+1,k-1,j));
    }
    return dp[i][k][prev]=ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  ll t=1;
  //cin>>t;
    while(t--){
            int k,i;
            ms(himj,0);
            ll temp=100;
            int j;
            fr(i,0,101)
            fr(j,0,101)
            fr(k,0,26)
            dp[i][j][k]=-1e16;
          //  cout<<dp[0][1][0]<<" ";
    cin>>s>>k;
    n=s.size();
    int m;
    cin>>m;
    while(m--)
    {
      char a,b;
      int w;
      cin>>a>>b>>w;
      himj[a-'a'][b-'a']=w;
    }
    ll ans=-1e16;
    if(k==0)
    {
    	ans=0;
        fr(i,1,n)
        ans+=himj[s[i-1]-'a'][s[i]-'a'];
    }
    else
    {
        fr(i,0,26)
    {
        if(s[0]-'a'==i)
            ans=max(ans,solve(1,k,i));
        else
            ans=max(ans,solve(1,k-1,i));
    }}
    cout<<ans;

    }
  return 0;
  }


//submitted by HimJ
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i,s,e) for(i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
const int mxn=1e5+1;
int himj[26][26];
ll dp[101][101][26];
int n;
string s;
ll solve(int i,int k,int prev)
{
    if(i==n)
        return 0;
    if(dp[i][k][prev]!=-1)
        return dp[i][k][prev];
    if(k==0)
        return dp[i][k][prev]=himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a');
    ll ans=0;
    int j;
    fr(j,0,26)
    {
        if(s[i]-'a'==j)
            ans=max(ans,himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a'));
        else
        ans=max(ans,himj[prev][j]+solve(i+1,k-1,j));
    }
    return dp[i][k][prev]=ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  ll t=1;
  //cin>>t;
    while(t--){
            int k,i;
            ms(himj,0);
            ms(dp,-1);
    cin>>s>>k;
    n=s.size();
    int m;
    cin>>m;
    while(m--)
    {
      char a,b;
      int w;
      cin>>a>>b>>w;
      himj[a-'a'][b-'a']=w;
    }
    ll ans=0;
    if(k==0)
    {
        fr(i,1,n)
        ans+=himj[s[i]-'a'][s[i-1]-'a'];
    }
    else
    {
        fr(i,0,26)
    {
        if(s[0]-'a'==i)
            ans=max(ans,solve(1,k,i));
        else
            ans=max(ans,solve(1,k-1,i));
    }}
    cout<<ans;

    }
  return 0;
  }


//submitted by HimJ
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i,s,e) for(i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
const int mxn=1e5+1;
int himj[26][26];
ll dp[101][101][26];
int n;
string s;
ll solve(int i,int k,int prev)
{
    if(i==n)
        return 0;
    if(dp[i][k][prev]!=-1e16)
        return dp[i][k][prev];
    if(k==0)
        return dp[i][k][prev]=himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a');
    ll ans=-1e16;
    int j;
    fr(j,0,26)
    {
        if(s[i]-'a'==j)
            ans=max(ans,himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a'));
        else
        ans=max(ans,himj[prev][j]+solve(i+1,k-1,j));
    }
    return dp[i][k][prev]=ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  ll t=1;
  //cin>>t;
    while(t--){
            int k,i;
            ms(himj,0);
            ll temp=100;
            int j;
            fr(i,0,101)
            fr(j,0,101)
            fr(k,0,26)
            dp[i][j][k]=-1e16;
          //  cout<<dp[0][1][0]<<" ";
    cin>>s>>k;
    n=s.size();
    int m;
    cin>>m;
    while(m--)
    {
      char a,b;
      int w;
      cin>>a>>b>>w;
      himj[a-'a'][b-'a']=w;
    }
    ll ans=-1e16;
    if(k==0)
    {
    	ans=0;
        fr(i,1,n)
        ans+=himj[s[i]-'a'][s[i-1]-'a'];
    }
    else
    {
        fr(i,0,26)
    {
        if(s[0]-'a'==i)
            ans=max(ans,solve(1,k,i));
        else
            ans=max(ans,solve(1,k-1,i));
    }}
    cout<<ans;

    }
  return 0;
  }


//submitted by HimJ
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i,s,e) for(i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
const int mxn=1e5+1;
int himj[26][26];
ll dp[101][101][26];
int n;
string s;
ll solve(int i,int k,int prev)
{
    if(i==n)
        return 0;
    if(dp[i][k][prev]!=-1e16)
        return dp[i][k][prev];
    if(k==0)
        return dp[i][k][prev]=himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a');
    ll ans=0;
    int j;
    fr(j,0,26)
    {
        if(s[i]-'a'==j)
            ans=max(ans,himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a'));
        else
        ans=max(ans,himj[prev][j]+solve(i+1,k-1,j));
    }
    return dp[i][k][prev]=ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  ll t=1;
  //cin>>t;
    while(t--){
            int k,i;
            ms(himj,0);
            ll temp=100;
            int j;
            fr(i,0,101)
            fr(j,0,101)
            fr(k,0,26)
            dp[i][j][k]=-1e16;
          //  cout<<dp[0][1][0]<<" ";
    cin>>s>>k;
    n=s.size();
    int m;
    cin>>m;
    while(m--)
    {
      char a,b;
      int w;
      cin>>a>>b>>w;
      himj[a-'a'][b-'a']=w;
    }
    ll ans=0;
    if(k==0)
    {
        fr(i,1,n)
        ans+=himj[s[i]-'a'][s[i-1]-'a'];
    }
    else
    {
        fr(i,0,26)
    {
        if(s[0]-'a'==i)
            ans=max(ans,solve(1,k,i));
        else
            ans=max(ans,solve(1,k-1,i));
    }}
    cout<<ans;

    }
  return 0;
  }


//submitted by HimJ
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i,s,e) for(i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
const int mxn=1e5+1;
int himj[26][26];
ll dp[101][101][26];
int n;
string s;
ll solve(int i,int k,int prev)
{
    if(i==n)
        return 0;
    if(dp[i][k][prev]!=-1e16)
        return dp[i][k][prev];
    if(k==0)
        return dp[i][k][prev]=himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a');
    ll ans=-1e16;
    int j;
    fr(j,0,26)
    {
        if(s[i]-'a'==j)
            ans=max(ans,himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a'));
        else
        ans=max(ans,himj[prev][j]+solve(i+1,k-1,j));
    }
    return dp[i][k][prev]=ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  ll t=1;
  //cin>>t;
    while(t--){
            int k,i;
            ms(himj,0);
            ll temp=100;
            int j;
            fr(i,0,101)
            fr(j,0,101)
            fr(k,0,26)
            dp[i][j][k]=-1e16;
          //  cout<<dp[0][1][0]<<" ";
    cin>>s>>k;
    n=s.size();
    int m;
    cin>>m;
    while(m--)
    {
      char a,b;
      int w;
      cin>>a>>b>>w;
      himj[a-'a'][b-'a']=w;
    }
    ll ans=-1e16;
    if(k==0)
    {
        fr(i,1,n)
        ans+=himj[s[i]-'a'][s[i-1]-'a'];
    }
    else
    {
        fr(i,0,26)
    {
        if(s[0]-'a'==i)
            ans=max(ans,solve(1,k,i));
        else
            ans=max(ans,solve(1,k-1,i));
    }}
    cout<<ans;

    }
  return 0;
  }


//submitted by HimJ
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
#define fr(i,s,e) for(i=s;i<e;i++)
#define ms(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
const int mxn=1e5+1;
int himj[26][26];
ll dp[101][101][26];
int n;
string s;
ll solve(int i,int k,int prev)
{
    if(i==n)
        return 0;
    if(dp[i][k][prev]!=-1)
        return dp[i][k][prev];
    if(k==0)
        return dp[i][k][prev]=himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a');
    ll ans=0;
    int j;
    fr(j,0,26)
    {
        if(s[i]-'a'==j)
            ans=max(ans,himj[prev][s[i]-'a']+solve(i+1,k,s[i]-'a'));
        else
        ans=max(ans,himj[prev][j]+solve(i+1,k-1,j));
    }
    return dp[i][k][prev]=ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  ll t=1;
  //cin>>t;
    while(t--){
            int k,i;
            ms(himj,0);
            ms(dp,-1);
    cin>>s>>k;
    n=s.size();
    int m;
    cin>>m;
    while(m--)
    {
      char a,b;
      int w;
      cin>>a>>b>>w;
      himj[a-'a'][b-'a']=w;
    }
    ll ans=0;
    fr(i,0,26)
    {
        if(s[0]-'a'==i)
            ans=max(ans,solve(1,k,i));
        else
            ans=max(ans,solve(1,k-1,i));
    }
    cout<<ans;

    }
  return 0;
  }


#include<bits/stdc++.h>
// __builtin_ctz : no of trailing zeros
#define int long long
#define ll long long
#define pp pair<int,int>
#define mod 1000000007
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define sa(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define scan() int n; cin>>n ; int a[n]; for(int i = 0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' '
using namespace std;
int max(int x, int y)
{
    return x>y ? x : y;
}
int min(int x, int y)
{
    return x<y ? x : y;
}
void _(int&a , int b)
{
    a=(a%mod+b%mod)%mod;
    a=(a+mod)%mod;
}string s;ll n;ll a[26][26];ll k;
ll dp[26][100][101];
ll dfs(ll c,ll i,ll j)
{
	if(i==n)return 0;
	if(j<0)return 0;
	if(dp[c][i][j]!=-1)return dp[c][i][j];
	int ans=a[c][s[i]-'a']+dfs(s[i]-'a',i+1,j);
	if(j)
	{
		for (int u = 0; u < 26; ++u)
		{
			if(u==s[i]-'a')continue;
			ans=max(ans,a[c][u]+dfs(u,i+1,j-1));
		}
	}
	return dp[c][i][j]=ans;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> s >> k;ll m;cin >> m;n=s.size();
    for (int i = 0; i < m; ++i)
    {
    	char c1,c2;cin >> c1 >> c2 ;ll u;cin >> u;
    	a[c1-'a'][c2-'a']=u;
    }
    memset(dp,-1,sizeof(dp));
    int ans=0;
    for (int i = 0; i < 26; ++i)
    {
    	ans=max(ans,dfs(i,1,k-(i!=(s[0]-'a'))));
    }
    cout << ans;
    }


#include<bits/stdc++.h>
// __builtin_ctz : no of trailing zeros
#define int long long
#define ll long long
#define pp pair<int,int>
#define mod 1000000007
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define sa(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define scan() int n; cin>>n ; int a[n]; for(int i = 0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' '
using namespace std;
int max(int x, int y)
{
    return x>y ? x : y;
}
int min(int x, int y)
{
    return x<y ? x : y;
}
void _(int&a , int b)
{
    a=(a%mod+b%mod)%mod;
    a=(a+mod)%mod;
}string s;ll n;ll a[26][26];ll k;
ll dp[26][100][101];
ll dfs(ll c,ll i,ll j)
{
	if(i==n)return 0;
	if(dp[c][i][j]!=-1)return dp[c][i][j];
	int ans=a[c][s[i]-'a']+dfs(s[i]-'a',i+1,j);
	if(j)
	{
		for (int u = 0; u < 26; ++u)
		{
			if(u==s[i]-'a')continue;
			ans=max(ans,a[c][u]+dfs(u,i+1,j-1));
		}
	}
	return dp[c][i][j]=ans;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> s >> k;ll m;cin >> m;n=s.size();
    for (int i = 0; i < m; ++i)
    {
    	char c1,c2;cin >> c1 >> c2 ;ll u;cin >> u;
    	a[c1-'a'][c2-'a']=u;
    }
    memset(dp,-1,sizeof(dp));
    int ans=-1e18;
    if(k)
    for (int i = 0; i < 26; ++i)
    {
    	ans=max(ans,dfs(i,1,k-(i!=(s[0]-'a'))));
    }
    else ans=max(ans,dfs(s[0]-'a',1,0));
    cout << ans;
    }


#include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

typedef int long long ll;
typedef unsigned int long long ul;
typedef long double ld;
#define mod 1000000007
#define mod1 998244353
#define pb push_back
#define mk make_pair
#define pi 3.14159265358979323846
#define vi vector<ll>
#define pii pair<ll, ll>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define alla(arr,i,n) arr+i, arr+n
#define sz(a) a.size()
#define mp map<ll,ll>
#define ump unordered_map<ll,ll>
#define mmp multimap<ll,ll>
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

//#define o_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//#define o_setpll tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set

void dbg_out(){cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b){
    return a.ss<=b.ss;
}

ll inline power(ll a, ll b, ll p){
    a %= p;
    ll ans = 1;
    while(b>0){
        if(b & 1)
            ans = (ans*a)%p;
        a = (a*a)%p;
        b >>= 1;
    }
    return ans;
}

ll inv(ll n, ll p){
    return power(n,p-2, p);
}

bool inline isPrime(ll n) {
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;
    for (ll i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
            return false;
    return true;
}

ll gcd(ll a, ll b)
{
    if(b == 0)
        return a;
    return gcd(b, a % b);
}

void solve()
{
    ll n,m,i,j,k,l,r;
    string s;
    cin>>s;
    cin>>k;
    n=sz(s);
    cin>>j;
    map<pair<ll,ll>,ll> bon;
    for(i=0;i<j;i++)
    {
        char c1,c2;
        ll val;
        cin>>c1>>c2;
        cin>>val;
        bon[{c1-'a',c2-'a'}]=val;
    }
    ll dp[n][26][k+1];
    for(i=0;i<n;i++)
    {
        for(j=0;j<26;j++)
        {
            for(l=0;l<=k;l++)
            {
                dp[i][j][l]=-1e18;
            }
        }
    }
    for(i=0;i<26;i++)
    {
        if(k>0)
        {
            if(i!=s[0]-'a') dp[0][i][1]=0;
        }
        if(i==s[0]-'a') dp[0][i][0]=0;
    }
    for(i=1;i<n;i++)
    {
        for(j=0;j<=k;j++)
        {
            for(l=0;l<26;l++)
            {
                for(r=0;r<26;r++)
                {
                    if(r==s[i]-'a')
                    {
                        dp[i][r][j]=max(dp[i-1][l][j]+bon[{l,r}],dp[i][r][j]);
                    }
                    else
                    {
                        if(j-1>=0) dp[i][r][j]=max(dp[i-1][l][j-1]+bon[{l,r}],dp[i][r][j]);
                    }
                }
            }
        }
    }
    ll mx=-1e18;
    for(j=0;j<26;j++)
    {
        //cout<<j<<" ";
        for(l=0;l<=k;l++)
        {
            //cout<<dp[n-1][j][l]<<" ";
            mx=max(mx,dp[n-1][j][l]);
        }
        //cout<<"\n";
    }
    cout<<mx<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
}


#include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

typedef int long long ll;
typedef unsigned int long long ul;
typedef long double ld;
#define mod 1000000007
#define mod1 998244353
#define pb push_back
#define mk make_pair
#define pi 3.14159265358979323846
#define vi vector<ll>
#define pii pair<ll, ll>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define alla(arr,i,n) arr+i, arr+n
#define sz(a) a.size()
#define mp map<ll,ll>
#define ump unordered_map<ll,ll>
#define mmp multimap<ll,ll>
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

//#define o_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//#define o_setpll tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set

void dbg_out(){cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b){
    return a.ss<=b.ss;
}

ll inline power(ll a, ll b, ll p){
    a %= p;
    ll ans = 1;
    while(b>0){
        if(b & 1)
            ans = (ans*a)%p;
        a = (a*a)%p;
        b >>= 1;
    }
    return ans;
}

ll inv(ll n, ll p){
    return power(n,p-2, p);
}

bool inline isPrime(ll n) {
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;
    for (ll i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
            return false;
    return true;
}

ll gcd(ll a, ll b)
{
    if(b == 0)
        return a;
    return gcd(b, a % b);
}

void solve()
{
    ll n,m,i,j,k,l,r;
    string s;
    cin>>s;
    cin>>k;
    n=sz(s);
    cin>>j;
    map<pair<ll,ll>,ll> bon;
    for(i=0;i<j;i++)
    {
        char c1,c2;
        ll val;
        cin>>c1>>c2;
        cin>>val;
        bon[{c1-'a',c2-'a'}]=val;
    }
    ll dp[n][26][k+1];
    for(i=0;i<n;i++)
    {
        for(j=0;j<26;j++)
        {
            for(l=0;l<=k;l++)
            {
                dp[i][j][l]=-1e18;
            }
        }
    }
    for(i=0;i<26;i++)
    {
        if(i!=s[0]-'a') dp[0][i][1]=0;
        if(i==s[0]-'a') dp[0][i][0]=0;
    }
    for(i=1;i<n;i++)
    {
        for(j=0;j<=k;j++)
        {
            for(l=0;l<26;l++)
            {
                for(r=0;r<26;r++)
                {
                    if(r==s[i]-'a')
                    {
                        dp[i][r][j]=max(dp[i-1][l][j]+bon[{l,r}],dp[i][r][j]);
                    }
                    else
                    {
                        if(j-1>=0) dp[i][r][j]=max(dp[i-1][l][j-1]+bon[{l,r}],dp[i][r][j]);
                    }
                }
            }
        }
    }
    ll mx=-1e18;
    for(j=0;j<26;j++)
    {
        //cout<<j<<" ";
        for(l=0;l<=k;l++)
        {
            //cout<<dp[n-1][j][l]<<" ";
            mx=max(mx,dp[n-1][j][l]);
        }
        //cout<<"\n";
    }
    cout<<mx<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace __gnu_pbds;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
//#define lc (o<<1)
//#define rc (o<<1|1)
//#include<ext/pb_ds/priority_queue.hpp>
//typedef __gnu_pbds::priority_queue<no> heap;  //��ƫ��
typedef long long ll;
typedef unsigned long long ull;
ull MOD=212370440130137957ll;
int mod=1e9+7;
ll INF=1e18;
const int maxn=2e5+10;
const int maxl=260;
const double PI=acos(-1.0);
const double eps=1e-8;
int k;
char s[maxl];
int dp[maxl][maxl][maxl];
int c[maxl][maxl];
int main()
{
	fast;cin>>s+1>>k;int n;cin>>n;while(n--)
	{
		char a,b;cin>>a>>b;cin>>c[a-'a'][b-'a'];
	}
	ms_ninf(dp);
	n=strlen(s+1);
	_for(r,1,n+1)
	{
		dp[0][r][s[r]-'a']=0;
		_for(i,2,r+1)dp[0][r][s[r]-'a']+=c[s[i-1]-'a'][s[i]-'a'];
	}
	_for(le,1,k+1)
	{
		_for(bac,0,26)dp[le][1][bac]=0;
		_for(r,2,n+1)_for(bac,0,26)
		{
			int&e=dp[le][r][bac];
		int la=le;if(bac!=s[r]-'a')--la;
		_for(i,0,26)
		e=max(e,dp[la][r-1][i]+c[i][bac]);
		}
	}
	int ans=0;
	_for(le,0,k+1)_for(bac,0,26)
	ans=max(ans,dp[le][n][bac]);
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace __gnu_pbds;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
//#define lc (o<<1)
//#define rc (o<<1|1)
//#include<ext/pb_ds/priority_queue.hpp>
//typedef __gnu_pbds::priority_queue<no> heap;  //��ƫ��
typedef long long ll;
typedef unsigned long long ull;
ull MOD=212370440130137957ll;
int mod=1e9+7;
ll INF=1e18;
const int maxn=2e5+10;
const int maxl=260;
const double PI=acos(-1.0);
const double eps=1e-8;
int k;
char s[maxl];
int dp[maxl][maxl][maxl];
int c[maxl][maxl];
int main()
{
	fast;cin>>s+1>>k;int n;cin>>n;while(n--)
	{
		char a,b;cin>>a>>b;cin>>c[a-'a'][b-'a'];
	}
	ms_ninf(dp);
	n=strlen(s+1);
	_for(le,0,k+1)_for(bac,0,26)
	{
		if(!le&&bac!=s[1]-'a')continue;
		dp[le][1][bac]=0;
	}
	_for(r,2,n+1)dp[0][r][s[r]-'a']=c[s[r-1]-'a'][s[r]-'a'];
	_for(le,1,k+1)_for(r,2,n+1)_for(bac,0,26)
	{
		int&e=dp[le][r][bac];
		int la=le;if(bac!=s[r]-'a')--la;
		_for(i,0,26)
		e=max(e,dp[la][r-1][i]+c[i][bac]);
	}
	int ans=0;
	_for(le,0,k+1)_for(bac,0,26)
	ans=max(ans,dp[le][n][bac]);
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace __gnu_pbds;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
//#define lc (o<<1)
//#define rc (o<<1|1)
//#include<ext/pb_ds/priority_queue.hpp>
//typedef __gnu_pbds::priority_queue<no> heap;  //��ƫ��
typedef long long ll;
typedef unsigned long long ull;
ull MOD=212370440130137957ll;
int mod=1e9+7;
ll INF=1e18;
const int maxn=2e5+10;
const int maxl=260;
const double PI=acos(-1.0);
const double eps=1e-8;
int k;
char s[maxl];
int dp[maxl][maxl][maxl];
int c[maxl][maxl];
int main()
{
	fast;cin>>s+1>>k;int n;cin>>n;while(n--)
	{
		char a,b;cin>>a>>b;cin>>c[a-'a'][b-'a'];
	}
	ms_ninf(dp);
	n=strlen(s+1);
	_for(r,1,n+1)
	{
		dp[0][r][s[r]-'a']=0;
		_for(i,2,r+1)dp[0][r][s[r]-'a']+=c[s[i-1]-'a'][s[i]-'a'];
	}
	_for(le,1,k+1)
	{
		_for(bac,0,26)dp[le][1][bac]=0;
		_for(r,2,n+1)_for(bac,0,26)
		{
			int&e=dp[le][r][bac];
		int la=le;if(bac!=s[r]-'a')--la;
		_for(i,0,26)
		e=max(e,dp[la][r-1][i]+c[i][bac]);
		}
	}
	int ans=-1<<30;
	_for(le,0,k+1)_for(bac,0,26)
	ans=max(ans,dp[le][n][bac]);
	cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)

ll max(ll a, ll b){
    if(a>b) return a;
    return b;}

ll min(ll a, ll b){
    if(a<b) return a;
    return b;}

#define pos first
#define val second
const ll MOD = 998244353, N = 105, CO = 680, C = 26, INF = INT_MAX, M = 1e6;

ll dp[N][N][C];
ll prf[N];
map< pair<char, char>, ll > m;

int main(){
	//FastIO;
	int n, k, q;
	string a;
	cin >> a >> k >> q;
	n = a.size();

	char x, y;
	int ct = 0;
	for(int i = 0; i < q; i++){
		cin >> x >> y >> ct;
		m[ {x, y} ] = ct;
	}

	for(int i = 1; i < n; i++){
		prf[i] = prf[i-1] + m[ {a[i-1], a[i]} ];
	}

	for(int i = 0 ; i < n; i++){
		for(int j = 0; j <= k; j++){
			for(int c = 0; c < 26; c++){
				dp[i][j][c] = -1;
			}
		}
	}

	dp[0][0][ a[0]-'a' ] = 0;
	for(int i = 1; i <= k; i++){
		for(int c = 0; c < 26; c++)dp[0][i][ c ] = 0;
	}

	ll ans = 0;
	int tj = 0;
	for(int i = 1 ; i < n; i++){
		for(int j = 0; j <= k; j++){
			for(int c = 0; c < 26; c++){
				if(a[i] != c+'a' && j == 0)continue;
				if(j == 0){
					dp[i][j][c] = prf[i];
					continue;
				}
				tj = j;
				if(a[i] != c+'a')tj--;

				for(int c2 = 0; c2 < 26; c2++){
					if( dp[i-1][tj][c2] != -1) dp[i][j][c] = max(dp[i][j][c], m[ {c2+'a', c+'a'} ] + dp[i-1][tj][c2]);
				}
			}
		}
	}

	for(int i = 0; i < 26; i++)ans = max(ans, dp[n-1][k][i]);
	cout << ans << "\n";

	/*while(1){
		int r;
		cin >> r;
		cout << " " << prf[r] << "\n";
	}*/

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)

ll max(ll a, ll b){
    if(a>b) return a;
    return b;}

ll min(ll a, ll b){
    if(a<b) return a;
    return b;}

#define pos first
#define val second
const ll MOD = 998244353, N = 105, CO = 680, C = 26, INF = 1e18, M = 1e6;

ll dp[N][N][C];
ll prf[N];
map< pair<char, char>, ll > m;

int main(){
	FastIO;
	int n, k, q;
	string a;
	cin >> a >> k >> q;
	n = a.size();

	char x, y;
	int ct = 0;
	for(int i = 0; i < q; i++){
		cin >> x >> y >> ct;
		m[ {x, y} ] = ct;
	}

	for(int i = 1; i < n; i++){
		prf[i] = prf[i-1] + m[ {a[i-1], a[i]} ];
	}

	for(int i = 0 ; i < N; i++){
		for(int j = 0; j < N; j++){
			for(int c = 0; c < 26; c++){
				dp[i][j][c] = -INF;
			}
		}
	}

	dp[0][0][ a[0]-'a' ] = 0;
	for(int i = 1; i <= k; i++){
		for(int c = 0; c < 26; c++)dp[0][i][ c ] = 0;
	}

	ll ans = -INF;
	int tj = 0;
	for(int i = 1 ; i < n; i++){
		for(int j = 0; j <= k; j++){
			for(int c = 0; c < 26; c++){
				if(a[i] != 'a'+c && j == 0)continue;
				if(j == 0){
					dp[i][j][c] = prf[i];
					continue;
				}
				tj = j;
				if(a[i] != 'a'+c)tj--;

				for(int c2 = 0; c2 < 26; c2++){
					if( dp[i-1][tj][c2] != -INF) dp[i][j][c] = max(dp[i][j][c], m[ {'a'+c2, 'a'+c} ] + dp[i-1][tj][c2]);
				}
				//if(i == n-1)ans = max(ans, dp[i][j][c]);
			}
		}
	}

	for(int i = 0; i < 26; i++)ans = max(ans, dp[n-1][k][i]);
	//ans = max(ans, prf[n-1]);
	cout << ans << "\n";

	/*while(1){
		int r;
		cin >> r;
		cout << " " << prf[r] << "\n";
	}*/

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define FastIO cin.sync_with_stdio(false),cin.tie(NULL)

ll max(ll a, ll b){
    if(a>b) return a;
    return b;}

ll min(ll a, ll b){
    if(a<b) return a;
    return b;}

#define pos first
#define val second
const ll MOD = 998244353, N = 105, CO = 680, C = 26, INF = INT_MAX, M = 1e6;

ll dp[N][N][C];
ll prf[N];
map< pair<char, char>, int > m;

int main(){
	//FastIO;
	int n, k, q;
	string a;
	cin >> a >> k >> q;
	n = a.size();

	char x, y;
	int ct = 0;
	for(int i = 0; i < q; i++){
		cin >> x >> y >> ct;
		m[ {x, y} ] = ct;
	}

	for(int i = 1; i < n; i++){
		prf[i] = prf[i-1] + m[ {a[i-1], a[i]} ];
	}

	for(int i = 0 ; i < n; i++){
		for(int j = 0; j <= k; j++){
			for(int c = 0; c < 26; c++){
				dp[i][j][c] = -1;
			}
		}
	}

	dp[0][0][ a[0]-'a' ] = 0;
	for(int i = 1; i <= k; i++){
		for(int c = 0; c < 26; c++)dp[0][i][ c ] = 0;
	}

	int ans = 0;
	int tj = 0;
	for(int i = 1 ; i < n; i++){
		for(int j = 0; j <= k; j++){
			for(int c = 0; c < 26; c++){
				if(a[i] != c+'a' && !j)continue;
				if(!j){
					dp[i][j][c] = prf[i];
					continue;
				}
				tj = j;
				if(a[i] != c+'a')tj--;

				for(int c2 = 0; c2 < 26; c2++){
					if( dp[i-1][tj][c2] != -1) dp[i][j][c] = max(dp[i][j][c], m[ {c2+'a', c+'a'} ] + dp[i-1][tj][c2]);
				}
			}
		}
	}

	for(int i = 0; i < 26; i++)ans = max(ans, dp[n-1][k][i]);
	cout << ans << "\n";

	/*while(1){
		int r;
		cin >> r;
		cout << " " << prf[r] << "\n";
	}*/

	return 0;
}