This page is still under construction.

Parts of this page are still being built. What you see may change.

Detour Buster

Time limit1sMemory limit128 MB

Summary
Given a piecewise-linear track, find the shortest distance from the first point to the last while staying on the track, allowing travel in either direction.
Level

Hard8 of 10

Topics
Geometry, Graph, Shortest path, Sorting
Solved
No attempts yet

Problem

A parcel-delivery company employs cyclists to deliver packages across a large metropolitan city and pays each cyclist according to the total distance travelled. Every company bicycle carries a GPS unit that records its position once every few seconds. The sequence of recorded positions for one delivery is called a track, and the length of a track -- the sum of the Euclidean distances between consecutive recorded points -- is used to compute the cyclist's pay.

A recent audit revealed that some tracks self-intersect, which means some cyclists made unnecessary detours. Write a program that, given a track, computes the length of the shortest possible route from the first recorded point to the last. The route must lie entirely on the original track and may travel along it in either the same or the opposite direction.

Input

The first line contains an integer TT, the number of tracks to process.

Each track begins with a line containing a positive integer NN, the number of recorded points that define the track. Each of the next NN lines contains two integers separated by a single space, giving the xx- and yy-coordinates (in metres) of a point on the track. The points are listed in the order they were recorded.

Two consecutive points form a segment. Consecutive points are at most 30 metres apart, and each segment intersects at most 20 other segments. All coordinates are integers between -10,000,000 and 10,000,000 inclusive, and NN satisfies 1≤N≤1000001 \le N \le 100000.

Output

For each track, output on its own line a single integer: the length of the shortest route, in metres, rounded to the nearest integer.

Hint

Rounding a positive number R.xyzR.xyz to the nearest integer:

  • if the first decimal digit xx is less than 5, the rounded value is RR;
  • otherwise the rounded value is R+1R+1.

Examples1

  1. Example 1

    Input
    2
    5
    0 0
    12 0
    20 0
    10 10
    10 -10
    6
    0 0
    15 0
    10 -5
    4 1
    10 1
    10 -10
    
    Expected output
    20
    17