How to merge consecutive record pairs over a table?
Basically, use common table expressions to isolate and number rows in 2 groups with one helper column NULL in each, and then join the two groups on row numbers.  

WITH cte1 AS (
    SELECT [id], [data], [from] , ROW_NUMBER() OVER(ORDER BY [id], [data], [from]) AS [row]
        FROM #temp3
        WHERE [prevfrom] IS NULL),
    cte2 AS (
    SELECT [to], ROW_NUMBER() OVER(ORDER BY [id], [data], [from]) AS [row]
        FROM #temp3
        WHERE [nextto] IS NULL)
SELECT t1.id, t1.data, t1.from, t2.to
    FROM cte1 AS t1
    JOIN cte2 AS t2
    ON t1.[row]=t2.[row];