import java.util.*;
import java.io.*;
 
public class Solution{
	static PrintWriter out=new PrintWriter(System.out);
	public static void main (String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		String[] input=br.readLine().trim().split(" ");
		
		int numTestCases=Integer.parseInt(input[0]);
		
		
		while(numTestCases-->0){
		    input=br.readLine().trim().split(" ");
		    int n = Integer.parseInt(input[0]);
		    int m = Integer.parseInt(input[1]);
		    int k = Integer.parseInt(input[2]);
		    
		    ArrayList<Integer> arr = new ArrayList<>();
		    for(int i = 0; i < m; i++){
		        input = br.readLine().trim().split(" ");
		        int speedLimit = Integer.parseInt(input[2]);
		        arr.add(speedLimit);
		    }
		    out.println(minSteps(arr, n, k));
		}
		
		out.flush();
		out.close();
	}
	public static int minSteps(ArrayList<Integer> arr, int n, int k)
	{
	    int ans = Integer.MAX_VALUE;
	    Collections.sort(arr);
	    int numRemovals = arr.size() - (n - 1);
	    for(int i = arr.size() - 1; i >= 0; i--)
	    {
	        int count = arr.size() - 1 - i;
	        if(count <= numRemovals)
	        {
	            int diff = Math.abs(arr.get(i) - k);
	            ans = Math.min(diff, ans);
	        }
	    }
	    return ans;
	}
}