#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define int long long
#define PI 3.1415926535897932384626
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;

const int M=1000000007;

void solution()
{
    int i, j, k, n, m;
    cin>>n;

    pi arr[n];
    for(int i=0;i<n;i++) cin>>arr[i].S>>arr[i].F;

    sort(arr,arr+n);
    vector<pi>pos;
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && arr[j].F==arr[i].F) j++;
        j--;
        pos.pb({arr[i].S,arr[j].S});
        i=j+1;
    }

    int ans=0;
    m=pos.size();
    int dp[m][2];
    dp[0][0]=abs(pos[0].F);
    dp[0][1]=abs(pos[0].S);
    for(int i=1;i<m;i++)
    {
        dp[i][0]=min(abs(pos[i].F-pos[i-1].F)+dp[i-1][1],abs(pos[i].F-pos[i-1].S)+dp[i-1][0]);
        dp[i][1]=min(abs(pos[i].S-pos[i-1].F)+dp[i-1][1],abs(pos[i].S-pos[i-1].S)+dp[i-1][0]);
    }

    ans=min(abs(pos[m-1].F)+dp[m-1][1],abs(pos[m-1].S)+dp[m-1][0]);
    for(pi p:pos) ans+=abs(p.F-p.S);
    cout<<ans;
}

signed main()
{
    int t=1;
    //cin>>t;

    while(t--)
    {
        solution();
    }

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

typedef long long ll;
const int N = 2e5+7;
ll seg[N<<2],tag[N<<2];
int fa[N],dep[N],siz[N],son[N],top[N],dfn[N],rk[N], w[N];
int a[N], b[N];
int n,m,root;
int tot=0;
vector<int> G[N];
void pushDown(int node,int t){
    if(tag[node]!=0){
        tag[node<<1]+=tag[node];
        tag[node<<1|1]+=tag[node];
        seg[node<<1]=(seg[node<<1]+tag[node]*(t-(t>>1)));
        seg[node<<1|1]=(seg[node<<1|1]+tag[node]*(t>>1));
        tag[node]=0;
    }
    return ;
}
void dfs1(int u,int f,int dp){
    fa[u]=f;
	dep[u]=dp;
	siz[u]=1;
	son[u]=-1;
	for(size_t i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(v==fa[u]) continue;
		dfs1(v,u,dp+1);
		siz[u]+=siz[v];
		if(son[u]==-1 || siz[v]>siz[son[u]]){
			son[u]=v;
		}
	}
}
void dfs2(int u,int tp){
	top[u]=tp;
	tot++;
	dfn[u]=tot;
	rk[tot]=u;
	//rk[tot]=w[u];
	if(son[u]==-1) return ;
	dfs2(son[u],tp);
	for(size_t i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(v==fa[u] || v==son[u]) continue;
		dfs2(v,v);
	}
}
void build(int node,int l,int r){
	if(l==r){
		//seg[node]=w[rk[l]];
        seg[node] = 0;
		//seg[node]=rk[l]%mod;
		return ;
	}
	int mid=(l+r)>>1;
	build(node<<1,l,mid);
	build(node<<1|1,mid+1,r);
	seg[node]=(seg[node<<1]+seg[node<<1|1]);
}
void update(int node,int l,int r,int ul,int ur,ll val){
    if(r<ul || l>ur)
        return ;
    if(l>=ul && r<=ur){
        tag[node]+=val;
        seg[node]+=val*(r-l+1);
        return ;
    }
    pushDown(node,r-l+1);
    int mid=(l+r)>>1;
    update(node<<1,l,mid,ul,ur,val);
    update(node<<1|1,mid+1,r,ul,ur,val);
    seg[node]=(seg[node<<1]+seg[node<<1|1]);
}
void update1(int u,int v,ll val){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]]) swap(u,v);
		update(1,1,n,dfn[top[u]],dfn[u],val);
		u=fa[top[u]];
	}
	if(dep[u]>dep[v]) swap(u,v);
	update(1,1,n,dfn[u],dfn[v],val);
}
void update2(int u,ll val){
	update(1,1,n,dfn[u],dfn[u]+siz[u]-1,val);
}
ll query(int node,int l,int r,int ql,int qr){
	if(ql>r || qr<l) return 0;
	if(ql<=l && qr>=r) return seg[node];
	pushDown(node,r-l+1);
	int mid=(l+r)>>1;
	return (query(node<<1,l,mid,ql,qr)+query(node<<1|1,mid+1,r,ql,qr));
}
ll query1(int u,int v){
	ll res=0;
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]]) swap(u,v);
		res=(res+query(1,1,n,dfn[top[u]],dfn[u]));
		u=fa[top[u]];
	}
	if(dep[u]>dep[v]) swap(u,v);
	res=(res+query(1,1,n,dfn[u],dfn[v]));
	return res;
}
ll query2(int u){
	return query(1,1,n,dfn[u],dfn[u]+siz[u]-1);
}
int main(){
	#ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
	scanf("%d", &n);
	//for(int i=1;i<=n;i++) scanf("%d", &w[i]);
	for(int i=1;i<n;i++){
		/* int u,v;
		scanf("%d %d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u); */
        scanf("%d %d", &a[i], &b[i]);
        G[a[i]].push_back(b[i]);
        G[b[i]].push_back(a[i]);
	}
    root = 1;
	dfs1(root,root,1);
	dfs2(root,root);
	build(1,1,n);

    scanf("%d", &m);
    int t, e;
    ll x, sum = 0;
	while(m--){
		//int op,u,v,val;
		//scanf("%d", &op);
        scanf("%d %d %lld", &t, &e, &x);
		/* if(op==1){
			scanf("%d %d %d", &u, &v, &val);
			update1(u,v,val);
		}else if(op==2){
			scanf("%d %d", &u, &v);
			printf("%d\n", query1(u,v));
		}else if(op==3){
			scanf("%d %d", &u, &val);
			update2(u,val);
		}else {
			scanf("%d", &u);
			printf("%d\n", query2(u));
		} */
        if(dep[b[e]] > dep[a[e]]) {
            if(t == 1) {
                update2(b[e], -x);
                sum += x;
            }else update2(b[e], x);
        }else {
            if(t == 2) {
                update2(a[e], -x);
                sum += x;
            }else update2(a[e], x);
        }
	}
    for(int i = 1; i <= n; i ++) {
        printf("%lld\n", query(1, 1, n, dfn[i], dfn[i]) + sum);
    }
 	return 0;
}
