#include <bits/stdc++.h>
using namespace std;
const int N = 26, M = 1e5 + 10, INF = 1e9 + 100;
int adj[N][N];
int main() {
  string s, t;
  cin >> s >> t;
  int m;
  cin >> m;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) adj[i][j] = INF;
  for (int i = 0; i < N; i++) adj[i][i] = 0;
  for (int i = 0; i < m; i++) {
    char a, b;
    cin >> a >> b;
    int w;
    cin >> w;
    adj[int(a - 'a')][int(b - 'a')] = min(adj[int(a - 'a')][int(b - 'a')], w);
  }
  if (s.size() != t.size()) {
    cout << -1 << endl;
    return 0;
  }
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
  int cst = 0;
  for (int i = 0; i < s.size(); i++) {
    int i1 = s[i] - 'a', i2 = t[i] - 'a';
    int mn = INF;
    char c;
    for (int j = 0; j < 26; j++) {
      if (adj[i1][j] >= INF or adj[i2][j] >= INF) continue;
      if (mn > adj[i1][j] + adj[i2][j]) {
        mn = adj[i1][j] + adj[i2][j];
        c = char(j) + 'a';
      }
    }
    if (mn >= INF) {
      cout << -1 << endl;
      return 0;
    }
    s[i] = c;
    cst += mn;
  }
  cout << cst << endl << s << endl;
}
