// Arup Guha
// 6/20/2014
// Solution to 2013 Chicago Problem A: Winter Roads
// Note: This takes about 34 sec on my laptop but I was told that the time limit was one minute.

import java.util.*;

public class a {

	public static int n;
	public static int e;
	public static edge[] orig;
	public static edge[] graph;
	public static int[][] ans;
	public static HashSet<Integer> mst;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();
		e = stdin.nextInt();

		while (n != 0) {

			// Read in edges.
			orig = new edge[e];
			for (int i=0; i<e; i++) {
				int a = stdin.nextInt() - 1;
				int b = stdin.nextInt() - 1;
				int c = stdin.nextInt();
				orig[i] = new edge(i,a,b,c);
			}

			graph = new edge[e];
			for (int i=0; i<e; i++)
				graph[i] = orig[i];

			// Get ready to do modified Kruskal's.
			Arrays.sort(graph);
			ans = new int[n][n];
			kruskal();

			// Process each query.
			int queries = stdin.nextInt();
			for (int i=0; i<queries; i++) {

				String cmd = stdin.next();

				// Regular query.
				if (cmd.equals("S")) {
					int v1 = stdin.nextInt()-1;
					int v2 = stdin.nextInt()-1;
					int w = stdin.nextInt();
					if (ans[v1][v2] >= w) System.out.println("1");
					else                  System.out.println("0");
				}

				// Breakdown.
				else if (cmd.equals("B")){

					// Change this edge.
					int road = stdin.nextInt() - 1;
					int cap = stdin.nextInt();

					// Edit sorted list.
					int j = 0;
					while (graph[j].ID != road) j++;
					graph[j].w = cap;
					while (j < e-1 && graph[j].w < graph[j+1].w) {
						edge tmp = graph[j];
						graph[j] = graph[j+1];
						graph[j+1] = tmp;
						j++;
					}

					// Rerun if necessary.
					if (mst.contains(road)) kruskal();
				}
				else {

					// Change this edge.
					int road = stdin.nextInt() - 1;
					int cap = stdin.nextInt();

					// Edit sorted list.
					int j = e-1;
					while (graph[j].ID != road) j--;
					graph[j].w = cap;
					while (j > 0 && graph[j].w > graph[j-1].w) {
						edge tmp = graph[j];
						graph[j] = graph[j-1];
						graph[j-1] = tmp;
						j--;
					}

					// Always rerun in this case.
					kruskal();
				}
			}

			// Get next case.
			n = stdin.nextInt();
			e = stdin.nextInt();
		}
	}

	// Typical Kruskal's.
	public static void kruskal() {

		int i = 0, numE = 0;
		DJSet dj = new DJSet(n);
		for (int j=0; j<n; j++) {
			Arrays.fill(ans[j], 0);
			ans[j][j] = 1000000000;
		}
		mst = new HashSet<Integer>();

		while (numE < n-1) {

			int p1 = dj.find(graph[i].v1);
			int p2 = dj.find(graph[i].v2);

			if (p1 == p2) {
				i++;
				continue;
			}

			mst.add(graph[i].ID);
			numE++;
			LinkedList<Integer> list1 = dj.listTree(p1);
			LinkedList<Integer> list2 = dj.listTree(p2);
			int list1Size = list1.size();
			int list2Size = list2.size();

			for (int j=0; j<list1Size; j++) {
				int cur = list1.pollFirst();
				for (int k=0; k<list2Size; k++) {
					int next = list2.pollFirst();
					ans[cur][next] = graph[i].w;
					ans[next][cur] = graph[i].w;
					list2.offer(next);
				}
				list1.offer(cur);
			}
			dj.union(p1, p2);
			i++;
		}
	}
}

class edge implements Comparable<edge> {

	public int ID;
	public int v1;
	public int v2;
	public int w;

	public edge(int index, int start, int end, int weight) {
		ID = index;
		v1 = start;
		v2 = end;
		w = weight;
	}

	public int compareTo(edge other) {
		return other.w - this.w;
	}
}

class node {

	public int par;
	public ArrayList<Integer> kids;
	public int height;

	public node(int index) {
		par = index;
		kids = new ArrayList<Integer>();
		height = 0;
	}
}

class DJSet {

	public node[] list;

	public DJSet(int n) {
		list = new node[n];
		for (int i=0; i<n; i++)
			list[i] = new node(i);
	}

	public int find(int item) {
		if (list[item].par == item) return item;
		int myroot = find(list[item].par);
		list[item].par = myroot;
		return myroot;
	}

	public boolean union(int a, int b) {

		int p1 = find(a);
		int p2 = find(b);
		if (p1 == p2) return false;

		if (list[p1].height > list[p2].height) {
			list[p2].par = p1;
			list[p1].kids.add(p2);
		}
		else if (list[p2].height > list[p1].height) {
			list[p1].par = p2;
			list[p2].kids.add(p1);
		}
		else {
			list[p2].par = p1;
			list[p1].kids.add(p2);
			list[p1].height++;
		}
		return true;
	}

	// Wrapper function to return all items in the tree with a.
	public LinkedList<Integer> listTree(int a) {
		int p = find(a);
		LinkedList<Integer> ans = new LinkedList<Integer>();
		listTreeRec(p, ans);
		return ans;
	}

	// Do preorder traversal here.
	public void listTreeRec(int item, LinkedList<Integer> ans) {
		ans.offer(item);
		for (int i=0; i<list[item].kids.size(); i++)
			listTreeRec(list[item].kids.get(i), ans);
	}
}