#include <bits/stdc++.h>
using namespace std;
const int maxn = 101000;
int n, m, sg, vis[maxn], fa[maxn], q[maxn];
int ask, ax[maxn], ay[maxn], ans[maxn];
vector<int> g[maxn];
int find(int t) { return fa[t] == t ? t : (fa[t] = find(fa[t])); }
struct nodd {
  int a, b, c;
} a[maxn];
bool cmp(nodd a, nodd b) { return a.c < b.c; }
void init() {
  for (int i = (1); i <= (m); ++i) scanf("%d%d%d", &a[i].a, &a[i].b, &a[i].c);
  sort(a + 1, a + m + 1, cmp);
  a[0].c = a[1].c - 1;
  scanf("%d", &ask);
  for (int i = (1); i <= (ask); ++i) scanf("%d%d", &ax[i], &ay[i]);
  memset(ans, 0, sizeof(ans));
  memset(vis, 0, sizeof(vis));
}
void work() {
  map<pair<int, int>, int> h;
  for (int i = (1); i <= (m); ++i)
    if (a[i].c != a[i - 1].c) {
      int first = i, second, s = 0, vv = 0;
      while (first < m && a[first + 1].c == a[i].c) first++;
      for (int j = (i); j <= (first); ++j) {
        second = a[j].a;
        if (vis[second] != i) vis[second] = i, q[++s] = second;
        second = a[j].b;
        if (vis[second] != i) vis[second] = i, q[++s] = second;
      }
      for (int j = (1); j <= (s); ++j) fa[q[j]] = q[j], g[q[j]].clear();
      for (int j = (i); j <= (first); ++j) fa[find(a[j].a)] = find(a[j].b);
      for (int j = (1); j <= (s); ++j) g[find(q[j])].push_back(q[j]);
      for (int j = (1); j <= (s); ++j)
        if (fa[q[j]] == q[j]) {
          int second = q[j], sum = g[q[j]].size() - 1;
          if (sum <= 100) {
            for (int k = (0); k <= (sum); ++k)
              for (int r = (k + 1); r <= (sum); ++r)
                h[make_pair(g[second][k], g[second][r])]++;
            for (int k = (0); k <= (sum); ++k) vis[g[second][k]] = 0;
          } else {
            vv = 1;
          }
        }
      if (vv)
        for (int k = (1); k <= (ask); ++k)
          if (vis[ax[k]] == i && vis[ay[k]] == i)
            ans[k] += (fa[ax[k]] == fa[ay[k]]);
    }
  for (int i = (1); i <= (ask); ++i) {
    int first = ax[i], second = ay[i];
    printf("%d\n",
           ans[i] + h[make_pair(first, second)] + h[make_pair(second, first)]);
  }
}
int main() {
  while (cin >> n >> m) {
    init();
    work();
  }
  return 0;
}
