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 <complex>
#include <vector>
#include <algorithm>

#define EPS 1e-6

using namespace std;

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);
}

bool intersectSP(const L &s, const P &p) {
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

int main(){
    int n,m;                                     
    double x,y,r;
    double a,b;
    vector<C> circles;
    while(cin >> n && n){
        for(int i=0;i<n;i++){
            cin >> x >> y >> r;
            circles.push_back(C(P(x,y), r));
        }
        cin >> m;
        for(int i=0;i<m;i++){
            bool isok = false;
            cin >> x >> y;
            cin >> a >> b;
            L line = L(P(x,y), P(a,b));
            for(int j=0;j<n;j++){
                if((abs(line[0]-circles[j].p) < circles[j].r &&
                    abs(line[1]-circles[j].p) < circles[j].r) ||
                   distanceSP(line, circles[j].p) > circles[j].r){
                }else{
                    isok = true;
                }
            }
            if(isok){
                cout << "Safe" << endl;
            }else{
                cout << "Danger" << endl;
            }
        }
    }
}

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;
	vector<C> walls;
	
	scanf("%d", &N);
	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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
vector<P> vec;
vector<int> rad;
P s,t;
bool in(int x,P a)
{
	return hypot(a.fi-vec[x].fi,a.sec-vec[x].sec)<(double)rad[x];
}
double sq(double x)
{
	return x*x;
}
bool check()
{
	for(int i=0;i<n;i++)
	{
		bool in1,in2;
		in1=in(i,s);in2=in(i,t);
		if(in1!=in2)return false;
		if(in1&&in2)continue;
		double a=s.fi,b=s.sec,c=t.fi,d=t.sec,p=vec[i].fi,q=vec[i].sec;
		if(a>c)
		{
			swap(a,c);swap(c,d);
		}
		ll A=sq(d-b)+sq(c-a);
		ll B=(b*c-a*d)*(d-b)-p*sq(c-a)-(c-a)*(d-b)*q;
		ll C=p*p*sq(c-a)+sq(b*c-a*d-q*c+q*a)-sq(rad[i])*sq(c-a);
		cout << A << ' ' << B << ' ' << C  << ' ' << B*B-A*C<< endl;
		if(B*B-A*C<0)continue;
		else if(B*B-A*C==0)
		{
			if(a<=(-B/A)&&(-B/A)<=c)return false;
			else continue;
		}
		else
		{
			double ans1=(-B+sqrt(B*B-A*C))/A;
			double ans2=(-B-sqrt(B*B-A*C))/A;
			if(ans1>=a&&ans2<=c)return false;
			else continue;
		}
	}
	return true;
}
int main()
{
	while(1)
	{
		vec.clear();
		rad.clear();
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			P k;
			int r;
			scanf("%d %d %d",&k.fi,&k.sec,&r);
			vec.pb(k);
			rad.pb(r);
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d %d %d %d",&t.fi,&t.sec,&s.fi,&s.sec);
			if(check())puts("Danger");
			else puts("Safe");
		}
	}
	return 0;
}