content: //{ Driver Code Starts
  import java.io.*;
  import java.lang.*;
  import java.util.*;
  
  
  // } Driver Code Ends
  // User function Template for Java
   class Solution {
      int countPairs(int a[], int target) {
          // Your code here
          int left=0;
          int right = a.length-1;
          Arrays.sort(a);
          int c=0;
          while(left<right){
              if(a[left]+a[right]>=target){
               right--;
              }
              else{
                  c+=(right-left);
                  left++;
              }
          }
          return c;
      }
  }
  
  
  //{ Driver Code Starts.
  
  class Main {
      public static void main(String args[]) throws IOException {
          BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
          int t = Integer.parseInt(read.readLine());
  
          while (t-- > 0) {
              String line = read.readLine().trim();
              String[] numsStr = line.split(" ");
              int[] nums = new int[numsStr.length];
              for (int i = 0; i < numsStr.length; i++) {
                  nums[i] = Integer.parseInt(numsStr[i]);
              }
  
              int target = Integer.parseInt(read.readLine());
  
              Solution obj = new Solution();
  
              System.out.println(obj.countPairs(nums, target));
  
              System.out.println("~");
          }
      }
  }
  // } Driver Code Ends