In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of November 8th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #05 exercise sheet]


[AED032] Squared Notebook

You already know that Sarah loves her squared notebook, right? To pass the time, she began writing ainteger numbers consecutively. However, she found that doing it from left to right and top to bottom was very boring! She therefore decided to fill in the numbers diagonally using the following pattern:

This results in the grid being filled in as shown below, where part of Sarah's notebook can be seen (the other rows and columns that do not appear in the figure are also filled in with numbers):

1361015212836
2591420273544
48131926344353
712182533425263
1117243241516274
1623314050617386

Sarah thinks it looks really cool! As her school is currently teaching addition, she decided to start adding up the numbers inside the rectangles. For example, if she considered the rectangle in the following figure (with corners at numbers 13 and 51), the sum would be equal to 358 (13+19+26+34+18+25+33+42+24+32+41+51):

1361015212836
2591420273544
48131926344353
712182533425263
1117243241516274
1623314050617386

Sarah would like to be able to check whether the sums are correct. Of course, she would like to know the sum of any given rectangle. Can you help her?

The Problem

Given a set of N rectangles, each indicated by the numbers in their corners Ai and Bi, determine the sum of the numbers contained in each rectangle, assuming that the squared notebook has been filled in by the diagonals as described above. You can assume that the notebook is so large that Sarah always has a square available when she needs to write a new number.

Input

The first line contains an integer N, indicating the number of rectangles to consider. This is followed by N lines, each containing two integers Ai and Bi ndicating the top-left and bottom-right corners, respectively, of the i-th rectangle.

Output

The output should contain N lines, one for each rectangle, containing the sum of the numbers contained in the correspondent rectangle.

Constraints

The following limits are guaranteed in all the test cases that will be given to your program:

1 ≤ N ≤ 1 000       Number of rectangles
1 ≤ AiBi ≤ 109       Corners of a rectangle
1 ≤ Rows, Cols ≤ 10 000       Number of rows and columns of each rectangle
1 ≤ Sum ≤ 1018       Sum of each rectangle (the answer to each line of input)

Example Input Example Output
4
13 51
5 31
28 44
25 245
358
160
143
7480

Explanation of the example

The four rectangles in the example correspond to the rectangles in the following figures:

1361015212836
2591420273544
48131926344353
712182533425263
1117243241516274
1623314050617386
   
1361015212836
2591420273544
48131926344353
712182533425263
1117243241516274
1623314050617386
   
1361015212836
2591420273544
48131926344353
712182533425263
1117243241516274
1623314050617386

13610152128364555667891105120
259142027354454657790104119135
48131926344353647689103118134151
7121825334252637588102117133150168
111724324151627487101116132149167186
1623314050617386100115131148166185205
2230394960728599114130147165184204225
29384859718498113129146164183203224246
374758708397112128145163182202223245268
4657698296111127144162181201222244267291

(this is an adaptation from a National Olympiad in Informatics problem)
 
Algorithms and Data Structures (L.EIC011) 2025/2026
DCC/FCUP & DEI/FEUP - University of Porto