Under construction

Still being built. Things may change

Topological Ordering

Time limit4sMemory limit512 MB

Summary
Given a DAG of up to 20 vertices, count the topological orderings in which vertex j comes before vertex i, for every ordered pair (i, j).
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation, Topological sort
Solved
No attempts yet

Problem

The topological ordering of a directed acyclic graph is a permutation of its vertices p1,…,pnp_1, \ldots, p_n such that, for each arc, its source comes before its target in the permutation.

You are given a directed acyclic graph. For each pair of vertices (u,v)(u, v), count the number of topological orderings in which vertex uu comes before vertex vv.

Input

The first line contains a single integer tt, the number of test cases. Each test case follows.

The first line of each test case contains two integers nn and mm, the number of vertices and arcs (1≤n≤201 \le n \le 20, 0≤m≤n(n−1)/20 \le m \le n(n-1)/2). Each of the next mm lines contains two integers uiu_i and viv_i, denoting an arc from vertex uiu_i to vertex viv_i (1≤ui<vi≤n1 \le u_i < v_i \le n).

There are at most 100 test cases. In at most 5 of them, n>10n > 10.

Output

For each test case, print nn lines of nn numbers each. The jj-th number in the ii-th line equals the number of topological orderings in which vertex jj comes before vertex ii. When i=ji = j, the number is 0.

Examples1

  1. Example 1

    Input
    2
    3 2
    1 2
    1 3
    4 2
    1 2
    3 4
    
    Expected output
    0 0 0
    2 0 1
    2 1 0
    0 0 3 1
    6 0 5 3
    3 1 0 0
    5 3 6 0