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 <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){
	if(a==c && b==d)break;
	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;
	}
	if(myin && enin)continue;

	int varxy=(a*a-2*a*c+b*b-2*b*d+c*c+d*d);
	double xx=(a*a*e-a*b*d+a*b*f-2*a*c*e+a*d*d-a*d*f+b*b*c-b*c*d-b*c*f+c*c*e+c*d*f);
	xx=xx/varxy;
	double yy=(a*a*d-a*b*c+a*b*e-a*c*d-a*d*e+b*b*f+b*c*c-b*c*e-2*b*d*f+c*d*e+d*d*f);
	yy=yy/varxy;
	if((e-xx)*(e-xx)+(f-yy)*(f-yy)>g*g)continue;

	if((a<=xx && xx<=c) || (c<=xx && xx<=a)){
	  safe=true;
	  break;
	}
      }
      if(safe)cout<<"Safe"<<endl;
      else cout<<"Danger"<<endl;
    }
  }
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <sstream>
#include <cmath>
#include <climits>
#include <set>
#include <iostream>
#include <map>
#include <functional>
#include <cstdlib>
#include <numeric>
#include <queue>
#include <complex>
#include <sstream>
#include <stack>

using namespace std;

#define reep(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reep(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const double EPS = 1e-8;
const double INF = 1e12;

// ½ÊãÌ_ExNg
typedef complex<double> P;
namespace std{
	bool operator< (const P& a, const P& b){
		return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
	}
}

// OÏ
double cross(const P& a, const P& b){
	return imag(conj(a)*b);
}

// àÏ
double dot(const P& a, const P& b){
	return real(conj(a)*b);
}

// ¼ü
struct L : public vector<P> {
	L(const P &a, const P &b){
		push_back(a);
		push_back(b);
	}
};

// ½p`
typedef vector<P> G;

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

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]);
}
bool intersectSP(const L& s, const P& p){
	return abs(s[0]-p) + abs(s[1]-p) - abs(s[1]-s[0]) < EPS;
}
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;
	while(scanf("%d", &n), n){
		vector<C> cs(n);
		rep(i, n){
			scanf("%lf%lf%lf", &cs[i].p.real(), &cs[i].p.imag(), &cs[i].r);
		}
		
		int m;
		scanf("%d", &m);
		rep(i, m){
			P a, b;
			scanf("%lf%lf%lf%lf", &a.real(), &a.imag(), &b.real(), &b.imag());
			L l(a, b);

			bool visible = true;
			rep(i, n){
				bool inner = abs(l[0]-cs[i].p)<cs[i].r && abs(l[1]-cs[i].p)<cs[i].r;
				if(!inner && distanceSP(l, cs[i].p) - cs[i].r < EPS){
					visible = false;
					break;
				}
			}
			puts(visible ? "Danger" : "Safe");
		}
	}

	return 0;
}

C++: #include <iostream>
#include <cmath>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
double dot(double ax, double ay, double bx, double by) {
	return ax*bx+ay*by;
}
int main() {
	int n,m;
	while(cin >> n, n) {
		double x[100],y[100],r[100];
		rep(i,n) {
			cin >> x[i] >> y[i] >> r[i];
		}
		cin >> m;
		double tx,ty,sx,sy;
		rep(i,m) {
			cin >> tx >> ty >> sx >> sy;
			bool flag = 0;
			rep(j,n) {
				double vx1 = tx-sx, vy1 = ty-sy;
				double vx2 = x[j]-sx, vy2 = y[j]-sy;
				double slen = sqrt(vx2*vx2+vy2*vy2);
				double tlen = sqrt(pow(x[j]-tx,2)+pow(y[j]-ty,2));				
				if(slen<r[j] && tlen>r[j] || slen>r[j] && tlen<r[j]) {
					flag = 1;
					break;
				} else {
					double n1 = dot(vx1,vy1,vx2,vy2);
					double n2 =	dot(vx1,vy1,vx1,vy1);
					double n3 = dot(vx2,vy2,vx2,vy2);
					if(n1>0 && n2>n1 && !(tlen<r[j]&&slen<r[j]) && n3-(n1/n2)*n1<=r[j]*r[j]) {
						flag = 1;
						break;
					}
				}
			}
			if(flag) cout << "Safe" << endl;
			else cout << "Danger" << endl;
		}
	}
	return 0;
}