#include <bits/stdc++.h>
using namespace std;
const int N = 30;
const int inf = 1e6;
int dist[N][N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) dist[i][j] = i == j ? 0 : inf;
  string a, b;
  cin >> a >> b;
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    char u, v;
    cin >> u >> v;
    int w;
    cin >> w;
    dist[u - 'a'][v - 'a'] = min(dist[u - 'a'][v - 'a'], w);
  }
  for (int k = 0; k < 26; k++) {
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < 26; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  if (a.length() != b.length()) {
    cout << -1;
    exit(0);
  }
  string ans = "";
  int sum = 0;
  int n = a.length();
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      ans += a[i];
      continue;
    }
    int u = a[i] - 'a';
    int v = b[i] - 'a';
    int mn = inf;
    for (int j = 0; j < 26; j++) {
      mn = min(mn, dist[u][j] + dist[v][j]);
    }
    if (mn >= inf) {
      cout << -1;
      exit(0);
    }
    for (int j = 0; j < 26; j++) {
      if (dist[u][j] + dist[v][j] == mn) {
        ans += (char)(j + 'a');
        sum += mn;
        break;
      }
    }
  }
  cout << sum << "\n";
  cout << ans;
  return 0;
}
