#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m, q;
int num[N];
vector<pair<int, int> > e[N], qs;
map<pair<int, int>, int> ans, lst;
int f[N], sz[N], qto[N];
int find(int i) { return f[i] == i ? i : f[i] = find(f[i]); }
void merge(int u, int v) {
  u = find(u);
  v = find(v);
  if (u != v) {
    if (sz[u] > sz[v]) {
      f[v] = u;
      sz[u] += sz[v];
    } else {
      f[u] = v;
      sz[v] += sz[u];
    }
  }
}
void bao1(int co) {
  for (int i = (1); i <= (n); ++i) f[i] = i, sz[i] = 1;
  for (vector<pair<int, int> >::iterator ii = e[co].begin(); ii != e[co].end();
       ++ii) {
    merge(ii->first, ii->second);
  }
  for (vector<pair<int, int> >::iterator ii = qs.begin(); ii != qs.end();
       ++ii) {
    int u = ii->first, v = ii->second;
    u = find(u), v = find(v);
    if (u == v) {
      ++ans[make_pair(ii->first, ii->second)];
    }
  }
}
void bao2(int co) {
  int ps[N], tot = 0;
  for (vector<pair<int, int> >::iterator ii = e[co].begin(); ii != e[co].end();
       ++ii) {
    ps[++tot] = ii->first;
    ps[++tot] = ii->second;
  }
  sort(ps + 1, ps + tot + 1);
  tot = unique(ps + 1, ps + tot + 1) - ps - 1;
  for (int i = (1); i <= (tot); ++i) f[ps[i]] = ps[i], sz[ps[i]] = 1;
  for (vector<pair<int, int> >::iterator ii = e[co].begin(); ii != e[co].end();
       ++ii) {
    merge(ii->first, ii->second);
  }
  for (int i = (1); i <= (tot); ++i) {
    for (int j = (i + 1); j <= (tot); ++j) {
      bool flg = 0;
      int u = find(ps[i]), v = find(ps[j]);
      if (u == v) flg = 1;
      if (flg && ans.count(make_pair(ps[i], ps[j]))) {
        ans[make_pair(ps[i], ps[j])]++;
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (m); ++i) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    num[c]++;
    e[c].push_back(make_pair(min(u, v), max(u, v)));
  }
  scanf("%d", &q);
  int now = 0;
  for (int i = (1); i <= (q); ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (!ans.count(make_pair(min(u, v), max(u, v)))) {
      qs.push_back(make_pair(min(u, v), max(u, v)));
      ++now;
      lst[make_pair(min(u, v), max(u, v))] = now;
      qto[i] = now;
    } else
      qto[i] = lst[make_pair(min(u, v), max(u, v))];
    ans[make_pair(min(u, v), max(u, v))] = 0;
  }
  for (int i = (1); i <= (m); ++i) {
    if (num[i]) {
      if (num[i] >= sqrt(m)) {
        bao1(i);
      } else
        bao2(i);
    }
  }
  for (int i = (1); i <= (q); ++i) {
    int u = qs[qto[i] - 1].first, v = qs[qto[i] - 1].second;
    printf("%d\n", ans[make_pair(u, v)]);
  }
  return 0;
}
