#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
vector<string> svi[2];
struct node {
  char c;
  node* parent;
  node* child[26];
  vector<int> col[2];
  node() {}
  node(char _c, node* _parent) {
    c = _c;
    parent = _parent;
    for (int i = 0; i < 26; i++) {
      child[i] = nullptr;
    }
  }
};
node root('\0', nullptr);
vector<int> v;
bitset<maxn> bio[2];
vector<pair<int, int> > sol;
long long solution;
void dfs(node* x, int dep) {
  for (int i = 0; i < 26; i++) {
    if (x->child[i] != nullptr) {
      dfs(x->child[i], dep + 1);
    }
  }
  while (!x->col[0].empty() && !x->col[1].empty()) {
    if (bio[0][x->col[0].back()]) {
      x->col[0].pop_back();
    } else if (bio[1][x->col[1].back()]) {
      x->col[1].pop_back();
    } else {
      solution += dep;
      sol.push_back(make_pair(x->col[0].back(), x->col[1].back()));
      bio[0][x->col[0].back()] = 1;
      bio[1][x->col[1].back()] = 1;
      x->col[0].pop_back();
      x->col[1].pop_back();
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s;
  for (int k = 0; k < 2; k++) {
    for (int i = 0; i < n; i++) {
      cin >> s;
      svi[k].push_back(s);
      node* curr = &root;
      for (int j = 0; j < s.size(); j++) {
        curr->col[k].push_back(i);
        if (curr->child[s[j] - 'a'] == nullptr) {
          curr->child[s[j] - 'a'] = new node(s[j], curr);
        }
        curr = curr->child[s[j] - 'a'];
      }
      curr->col[k].push_back(i);
    }
  }
  dfs(&root, 0);
  cout << solution << '\n';
  for (int i = 0; i < n; i++) {
    cout << sol[i].first + 1 << " " << sol[i].second + 1 << '\n';
  }
  return 0;
}
