#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool read(T &n) {
  T x = 0, tmp = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
  if (c == EOF) return false;
  if (c == '-') c = getchar(), tmp = -1;
  while (c >= '0' && c <= '9') x *= 10, x += (c - '0'), c = getchar();
  n = x * tmp;
  return true;
}
template <class T>
inline void write(T n) {
  if (n < 0) {
    putchar('-');
    n = -n;
  }
  int len = 0, data[20];
  while (n) {
    data[len++] = n % 10;
    n /= 10;
  }
  if (!len) data[len++] = 0;
  while (len--) putchar(data[len] + 48);
}
map<pair<int, int>, int> ans;
unordered_map<int, int> edge[100100];
int n, m, fa[100100];
int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); }
int main() {
  int x, y, z;
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    read(x);
    read(y);
    read(z);
    int tmp1 = 0, tmp2 = 0;
    if (edge[x].count(z)) tmp1 = edge[x][z];
    if (edge[y].count(z)) tmp2 = edge[y][z];
    edge[x][z] = i;
    edge[y][z] = i;
    if (tmp1) {
      fa[findset(tmp1)] = findset(i);
    }
    if (tmp2) {
      fa[findset(tmp2)] = findset(i);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    read(x), read(y);
    if (ans.count(make_pair(x, y))) {
      write(ans[make_pair(x, y)]);
      putchar('\n');
      continue;
    } else {
      if (edge[x].size() > edge[y].size()) swap(x, y);
      z = 0;
      for (auto it = edge[x].begin(); it != edge[x].end(); ++it) {
        auto that = edge[y].find(it->first);
        if (that != edge[y].end() &&
            findset(it->second) == findset(that->second)) {
          z++;
        }
      }
      write(z);
      putchar('\n');
      ans[make_pair(x, y)] = ans[make_pair(y, x)] = z;
    }
  }
}
