Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	x := make([]int, 100)
	y := make([]int, 100)
	r := make([]int, 100)
	for scanner.Scan() {
		a, _ := strconv.Atoi(scanner.Text())
		if a == 0 {
			break
		}
		for i := 0; i < a; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			tmpx, _ := strconv.Atoi(input[0])
			tmpy, _ := strconv.Atoi(input[1])
			tmpr, _ := strconv.Atoi(input[2])
			x[i], y[i], r[i] = tmpx, tmpy, tmpr
		}
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(input[0])
			d, _ := strconv.Atoi(input[1])
			e, _ := strconv.Atoi(input[2])
			f, _ := strconv.Atoi(input[3])
			ok := false
			for j := 0; j < a; j++ {
				if (c-x[j])*(c-x[j])+(d-y[j])*(d-y[j]) >= r[j]*r[j] || (e-x[j])*(e-x[j])+(f-y[j])*(f-y[j]) >= r[j]*r[j] {
					if (e-c)*(x[j]-c)+(f-d)*(y[j]-d) < 0 {
						if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d) <= r[j]*r[j] {
							ok = true
						}
					} else {
						if (e-c)*(x[j]-c)+(f-d)*(y[j]-d)>(e-c)*(e-c)+(f-d)*(f-d) {
							if (x[j]-e)*(x[j]-e)+(y[j]-f)*(y[j]-f)<=r[j]*r[j] {
								ok = true
							}
						} else {
							if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d)-((e-c)*(x[j]-c)+(f-d)*(y[j]-d))*((e-c)*(x[j]-c)+(f-d)*(y[j]-d))/((e-c)*(e-c)+(f-d)*(f-d))<=r[j]*r[j] {
								ok = true
							}
						}
					}

				}
			}
			if ok {
				fmt.Println("Safe")
			} else {
				fmt.Println("Danger")
			}
		}
	}
}

C++: #include <cstdio>
#include <complex>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

#define EPS 1e-8
typedef complex<double> P;

struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
};

struct C {
  P p; double r;
  C(const P &p, double r) : p(p), r(r) { }
};

double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}

double dist(const P& a, const P& b)
{
	return sqrt((a.real()-b.real())*(a.real()-b.real())+(a.imag()-b.imag())*(a.imag()-b.imag()));
}

bool isIntoC(const L& l, const C& c)
{
	return dist(l[0], c.p)+EPS < c.r && dist(l[1], c.p)+EPS < c.r;
}

bool intersectSC(const L& l, const C& c)
{
	if(isIntoC(l, c)) return false;
	
	double vc=dot(l[1]-l[0], c.p-l[0]);
	if(vc+EPS<0)
	{
		return dist(l[0], c.p)<c.r+EPS;
	}
	
	double vv=dot(l[1]-l[0], l[1]-l[0]);
	
	if(vc > vv)
	{
		double d=dist(l[1], c.p);
		
		return d*d<c.r*c.r+EPS;
	}
	
	double cc=dot(c.p-l[0], c.p-l[0]);
	
	return cc-(vc/vv)*vc < c.r*c.r+EPS;
}

int main()
{
	int N,M;
	
	while(scanf("%d", &N), N)
	{
		vector<C> walls;
		while(N--)
		{
			int x,y,r;
			scanf("%d%d%d", &x,&y,&r);
			walls.push_back(C(P(x,y), r));
		}
		
		scanf("%d", &M);
		while(M--)
		{
			int tx,ty,sx,sy;
			scanf("%d%d%d%d", &tx,&ty,&sx,&sy);
			bool g=false;
			for(int i=0; i<walls.size(); i++)
			{
				if(intersectSC(L(P(tx,ty),P(sx,sy)), walls[i]))
				{
					g=true;
					break;
				}
			}
			
			puts((g?"Safe":"Danger"));
		}
	}
}

C++: #include<iostream>
#include<cmath>
#include<cstdlib>

using namespace std;

int main() {
  int n;
  while(cin >> n, n != 0) {
    int m, wx[101], wy[101], wr[101];
    for(int i = 0; i < n; i++)  cin >> wx[i] >> wy[i] >> wr[i];
    cin >> m;
    while(m--) {
      int tx, ty, sx, sy, a, b, c, d;
      double e, len;
      cin >> tx >> ty >> sx >> sy;
      //|a*wx + b*wy + c| / √((a*a)+ (b*b)) = len
      a = (tx - sx);
      b = 0 - (ty - sy);
      c = 0 - (a * ty + b * tx);
      e = (double)sqrt(a * a + b * b);
      
      int cnt = 0;
      for(int i = 0; i < n; i++) {
	
	if( (d = b * wx[i] + a * wy[i] + c ) < 0) d = 0 - d; 
	len  = d / e;
	
	if(len > wr[i]) cnt = 1; //Danger交点0
	else if ((wx[i] - tx)*(wx[i] - sx) > 0 && (wy[i] - ty)*(wy[i] - sy) > 0) cnt = 1; //線分上じゃない時交点0
	else {//交点あり
	  double len_t = sqrt((tx - wx[i]) * (tx - wx[i]) + (ty - wy[i]) * (ty - wy[i])) , len_s = sqrt((sx - wx[i]) * (sx - wx[i]) + (sy - wy[i]) * (sy - wy[i]));
	  if(!((len_t < wr[i] && len_s < wr[i]))){ cnt = 2; break; } //鬼, 自分どちらかが囲まれているならSafe
	}
      }
      if (cnt  == 2 || cnt == 0) cout << "Safe" << endl;
      else if( cnt == 1 ) cout << "Danger" << endl;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define EPS 1e-10
#define PB push_back
#define fi first
#define se second
#define ll long long 
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
typedef pair<double,double> Pdd;
double check_p(double A,double B,double C,double D){
    return (-B+sqrt(D))/(2*A);
}
double check_m(double A,double B,double C,double D){
    return (-B-sqrt(D))/(2*A);
}
int main(){
    int n;
    while(scanf("%d",&n),n){
        vector < Pdd > circle;
        vector < double > R;
        rep(i,n){
            double a,b,r;
            scanf("%lf%lf%lf",&a,&b,&r);
            circle.PB(Pdd(a,b));
            R.PB(r);
        }
        int m;
        scanf("%d",&m);
        rep(i,m){
            double tx,ty,sx,sy;
            scanf("%lf%lf%lf%lf",&tx,&ty,&sx,&sy);
            double m,d;
            double a,b,r;
            bool isstraight = false;
            m = (sy-ty)/(sx-tx);
            d = sy - m*sx;
            if(sx == tx){
                isstraight = true;
            }
            bool flg = false;
            rep(j,n){
                a = circle[j].fi;
                b = circle[j].se;
                r = R[j];
                double A,B,C;
                A = m*m+1;
                B = (-2.0*a+2.0*m*d-2.0*m*b);
                C = a*a+b*b+d*d-2.0*b*d-r*r;
                if(isstraight){
                    if(fabs(a-sx) <= r){
                        flg = true;
                        break;
                    }
                }
                else if(!(B*B-4.0*A*C < EPS)){
                    if(min(sx,tx)-check_p(A,B,C,B*B-4.0*A*C) < EPS  && check_p(A,B,C,B*B-4.0*A*C)-max(sx,tx) < EPS ){
                        flg = true;
                        break;
                    }
                    if(min(sx,tx)-check_m(A,B,C,B*B-4.0*A*C) < EPS && check_m(A,B,C,B*B-4.0*A*C)-max(sx,tx) < EPS){  
                        flg = true;
                        break;  
                    }
                }
            }
            if(flg)puts("Safe");
            else puts("Danger");
        }
    }
    return 0;
}