#include <bits/stdc++.h>
using namespace std;
const int maxn = 28, inf = 1e9 + 7;
int dis[maxn][maxn], n;
vector<pair<int, int>> graph[maxn];
string s1, s2, ans;
long long anss;
bool mark[maxn];
queue<int> q;
void SPFA(int s) {
  dis[s][s] = 0;
  q.push(s);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    mark[x] = 0;
    for (auto u : graph[x])
      if (dis[s][u.first] > dis[s][x] + u.second) {
        dis[s][u.first] = dis[s][x] + u.second;
        if (!mark[u.first]) q.push(u.first), mark[u.first] = 1;
      }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (int i = 0; i < maxn; i++)
    for (int j = 0; j < maxn; j++) dis[i][j] = inf;
  cin >> s1 >> s2 >> n;
  for (int i = 0, w; i < n; i++) {
    char u, v;
    cin >> u >> v >> w;
    graph[(int)u - 'a'].push_back({(int)v - 'a', w});
  }
  for (int i = 0; i < 26; i++) SPFA(i);
  if (s1.size() != s2.size()) return cout << -1 << endl, 0;
  for (int i = 0; i < s1.size(); i++) {
    long long mn = inf, h = 27;
    for (int j = 0; j < 26; j++)
      if (mn > dis[(int)s1[i] - 'a'][j] + dis[(int)s2[i] - 'a'][j])
        h = j, mn = dis[(int)s1[i] - 'a'][j] + dis[(int)s2[i] - 'a'][j];
    if (mn == inf) return cout << -1 << endl, 0;
    anss += mn;
    ans += (char)h + 'a';
  }
  cout << anss << endl << ans << endl;
}
