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<bits/stdc++.h>
#define int long long
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int f(int k) { return k * k; }
signed main() {
	int n, q, x0[110], y0[110], r[110], x1, x2, y1, y2;
	while (cin >> n, n) {
		for1(i, n)cin >> x0[i] >> y0[i] >> r[i];
		cin >> q;
		while (q--) {
			cin >> x1 >> y1 >> x2 >> y2;
			bool b = 0;
			for1(i, n) {
				if ((f(x1 - x0[i]) + f(y1 - y0[i]) - f(r[i])) < 0 && (f(x2 - x0[i]) + f(y2 - y0[i]) - f(r[i])) < 0)continue;
				if ((f(x1 - x0[i]) + f(y1 - y0[i]) - f(r[i])) * (f(x2 - x0[i]) + f(y2 - y0[i]) - f(r[i])) < 0)b = 1;
				else if (f((y1 - y2) * x0[i] - (x1 - x2) * y0[i] + x1 * y2 - x2 * y1) <= f(r[i]) * (f(x1 - x2) + f(y1 - y2)) && ((x1 - x2) * (x0[i] - x1) + (y1 - y2) * (y0[i] - y1)) * ((x1 - x2) * (x0[i] - x2) + (y1 - y2) * (y0[i] - y2)) <= 0)b = 1;
				if (b)break;
			}
			cout << (b ? "Safe\n" : "Danger\n");
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef complex<double> P;
typedef pair<double, P> C;
vector<C> circles;
#define FST first
#define SND second
typedef pair<P, P> S;
#define EPS (1e-5)

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

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

double distanceLP(S s, P a) {
  // d = |y||sin(th)| = |x||y|sin(th)/|x| = |cross(x, y)|/|x|
  return abs(cross(s.SND-s.FST, a-s.FST)) / abs(s.SND-s.FST);
}

double distanceSP(S s, P a) {
  // 2ベクトルのなす角が鈍角なら端点と１点との距離を返す
  if (dot(s.SND-s.FST, a-s.FST) < EPS) return abs(a-s.FST);
  if (dot(s.FST-s.SND, a-s.SND) < EPS) return abs(a-s.SND);
  
  return distanceLP(s, a);
}

int main() {
  int N;
  while(cin >> N && N) {
    circles.clear();
    circles.resize(N);
    for(int i=0; i<N; i++) {
      int x, y, r;
      cin >> x >> y >> r;
      circles[i] = C(r, P(x, y));
    }
    int Q; cin >> Q;
    while(Q--) {
      int x, y;
      cin >> x >> y; P taro(x, y);
      cin >> x >> y; P ogre(x, y);
      for(int i=0; i<N; i++) {
        if(distanceSP(S(taro, ogre), circles[i].SND) < circles[i].FST) {
          if(   abs(circles[i].SND-taro) > circles[i].FST
             || abs(circles[i].SND-ogre) > circles[i].FST) {
            cout << "Safe" << endl;
            goto EXIT;
          }
        }
      }
      
      cout << "Danger" << endl;
      
    EXIT:;
    }
  }
  
  return 0;
}

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) % 2 < 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;
//				printf("(%Lf %Lf) (%Lf %Lf)\n", l.x);
				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;
}