Problem: Anchored Balloon

A balloon placed on the ground is connected to one or more anchors on the ground with ropes. Each rope is long enough to connect the balloon and the anchor. No two ropes cross each other. Figure E-1 shows such a situation.

<image>
Figure E-1: A balloon and ropes on the ground

Now the balloon takes off, and your task is to find how high the balloon can go up with keeping the rope connections. The positions of the anchors are fixed. The lengths of the ropes and the positions of the anchors are given. You may assume that these ropes have no weight and thus can be straightened up when pulled to whichever directions. Figure E-2 shows the highest position of the balloon for the situation shown in Figure E-1.

<image>
Figure E-2: The highest position of the balloon

Input

The input consists of multiple datasets, each in the following format.

> n
>  x1 y1 l1
>  ...
>  xn yn ln
>

The first line of a dataset contains an integer n (1 ≤ n ≤ 10) representing the number of the ropes. Each of the following n lines contains three integers, xi, yi, and li, separated by a single space. Pi = (xi, yi) represents the position of the anchor connecting the i-th rope, and li represents the length of the rope. You can assume that −100 ≤ xi ≤ 100, −100 ≤ yi ≤ 100, and 1 ≤ li ≤ 300. The balloon is initially placed at (0, 0) on the ground. You can ignore the size of the balloon and the anchors.

You can assume that Pi and Pj represent different positions if i ≠ j. You can also assume that the distance between Pi and (0, 0) is less than or equal to li−1. This means that the balloon can go up at least 1 unit high.

Figures E-1 and E-2 correspond to the first dataset of Sample Input below.

The end of the input is indicated by a line containing a zero.

Output

For each dataset, output a single line containing the maximum height that the balloon can go up. The error of the value should be no greater than 0.00001. No extra characters should appear in the output.

Sample Input


3
10 10 20
10 -10 20
-10 10 120
1
10 10 16
2
10 10 20
10 -10 20
2
100 0 101
-90 0 91
2
0 0 53
30 40 102
3
10 10 20
10 -10 20
-10 -10 20
3
1 5 13
5 -3 13
-3 -3 13
3
98 97 168
-82 -80 193
-99 -96 211
4
90 -100 160
-80 -80 150
90 80 150
80 80 245
4
85 -90 290
-80 -80 220
-85 90 145
85 90 170
5
0 0 4
3 0 5
-3 0 5
0 3 5
0 -3 5
10
95 -93 260
-86 96 211
91 90 177
-81 -80 124
-91 91 144
97 94 165
-90 -86 194
89 85 167
-93 -80 222
92 -84 218
0


Output for the Sample Input


17.3205081
16.0000000
17.3205081
13.8011200
53.0000000
14.1421356
12.0000000
128.3928757
94.1879092
131.1240816
4.0000000
72.2251798






Example

Input

3
10 10 20
10 -10 20
-10 10 120
1
10 10 16
2
10 10 20
10 -10 20
2
100 0 101
-90 0 91
2
0 0 53
30 40 102
3
10 10 20
10 -10 20
-10 -10 20
3
1 5 13
5 -3 13
-3 -3 13
3
98 97 168
-82 -80 193
-99 -96 211
4
90 -100 160
-80 -80 150
90 80 150
80 80 245
4
85 -90 290
-80 -80 220
-85 90 145
85 90 170
5
0 0 4
3 0 5
-3 0 5
0 3 5
0 -3 5
10
95 -93 260
-86 96 211
91 90 177
-81 -80 124
-91 91 144
97 94 165
-90 -86 194
89 85 167
-93 -80 222
92 -84 218
0


Output

17.3205081
16.0000000
17.3205081
13.8011200
53.0000000
14.1421356
12.0000000
128.3928757
94.1879092
131.1240816
4.0000000
72.2251798

Solution (in CPP):

