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 <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i,m,n) for(int i=m;i<n;i++)
#define rep(i,n) rep2(i,0,n)
#define ll long long
//#define P pair<int,int>
#define pb push_back
typedef complex<double> P;
typedef vector<P> G;

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

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

int ccw(P a, P b, P c) {
  b -= a; c -= a;
  if (cross(b, c) > 0)   return +1;       // counter clockwise
  if (cross(b, c) < 0)   return -1;       // clockwise
  if (dot(b, c) < 0)     return +2;       // c--a--b on line
  if (norm(b) < norm(c)) return -2;       // a--b--c on line
  return 0;
}

bool intersectLL(const L &l, const L &m) {
  return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel
         abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;   // same line
}
bool intersectLS(const L &l, const L &s) {
  return cross(l[1]-l[0], s[0]-l[0])*       // s[0] is left of l
         cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l
}
bool intersectLP(const L &l, const P &p) {
  return abs(cross(l[1]-p, l[0]-p)) < EPS;
}
bool intersectSS(const L &s, const L &t) {
  return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
         ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
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]);
}
P reflection(const L &l, const P &p) {
  return p + 2.0 * (projection(l, p) - p);
}
double distanceLP(const L &l, const P &p) {
  return abs(p - projection(l, p));
}
double distanceLL(const L &l, const L &m) {
  return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);
}
double distanceLS(const L &l, const L &s) {
  if (intersectLS(l, s)) return 0;
  return min(distanceLP(l, s[0]), distanceLP(l, s[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));
}
double distanceSS(const L &s, const L &t) {
  if (intersectSS(s, t)) return 0;
  return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
             min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}
P crosspoint(const L &l, const L &m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line
  if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
  return m[0] + B / A * (m[1] - m[0]);
}

int n,wx[100],wy[100],r[100],m;

int sq(int x){
	return x*x;
}

bool inner(int no,int x,int y){
	return sq(wx[no]-x)+sq(wy[no]-y)<sq(r[no]);
}

int main(){
	while(cin>>n&&n){
		rep(i,n)cin>>wx[i]>>wy[i]>>r[i];
		cin>>m;
		rep(i,m){
			int tx,ty,sx,sy,safe=0;
			cin>>tx>>ty>>sx>>sy;
			rep(j,n){
				bool t=inner(j,tx,ty),s=inner(j,sx,sy);
				if(s&&t)continue;
				if(s||t){safe=1;break;}
				P p=P(wx[j],wy[j]);
				L l=L(P(tx,ty),P(sx,sy));
				if(distanceSP(l,p)<r[j]+EPS){safe=1;break;}
			}
			cout<<(safe?"Safe":"Danger")<<endl;
		}
	}
}

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

/* 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;
      
      //y = ax+b
      double a = -1/l.a;
      double b = wy[j] - a*wx[j];

      double cx = (- l.c - b * l.b)/(l.a + a * l.b);
      
      if(min(tx,sx) <= cx && cx <= max(tx,sx)){
        flag = true;
      }else{

        if(dist1 <= r[j]*r[j] || dist2 <= r[j]*r[j]){
          flag = true;
        }
      }
      
    }

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


  
  return true;
}

int main(){

  while(solve());

}