This page is still under construction.

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

Jack of All Trades

Time limit1sMemory limit128 MB

Summary
Given directed trades between items, find the minimum exchange ratio from one item to another using at most 9 trades, and count the chains achieving it.
Level

Medium6 of 10

Topics
Graph, DFS, Shortest path
Solved
No attempts yet

Problem

Jack loves to barter: he will trade anything for anything as long as he comes out ahead. Right now he wants to acquire one kind of item and is willing to pay in another kind of item, possibly through a chain of intermediate trades with his friends.

Each friend offers a one-directional trade: a fixed amount of one item in exchange for a fixed amount of another item. A friend willing to give a1a_1 units of name1 in exchange for a2a_2 units of name2 does not necessarily make the reverse trade.

To keep things manageable, Jack never involves more than 99 other people in a single chain of transactions, so a chain consists of at most 99 trades.

The exchange ratio of a chain is the number of units of the item Jack is willing to trade that he must give up in order to obtain one unit of the item he wants. Among all chains that convert the item Jack is willing to trade into the item he wants, find the best (smallest) achievable ratio, and the number of distinct chains that achieve exactly that ratio.

Input

The first line contains an integer nn, the number of test cases.

Each test case begins with a line containing two item names followed by a positive integer mm (m≤50m \le 50): the first name is the item Jack wants, the second name is the item Jack is willing to trade, and mm is the number of available trades. Each of the next mm lines has the form

a1 name1 a2 name2

meaning that some friend is willing to give a1a_1 units of item name1 in exchange for a2a_2 units of item name2 (the friend gives name1 and receives name2; the reverse trade is not implied). Each of a1a_1 and a2a_2 is a positive integer at most 2020. No trade ever requires more than 231−12^{31}-1 items to complete.

Output

For each test case, print Case i: (where ii is the case number, starting at 11), followed by a space, the best ratio Jack can obtain, a single space, and the number of distinct ways Jack could obtain that ratio.

The ratio is written in ordinary decimal (fixed-point) notation, never in scientific notation, rounded to 55 significant digits with trailing zeros kept, exactly as in the examples (e.g. 1.8000, 0.28571). A value lying exactly halfway between two 55-significant-digit results is rounded up.

Examples2

  1. Example 1

    Input
    2
    goldfish marbles 3
    1 goldfish 2 marbles
    5 shovels 3 marbles
    1 goldfish 3 shovels
    this that 4
    7 this 2 that
    14 this 4 that
    7 this 2 theother
    1 theother 1 that
    
    Expected output
    Case 1: 1.8000 1
    Case 2: 0.28571 3
    
  2. Example 2

    Input
    1
    gold silver 1
    1 gold 1 silver
    
    Expected output
    Case 1: 1.0000 1