Solution:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#define EPS 1e-10

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
	while(1){
		int n;
		scanf("%d", &n);
		if(n==0) return 0;
		double x[10], y[10], l[10];
		for(int i=0; i<n; i++){
			int x1, y1, l1;
			scanf("%d %d %d", &x1, &y1, &l1);
			x[i]=(double)x1, y[i]=(double)y1, l[i]=(double)l1;
		}
		if(n==1){
			printf("%.7lf\n", l[0]);
			continue;
		}
		double h1=1.0, h2=300.0;
		for(int t=0; t<100; t++){
			double h=(h1+h2)/2.0;
			bool e=0;
			double r[10];
			for(int i=0; i<n; i++){
				if(l[i]<h+EPS){
					e=1;
					break;
				}
				r[i]=sqrt(l[i]*l[i]-h*h);
			}
			if(e==1){
				h2=h;
				continue;
			}
			bool nn[10]={};
			for(int i=0; i<n; i++){
				for(int j=i+1; j<n; j++){
					double d=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
					if(abs(r[i]-r[j])>=d-EPS){
						if(r[i]>r[j]){
							nn[i]=1;
						}else{
							nn[j]=1;
						}
					}
					if(r[i]+r[j]<d+EPS){
						e=1;
					}
				}
			}
			if(e==1){
				h2=h;
				continue;
			}
          int count=0;
          for(int i=0; i<n; i++){
            if(nn[i]==0) count++;
          }
          if(count<=2){
            h1=h;
            continue;
          }
			for(int i=0; i<n; i++){
				for(int j=i+1; j<n; j++){
                  if(i==j) continue;
					if(nn[i]==1 || nn[j]==1) continue;
					double s=x[i]*x[i]-x[j]*x[j]+y[i]*y[i]-y[j]*y[j]-l[i]*l[i]+l[j]*l[j];
					double x1, y1, x2, y2;
					if(abs(y[i]-y[j])<EPS){
						x1=s/(2.0*(x[i]-x[j]));
						x2=x1;
						if(l[i]*l[i]-h*h<(x1-x[i])*(x1-x[i])+EPS){
							y1=y[i], y2=y[i];
						}else{
							y1=sqrt(l[i]*l[i]-h*h-(x1-x[i])*(x1-x[i]))+y[i];
							y2=-sqrt(l[i]*l[i]-h*h-(x1-x[i])*(x1-x[i]))+y[i];
						}
					}else{
						double t1=(x[i]-x[j])/(y[i]-y[j]);
						s/=(y[i]-y[j]);
                      	s/=2.0;
						double a=1.0+t1*t1;
						double b=x[i]+t1*(s-y[i]);
						double c=x[i]*x[i]+(s-y[i])*(s-y[i])-l[i]*l[i]+h*h;
						if(b*b<a*c+EPS){
							x1=b, x2=b;
						}else{
							x1=(b+sqrt(b*b-a*c))/a;
							x2=(b-sqrt(b*b-a*c))/a;
						}
						y1=-t1*x1+s;
						y2=-t1*x2+s;
					}
					bool e1=0;
					for(int k=0; k<n; k++){
                      if(k==i || k==j || nn[k]==1) continue;
						if((x1-x[k])*(x1-x[k])+(y1-y[k])*(y1-y[k])>l[k]*l[k]-h*h-EPS){
							e1=1;
							break;
						}
					}
					if(e1==0){
						e=1;
						break;
					}
					e1=0;
					for(int k=0; k<n; k++){
                      if(k==i || k==j || nn[k]==1) continue;
						if((x2-x[k])*(x2-x[k])+(y2-y[k])*(y2-y[k])>l[k]*l[k]-h*h-EPS){
							e1=1;
							break;
						}
					}
					if(e1==0){
						e=1;
						break;
					}
				}
				if(e==1) break;
            }
			if(e==1){
				h1=h;
			}else{
				h2=h;
			}
		}
		printf("%.7lf\n", h1);
	}
	return 0;
}