#include <bits/stdc++.h>
using namespace std;
int mp[900001][26], sm, cnt = 1, n;
vector<int> lea[900001];
bool used[900001];
vector<pair<int, int> > ans;
void dfs(int k, int depth) {
  for (int i = 0; i < 26; i++)
    if (mp[k][i]) dfs(mp[k][i], depth + 1);
  vector<int> v1, v2;
  for (int i = 0; i < (int)lea[k].size(); i++)
    if (!used[lea[k][i]]) {
      if (lea[k][i] <= n)
        v1.push_back(lea[k][i]);
      else
        v2.push_back(lea[k][i]);
    }
  int tmp = (int)min(v1.size(), v2.size());
  for (int i = 0; i < tmp; i++) {
    sm += depth;
    ans.push_back(pair<int, int>(v1[i], v2[i]));
    used[v1[i]] = used[v2[i]] = true;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= 2 * n; i++) {
    string s;
    cin >> s;
    int cur = 1;
    lea[cur].push_back(i);
    for (int j = 0; j < (int)s.size(); j++) {
      if (!mp[cur][s[j] - 'a']) mp[cur][s[j] - 'a'] = ++cnt;
      cur = mp[cur][s[j] - 'a'];
      lea[cur].push_back(i);
    }
  }
  dfs(1, 0);
  cout << sm << endl;
  for (int i = 0; i < (int)ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second - n << endl;
  return 0;
}
