#include <bits/stdc++.h>
using namespace std;
int n;
string a, b;
int d[30][30];
string resp;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> a >> b >> n;
  for (int i = 0; i < 26; i++)
    for (int j = 0; j < 26; j++)
      if (i != j) d[i][j] = 1e8;
  for (int i = 1; i <= n; i++) {
    char A, B;
    int c;
    cin >> A >> B >> c;
    int u = A - 'a', v = B - 'a';
    d[u][v] = min(d[u][v], c);
  }
  if (a.size() != b.size()) {
    cout << "-1" << '\n';
    return 0;
  }
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < 26; i++)
      for (int j = 0; j < 26; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  int ans = 0;
  for (int i = 0; i < a.size(); i++) {
    int x = a[i] - 'a', y = b[i] - 'a';
    int menor = 1e8, id = -1;
    for (int c = 0; c < 26; c++)
      if (d[x][c] != 1e8 && d[y][c] != 1e8 && d[x][c] + d[y][c] < menor)
        menor = d[x][c] + d[y][c], id = c;
    if (id == -1) {
      cout << "-1" << '\n';
      return 0;
    }
    ans += menor;
    resp.push_back(char(id + 'a'));
  }
  cout << ans << '\n' << resp << '\n';
  return 0;
}
