#include <bits/stdc++.h>
using namespace std;
map<int, long long> mp;
set<int> st;
vector<pair<long long, long long> > v[150];
set<int>::iterator P;
long long A[200001], n, m, i, j, t, k, w, ans;
string s, s1;
vector<pair<char, long long> > graph[501];
long long vis[150], vis1[150], sul[150][150];
char zz, z[150][150];
void update(char x, int k, char y) {
  vis1[x] = true;
  for (int j = 0; j < graph[x].size(); j++)
    if (!vis1[graph[x][j].first]) {
      for (int q = 0; q < v[y].size(); q++)
        if (v[y][q].first == graph[x][j].first &&
            v[y][q].second > k + graph[x][j].second) {
          v[y][q].second = k + graph[x][j].second;
          int vis2[150];
          for (int c = 'a'; c <= 'z'; c++) vis2[c] = vis1[c];
          memset(vis1, 0, sizeof(vis1));
          update(graph[x][j].first, k + graph[x][j].second, y);
          for (int c = 'a'; c <= 'z'; c++) vis1[c] = vis2[c];
        }
    }
}
void AllElement(char x, long long cost, char y) {
  vis[x] = true;
  for (int j = 0; j < graph[x].size(); j++)
    if (!vis[graph[x][j].first]) {
      v[y].push_back({graph[x][j].first, cost + graph[x][j].second});
      AllElement(graph[x][j].first, cost + graph[x][j].second, y);
    } else {
      for (int q = 0; q < v[y].size(); q++)
        if (v[y][q].first == graph[x][j].first &&
            v[y][q].second > cost + graph[x][j].second) {
          v[y][q].second = cost + graph[x][j].second;
          memset(vis1, 0, sizeof(vis1));
          update(v[y][q].first, v[y][q].second, y);
        }
    }
}
void short_path(char x, char y) {
  memset(vis, 0, sizeof(vis));
  AllElement(y, (long long)0, y);
  memset(vis, 0, sizeof(vis));
  AllElement(x, (long long)0, x);
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i].first == y && k > v[x][i].second) k = v[x][i].second, zz = y;
  for (int i = 0; i < v[y].size(); i++)
    if (v[y][i].first == x && k > v[y][i].second) k = v[y][i].second, zz = x;
  for (int i = 0; i < v[x].size(); i++)
    for (int j = 0; j < v[y].size(); j++)
      if (v[x][i].first == v[y][j].first) {
        int xx = v[x][i].second + v[y][j].second;
        if (k > xx) k = xx, zz = v[x][i].first;
      }
  v[y].clear();
  v[x].clear();
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  string s, t;
  char x, y;
  cin >> s >> t >> n;
  for (i = 0; i < n; i++) {
    bool l = false;
    cin >> x >> y >> w;
    if (x == y) continue;
    for (j = 0; j < graph[x].size(); j++)
      if (graph[x][j].first == y)
        graph[x][j].second = min(w, graph[x][j].second), l = true;
    if (!l) graph[x].push_back({y, w});
  }
  if (s.size() != t.size()) return cout << -1 << endl, 0;
  memset(sul, -1, sizeof(sul));
  for (i = 0; i < s.size(); i++)
    if (s[i] == t[i])
      continue;
    else {
      if (sul[t[i]][s[i]] != -1) swap(s[i], t[i]);
      if (sul[s[i]][t[i]] == -1) {
        k = 1e18;
        short_path(s[i], t[i]);
        sul[s[i]][t[i]] = k;
        z[s[i]][t[i]] = zz;
      }
      if (sul[s[i]][t[i]] == 1e18) return cout << -1, 0;
      ans += sul[s[i]][t[i]];
      s[i] = z[s[i]][t[i]];
    }
  cout << ans << endl << s;
  return 0;
}
