You are given a tree with N vertices and N-1 edges. Each edge connects two vertices. Define a function f(L, R) as the number of connected components in a subgraph formed from the vertex set S and edges whose endpoints belong to S. Compute the sum of f(L, R) for all pairs (L, R) where 1 ≤ L ≤ R ≤ N. Constraints: 1 ≤ N ≤ 2*10^5, 1 ≤ u_i, v_i ≤ N, and the given graph is a tree. Input format: N, followed by N-1 lines of u_i and v_i. Output the sum of f(L, R) for all pairs (L, R). Example: Input 3, then 1 3, 2 3. Output 7, as there are 6 possible pairs (L, R) with their respective connected components.

# Buggy version:
def main() {
    n = int(input())
    ans = n * (n + 1) * (n + 2) // 6;
    for i in range(n):
        x, y = map(int, input().split())
        if (x > y) { y, x = x, y; }
        ans -= x * (n - y + 1);
    }
    print(ans);
}
main()

# Fixed version:
def main():
    n = int(input())
    ans = n * (n + 1) * (n + 2) // 6
    
    for i in range(n-1):
        x, y = map(int, input().split())
        if x > y:
            y, x = x, y
        ans -= x * (n - y + 1)
    print(ans)
    
main()

# Buggy version:
n = int(input())
ans = n*(n+1)*(n+2)//6
for i in range(n-1):
  L,R = map(int,input().split())
  ans -= L*(n-R+1)
print(ans)

# Fixed version:
n = int(input())
ans = n*(n+1)*(n+2)//6

for i in range(n-1):
  L,R = map(int,input().split())
  if L>R:L,R=R,L
  ans -= L*(n-R+1)
print(ans)

# Buggy version:
N = int(input())
V = 0
E = 0
for i in range(N+1):
    V += i * (N-i+1)
for i in range(N):
    a, b = map(int, input().split())
    if a > b:
        a, b = b, a
    E += a * (N-b+1)
ans = V - E
print(ans)


# Fixed version:
N = int(input())
V = 0
E = 0
for i in range(N+1):
    V += i * (N-i+1)
for i in range(N-1):
    a, b = map(int, input().split())
    if a > b:
        a, b = b, a
    E += a * (N-b+1)
ans = V - E
print(ans)

# Buggy version:
n = int(input())

ans = 0

for i in range(1,n+1): #頂点数の総和を求める
    ans += i*(n-i+1)
    print(ans)

for _ in range(n-1): #各辺についてu*(n-v+1)を頂点数の総和から引く
    u,v = map(int, input().split())
    if u > v: #上記ではu<vを仮定しているのでu>vのときuとvを入れ替える
        u,v = v,u
    ans -= u*(n-v+1)
    print(ans)
print(ans)

# Fixed version:
# coding: utf-8

n = int(input())

ans = 0

for i in range(1,n+1): #頂点数の総和を求める
    ans += i*(n-i+1)
    #print(ans)

for _ in range(n-1): #各辺についてu*(n-v+1)を頂点数の総和から引く
    u,v = map(int, input().split())
    if u > v: #上記ではu<vを仮定しているのでu>vのときuとvを入れ替える
        u,v = v,u
    ans -= u*(n-v+1)
    #print(ans)
print(ans)

# Buggy version:
n = int(input())
ans = n*(n+1)*(n+2)//6
for i in range(n):
  a, b = map(int, input.split())
  ans -= a*(n-b+1)
print(ans)

# Fixed version:
n = int(input())
ans = n*(n+1)*(n+2)//6
for i in range(n-1):
  a, b = map(int, input().split())
  if b < a:
    a, b = b, a
  ans -= a*(n-b+1)
print(ans)

# Buggy version:
N = int(input())
es = [[] for _ in range(N)]
H = []
for i in range(N-1):
  a,b = map(int,input().split())
  if a > b:
    a,b = b,a
  a-=1;b-=1
  #es[a].append(b)
  #es[b].append(a)
  H.append([a,b])
print(es)

ten = N*(N+1)*(N+2)//6
hen = 0
for i in range(N-1):
  p,q = H[i]
  hen += (p+1)*(N-1-q+1)
ans = ten -hen
print(ans)

# Fixed version:
N = int(input())
es = [[] for _ in range(N)]
H = []
for i in range(N-1):
  a,b = map(int,input().split())
  if a > b:
    a,b = b,a
  a-=1;b-=1
  #es[a].append(b)
  #es[b].append(a)
  H.append([a,b])

ten = N*(N+1)*(N+2)//6
hen = 0
for i in range(N-1):
  p,q = H[i]
  hen += (p+1)*(N-1-q+1)
ans = ten -hen
print(ans)

# Buggy version:
# Fast IO (only use in integer input) or take care about string

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

N = int(input())

Ans = 0
for i in range(N):
  Ans += (N-i) * (i+1)
for _ in range(N-1):
  a,b = map(int,input().split())
  if a > b:
    c = a
    a = b
    b = c
  ans -= a * (n - b + 1)
print(ans)

# Fixed version:
# Fast IO (only use in integer input) or take care about string
 
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
 
N = int(input())
 
ans = 0
for i in range(N):
  ans += (N-i) * (i+1)
for _ in range(N-1):
  a,b = map(int,input().split())
  if a > b:
    c = a
    a = b
    b = c
  ans -= a * (N - b + 1)
print(ans)

# Buggy version:
n=int(input())
ans=n*(n+1)*(n+2)//6
for i in range(n-1):
    u=map(int,input().split())
    ans-=min(u)*(n-max(u)+1)
print(ans)

# Fixed version:
n=int(input())
ans=n*(n+1)*(n+2)//6
for i in range(n-1):
    u=list(map(int,input().split()))
    ans-=min(u)*(n-max(u)+1)
print(ans)

# Buggy version:
n=int(input())
ans=0
for i in range(1,n+1):
    ans += i*(i+1)/2
for i in range(n-1):
    u,v=map(int,input().split())
    if u > v:
        u,v=v,u
    ans -= u*(n-v+1)
print(ans)

# Fixed version:
n = int(input())
ans = n*(n+1)*(n+2)//6
for i in range(n-1):
    u,v = map(int,input().split())
    if u > v:
        u,v = v,u
    ans -= u*(n-v+1)
print(ans)