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 <map>
#include <queue>
#include <algorithm>
#include <complex>
#include <cassert>

using namespace std;
typedef long long ll;

typedef long double R;
typedef complex<R> P;


namespace std {
    bool operator < (P a, P b) {
        return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
    }
    bool operator > (P a, P b) {
        return (b < a);
    }
}

const R EPS = 1e-7;
const R PI = acos(-1);

int sgn(R a) {
    if (a < -EPS) return -1;
    if (a > EPS) return 1;
    return 0;
}


bool near(P a, P b) {
    return !sgn(abs(a-b));
}

R cross(P a, P b) { return imag(conj(a)*b); }
R dot(P a, P b) { return real(conj(a)*b); }

/* 1->cclock
  -1->clock
   0->on
   2->back
  -2->front
  */
int ccw(P a, P b, P c) {
    assert(!near(a, b));
    if (near(a, c) || near(b, c)) return 0;
    int s = sgn(cross(b-a, c-a));
    if (s) return s;
    if (a > b) swap(a, b);
    if (c < a) return 2;
    if (b < c) return -2;
    return 0;
}

struct L {
    P x, y;
    L() {};
    L(P x, P y) :x(x), y(y) {};
};

R abs(const L &l) {
    return abs(l.y - l.x);
}

struct C {
    P p;
    R r;
    C () {};
    C (P p, R r) : p(p), r(r) {};
};

R distLP(const L &l, const P &p) {
	assert(!near(l.x, l.y));
    return abs(cross(l.y-l.x, p-l.x)/abs(l));
}

//need Intersect/distLP
int crossP(const C &c, const L &l, L &r) {
    R u = distLP(l, c.p);
    int si = sgn(u - c.r);
    if (si == 1) return 0;
    P v = (l.y-l.x) / abs(l) * P(0, -1); //l?????´???????????????????????????
    v *= c.r;
    if (ccw(l.x, l.y, c.p) < 0) v *= -1;
    //v?????????????????????l?????¨??????????????????????????????
    if (si == 0) {
    	r.x = r.y = c.p + v;
    	return 1;
    }

    R th = acos(u/c.r);
    r.x = c.p+v*polar<R>(1.0,-th);
    r.y = c.p+v*polar<R>(1.0,th);
    return 2;
}

C ci[110];
int main() {
	while (true) {
		int n;
		cin >> n;
		if (!n) break;
		for (int i = 0; i < n; i++) {
			R x, y, r;
			cin >> x >> y >> r;
			ci[i] = C(P(x, y), r);
		}
		int m;
		cin >> m;
		for (int i = 0; i < m; i++) {
			R tx, ty, sx, sy;
			cin >> tx >> ty >> sx >> sy;
			P t = P(tx, ty), s = P(sx, sy);
			L l;
			bool f = true;
			for (int j = 0; j < n; j++) {
				L l;
				int cp = crossP(ci[j], L(t, s), l);
//				printf("%d %d %d %Lf\n", i, j, cp, distLP(L(t, s), ci[j].p) );
				if (!cp) continue;
//				cout << l.x << " " << l.y << endl;
//				cout << abs(l.x - ci[j].p) << " " << abs(l.y - ci[j].p) << endl;
				if (!ccw(t, s, l.x) || !ccw(t, s, l.y)) {
					//printf("%d %d\n", i, j);
					f = false;
					//break;
				}
			}
			if (f) {
				cout << "Danger" << endl;
			} else {
				cout << "Safe" << endl;
			}
		}
	}
	return 0;
}

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

typedef complex<double> P;
const double eps = 1e-8;

bool equals(double a, double b) {
  return abs(a-b) < eps;
}

double cross(P a, P b) {
  return a.real()*b.imag() - a.imag()*b.real();
}

double dot(P a, P b) {
  return a.real()*b.real() + a.imag()*b.imag();
}

double getDistanceLP(P s1, P s2, P p) {
  return abs(cross(s2-s1, p-s1)/abs(s2-s1));
}

double getDistanceSP(P s1, P s2, P p) {
  if(dot(s2-s1, p-s1) < 0.0) return abs(p-s1);
  if(dot(s1-s2, p-s2) < 0.0) return abs(p-s2);
  return getDistanceLP(s1, s2, p);
}

const int MAXN = 100;
int n;
P w[MAXN];
double r[MAXN];
int m;

bool safe(const P &t, const P &s) {
  for(int i = 0; i < n; ++i) {
    bool a, b;
    a = abs(w[i]-t) < r[i];
    b = abs(w[i]-s) < r[i];
    if(a ^ b) return true;
    if(!a) {
      double dist = getDistanceSP(t, s, w[i]);
      if(equals(dist, r[i]) || dist < r[i]) return true;
    }
  }
  return false;
}

int main() {
  while(cin >> n && n) {
    for(int i = 0; i < n; ++i) {
      cin >> w[i].real() >> w[i].imag() >> r[i];
    }
    cin >> m;
    while(m--) {
      P t, s;
      cin >> t.real() >> t.imag() >> s.real() >> s.imag();
      if(safe(t,s)) cout << "Safe" << endl;
      else cout << "Danger" << endl;
    }
  }
  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){
	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;
	}
	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;
	while(isnan(x1) || isnan(x2));
	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;
    }
  }
}