QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 128 MB Total points: 100

#13302. Cloakroom

Statistics

The annual rich citizen's reunion is taking place in Byteotia. They meet to boast about their incomes, Lebytene's shoes and other luxuries. Naturally, not all these objects of pride are carried into the banquet - coats, jackets, umbrellas and such are left in the cloakroom, and picked up upon leave.

Unfortunately for the well off, a gang of Byteotian thieves plans to break into the cloakroom and steal part of the items stored therein. At this very moment the gang's leader is examining the plans of the robbery put forward by other gang members (they are a democratic lot!). A plan typically looks as follows: the thieves break into the cloakroom at time m_{j}, take items worth exactly k_{j} and escape, where the whole heist takes them s_{j} time. The gang leader would first of all like to know which of these plans are feasible and which are not. A plan is feasible if at time m_{j} it is possible to collect items of total value k_{j} in such a way that up to the very m_{j}+s_{j} moment no one shows up to retrieve any of the stolen goods (in such event, they would notify security, and the robbery would fail). In particular, if at time m_{j} it is impossible to select items of exact total worth k_{j}, then the plan is infeasible and consequently rejected. Knowing the drop off and retrieval times for each item, determine which plans are feasible and which are not. We assume that an item left in the cloakroom the moment a robbery starts can already be stolen (see the example).

Input Format

In the first line of the standard input there is a single integer n (1 ≤ n ≤ 1,000) denoting the number of items that will be left in the cloakroom. Those items are described in the n lines that follow. Each of those lines consists of three integers c_{i}, a_{i}, and b_{i} (1 ≤ c_{i} ≤ 1,000, 1 ≤ a_{i }< b_{i }≤ 10^{9}), separated by single spaces, that denote respectively: the item's value, the moment it is left in the cloakroom, and the moment it will be retrieved by the owner.

The next line holds an integer p (1 ≤ p ≤ 1,000,000), the number of plans the gang came up with. Each is specified in a separate line by three integers, m_{j}, k_{j} and s_{j }(1 ≤ m_{j} ≤ 10^{9}, 1 ≤ k_{j} ≤ 100,000, 0 ≤ s_{j} ≤ 10^{9} ), separated by single spaces, that denote respectively: the moment the thieves would enter the cloakroom, the value of goods they would like to steal, and the time the robbery would take them.

In test worth 16% of the total points p ≤ 10 holds in addition.

In some other tests, also worth 16% of the points, all the items have the same a_{i} values.

In yet other tests, worth 24% of the points, all the queries share the same s_{j} value.

Output Format

For each plan put forward by the gang determine if it is feasible, i.e., whether it is possible to steal items of total worth exactly k_{j} and escape before anyone asks for their belongings. If the plan is feasible, your program should print the word TAK (Polish for yes) on the standard output, otherwise it should print NIE (Polish for no).

Example

Input

5
6 2 7
5 4 9
1 2 4
2 5 8
1 3 9
5
2 7 1
2 7 2
3 2 0
5 7 2
4 1 5

Output

TAK
NIE
TAK
TAK
NIE
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.