This page is still under construction.

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

London Underground

Time limit1sMemory limit512 MB

Summary
Given subway lines with stop times and a line-change cost, find the minimum travel time between two stations.
Level

Medium6 of 10

Topics
Shortest path, Graph, Dynamic programming
Solved
No attempts yet

Problem

The London Underground has NN stations, numbered 1 to NN, and MM lines, numbered 1 to MM. Each line stops at a fixed sequence of stations and runs back and forth all day. Two consecutive stations on a line are the same distance apart in both directions, and a train leaves every station in both directions every minute, so waiting for a train takes no time.

A line is given as its stops in order, and each stop carries the number of minutes from the start of the line to that station. Riding one line from one of its stops to another takes the difference between those two numbers.

You can change from line L1L_1 to line L2L_2 at station SiS_i when both lines stop at SiS_i, and one change takes SS minutes. Boarding the first line at the start of the trip and leaving the last line at the end take no time.

Alice and Bob are visiting London. They ride the underground a lot, and they keep feeling that they are not always taking the fastest route. Find the shortest travel time from station AA to station BB.

Input

The first line has one integer TT, the number of test cases.

Each test case begins with a line of five integers SS, NN, MM, AA, BB, separated by spaces. They give the number of minutes one line change takes, the number of stations, the number of lines, the start station, and the end station.

The next MM lines describe the subway lines. Each of those lines begins with an integer XX, the number of stops on that subway line, followed by XX stop specifications separated by spaces, in the order the stops appear along the line. A stop specification is two integers, where the first number SniSn_i is the station number and the second number StiSt_i is the number of minutes from the start of the line to that station.

  • 1≤T≤201 \le T \le 20
  • 1≤S≤1001 \le S \le 100
  • 2≤N≤1002 \le N \le 100
  • 1≤M≤101 \le M \le 10
  • 1≤A,B≤N1 \le A, B \le N and A≠BA \ne B
  • 1≤X≤N1 \le X \le N for every line
  • St0=0St_0 = 0 for every line
  • Sti>Sti−1St_i > St_{i-1} for every line
  • 0≤Sti≤10000 \le St_i \le 1000 for every line
  • Every station from 1 to NN is on at least one line.
  • Every station is reachable from every other station.

Output

For each test case, print on its own line the smallest number of minutes needed to travel from station AA to station BB.

Examples2

  1. Example 1

    Input
    3
    1 5 1 1 5
    5 2 0 1 2 3 5 5 10 4 15
    3 4 2 1 4
    3 1 0 2 2 3 5
    3 2 0 3 10 4 11
    1 4 2 1 4
    3 1 0 2 2 4 15
    3 2 0 3 1 4 2
    
    Expected output
    8
    9
    5
    
  2. Example 2

    Input
    2
    5 2 1 1 2
    2 1 0 2 7
    10 3 2 1 3
    2 1 0 2 4
    2 2 0 3 6
    
    Expected output
    7
    20