Connecting points
Difficulty: Hard

John has his own 3-dimensional space. He currently has $N$ distinct points that belong to this space.  

Today John is connecting the points with each other. In each step, he chooses two  points and connects them together by drawing a curved line between them. After that he always puts single point somewhere on the curved line. Note that this point initially has 2 connections, and it can be further connected by curve lines with others.  Also note that multiple curved-lines can be drawn between each pair of points.  
<br>
There is one condition: each point which currently has strictly less than $K$ connections can be used for making new connections again.  
<br>
John wants to maximize total number of drawn curve lines. Help him and calculate how many curve lines can John draw. Note that a pair of point can be connected multiple times with different curved lines.

PS: Refer to the explanation/image given below for more clarity.

**Input Format**  
The first line contains a single integer $T$, representing the number of test cases to follow. Each of the following $T$ lines (or testcases) contains two integers separated by single space: $N$ - the number of points in three-dimensional space, $K$ - number of maximum connections that can be made for the present testcase.  

**Output Format**  
For each of the test case, print the count of drawn curve lines, or $-1$ if John will never end connecting the points.  


**Constraints**  
$1 \le T \le 50$  
$1 \le N \le 10^9$  
$0 \le K \le 10$  

**Sample Input:**

    5
    7 0
    47 1
    2 2
    2 3
    3 10

**Sample Output:**

    0
    23
    2
    5
    -1

**Explanation**  
*Sample Case #00:* Points are not allowed to be connected at all, so no curved-line can be drawn.  
*Sample Case #01:* Since only one line can be drawn out of a point, at most 23 pair of points can be joined via curves.  
*Sample Case #02:* John connects both points twice. It yields four points two initial and two appended. All points are connected twice.  
*Sample Case #03:* Following picture illustrates the process of connecting the points.  Orange points with three edges no longer can be connected.  
![connecting-points.gif](https://hr-challenge-images.s3.amazonaws.com/3051/connecting-points.gif)  
<br>
*Sample Case #04:* John will never stop adding points.

---
**Tested by** [Shang-En Huang](https://www.hackerrank.com/tmt514)

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    
    #include <iostream>
    #include <sstream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <cstring>
    #include <vector>
    #include <list>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <map>
    #include <set>
    #include <algorithm>
    #include <numeric>
    using namespace std;

    typedef pair<int, int> PII;
    typedef vector<int> VI;

    #define FOR(i, a, b) for(int i = (a); i < (b); ++i)
    #define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); --i)
    #define CLEAR(a, b) memset(a, b, sizeof(a))
    #define ALL(a) (a).begin(),(a).end()
    #define PB push_back
    #define MP make_pair
    #define MOD 1000000007
    #define INF 1000000007
    #define y1 agaga
    #define ll long long
    #define ull unsigned long long

    vector<int> V;
    int n,k;

    int f(int n, int k)
    {
        if (n == 1 || k == 0) return 0;
        if (k == 1) return n / 2;
        if (k == 2) return n;
        if (k >= 4) return -1;
        return -2;       
    }

    int main()
    {
        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);

        int t;
        cin >> t;

        while (t--)
            {

        cin >> n >> k;

        if (f(n,k)!=-2)
        {
            cout << f(n,k) << endl;
            continue;
        }


            cout << (ll)(n)*3-1<< endl;

        }


        return 0;
    }