Under construction

Still being built. Things may change

Counting Pairs

Time limit4sMemory limit256 MB

Summary
Given an undirected multigraph with self-loops, count pairs of vertices whose combined incident-edge count exceeds k for each of up to 10^6 queries.
Level

Hard8 of 10

Topics
Graph, Sorting, Prefix sum, Two pointers
Solved
No attempts yet

Problem

You are given an undirected graph GG with NN vertices, numbered from 11 to NN, and MM edges.

Consider a pair of vertices (a,b)(a, b) with a<ba < b. The incidence of (a,b)(a, b) is the total number of edges that have at least one endpoint equal to aa or bb.

You have to answer QQ queries. Each query gives an integer kk and asks how many pairs of vertices (a,b)(a, b) in GG satisfy a<ba < b and have incidence strictly greater than kk.

Input

The first line contains two integers NN and MM, the number of vertices and the number of edges (1≤N,M≤1061 \le N, M \le 10^6).

The next MM lines follow. The ii-th of them contains two integers xix_i and yiy_i, the endpoints of the ii-th edge (1≤xi,yi≤N1 \le x_i, y_i \le N). There may be self-loops or parallel edges.

The next line contains one integer QQ, the number of queries (1≤Q≤1061 \le Q \le 10^6).

The next QQ lines follow. The ii-th of them contains an integer kik_i, the ii-th query (1≤ki≤1061 \le k_i \le 10^6).

Output

For each query, print a single line with a single integer: the answer to the query.

Examples1

  1. Example 1

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