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 <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 1010

#define double long double

/* Point (x,y)*/
struct P{
  typedef double Type;
  
  Type x,y;
  
  P(Type X,Type Y){
    x = X;
    y = Y;
  }
  
  P rotate(double theta){
    return P(y*cos(theta)-x*sin(theta),y*sin(theta)+x*cos(theta));
  }
  
  bool operator< (const P &B) const{
    return x==B.x ? y < B.y : x < B.x;
  }
  
};

/* Line ax+bx+c=0 */
struct Line{
  typedef double Type;
  
  Type a,b,c;
  
  Line(Type A,Type B,Type C){
    a = A;
    b = B;
    c = C;
  }
  
  //2???(x_1,y_1),(x_2,y_2)???????????´???
  Line(Type x_1,Type y_1,Type x_2,Type y_2){
    a = y_1-y_2;
    b = x_2-x_1;
    c = y_1*(x_1-x_2)-x_1*(y_1-y_2);
  }
  
  /*??\??????Type???????????????double*/
  
  // y??§?¨???????x??§?¨??????????
  double x(double y){
    return -(double)(b*y+c)/a;
  }
  
  // x??§?¨???????y??§?¨??????????
  double y(double x){
    return -(double)(a*x+c)/b;
  }
  
};


int solve(){
  int n,m;
  int wx[SIZE], wy[SIZE], r[SIZE];
  const double eps = 1e-10;
  
  scanf("%d",&n);

  if(n == 0) return false;
  
  for(int i=0;i<n;i++){
    scanf("%d%d%d",wx+i, wy+i,r+i);
  }

  scanf("%d",&m);
  
  for(int i=0;i<m;i++){
    int tx,ty,sx,sy;
    bool flag = false;
    
    scanf("%d%d%d%d",&tx,&ty,&sx,&sy);

    Line l(tx,ty,sx,sy);

    for(int j=0;j<n;j++){

      double dist = abs(l.a * wx[j] + l.b * wy[j] + l.c)/sqrt(l.a*l.a + l.b*l.b);
      int dist1 = (wx[j]-tx)*(wx[j]-tx) + (wy[j]-ty)*(wy[j]-ty);
      int dist2 = (wx[j]-sx)*(wx[j]-sx) + (wy[j]-sy)*(wy[j]-sy);

      if(dist1 <= r[j]*r[j] && dist2 <= r[j]*r[j]) continue; //??±????????´
      if(dist > r[j] + eps) continue;

      if(dist1 <= r[j]*r[j] || dist2 <= r[j]*r[j]){ //???????????´
        flag = true;
      }
      
      //y = ax+b
      double a = -1/l.a;
      double b = wy[j] - a*wx[j];
      /*
      aqx = qy - qb
      px = -qy - r

      (aq + p)x = - (qb + r)
      */
      double cx = - (l.c + b * l.b)/(l.a + a * l.b);
      
      if(min(tx,sx) <= cx-eps && cx+eps <= max(tx,sx)){
        flag = true;
      }
      
    }

    if(flag)
      puts("Safe");
    else
      puts("Danger");
  }
  
  return true;
}

int main(){

  while(solve());

}

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

// infty
#define ULLINF (ULLONG_MAX)
#define LLINF  (LLONG_MAX)
#define IINF   (INT_MAX)
#define INF    (1<<29)

// math
#define Sq(x) ((x)*(x))

// container utility
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define PB push_back
#define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++)

// rep
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;

typedef long long ll;

// pair util
#define FST first
#define SND second

// range validator
#define CK(n,a,b) (a<=n && n<b)

// conversion
template<class T> inline string toStr(T a) { ostringstream oss_; oss_ << a; return oss_.str(); }
inline int toInt(string s) { return atoi(s.c_str()); }

// prime
bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; }

int const dx[] = {-1,0,1,0,-1,1,1,-1};
int const dy[] = {0,-1,0,1,-1,-1,1,1};

//////////////////////////////////////////////////////////////

#define EPS (1e-5)

typedef complex<double> P;
typedef pair<double, P> C;
typedef pair<P, P> S;

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

typedef S Line;
double distanceLP(Line l, P a) {
  P base = l.SND-l.FST;
  return abs(cross(base, a-l.FST)) / abs(base);
}

double distanceSP(S s, P a) {
  {
    P X = s.SND - s.FST, Y = a - s.FST;
    if(dot(X, Y) < EPS) return abs(Y);
  }
  {
    P X = s.FST - s.SND, Y = a - s.SND;
    if(dot(X, Y) < EPS) return abs(Y);
  }
  
  return distanceLP(s, a);
}

int main() {
  
  while(1) {
    int N;
    cin >> N; if(N == 0) break;
    
    vector<C> circles(N);
    
    for(int i=0; i<N; i++) {
      double x, y, r;
      cin >> x >> y >> r;
      circles[i] = C(r, P(x, y));
    }
    
    int Q; cin >> Q;
    while(Q--) {
      double x, y;
      cin >> x >> y; P taro(x, y);
      cin >> x >> y; P devil(x, y);
      
      bool ok = 0;
      for(int idx=0; idx<N; idx++) {
	if( distanceSP( S(taro, devil), circles[idx].SND ) < circles[idx].FST + EPS ) {
	  if( abs(taro-circles[idx].SND) > circles[idx].FST
	      || abs(devil-circles[idx].SND) > circles[idx].FST ) ok = 1;
	}
      }
      
      if(ok) cout << "Safe" << endl;
      else cout << "Danger" << endl;
    }
    
  }
  
  return 0;
}