Winter Roads

No attempts yetTime limit10sMemory limit128 MB

Problem

Winter is coming, and the citizens of Winterfell are preparing for the long months ahead. One pressing concern is the stability of the roads and bridges: supply lines must stay uninterrupted as the season progresses.

The city's civil engineers want to model scenarios in which roads fail and are repaired. In their model, each road connects two landmarks, and each road has a carrying capacity. Trucks travel from landmark to landmark and may only use sufficiently sturdy roads: a supply truck of weight $w$ can travel on a road of capacity $c$ if and only if $w \le c$.

Over time a road's capacity may decrease (through wear or accident) or increase (through repair). Throughout these changes, the engineers still need to know whether a truck can get from a source landmark to a destination landmark.

Given a sequence of changes to the roads together with some supply-truck runs, determine for each run whether the supplies can still be delivered.

Input

The input contains several test cases. Each test case begins with a line containing two integers $n$ and $m$ ($1 \le n \le 1000$, $1 \le m \le 100000$), where $n$ is the number of landmarks and $m$ is the number of roads.

The next $m$ lines each contain three integers $a$, $b$, and $c$ ($1 \le a, b \le n$, $1 \le c \le 10^9$), describing a road between landmarks $a$ and $b$ with carrying capacity $c$. The roads are numbered $1, 2, \dots, m$ in the order they appear.

The next line contains a single integer $e$ ($1 \le e \le 100000$), the number of events. Each of the following $e$ lines starts with a capital letter followed by integers:

  • B r c: road $r$ breaks down and its capacity decreases to $c$ ($1 \le r \le m$, $1 \le c < 10^9$).
  • R r c: road $r$ is repaired and its capacity increases to $c$ ($1 \le r \le m$, $1 < c \le 10^9$).
  • S a b w: query whether a supply truck of weight $w$ can travel from landmark $a$ to landmark $b$ ($1 \le a, b \le n$, $1 \le w \le 10^9$).

Only the capital letters B, R, and S appear. Events take effect in the order given. There are at most $2000$ breakdown/repair (B/R) events in total. The input ends with a line containing two zeros.

Output

For each S a b w query, in order, output 1 if there is a path from $a$ to $b$ on which every road has capacity at least $w$, and 0 otherwise. Print each answer on its own line, with no extra spaces and no blank lines between answers.