#include <bits/stdc++.h>
using namespace std;
#ifndef yoshi_likes_e4
#define endl '\n'
#endif
#define problem ""
#define multitest 0
#define debug(x) cerr << #x << " = " << x << endl;
void init()
{
}
struct Dsu
{
vector<int> sz;
vector<pair<int, int>> changes;
int cc, orig_cc;
Dsu()
{
}
Dsu(int n)
{
sz.resize(n);
fill(sz.begin(), sz.end(), -1);
cc = orig_cc = sz.size();
}
void reset()
{
fill(sz.begin(), sz.end(), -1);
cc = orig_cc = sz.size();
}
void rollback()
{
while (changes.size())
{
sz[changes.back().first] = changes.back().second;
changes.pop_back();
}
cc = orig_cc;
}
int find_set(int v)
{
while (sz[v] >= 0)
v = sz[v];
return v;
}
int find_setPC(int v)
{
if (sz[v] < 0)
return v;
return sz[v] = find_set(sz[v]);
}
void union_sets(int a, int b)
{
a = find_set(a);
b = find_set(b);
if (a != b)
{
cc--;
if (sz[a] > sz[b])
swap(a, b);
changes.push_back({a, sz[a]});
changes.push_back({b, sz[b]});
sz[a] += sz[b];
sz[b] = a;
}
}
void union_setsPC(int a, int b)
{
a = find_setPC(a);
b = find_setPC(b);
if (a != b)
{
cc--;
if (sz[a] > sz[b])
swap(a, b);
sz[a] += sz[b];
sz[b] = a;
}
}
};
vector<tuple<int, int, int>> blockQ[367];
int mark_b[100000];
int cur_bridge_value[100000];
int bridge_values_before[100000];
const int B = 320;
void Yoshi()
{
int n, m;
cin >> n >> m;
vector<pair<int, int>> bridges;
vector<pair<int, int>> bridgeValues;
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c, a--, b--;
c = -c;
bridges.push_back({a, b});
bridgeValues.push_back({c, i});
cur_bridge_value[i] = c;
}
sort(bridgeValues.begin(), bridgeValues.end());
int UDQ = 0;
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
int a, b, c;
cin >> a >> b >> c;
b--;
c = -c;
blockQ[UDQ / B].push_back({a, b, c});
if (a == 1)
UDQ++;
}
vector<int> QRResults(q - UDQ);
Dsu d(n);
int query_pos = 0;
for (auto &i : blockQ)
{
copy(cur_bridge_value, cur_bridge_value + 100000, bridge_values_before);
vector<int> bs;
// w,s,update idx
int n_upd = 0;
vector<tuple<int, int, int, int>> queries;
vector<pair<int, int>> updates;
for (auto &j : i)
{
auto [a, b, c] = j;
if (a == 1)
{
if (!mark_b[b])
bs.push_back(b);
mark_b[b] = 1;
cur_bridge_value[b] = c;
n_upd++;
updates.push_back({b, c});
}
else
queries.push_back({c, b, n_upd, query_pos++});
}
sort(queries.begin(), queries.end());
vector<pair<int, int>> TmpBridges;
vector<pair<int, int>> bridge_sorted;
for (auto &[u, v] : bridgeValues)
if (mark_b[v])
bridge_sorted.push_back({cur_bridge_value[v], v});
else
TmpBridges.push_back({u, v});
sort(bridge_sorted.begin(), bridge_sorted.end());
merge(bridge_sorted.begin(), bridge_sorted.end(), TmpBridges.begin(), TmpBridges.end(), bridgeValues.begin());
for (auto &h : bs)
cur_bridge_value[h] = bridge_values_before[h];
int tmp_bridge_pointer = 0;
for (auto &[u, v, w, id] : queries)
{
while (tmp_bridge_pointer < TmpBridges.size() && TmpBridges[tmp_bridge_pointer].first <= u)
{
auto [a, b] = bridges[TmpBridges[tmp_bridge_pointer].second];
d.union_setsPC(a, b);
tmp_bridge_pointer++;
}
for (int i = 0; i < w; i++)
{
auto [a, b] = updates[i];
cur_bridge_value[a] = b;
}
for (auto &h : bs)
if (cur_bridge_value[h] <= u)
{
auto [aa, bb] = bridges[h];
d.union_sets(aa, bb);
}
QRResults[id] = -d.sz[d.find_set(v)];
d.rollback();
for (int i = 0; i < w; i++)
{
auto [a, b] = updates[i];
cur_bridge_value[a] = bridge_values_before[a];
}
}
for (auto &[a, b] : updates)
mark_b[a] = 0, cur_bridge_value[a] = b;
d.reset();
}
for (auto &i : QRResults)
cout << i << endl;
}
signed main()
{
#ifndef yoshi_likes_e4
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen(problem ".inp", "r"))
{
freopen(problem ".inp", "r", stdin);
freopen(problem ".out", "w", stdout);
}
#endif
init();
int t = 1;
#if multitest
cin >> t;
#endif
while (t--)
Yoshi();
}