This page is still under construction.

Parts of this page are still being built. What you see may change.

Sums

Time limit1sMemory limit128 MB

Summary
Given a set A of positive integers and queries b, decide for each b whether it can be written as an unlimited sum of elements of A.
Level

Hard8 of 10

Topics
Number theory, Dynamic programming, Greedy
Solved
No attempts yet

Problem

You are given a set AA of positive integers. Consider the set A′A' of non-negative integers where a number xx belongs to A′A' if and only if xx can be written as a sum of some elements of AA (each element may be used any number of times, including zero times).

For example, if A={2,5,7}A = \{2, 5, 7\}, then A′A' contains 00 (the empty sum), 22, 44 (2+22+2) and 1212 (5+75+7 or 2+2+2+2+2+22+2+2+2+2+2), while 11 and 33 do not belong to A′A'.

Given the description of the set AA and a sequence of integers b1,b2,…,bkb_1, b_2, \dots, b_k, write a program that decides, for each bib_i, whether it belongs to A′A'.

Input

The first line contains the number of elements nn of the set AA (1≤n≤50001 \le n \le 5000). Each of the next nn lines contains one element of AA: the (i+1)(i+1)-th line holds a positive integer aia_i (1≤ai≤500001 \le a_i \le 50000), with a1<a2<⋯<ana_1 < a_2 < \dots < a_n, so that A={a1,a2,…,an}A = \{a_1, a_2, \dots, a_n\}.

The (n+2)(n+2)-th line contains the number of queries kk (1≤k≤100001 \le k \le 10000). Each of the next kk lines contains one integer bib_i with 0≤bi≤1090 \le b_i \le 10^9.

Output

Print kk lines. The ii-th line must contain TAK (Polish for 'yes') if bib_i belongs to A′A', and NIE (Polish for 'no') otherwise.

Examples3

  1. Example 1

    Input
    3
    2
    5
    7
    6
    0
    1
    4
    12
    3
    2
    
    Expected output
    TAK
    NIE
    TAK
    TAK
    NIE
    TAK
    
  2. Example 2

    Input
    1
    1
    6
    0
    1
    2
    5
    999999999
    1000000000
    
    Expected output
    TAK
    TAK
    TAK
    TAK
    TAK
    TAK
    
  3. Example 3

    Input
    1
    3
    9
    0
    1
    2
    3
    6
    9
    10
    999999999
    1000000000
    
    Expected output
    TAK
    NIE
    NIE
    TAK
    TAK
    TAK
    NIE
    TAK
    NIE