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 <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <cassert>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

struct P{
    P(){}
    P(ll _r, ll _i):r(_r), i(_i){}
    void real(const ll& v){r = v;}
    void imag(const ll& v){i = v;}
    ll r, i;
};

ll real(const P& p){return p.r;}
ll imag(const P& p){return p.i;}
ll norm(const P& p){ll r = real(p), i = imag(p); return r * r + i * i;}

P operator+(const P& lhs, const P& rhs){
    return P(real(lhs)+real(rhs), imag(lhs)+imag(rhs));
}
P operator-(const P& lhs, const P& rhs){
    return P(real(lhs)-real(rhs), imag(lhs)-imag(rhs));
}
P operator-(const P& p){
    return P(-real(p), -imag(p));
}

ll cross(const P& lhs, const P& rhs){
    return real(lhs)*imag(rhs) - imag(lhs)*real(rhs);
}

ll dot(const P& lhs, const P& rhs){
    return real(lhs)*real(rhs) + imag(lhs)*imag(rhs);
}

ll distance(const P& a, const P& b, const P& p){
    ll c = cross(p-a, b-a);
    return c * c;
}

bool doesLineCrossCircle(const P& a, const P& b, const P& cc, const ll& radius){
    if(distance(a, b, cc) > radius * radius * norm(b-a)){return false;}
    if(norm(a-cc) > radius * radius && norm(b-cc) > radius * radius){
        return dot(cc-a, b-a) * dot(cc-b, b-a) <= 0;
    }
    return (norm(a-cc) < radius * radius) ^ (norm(b-cc) < radius * radius);
}

int N, M;
P wps[100100]; ll wrs[100100];

int main(){
    std::cin >> N;

    assert(N < 100100);
    REP(i, N){
        ll re, im;
        std::cin >> re >> im >> wrs[i];

        wps[i].real(re); wps[i].imag(im);
    }

    std::cin >> M;
    
    REP(i, M){
        P taro, oni;
        ll re, im;
        std::cin >> re >> im;
        taro.real(re); taro.imag(im);
        std::cin >> re >> im;
        oni.real(re); oni.imag(im);

        bool f = false;
        REP(j, N){
            assert(norm(taro-wps[j]) != wrs[j] * wrs[j] &&
                   norm(oni-wps[j]) != wrs[j] * wrs[j]);
            if(real(taro) == real(oni) &&
               imag(taro) == imag(oni)){
                continue;
            }
            if(doesLineCrossCircle(taro, oni, wps[j], wrs[j])){
                f = true;
            }
        }

        if(f){
            puts("Safe");
        }else{
            puts("Danger");
        }
    }
}

C++: #include <iostream>
#include <cmath>
using namespace std;

#define N 100
#define rep(i, n) for(int i=0; i<n; i++)
#define pow2(x) ((x)*(x))
#define S "Safe\n"
#define D "Danger\n"

int dist(int x1,int y1,int x2,int y2){
	return pow2(x1-x2)+pow2(y1-y2);
}

int main(){
	int n,m;
	int wx[N],wy[N],r[N];
	bool safe;
	int tx,ty,sx,sy;
	while(cin>>n,n){
		rep(i,n){
			cin>>wx[i]>>wy[i]>>r[i];
			r[i]*=r[i];
		}
		cin>>m;
		rep(i,m){
			cin>>tx>>ty>>sx>>sy;
			int dx=sx-tx,dy=sy-ty;
			safe=0;
			rep(j,n){
				if((dist(tx,ty,wx[j],wy[j])<=r[j])^(dist(sx,sy,wx[j],wy[j])<=r[j])){
					safe=1;
					break;
				}
				//この先危険
				//http://marupeke296.com/COL_2D_No5_PolygonToCircle.html
				int t0=pow2(dx)+pow2(dy);
				int t1=-(dx*(tx-wx[j])+dy*(ty-wy[j]));
				if(!(dist(sx,sy,wx[j],wy[j])<=r[j])&&!(dist(tx,ty,wx[j],wy[j])<=r[j])&&0<=t1&&t1<=t0&&pow2( dx*(ty-wy[j]) - dy*(tx-wx[j]) ) <= t0*r[j]){
					safe=1;
					break;
				}
			}
			cout<<(safe?S:D);
		}
	}
	
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int wx[100],wy[100],r[100];

main(){
  int n;
  while(cin>>n,n){
    rep(i,n)cin>>wx[i]>>wy[i]>>r[i];

    int m;
    cin>>m;
    while(m--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      bool safe=false;
      rep(i,n){
	int e=wx[i],f=wy[i],g=r[i];
	bool myin=(e-a)*(e-a)+(b-f)*(b-f)<g*g;
	bool enin=(e-c)*(e-c)+(f-d)*(f-d)<g*g;
	if(myin!=enin){
	  safe=true;
	  break;
	}
	int var1=(-2*a*a*e+2*a*b*d-2*a*b*f+4*a*c*e-2*a*d*d+2*a*d*f-2*b*b*c+2*b*c*d+2*b*c*f-2*c*c*e-2*c*d*f);
	int var2=var1*var1-4*(a*a-2*a*c+b*b-2*b*d+c*c+d*d)*
	  (a*a*d*d-2*a*a*d*f+a*a*e*e+a*a*f*f-a*a*g*g-2*a*b*c*d+2*a*b*c*f+2*a*c*d*f
	   -2*a*c*e*e-2*a*c*f*f+2*a*c*g*g+b*b*c*c-2*b*c*c*f+c*c*e*e+c*c*f*f-c*c*g*g);
	if(var2<0)continue;
	int var3=2*a*a*e-2*a*b*d+2*a*b*f-4*a*c*e+2*a*d*d-2*a*d*f+2*b*b*c-2*b*c*d-2*b*c*f+
	  2*c*c*e+2*c*d*f;
	int var4=2*(a*a-2*a*c+b*b-2*b*d+c*c+d*d);
	double x1=double(sqrt(var2)+var3)/var4;
	double x2=double(-sqrt(var2)+var3)/var4;
	if((a<=x1 && x1<=c) || (c<=x1 && x1<=a) || (a<=x2 && x2<=c) || (c<=x2 && x2<=a)){
	  safe=true;
	  break;
	}
      }
      if(safe)cout<<"Safe"<<endl;
      else cout<<"Danger"<<endl;
    }
  }
}