#include <bits/stdc++.h>
using namespace std;
const long long ALP = 26;
const long long INF = 1e14;
long long dist[ALP][ALP];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (long long i = 0; i < ALP; ++i)
    for (long long j = 0; j < ALP; ++j) dist[i][j] = INF;
  for (long long i = 0; i < ALP; ++i) dist[i][i] = 0;
  string s, t;
  cin >> s >> t;
  if (s.size() != t.size()) {
    cout << "-1\n";
    return 0;
  }
  long long m;
  cin >> m;
  while (m--) {
    char a, b;
    cin >> a >> b;
    a -= 'a';
    b -= 'a';
    long long w;
    cin >> w;
    dist[a][b] = min(dist[a][b], w);
  }
  for (long long k = 0; k < ALP; ++k)
    for (long long i = 0; i < ALP; ++i)
      for (long long j = 0; j < ALP; ++j)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  long long n = s.size(), ans = 0;
  for (long long i = 0; i < n; ++i) {
    long long ha = INF, id = 0;
    for (long long j = 0; j < 26; ++j)
      if (ha > dist[s[i] - 'a'][j] + dist[t[i] - 'a'][j]) {
        ha = dist[s[i] - 'a'][j] + dist[t[i] - 'a'][j];
        id = j;
      }
    ans += ha;
    s[i] = char(id + 'a');
  }
  if (ans >= INF)
    cout << "-1\n";
  else
    cout << ans << "\n" << s << "\n";
  return 0;
}
