#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
  int x, y, ans;
} q[N];
map<int, int> mp[N];
map<pair<int, int>, int> id, ret;
vector<int> v[N];
vector<node> e[N];
stack<pair<int, int> > s;
int n, m, qu, cnt, line;
int father[N], c[N];
int getfather(int x) {
  return father[x] ? father[x] = getfather(father[x]) : x;
}
int gf(int x) {
  if (father[x]) {
    s.push(make_pair(x, father[x]));
    father[x] = getfather(father[x]);
    return father[x];
  } else
    return x;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i(1); i <= (m); ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    if (x > y) swap(x, y);
    mp[z][x] = mp[z][y] = 1;
    e[z].push_back({x, y});
  }
  scanf("%d", &qu);
  for (int i(1); i <= (qu); ++i) {
    scanf("%d%d", &q[i].x, &q[i].y);
    if (q[i].x > q[i].y) swap(q[i].x, q[i].y);
    id[make_pair(q[i].x, q[i].y)] = i;
  }
  for (int i(1); i <= (m); ++i) {
    int now = (int)mp[i].size();
    if (now > 0) v[now].push_back(i);
  }
  line = sqrt(n);
  for (int i(1); i <= (line); ++i) {
    for (auto col : v[i]) {
      while (!s.empty()) s.pop();
      for (auto edge : e[col]) {
        int x = edge.x, y = edge.y;
        int fx = gf(x), fy = gf(y);
        if (fx != fy) {
          s.push(make_pair(fx, father[fx]));
          father[fx] = fy;
        }
      }
      cnt = 0;
      for (auto u : mp[col]) c[++cnt] = u.first;
      for (int j(1); j <= (cnt - 1); ++j) {
        for (int k(j + 1); k <= (cnt); ++k) {
          int x = c[j], y = c[k];
          if (x > y) swap(x, y);
          if (gf(x) == gf(y)) ++ret[make_pair(x, y)];
        }
      }
      while (!s.empty()) {
        father[s.top().first] = s.top().second;
        s.pop();
      }
    }
  }
  for (int i(line + 1); i <= (n); ++i) {
    for (auto col : v[i]) {
      memset(father, 0, sizeof father);
      for (auto edge : e[col]) {
        int x = edge.x, y = edge.y;
        int fx = getfather(x), fy = getfather(y);
        if (fx != fy) father[fx] = fy;
      }
      for (int j(1); j <= (qu); ++j)
        if (getfather(q[j].x) == getfather(q[j].y)) ++q[j].ans;
    }
  }
  for (int i(1); i <= (qu); ++i)
    printf("%d\n", q[i].ans + ret[make_pair(q[i].x, q[i].y)]);
  return 0;
}
