How to spawn objects on terrain surface depending the terrain height ?

RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (plane.GetComponent<MeshCollider>().Raycast(ray, out hit, Mathf.Infinity))
        {
            GameObject cube = Instantiate(prefabToSpawn);

            cube.transform.position = hit.point;

            Vector3 pos = cube.transform.position;
            pos.y = Terrain.activeTerrain.SampleHeight(cube.transform.position);
            cube.transform.position = pos;

            cube.transform.localScale = new Vector3(50, 50, 50);
        }

This part put the cubes on position 0 on the terrain. but I want it to position the cubes on the surface of the terrain even if there hills than on the hills surface. Now it’s position all the cubes on 0 on the Y make some of them or all of them a but inside the terrain.

Vector3 pos = cube.transform.position;
pos.y = Terrain.activeTerrain.SampleHeight(cube.transform.position);
cube.transform.position = pos;

Raycast is your friend here.