#include<bits/stdc++.h>
using namespace std;

#define pii pair<int, pair<int, int>> 

using ll = long long;
using ld = long double;

const int N = 1e5 + 69;

priority_queue<pii, vector<pii>, greater<pii>> edges;
vector<pair<int,int>> a, b;
ll ans;
int n, cnt;
int par[N], sz[N];

int fpar(int a) {
  return (a == par[a] ? a : par[a] = fpar(par[a]));
}

void merge(int a, int b) {
  int pa, pb;
  pa = fpar(a);
  pb = fpar(b);
  if(pa == pb)
    return;
  if(sz[a] > sz[b])
    swap(a, b);
  par[pa] = pb;
  sz[pb] += sz[pa];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  
  cin >> n;
  for(int i = 1; i <= n; i++)
    par[i] = i, sz[i] = 1;
  for(int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    a.emplace_back(x, i);
    b.emplace_back(y, i);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for(int i = 1; i < n; i++) {
    edges.emplace(a[i].first - a[i - 1].first, make_pair(a[i].second, a[i - 1].second));
    edges.emplace(b[i].first - b[i - 1].first, make_pair(b[i].second, b[i - 1].second));
  }
  while(cnt < n - 1) {
    pii cur = edges.top();
    edges.pop();
    if(fpar(cur.second.first) == fpar(cur.second.second))
      continue;
    merge(cur.second.first, cur.second.second);
    ans += cur.first;
    cnt++;
  }
  cout << ans << '\n';
  return 0;
}
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;

typedef pair<int, ll> p;

int main() {
	cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    vector<vector<p> > x(N, vector<p>());
    vector<bool> C(N);
    vector<ll> L(N);

    for(int i = 0; i < N - 1; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--;
        b--;
        x[a].push_back(make_pair(b, c));
        x[b].push_back(make_pair(a, c));
    }

    stack<p> s;

    int Q, K;
    cin >> Q >> K;
    K--;

    s.push(make_pair(K, 0));

    while(!s.empty()) {
        p y = s.top();
        s.pop();
        int n = y.first;
        ll l = y.second;

        C[n] = true;
        L[n] = l;

        for(int i = 0; i < x[n].size(); i++) {
            if(C[x[n][i].first]) continue;
            s.push(make_pair(x[n][i].first, l + x[n][i].second));
        }
    }

    vector<int> a(Q);
    vector<int> b(Q);

    for(int i = 0; i < Q; i++) cin >> a[i] >> b[i];

    for(int i = 0; i < Q; i++) {
        a[i]--;
        b[i]--;
        cout << L[a[i]] + L[b[i]] << endl;
    }
}