Under construction

Still being built. Things may change

Handong the Salesman!

Time limit1sMemory limit128 MB

Summary
Given a tree, start at node 1 and visit m listed nodes in order, summing the tree distances between consecutive stops.
Level

Medium4 of 10

Topics
Graph, Tree, DFS, Prefix sum
Solved
No attempts yet

Problem

Handong is a hardworking salesman who travels around the region. In the past he could pick and visit only the cities he wanted, but times have changed and he can no longer do that. Now his company hands him a list of cities to visit, and he must visit them one by one in exactly the order given. Unfortunately, the order the company provides is not optimized at all. Before setting out, Handong wants to know how much time it takes to visit every city in this order.

The cities in Pohang are numbered from 11 to nn. Handong always starts his trip at city 11, the Pohang City Hall. Every pair of directly connected cities is joined by a two-way road, and moving between two directly connected cities always takes 11 unit of time. Every city is reachable from city 11, and the roads never form a cycle; in other words, the nn cities and n−1n-1 roads form a tree.

Handong starts at city 11 and visits the listed cities in the given order. Whenever he travels between two cities he follows the shortest route along the roads. Find the minimum total time Handong needs to visit all of the listed cities in order.

Input

The first line contains the number of cities nn in Pohang (1≤n≤300001 \le n \le 30000).

Each of the next n−1n-1 lines contains two integers aa and bb, meaning that city aa and city bb are directly connected by a road (1≤a,b≤n1 \le a, b \le n, a≠ba \ne b).

The next line contains an integer mm, the number of cities Handong must visit (1≤m≤50001 \le m \le 5000).

Each of the following mm lines contains one city number; these are the cities Handong must visit, given in the order he must visit them.

Output

Print on the first line the minimum time Handong needs, starting at city 11, to visit all of the given cities in order.

Examples2

  1. Example 1

    Input
    5
    1 2
    1 5
    3 5
    4 5
    4
    1
    3
    2
    5
    
    Expected output
    7
    
  2. Example 2

    Input
    5
    1 2
    1 3
    1 4
    1 5
    4
    2
    3
    4
    5
    
    Expected output
    7