Chef and Segments

All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef has an array A consisting of N elements. He wants to find number of pairs of nonintersecting segments [a, b] and [c, d] (1 ≤ a ≤ b < c ≤ d ≤ N) such there is no number that occurs in the subarray {A_{a}, A_{a+1}, ... , A_{b}} and {A_{c}, A_{c+1}, ... , A_{d}} simultaneously.
Help Chef to find this number.
Input
 The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
 The first line of each test case contains a single integer N denoting the number of elements in the array.
 The second line contains N spaceseparated integers A_{1}, A_{2}, ..., A_{N}.
Output
 For each test case, output a single line containing one integer  number of pairs of nonintersecting segments.
Constraints
 1 ≤ T ≤ 5
 1 ≤ N ≤ 1000
 1 ≤ A_{i} ≤ 10^{9}
Subtasks
Subtask 1 (7 points)
 1 ≤ N ≤ 20
Subtask 2 (34 points)
 1 ≤ N ≤ 300
Subtask 3 (59 points)
 Original constraints
Example
Input: 2 3 1 2 3 4 1 2 1 2 Output: 5 4
Explanation
Example case 1.
All possible variants are correct: {[1, 1], [2, 2]}, {[1, 1], [2, 3]}, {[1, 2], [3, 3]}, {[2, 2], [3, 3]}, {[1,1], [3, 3]}.
Example case 2.
Correct segments: {[1, 1], [2, 2]}, {[1, 1], [4, 4]}, {[2, 2], [3, 3]}, {[3, 3], [4, 4]}.
Author:  antoniuk1 
Tester:  mugurelionut 
Editorial  http://discuss.codechef.com/problems/CHSGMNTS 
Tags  antoniuk1, dynamicprogramming, july16, medium 
Date Added:  18072015 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 