CF BUDDY
← Problems·

1259E · Two Fairs

1900 · combinatorics, dfs and similar, dsu

Problem: There are nn cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from 11 to nn.

Two fairs are currently taking place in Berland — they are held in two different cities aa and bb (1a,bn1 \le a, b \le n; aba \ne b).

Find the number of pairs of cities xx and yy (xa,xb,ya,ybx \ne a, x \ne b, y \ne a, y \ne b) such that if you go from xx to yy you will have to go through both fairs (the order of visits doesn't matter). Formally, you need to find the number of pairs of cities x,yx,y such that any path from xx to yy goes through aa and bb (in any order).

Print the required number of pairs. The order of two cities in a pair does not matter, that is, the pairs (x,y)(x,y) and (y,x)(y,x) must be taken into account only once.

Input Format: The first line of the input contains an integer tt (1t41041 \le t \le 4\cdot10^4) — the number of test cases in the input. Next, tt test cases are specified.

The first line of each test case contains four integers nn, mm, aa and bb (4n21054 \le n \le 2\cdot10^5, n1m5105n - 1 \le m \le 5\cdot10^5, 1a,bn1 \le a,b \le n, aba \ne b) — numbers of cities and roads in Berland and numbers of two cities where fairs are held, respectively.

The following mm lines contain descriptions of roads between cities. Each of road description contains a pair of integers ui,viu_i, v_i (1ui,vin1 \le u_i, v_i \le n, uiviu_i \ne v_i) — numbers of cities connected by the road.

Each road is bi-directional and connects two different cities. It is guaranteed that from any city you can pass to any other by roads. There can be more than one road between a pair of cities.

The sum of the values of nn for all sets of input data in the test does not exceed 21052\cdot10^5. The sum of the values of mm for all sets of input data in the test does not exceed 51055\cdot10^5.

Output Format: Print tt integers — the answers to the given test cases in the order they are written in the input.

Sample Cases

Case 1

Input

3
7 7 3 5
1 2
2 3
3 4
4 5
5 6
6 7
7 5
4 5 2 3
1 2
2 3
3 4
4 1
4 2
4 3 2 1
1 2
2 3
4 1

Output

4
0
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback