#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  int ss = ((int)(v).size());
  for (int(i) = 0; (i) < (ss); ++(i)) out << v[i] << " ";
  return out;
}
template <typename T, typename TT>
ostream& operator<<(ostream& out, const pair<T, TT>& p) {
  out << p.first << " " << p.second;
  return out;
}
template <typename T, typename TT>
istream& operator>>(istream& in, pair<T, TT>& s) {
  in >> s.first >> s.second;
  return in;
}
template <typename T>
bitset<sizeof(T) * 8> BIN(T a) {
  return bitset<sizeof(T) * 8>(a);
}
vector<string> words[2];
char buf[1000000];
struct Node {
  Node* adj[26];
  vector<int> words[2];
  Node() { memset(adj, 0, sizeof adj); }
};
Node nodes[1000000];
int lastNode = 1;
void ins(Node* node, int now, int ind, int which) {
  if (buf[now] == 0) {
    node->words[which].push_back(ind);
    return;
  }
  if (node->adj[buf[now] - 'a'] == 0) {
    node->adj[buf[now] - 'a'] = &nodes[lastNode];
    lastNode++;
  }
  ins(node->adj[buf[now] - 'a'], now + 1, ind, which);
}
vector<pair<int, int> > out;
long long dfs(Node* node, int dep, vector<int> car[2]) {
  long long ans = 0;
  vector<int> mycar[2];
  for (int i = 'a'; i <= 'z'; i++) {
    if (node->adj[i - 'a']) {
      ans += dfs(node->adj[i - 'a'], dep + 1, mycar);
    }
  }
  for (int j = 0; j < 2; j++) {
    int s = ((int)(node->words[j]).size());
    for (int i = 0; i < s; i++) {
      mycar[j].push_back(node->words[j][i]);
    }
  }
  int mn = min(((int)(mycar[0]).size()), ((int)(mycar[1]).size()));
  ans += mn * dep;
  for (int i = 0; i < mn; i++) {
    out.push_back(make_pair(mycar[0][i], mycar[1][i]));
  }
  for (int j = 0; j < 2; j++) {
    int s = ((int)(mycar[j]).size());
    for (int i = mn; i < s; i++) {
      car[j].push_back(mycar[j][i]);
    }
  }
  return ans;
}
int main() {
  int n;
  scanf("%d", &n);
  Node* head = &nodes[0];
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < n; i++) {
      scanf("%s", buf);
      ins(head, 0, i, j);
    }
  }
  vector<int> temp[2];
  cout << dfs(head, 0, temp) << endl;
  for (auto it : out) {
    cout << it.first + 1 << " " << it.second + 1 << endl;
  }
  return 0;
}
